Browse Source

Fixed datachannel map cleanup to erase only on remote reset

Paul-Louis Ageneau 2 years ago
parent
commit
ad50630461
2 changed files with 7 additions and 17 deletions
  1. 7 16
      src/impl/peerconnection.cpp
  2. 0 1
      src/impl/peerconnection.hpp

+ 7 - 16
src/impl/peerconnection.cpp

@@ -455,6 +455,12 @@ void PeerConnection::forwardMessage(message_ptr message) {
 		mDataChannels.emplace(stream, channel);
 	}
 
+	if (message->type == Message::Reset) {
+		// Incoming stream is reset, unregister it
+		std::unique_lock lock(mDataChannelsMutex); // we are going to erase
+		mDataChannels.erase(stream);
+	}
+
 	if (!channel) {
 		if (message->type == Message::Reset)
 			return; // ignore
@@ -557,7 +563,6 @@ void PeerConnection::forwardBufferedAmount(uint16_t stream, size_t amount) {
 }
 
 shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(string label, DataChannelInit init) {
-	cleanupDataChannels();
 	std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
 
 	// If the DataChannel is user-negotiated, do not negotiate it in-band
@@ -631,8 +636,7 @@ void PeerConnection::assignDataChannels() {
 			if (stream > maxStream)
 				throw std::runtime_error("Too many DataChannels");
 
-			auto it = mDataChannels.find(stream);
-			if (it == mDataChannels.end() || !it->second.lock())
+			if (mDataChannels.find(stream) == mDataChannels.end())
 				break;
 
 			stream += 2;
@@ -672,19 +676,6 @@ void PeerConnection::iterateDataChannels(
 	}
 }
 
-void PeerConnection::cleanupDataChannels() {
-	std::unique_lock lock(mDataChannelsMutex); // we are going to erase
-	auto it = mDataChannels.begin();
-	while (it != mDataChannels.end()) {
-		if (!it->second.lock()) {
-			it = mDataChannels.erase(it);
-			continue;
-		}
-
-		++it;
-	}
-}
-
 void PeerConnection::openDataChannels() {
 	if (auto transport = std::atomic_load(&mSctpTransport))
 		iterateDataChannels([&](shared_ptr<DataChannel> channel) {

+ 0 - 1
src/impl/peerconnection.hpp

@@ -62,7 +62,6 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
 	uint16_t maxDataChannelStream() const;
 	void assignDataChannels();
 	void iterateDataChannels(std::function<void(shared_ptr<DataChannel> channel)> func);
-	void cleanupDataChannels();
 	void openDataChannels();
 	void closeDataChannels();
 	void remoteCloseDataChannels();