Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/Network/NetherNet/NetherNetConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ namespace nethernet {

void setRemoteRequest(const std::string &request) { mRemoteRequest = request; }

// the candidates the peer signalled after its offer, as received, a line each
std::string getRemoteCandidates() const;

void addRemoteCandidate(const std::string &candidate);

// the channels the peer opened, in the order it opened them: label, stream and protocol, a line each
std::string getRemoteChannels() const;

bool attachChannel(const std::shared_ptr<rtc::DataChannel> &channel);

bool areChannelsReady() const;
Expand All @@ -83,6 +91,8 @@ namespace nethernet {
std::string mRemoteFingerprint;
std::string mRemoteOffer;
std::string mRemoteRequest;
std::string mRemoteCandidates;
std::string mRemoteChannels;
std::shared_ptr<rtc::PeerConnection> mPeerConnection;

mutable std::mutex mMutex;
Expand Down
1 change: 1 addition & 0 deletions include/Network/NetherNet/NetherNetInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class NetherNetInstance : public Connector {
std::shared_ptr<nethernet::Signaling> mOnlineSignaling;
unsigned int mSignalSubscription = 0;
std::unordered_map<std::string, std::weak_ptr<rtc::PeerConnection>> mNegotiating;
std::unordered_map<std::string, std::weak_ptr<nethernet::Connection>> mNegotiatingConnections;
std::unordered_map<std::string, std::vector<std::string>> mBufferedCandidates;
std::vector<std::thread> mNegotiationThreads;

Expand Down
31 changes: 31 additions & 0 deletions src/Network/NetherNet/NetherNetConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ namespace nethernet {
* is slowed down by the transport instead of filling memory.
*/
const size_t MAX_INCOMING_PACKETS = 256;

// what is kept of a peer's signalling for whoever inspects the connection
const size_t MAX_REMOTE_CANDIDATES = 8 * 1024;
const size_t MAX_REMOTE_CHANNELS = 1024;
}

Connection::Connection(const NetworkIdentifier &id, std::shared_ptr<rtc::PeerConnection> peerConnection)
Expand All @@ -31,10 +35,37 @@ namespace nethernet {
close();
}

std::string Connection::getRemoteCandidates() const {
std::lock_guard<std::mutex> lock(mMutex);
return mRemoteCandidates;
}

void Connection::addRemoteCandidate(const std::string &candidate) {
std::lock_guard<std::mutex> lock(mMutex);
if (mRemoteCandidates.size() + candidate.size() + 1 > MAX_REMOTE_CANDIDATES)
return;
mRemoteCandidates += candidate;
mRemoteCandidates.push_back('\n');
}

std::string Connection::getRemoteChannels() const {
std::lock_guard<std::mutex> lock(mMutex);
return mRemoteChannels;
}

bool Connection::attachChannel(const std::shared_ptr<rtc::DataChannel> &channel) {
const std::string label = channel->label();
const rtc::Reliability reliability = channel->reliability();

{
const std::optional<uint16_t> stream = channel->stream();
const std::string line = label + " " + (stream.has_value() ? std::to_string(stream.value()) : "-") + " "
+ channel->protocol() + "\n";
std::lock_guard<std::mutex> lock(mMutex);
if (mRemoteChannels.size() + line.size() <= MAX_REMOTE_CHANNELS)
mRemoteChannels += line;
}

ChannelReliability kind;

if (label == RELIABLE_CHANNEL_LABEL) {
Expand Down
16 changes: 16 additions & 0 deletions src/Network/NetherNet/NetherNetInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ void NetherNetInstance::_detachSignaling() {

std::lock_guard<std::mutex> lock(mMutex);
mNegotiating.clear();
mNegotiatingConnections.clear();
mBufferedCandidates.clear();
}

Expand Down Expand Up @@ -286,6 +287,7 @@ void NetherNetInstance::_answerOffer(nethernet::Signal offer) {

void NetherNetInstance::_addRemoteCandidate(const std::string &signalKey, const std::string &candidate) {
std::shared_ptr<rtc::PeerConnection> peerConnection;
std::shared_ptr<nethernet::Connection> connection;

{
std::lock_guard<std::mutex> lock(mMutex);
Expand All @@ -296,8 +298,14 @@ void NetherNetInstance::_addRemoteCandidate(const std::string &signalKey, const
mBufferedCandidates[signalKey].push_back(candidate);
return;
}

const auto owner = mNegotiatingConnections.find(signalKey);
connection = owner != mNegotiatingConnections.end() ? owner->second.lock() : nullptr;
}

if (connection != nullptr)
connection->addRemoteCandidate(candidate);

try {
peerConnection->addRemoteCandidate(rtc::Candidate(nethernet::Description::stripCandidatePrefix(candidate), "0"));
} catch (const std::exception &error) {
Expand Down Expand Up @@ -467,6 +475,7 @@ bool NetherNetInstance::_negotiateOffer(const std::string &networkID, const std:
{
std::lock_guard<std::mutex> lock(mMutex);
mNegotiating[signalKey] = peerConnection;
mNegotiatingConnections[signalKey] = connection;
const auto it = mBufferedCandidates.find(signalKey);
if (it != mBufferedCandidates.end()) {
buffered = std::move(it->second);
Expand Down Expand Up @@ -621,6 +630,13 @@ void NetherNetInstance::runEvents() {
++it;
}

for (auto it = mNegotiatingConnections.begin(); it != mNegotiatingConnections.end();) {
if (it->second.expired())
it = mNegotiatingConnections.erase(it);
else
++it;
}

for (auto it = mPeers.begin(); it != mPeers.end();) {
if (it->second->isClosed()) {
dropped.push_back(it->second);
Expand Down
Loading