Compare commits

...

12 Commits

10 changed files with 99 additions and 66 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.7) cmake_minimum_required(VERSION 3.7)
project(libdatachannel project(libdatachannel
DESCRIPTION "WebRTC Data Channels Library" DESCRIPTION "WebRTC Data Channels Library"
VERSION 0.7.0 VERSION 0.7.1
LANGUAGES CXX) LANGUAGES CXX)
# Options # Options

View File

@ -197,5 +197,6 @@ ws->open("wss://my.websocket/service");
``` ```
## External resources ## External resources
- Rust wrappers for libdatachannel: [lerouxrgd/datachannel-rs](https://github.com/lerouxrgd/datachannel-rs) - Rust wrappers for libdatachannel: [datachannel-rs](https://github.com/lerouxrgd/datachannel-rs)
- WebAssembly wrappers compatible with libdatachannel: [datachannel-wasm](https://github.com/paullouisageneau/datachannel-wasm)

2
deps/libjuice vendored

View File

@ -84,9 +84,24 @@ template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t,
template <typename... P> class synchronized_callback { template <typename... P> class synchronized_callback {
public: public:
synchronized_callback() = default; synchronized_callback() = default;
synchronized_callback(synchronized_callback &&cb) { *this = std::move(cb); }
synchronized_callback(const synchronized_callback &cb) { *this = cb; }
synchronized_callback(std::function<void(P...)> func) { *this = std::move(func); } synchronized_callback(std::function<void(P...)> func) { *this = std::move(func); }
~synchronized_callback() { *this = nullptr; } ~synchronized_callback() { *this = nullptr; }
synchronized_callback &operator=(synchronized_callback &&cb) {
std::scoped_lock lock(mutex, cb.mutex);
callback = std::move(cb.callback);
cb.callback = nullptr;
return *this;
}
synchronized_callback &operator=(const synchronized_callback &cb) {
std::scoped_lock lock(mutex, cb.mutex);
callback = cb.callback;
return *this;
}
synchronized_callback &operator=(std::function<void(P...)> func) { synchronized_callback &operator=(std::function<void(P...)> func) {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
callback = std::move(func); callback = std::move(func);

View File

@ -37,6 +37,8 @@ struct Message : binary {
Message(Iterator begin_, Iterator end_, Type type_ = Binary) Message(Iterator begin_, Iterator end_, Type type_ = Binary)
: binary(begin_, end_), type(type_) {} : binary(begin_, end_), type(type_) {}
Message(binary &&data, Type type_ = Binary) : binary(std::move(data)), type(type_) {}
Type type; Type type;
unsigned int stream = 0; unsigned int stream = 0;
std::shared_ptr<Reliability> reliability; std::shared_ptr<Reliability> reliability;
@ -68,6 +70,15 @@ inline message_ptr make_message(size_t size, Message::Type type = Message::Binar
return message; return message;
} }
inline message_ptr make_message(binary &&data, Message::Type type = Message::Binary,
unsigned int stream = 0,
std::shared_ptr<Reliability> reliability = nullptr) {
auto message = std::make_shared<Message>(std::move(data), type);
message->stream = stream;
message->reliability = reliability;
return message;
}
} // namespace rtc } // namespace rtc
#endif #endif

View File

@ -51,13 +51,17 @@ PeerConnection::PeerConnection(const Configuration &config)
} }
PeerConnection::~PeerConnection() { PeerConnection::~PeerConnection() {
close();
PLOG_VERBOSE << "Destroying PeerConnection"; PLOG_VERBOSE << "Destroying PeerConnection";
close();
mProcessor->join();
} }
void PeerConnection::close() { void PeerConnection::close() {
PLOG_VERBOSE << "Closing PeerConnection"; PLOG_VERBOSE << "Closing PeerConnection";
closeDataChannels();
// Close data channels asynchronously
mProcessor->enqueue(std::bind(&PeerConnection::closeDataChannels, this));
closeTransports(); closeTransports();
} }
@ -439,27 +443,30 @@ shared_ptr<SctpTransport> PeerConnection::initSctpTransport() {
void PeerConnection::closeTransports() { void PeerConnection::closeTransports() {
PLOG_VERBOSE << "Closing transports"; PLOG_VERBOSE << "Closing transports";
// Change state to sink state Closed to block init methods // Change state to sink state Closed
changeState(State::Closed); changeState(State::Closed);
// Reset callbacks now that state is changed // Reset callbacks now that state is changed
resetCallbacks(); resetCallbacks();
// Pass the references to a thread, allowing to terminate a transport from its own thread // Initiate transport stop on the processor after closing the data channels
auto sctp = std::atomic_exchange(&mSctpTransport, decltype(mSctpTransport)(nullptr)); mProcessor->enqueue([this]() {
auto dtls = std::atomic_exchange(&mDtlsTransport, decltype(mDtlsTransport)(nullptr)); // Pass the pointers to a thread
auto ice = std::atomic_exchange(&mIceTransport, decltype(mIceTransport)(nullptr)); auto sctp = std::atomic_exchange(&mSctpTransport, decltype(mSctpTransport)(nullptr));
ThreadPool::Instance().enqueue([sctp, dtls, ice]() mutable { auto dtls = std::atomic_exchange(&mDtlsTransport, decltype(mDtlsTransport)(nullptr));
if (sctp) auto ice = std::atomic_exchange(&mIceTransport, decltype(mIceTransport)(nullptr));
sctp->stop(); ThreadPool::Instance().enqueue([sctp, dtls, ice]() mutable {
if (dtls) if (sctp)
dtls->stop(); sctp->stop();
if (ice) if (dtls)
ice->stop(); dtls->stop();
if (ice)
ice->stop();
sctp.reset(); sctp.reset();
dtls.reset(); dtls.reset();
ice.reset(); ice.reset();
});
}); });
} }
@ -648,7 +655,12 @@ bool PeerConnection::changeState(State state) {
} while (!mState.compare_exchange_weak(current, state)); } while (!mState.compare_exchange_weak(current, state));
mProcessor->enqueue([this, state]() { mStateChangeCallback(state); }); if (state == State::Closed)
// This is the last state change, so we may steal the callback
mProcessor->enqueue([this, cb = std::move(mStateChangeCallback)]() { cb(State::Closed); });
else
mProcessor->enqueue([this, state]() { mStateChangeCallback(state); });
return true; return true;
} }

View File

@ -451,7 +451,7 @@ void SctpTransport::sendReset(uint16_t streamId) {
mWrittenCondition.wait_for(lock, 1000ms, mWrittenCondition.wait_for(lock, 1000ms,
[&]() { return mWritten || state() != State::Connected; }); [&]() { return mWritten || state() != State::Connected; });
} else if (errno == EINVAL) { } else if (errno == EINVAL) {
PLOG_VERBOSE << "SCTP stream " << streamId << " already reset"; PLOG_DEBUG << "SCTP stream " << streamId << " already reset";
} else { } else {
PLOG_WARNING << "SCTP reset stream " << streamId << " failed, errno=" << errno; PLOG_WARNING << "SCTP reset stream " << streamId << " failed, errno=" << errno;
} }
@ -479,32 +479,22 @@ int SctpTransport::handleRecv(struct socket * /*sock*/, union sctp_sockstore /*a
// therefore partial notifications and messages need to be handled separately. // therefore partial notifications and messages need to be handled separately.
if (flags & MSG_NOTIFICATION) { if (flags & MSG_NOTIFICATION) {
// SCTP event notification // SCTP event notification
mPartialNotification.insert(mPartialNotification.end(), data, data + len);
if (flags & MSG_EOR) { if (flags & MSG_EOR) {
if (!mPartialNotification.empty()) {
mPartialNotification.insert(mPartialNotification.end(), data, data + len);
data = mPartialNotification.data();
len = mPartialNotification.size();
}
// Notification is complete, process it // Notification is complete, process it
processNotification(reinterpret_cast<const union sctp_notification *>(data), len); processNotification(
reinterpret_cast<const union sctp_notification *>(mPartialNotification.data()),
mPartialNotification.size());
mPartialNotification.clear(); mPartialNotification.clear();
} else {
mPartialNotification.insert(mPartialNotification.end(), data, data + len);
} }
} else { } else {
// SCTP message // SCTP message
mPartialMessage.insert(mPartialMessage.end(), data, data + len);
if (flags & MSG_EOR) { if (flags & MSG_EOR) {
if (!mPartialMessage.empty()) {
mPartialMessage.insert(mPartialMessage.end(), data, data + len);
data = mPartialMessage.data();
len = mPartialMessage.size();
}
// Message is complete, process it // Message is complete, process it
processData(data, len, info.rcv_sid, PayloadId(htonl(info.rcv_ppid))); processData(std::move(mPartialMessage), info.rcv_sid,
PayloadId(htonl(info.rcv_ppid)));
mPartialMessage.clear(); mPartialMessage.clear();
} else {
mPartialMessage.insert(mPartialMessage.end(), data, data + len);
} }
} }
@ -539,62 +529,56 @@ int SctpTransport::handleWrite(byte *data, size_t len, uint8_t /*tos*/, uint8_t
return 0; // success return 0; // success
} }
void SctpTransport::processData(const byte *data, size_t len, uint16_t sid, PayloadId ppid) { void SctpTransport::processData(binary &&data, uint16_t sid, PayloadId ppid) {
PLOG_VERBOSE << "Process data, len=" << len; PLOG_VERBOSE << "Process data, size=" << data.size();
// The usage of the PPIDs "WebRTC String Partial" and "WebRTC Binary Partial" is deprecated. // The usage of the PPIDs "WebRTC String Partial" and "WebRTC Binary Partial" is deprecated.
// See https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.6 // See https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.6
// We handle them at reception for compatibility reasons but should never send them. // We handle them at reception for compatibility reasons but should never send them.
switch (ppid) { switch (ppid) {
case PPID_CONTROL: case PPID_CONTROL:
recv(make_message(data, data + len, Message::Control, sid)); recv(make_message(std::move(data), Message::Control, sid));
break; break;
case PPID_STRING_PARTIAL: // deprecated case PPID_STRING_PARTIAL: // deprecated
mPartialStringData.insert(mPartialStringData.end(), data, data + len); mPartialStringData.insert(mPartialStringData.end(), data.begin(), data.end());
break; break;
case PPID_STRING: case PPID_STRING:
if (mPartialStringData.empty()) { if (mPartialStringData.empty()) {
mBytesReceived += len; mBytesReceived += data.size();
recv(make_message(data, data + len, Message::String, sid)); recv(make_message(std::move(data), Message::String, sid));
} else { } else {
mPartialStringData.insert(mPartialStringData.end(), data, data + len); mPartialStringData.insert(mPartialStringData.end(), data.begin(), data.end());
mBytesReceived += mPartialStringData.size(); mBytesReceived += mPartialStringData.size();
recv(make_message(mPartialStringData.begin(), mPartialStringData.end(), Message::String, recv(make_message(std::move(mPartialStringData), Message::String, sid));
sid));
mPartialStringData.clear(); mPartialStringData.clear();
} }
break; break;
case PPID_STRING_EMPTY: case PPID_STRING_EMPTY:
// This only accounts for when the partial data is empty recv(make_message(std::move(mPartialStringData), Message::String, sid));
recv(make_message(mPartialStringData.begin(), mPartialStringData.end(), Message::String,
sid));
mPartialStringData.clear(); mPartialStringData.clear();
break; break;
case PPID_BINARY_PARTIAL: // deprecated case PPID_BINARY_PARTIAL: // deprecated
mPartialBinaryData.insert(mPartialBinaryData.end(), data, data + len); mPartialBinaryData.insert(mPartialBinaryData.end(), data.begin(), data.end());
break; break;
case PPID_BINARY: case PPID_BINARY:
if (mPartialBinaryData.empty()) { if (mPartialBinaryData.empty()) {
mBytesReceived += len; mBytesReceived += data.size();
recv(make_message(data, data + len, Message::Binary, sid)); recv(make_message(std::move(data), Message::Binary, sid));
} else { } else {
mPartialBinaryData.insert(mPartialBinaryData.end(), data, data + len); mPartialBinaryData.insert(mPartialBinaryData.end(), data.begin(), data.end());
mBytesReceived += mPartialStringData.size(); mBytesReceived += mPartialBinaryData.size();
recv(make_message(mPartialBinaryData.begin(), mPartialBinaryData.end(), Message::Binary, recv(make_message(std::move(mPartialBinaryData), Message::Binary, sid));
sid));
mPartialBinaryData.clear(); mPartialBinaryData.clear();
} }
break; break;
case PPID_BINARY_EMPTY: case PPID_BINARY_EMPTY:
// This only accounts for when the partial data is empty recv(make_message(std::move(mPartialBinaryData), Message::Binary, sid));
recv(make_message(mPartialBinaryData.begin(), mPartialBinaryData.end(), Message::Binary,
sid));
mPartialBinaryData.clear(); mPartialBinaryData.clear();
break; break;

View File

@ -86,7 +86,7 @@ private:
int handleSend(size_t free); int handleSend(size_t free);
int handleWrite(byte *data, size_t len, uint8_t tos, uint8_t set_df); int handleWrite(byte *data, size_t len, uint8_t tos, uint8_t set_df);
void processData(const byte *data, size_t len, uint16_t streamId, PayloadId ppid); void processData(binary &&data, uint16_t streamId, PayloadId ppid);
void processNotification(const union sctp_notification *notify, size_t len); void processNotification(const union sctp_notification *notify, size_t len);
const uint16_t mPort; const uint16_t mPort;

View File

@ -65,10 +65,20 @@ public:
virtual bool send(message_ptr message) { return outgoing(message); } virtual bool send(message_ptr message) { return outgoing(message); }
protected: protected:
void recv(message_ptr message) { mRecvCallback(message); } void recv(message_ptr message) {
try {
mRecvCallback(message);
} catch (const std::exception &e) {
PLOG_WARNING << e.what();
}
}
void changeState(State state) { void changeState(State state) {
if (mState.exchange(state) != state) try {
mStateChangeCallback(state); if (mState.exchange(state) != state)
mStateChangeCallback(state);
} catch (const std::exception &e) {
PLOG_WARNING << e.what();
}
} }
virtual void incoming(message_ptr message) { recv(message); } virtual void incoming(message_ptr message) { recv(message); }

View File

@ -317,7 +317,7 @@ void WebSocket::closeTransports() {
// Reset callbacks now that state is changed // Reset callbacks now that state is changed
resetCallbacks(); resetCallbacks();
// Pass the references to a thread, allowing to terminate a transport from its own thread // Pass the pointers to a thread, allowing to terminate a transport from its own thread
auto ws = std::atomic_exchange(&mWsTransport, decltype(mWsTransport)(nullptr)); auto ws = std::atomic_exchange(&mWsTransport, decltype(mWsTransport)(nullptr));
auto tls = std::atomic_exchange(&mTlsTransport, decltype(mTlsTransport)(nullptr)); auto tls = std::atomic_exchange(&mTlsTransport, decltype(mTlsTransport)(nullptr));
auto tcp = std::atomic_exchange(&mTcpTransport, decltype(mTcpTransport)(nullptr)); auto tcp = std::atomic_exchange(&mTcpTransport, decltype(mTcpTransport)(nullptr));