Overte C++ Documentation
NetworkSocket.h
1 //
2 // NetworkSocket.h
3 // libraries/networking/src/udt
4 //
5 // Created by David Rowe on 21 Jun 2021.
6 // Copyright 2021 Vircadia contributors.
7 //
8 
9 #ifndef overte_NetworkSocket_h
10 #define overte_NetworkSocket_h
11 
12 #include <QObject>
13 #include <QUdpSocket>
14 
15 #include <shared/WebRTC.h>
16 
17 #include "../SockAddr.h"
18 #include "../NodeType.h"
19 #include "../SocketType.h"
20 #if defined(WEBRTC_DATA_CHANNELS)
21 #include "../webrtc/WebRTCSocket.h"
22 #endif
23 
26 
27 
29 class NetworkSocket : public QObject {
30  Q_OBJECT
31 
32 public:
33 
36  NetworkSocket(QObject* parent);
37 
38 
43  void setSocketOption(SocketType socketType, QAbstractSocket::SocketOption option, const QVariant& value);
44 
49  QVariant socketOption(SocketType socketType, QAbstractSocket::SocketOption option);
50 
51 
56  void bind(SocketType socketType, const QHostAddress& address, quint16 port = 0);
57 
60  void abort(SocketType socketType);
61 
62 
66  quint16 localPort(SocketType socketType) const;
67 
71  qintptr socketDescriptor(SocketType socketType) const;
72 
73 
78  qint64 writeDatagram(const QByteArray& datagram, const SockAddr& sockAddr);
79 
86  qint64 bytesToWrite(SocketType socketType, const SockAddr& address = SockAddr()) const;
87 
88 
91  bool hasPendingDatagrams() const;
92 
95  qint64 pendingDatagramSize();
96 
103  qint64 readDatagram(char* data, qint64 maxSize, SockAddr* sockAddr = nullptr);
104 
105 
109  QAbstractSocket::SocketState state(SocketType socketType) const;
110 
111 
115  QAbstractSocket::SocketError error(SocketType socketType) const;
116 
120  QString errorString(SocketType socketType) const;
121 
122 
123 #if defined(WEBRTC_DATA_CHANNELS)
126  const WebRTCSocket* getWebRTCSocket();
127 #endif
128 
129 signals:
130 
132  void readyRead();
133 
137  void stateChanged(SocketType socketType, QAbstractSocket::SocketState socketState);
138 
142  void socketError(SocketType socketType, QAbstractSocket::SocketError socketError);
143 
144 private slots:
145 
146  void onUDPStateChanged(QAbstractSocket::SocketState socketState);
147  void onWebRTCStateChanged(QAbstractSocket::SocketState socketState);
148 
149  void onUDPSocketError(QAbstractSocket::SocketError socketError);
150  void onWebRTCSocketError(QAbstractSocket::SocketError socketError);
151 
152 private:
153 
154  QObject* _parent;
155 
156  QUdpSocket _udpSocket;
157 #if defined(WEBRTC_DATA_CHANNELS)
158  WebRTCSocket _webrtcSocket;
159 #endif
160 
161 #if defined(WEBRTC_DATA_CHANNELS)
162  SocketType _pendingDatagramSizeSocketType { SocketType::Unknown };
163  SocketType _lastSocketTypeRead { SocketType::Unknown };
164 #endif
165 };
166 
167 
169 
170 #endif // overte_NetworkSocket_h
Multiplexes a QUdpSocket and a WebRTCSocket so that they appear as a single QUdpSocket-style socket.
Definition: NetworkSocket.h:29
QVariant socketOption(SocketType socketType, QAbstractSocket::SocketOption option)
Gets the value of a UDP or WebRTC socket option.
Definition: NetworkSocket.cpp:52
QString errorString(SocketType socketType) const
Gets the description of the error that last occurred.
Definition: NetworkSocket.cpp:255
void socketError(SocketType socketType, QAbstractSocket::SocketError socketError)
qint64 bytesToWrite(SocketType socketType, const SockAddr &address=SockAddr()) const
Gets the number of bytes waiting to be written.
Definition: NetworkSocket.cpp:144
quint16 localPort(SocketType socketType) const
Gets the UDP or WebRTC local port number.
Definition: NetworkSocket.cpp:98
void bind(SocketType socketType, const QHostAddress &address, quint16 port=0)
Binds the UDP or WebRTC socket to an address and port.
Definition: NetworkSocket.cpp:67
qint64 pendingDatagramSize()
Gets the size of the next pending datagram, alternating between socket types if both have datagrams t...
Definition: NetworkSocket.cpp:167
NetworkSocket(QObject *parent)
Constructs a new NetworkSocket object.
Definition: NetworkSocket.cpp:14
void abort(SocketType socketType)
Immediately closes and resets the socket.
Definition: NetworkSocket.cpp:82
QAbstractSocket::SocketState state(SocketType socketType) const
Gets the state of the UDP or WebRTC socket.
Definition: NetworkSocket.cpp:226
void setSocketOption(SocketType socketType, QAbstractSocket::SocketOption option, const QVariant &value)
Set the value of a UDP or WebRTC socket option.
bool hasPendingDatagrams() const
Gets whether there is a pending datagram waiting to be read.
Definition: NetworkSocket.cpp:159
void readyRead()
Emitted each time new data becomes available for reading.
void stateChanged(SocketType socketType, QAbstractSocket::SocketState socketState)
Emitted when the state of the underlying UDP or WebRTC socket changes.
const WebRTCSocket * getWebRTCSocket()
Gets a pointer to the WebRTC socket object.
Definition: NetworkSocket.cpp:271
QAbstractSocket::SocketError error(SocketType socketType) const
Gets the type of error that last occurred.
Definition: NetworkSocket.cpp:241
qint64 readDatagram(char *data, qint64 maxSize, SockAddr *sockAddr=nullptr)
Reads the next datagram per the most recent pendingDatagramSize call if made, otherwise alternating b...
Definition: NetworkSocket.cpp:192
qint64 writeDatagram(const QByteArray &datagram, const SockAddr &sockAddr)
Sends a datagram to a network address.
Definition: NetworkSocket.cpp:128
qintptr socketDescriptor(SocketType socketType) const
Returns the native socket descriptor of the UDP or WebRTC socket.
Definition: NetworkSocket.cpp:112
Provides a QUdpSocket-style interface for using WebRTCDataChannels.
Definition: WebRTCSocket.h:31
SocketType
The types of network socket.
Definition: SocketType.h:22
@ Unknown
Socket type unknown or not set.