Overte C++ Documentation
WebRTCSocket.h
1 //
2 // WebRTCSocket.h
3 // libraries/networking/src/webrtc
4 //
5 // Created by David Rowe on 21 Jun 2021.
6 // Copyright 2021 Vircadia contributors.
7 //
8 
9 #ifndef overte_WebRTCSocket_h
10 #define overte_WebRTCSocket_h
11 
12 #include <shared/WebRTC.h>
13 
14 #if defined(WEBRTC_DATA_CHANNELS)
15 
16 #include <QAbstractSocket>
17 #include <QObject>
18 #include <QQueue>
19 
20 #include "WebRTCDataChannels.h"
21 
24 
25 
31 class WebRTCSocket : public QObject {
32  Q_OBJECT
33 
34 public:
35 
38  WebRTCSocket(QObject* parent);
39 
40 
47  void setSocketOption(QAbstractSocket::SocketOption option, const QVariant& value);
48 
55  QVariant socketOption(QAbstractSocket::SocketOption option);
56 
65  bool bind(const QHostAddress& address, quint16 port = 0, QAbstractSocket::BindMode mode
66  = QAbstractSocket::DefaultForPlatform);
67 
72  QAbstractSocket::SocketState state() const;
73 
75  void abort();
76 
80  quint16 localPort() const { return 0; }
81 
85  qintptr socketDescriptor() const { return -1; }
86 
87 
92  qint64 writeDatagram(const QByteArray& datagram, const SockAddr& destination);
93 
97  qint64 bytesToWrite(const SockAddr& destination) const;
98 
101  bool hasPendingDatagrams() const;
102 
105  qint64 pendingDatagramSize() const;
106 
114  qint64 readDatagram(char* data, qint64 maxSize, QHostAddress* address = nullptr, quint16* port = nullptr);
115 
116 
119  QAbstractSocket::SocketError error() const;
120 
123  QString errorString() const;
124 
125 public slots:
126 
131  void onDataChannelReceivedMessage(const SockAddr& source, const QByteArray& message);
132 
133 signals:
134 
137  void stateChanged(QAbstractSocket::SocketState socketState);
138 
140  void readyRead();
141 
144  void onSignalingMessage(const QJsonObject& json);
145 
148  void sendSignalingMessage(const QJsonObject& message);
149 
150 
151 private:
152 
153  void setError(QAbstractSocket::SocketError errorType, QString errorString);
154  void clearError();
155 
156  WebRTCDataChannels _dataChannels;
157 
158  bool _isBound { false };
159 
160  QQueue<QPair<SockAddr, QByteArray>> _receivedQueue; // Messages received are queued for reading from the "socket".
161 
162  QAbstractSocket::SocketError _lastErrorType { QAbstractSocket::UnknownSocketError };
163  QString _lastErrorString;
164 };
165 
166 
168 
169 #endif // WEBRTC_DATA_CHANNELS
170 
171 #endif // overte_WebRTCSocket_h
Manages WebRTC data channels on the domain server or an assignment client that Interface clients can ...
Definition: WebRTCDataChannels.h:230
Provides a QUdpSocket-style interface for using WebRTCDataChannels.
Definition: WebRTCSocket.h:31
qint64 writeDatagram(const QByteArray &datagram, const SockAddr &destination)
Sends a datagram.
Definition: WebRTCSocket.cpp:81
void onSignalingMessage(const QJsonObject &json)
Emitted when a WebRTC signaling message has been received from the signaling server for this WebRTCSo...
qint64 pendingDatagramSize() const
Gets the size of the first pending datagram.
Definition: WebRTCSocket.cpp:99
QAbstractSocket::SocketState state() const
Gets the state of the socket.
Definition: WebRTCSocket.cpp:72
QAbstractSocket::SocketError error() const
Gets the type of error that last occurred.
Definition: WebRTCSocket.cpp:131
void sendSignalingMessage(const QJsonObject &message)
Emitted when there's a WebRTC signaling message to send via the signaling server.
void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value)
Nominally sets the value of a socket option.
Definition: WebRTCSocket.cpp:31
QVariant socketOption(QAbstractSocket::SocketOption option)
Nominally gets the value of a socket option.
Definition: WebRTCSocket.cpp:45
WebRTCSocket(QObject *parent)
Constructs a new WebRTCSocket object.
Definition: WebRTCSocket.cpp:19
void abort()
Immediately closes all connections and resets the socket.
Definition: WebRTCSocket.cpp:76
quint16 localPort() const
Nominally gets the host port number. Included for compatibility with the QUdpSocket interface.
Definition: WebRTCSocket.h:80
void stateChanged(QAbstractSocket::SocketState socketState)
Emitted when the state of the socket changes.
QString errorString() const
Gets the description of the error that last occurred.
Definition: WebRTCSocket.cpp:135
qintptr socketDescriptor() const
Nominally gets the socket descriptor. Included for compatibility with the QUdpSocket interface.
Definition: WebRTCSocket.h:85
bool hasPendingDatagrams() const
Gets whether there's a datagram waiting to be read.
Definition: WebRTCSocket.cpp:95
void onDataChannelReceivedMessage(const SockAddr &source, const QByteArray &message)
Handles the WebRTC data channel receiving a message.
Definition: WebRTCSocket.cpp:150
qint64 readDatagram(char *data, qint64 maxSize, QHostAddress *address=nullptr, quint16 *port=nullptr)
Reads the next datagram, up to a maximum number of bytes.
Definition: WebRTCSocket.cpp:106
qint64 bytesToWrite(const SockAddr &destination) const
Gets the number of bytes waiting to be written.
Definition: WebRTCSocket.cpp:90
void readyRead()
Emitted each time new data becomes available for reading.
bool bind(const QHostAddress &address, quint16 port=0, QAbstractSocket::BindMode mode=QAbstractSocket::DefaultForPlatform)
Nominally binds the WebRTC socket to an address and port.
Definition: WebRTCSocket.cpp:62