diff --git a/libraries/networking/src/webrtc/WebRTCDataChannels.cpp b/libraries/networking/src/webrtc/WebRTCDataChannels.cpp index 321b844eb0..92cba6fd96 100644 --- a/libraries/networking/src/webrtc/WebRTCDataChannels.cpp +++ b/libraries/networking/src/webrtc/WebRTCDataChannels.cpp @@ -129,7 +129,7 @@ WDCConnection::WDCConnection(quint16 webSocketID, WebRTCDataChannels* parent) : _parent(parent) { #ifdef WEBRTC_DEBUG - qCDebug(networking_webrtc) << "WebRTCDataChannels::WebRTCDataChannels()"; + qCDebug(networking_webrtc) << "WDCConnection::WDCConnection() :" << webSocketID; #endif // Create observers. @@ -267,7 +267,7 @@ void WDCConnection::onDataChannelOpened(rtc::scoped_refptr #endif _dataChannel = dataChannel; - _dataChannelID = dataChannel->id(); + _dataChannelID = _parent->getNewDataChannelID(); // Not dataChannel->id() because it's only unique per peer connection. _dataChannel->RegisterObserver(_dataChannelObserver.get()); _parent->onDataChannelOpened(this, _dataChannelID); @@ -346,7 +346,7 @@ WebRTCDataChannels::WebRTCDataChannels(NodeType_t nodeType, QObject* parent) : } WebRTCDataChannels::~WebRTCDataChannels() { - QHashIterator i(_connectionsByDataChannel); + QHashIterator i(_connectionsByDataChannel); while (i.hasNext()) { i.next(); delete i.value(); @@ -363,14 +363,20 @@ WebRTCDataChannels::~WebRTCDataChannels() { _rtcNetworkThread = nullptr; } -void WebRTCDataChannels::onDataChannelOpened(WDCConnection* connection, int dataChannelID) { +quint16 WebRTCDataChannels::getNewDataChannelID() { + static const int QUINT16_LIMIT = std::numeric_limits::max() + 1; + _lastDataChannelID = std::max((_lastDataChannelID + 1) % QUINT16_LIMIT, 1); + return _lastDataChannelID; +} + +void WebRTCDataChannels::onDataChannelOpened(WDCConnection* connection, quint16 dataChannelID) { #ifdef WEBRTC_DEBUG qCDebug(networking_webrtc) << "WebRTCDataChannels::onDataChannelOpened() :" << dataChannelID; #endif _connectionsByDataChannel.insert(dataChannelID, connection); } -void WebRTCDataChannels::onDataChannelClosed(WDCConnection* connection, int dataChannelID) { +void WebRTCDataChannels::onDataChannelClosed(WDCConnection* connection, quint16 dataChannelID) { #ifdef WEBRTC_DEBUG qCDebug(networking_webrtc) << "WebRTCDataChannels::onDataChannelClosed() :" << dataChannelID; #endif diff --git a/libraries/networking/src/webrtc/WebRTCDataChannels.h b/libraries/networking/src/webrtc/WebRTCDataChannels.h index 9ea75bc5ff..ca14e9ae81 100644 --- a/libraries/networking/src/webrtc/WebRTCDataChannels.h +++ b/libraries/networking/src/webrtc/WebRTCDataChannels.h @@ -206,15 +206,19 @@ public: return _nodeType; } + /// @brief Get a new data channel ID to uniquely identify a WDCConnection. + /// @return A new data channel ID. + quint16 getNewDataChannelID(); + /// @brief Handles a WebRTC data channel opening. /// @param connection The WebRTC data channel connection. /// @param dataChannelID The WebRTC data channel ID. - void onDataChannelOpened(WDCConnection* connection, int dataChannelID); + void onDataChannelOpened(WDCConnection* connection, quint16 dataChannelID); /// @brief Handles a WebRTC data channel closing. /// @param connection The WebRTC data channel connection. /// @param dataChannelID The WebRTC data channel ID. - void onDataChannelClosed(WDCConnection* connection, int dataChannelID); + void onDataChannelClosed(WDCConnection* connection, quint16 dataChannelID); /// @brief Emits a signalingMessage received for the Interface client. /// @param message The WebRTC signaling message to send. @@ -268,8 +272,10 @@ private: rtc::scoped_refptr _peerConnectionFactory { nullptr }; + quint16 _lastDataChannelID { 0 }; + QHash _connectionsByWebSocket; - QHash _connectionsByDataChannel; + QHash _connectionsByDataChannel; };