Browse Source

Merge pull request #514 from paullouisageneau/refactor-iterate-dc

Refactor PeerConnection::iterateDataChannels()
Paul-Louis Ageneau 3 years ago
parent
commit
5ab205d6c2
2 changed files with 11 additions and 7 deletions
  1. 10 7
      src/impl/peerconnection.cpp
  2. 1 0
      src/impl/peerconnection.hpp

+ 10 - 7
src/impl/peerconnection.cpp

@@ -565,6 +565,7 @@ void PeerConnection::forwardBufferedAmount(uint16_t stream, size_t amount) {
 }
 }
 
 
 shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(string label, DataChannelInit init) {
 shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(string label, DataChannelInit init) {
+	cleanupDataChannels();
 	std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
 	std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
 	uint16_t stream;
 	uint16_t stream;
 	if (init.id) {
 	if (init.id) {
@@ -630,23 +631,26 @@ void PeerConnection::shiftDataChannels() {
 	}
 	}
 }
 }
 
 
-void PeerConnection::iterateDataChannels(
-    std::function<void(shared_ptr<DataChannel> channel)> func) {
-	// Iterate
+void PeerConnection::iterateDataChannels(std::function<void(shared_ptr<DataChannel> channel)> func) {
+	std::vector<shared_ptr<DataChannel>> locked;
 	{
 	{
 		std::shared_lock lock(mDataChannelsMutex); // read-only
 		std::shared_lock lock(mDataChannelsMutex); // read-only
+		locked.reserve(mDataChannels.size());
 		auto it = mDataChannels.begin();
 		auto it = mDataChannels.begin();
 		while (it != mDataChannels.end()) {
 		while (it != mDataChannels.end()) {
 			auto channel = it->second.lock();
 			auto channel = it->second.lock();
 			if (channel && !channel->isClosed())
 			if (channel && !channel->isClosed())
-				func(channel);
+				locked.push_back(std::move(channel));
 
 
 			++it;
 			++it;
 		}
 		}
 	}
 	}
 
 
-	// Cleanup
-	{
+	for(auto &channel : locked)
+		func(std::move(channel));
+}
+
+void PeerConnection::cleanupDataChannels() {
 		std::unique_lock lock(mDataChannelsMutex); // we are going to erase
 		std::unique_lock lock(mDataChannelsMutex); // we are going to erase
 		auto it = mDataChannels.begin();
 		auto it = mDataChannels.begin();
 		while (it != mDataChannels.end()) {
 		while (it != mDataChannels.end()) {
@@ -657,7 +661,6 @@ void PeerConnection::iterateDataChannels(
 
 
 			++it;
 			++it;
 		}
 		}
-	}
 }
 }
 
 
 void PeerConnection::openDataChannels() {
 void PeerConnection::openDataChannels() {

+ 1 - 0
src/impl/peerconnection.hpp

@@ -69,6 +69,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
 	shared_ptr<DataChannel> findDataChannel(uint16_t stream);
 	shared_ptr<DataChannel> findDataChannel(uint16_t stream);
 	void shiftDataChannels();
 	void shiftDataChannels();
 	void iterateDataChannels(std::function<void(shared_ptr<DataChannel> channel)> func);
 	void iterateDataChannels(std::function<void(shared_ptr<DataChannel> channel)> func);
+	void cleanupDataChannels();
 	void openDataChannels();
 	void openDataChannels();
 	void closeDataChannels();
 	void closeDataChannels();
 	void remoteCloseDataChannels();
 	void remoteCloseDataChannels();