diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b84430b..7074f6c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.15) cmake_policy(SET CMP0091 NEW) -project(node_datachannel VERSION 0.0.23) +project(node_datachannel VERSION 0.0.25) include_directories(${CMAKE_JS_INC}) diff --git a/package-lock.json b/package-lock.json index 506904a2..6617076e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-datachannel", - "version": "0.0.24", + "version": "0.0.25", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/data-channel-wrapper.cpp b/src/data-channel-wrapper.cpp index fcf82462..cc166880 100644 --- a/src/data-channel-wrapper.cpp +++ b/src/data-channel-wrapper.cpp @@ -35,69 +35,6 @@ Napi::Object DataChannelWrapper::Init(Napi::Env env, Napi::Object exports) DataChannelWrapper::DataChannelWrapper(const Napi::CallbackInfo &info) : Napi::ObjectWrap(info) { mDataChannelPtr = *(info[0].As>>().Data()); - - // Signals - mDataChannelPtr->onOpen([&]() { - if (mOnOpenCallback) - mOnOpenCallback->call([](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - args = {}; - }); - }); - - mDataChannelPtr->onClosed([&]() { - if (mOnClosedCallback) - mOnClosedCallback->call([](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - args = {}; - }); - }); - - mDataChannelPtr->onError([&](const std::string &error) { - if (mOnErrorCallback) - mOnErrorCallback->call([error](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - args = {Napi::String::New(env, error)}; - }); - }); - - mDataChannelPtr->onAvailable([&]() { - if (mOnAvailableCallback) - mOnAvailableCallback->call([](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - args = {}; - }); - }); - - mDataChannelPtr->onBufferedAmountLow([&]() { - if (mOnBufferedAmountLowCallback) - mOnBufferedAmountLowCallback->call([](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - args = {}; - }); - }); - - mDataChannelPtr->onMessage([&](const std::variant &message) { - if (mOnMessageCallback) - mOnMessageCallback->call([message](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - Napi::Object payload = Napi::Object::New(env); - if (std::holds_alternative(message)) - { - args = {Napi::String::New(env, std::get(message))}; - } - else - { - args = {Napi::Buffer::Copy(env, std::get(message).data(), std::get(message).size())}; - } - }); - }); } DataChannelWrapper::~DataChannelWrapper() @@ -324,6 +261,15 @@ void DataChannelWrapper::onOpen(const Napi::CallbackInfo &info) // Callback mOnOpenCallback = std::make_shared(info[0].As()); + + mDataChannelPtr->onOpen([&]() { + if (mOnOpenCallback) + mOnOpenCallback->call([](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + args = {}; + }); + }); } void DataChannelWrapper::onClosed(const Napi::CallbackInfo &info) @@ -339,6 +285,15 @@ void DataChannelWrapper::onClosed(const Napi::CallbackInfo &info) // Callback mOnClosedCallback = std::make_shared(info[0].As()); + + mDataChannelPtr->onClosed([&]() { + if (mOnClosedCallback) + mOnClosedCallback->call([](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + args = {}; + }); + }); } void DataChannelWrapper::onError(const Napi::CallbackInfo &info) @@ -354,6 +309,15 @@ void DataChannelWrapper::onError(const Napi::CallbackInfo &info) // Callback mOnErrorCallback = std::make_shared(info[0].As()); + + mDataChannelPtr->onError([&](const std::string &error) { + if (mOnErrorCallback) + mOnErrorCallback->call([error](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + args = {Napi::String::New(env, error)}; + }); + }); } void DataChannelWrapper::onAvailable(const Napi::CallbackInfo &info) @@ -369,6 +333,15 @@ void DataChannelWrapper::onAvailable(const Napi::CallbackInfo &info) // Callback mOnAvailableCallback = std::make_shared(info[0].As()); + + mDataChannelPtr->onAvailable([&]() { + if (mOnAvailableCallback) + mOnAvailableCallback->call([](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + args = {}; + }); + }); } void DataChannelWrapper::onBufferedAmountLow(const Napi::CallbackInfo &info) @@ -384,6 +357,15 @@ void DataChannelWrapper::onBufferedAmountLow(const Napi::CallbackInfo &info) // Callback mOnBufferedAmountLowCallback = std::make_shared(info[0].As()); + + mDataChannelPtr->onBufferedAmountLow([&]() { + if (mOnBufferedAmountLowCallback) + mOnBufferedAmountLowCallback->call([](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + args = {}; + }); + }); } void DataChannelWrapper::onMessage(const Napi::CallbackInfo &info) @@ -399,4 +381,21 @@ void DataChannelWrapper::onMessage(const Napi::CallbackInfo &info) // Callback mOnMessageCallback = std::make_shared(info[0].As()); + + mDataChannelPtr->onMessage([&](const std::variant &message) { + if (mOnMessageCallback) + mOnMessageCallback->call([message](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + Napi::Object payload = Napi::Object::New(env); + if (std::holds_alternative(message)) + { + args = {Napi::String::New(env, std::get(message))}; + } + else + { + args = {Napi::Buffer::Copy(env, std::get(message).data(), std::get(message).size())}; + } + }); + }); } \ No newline at end of file diff --git a/src/peer-connection-wrapper.cpp b/src/peer-connection-wrapper.cpp index 693caf30..1500dc28 100644 --- a/src/peer-connection-wrapper.cpp +++ b/src/peer-connection-wrapper.cpp @@ -158,62 +158,6 @@ PeerConnectionWrapper::PeerConnectionWrapper(const Napi::CallbackInfo &info) : N Napi::Error::New(env, std::string("libdatachannel error while creating peerConnection# ") + ex.what()).ThrowAsJavaScriptException(); return; } - - // Signals - mRtcPeerConnPtr->onLocalDescription([&](const rtc::Description &sdp) { - if (mOnLocalDescriptionCallback) - mOnLocalDescriptionCallback->call([sdp](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - args = { - Napi::String::New(env, std::string(sdp)), - Napi::String::New(env, sdp.typeString())}; - }); - }); - - mRtcPeerConnPtr->onLocalCandidate([&](const rtc::Candidate &candidate) { - if (mOnLocalCandidateCallback) - mOnLocalCandidateCallback->call([candidate](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - args = { - Napi::String::New(env, std::string(candidate)), - Napi::String::New(env, candidate.mid())}; - }); - }); - - mRtcPeerConnPtr->onStateChange([&](rtc::PeerConnection::State state) { - if (mOnStateChangeCallback) - mOnStateChangeCallback->call([state](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - std::ostringstream stream; - stream << state; - args = {Napi::String::New(env, stream.str())}; - }); - }); - - mRtcPeerConnPtr->onGatheringStateChange([&](rtc::PeerConnection::GatheringState state) { - if (mOnGatheringStateChangeCallback) - mOnGatheringStateChangeCallback->call([state](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - std::ostringstream stream; - stream << state; - args = {Napi::String::New(env, stream.str())}; - }); - }); - - mRtcPeerConnPtr->onDataChannel([&](std::shared_ptr dc) { - if (mOnDataChannelCallback) - mOnDataChannelCallback->call([dc](Napi::Env env, std::vector &args) { - // This will run in main thread and needs to construct the - // arguments for the call - std::shared_ptr dataChannel = dc; - auto instance = DataChannelWrapper::constructor.New({Napi::External>::New(env, &dataChannel)}); - args = {instance}; - }); - }); } PeerConnectionWrapper::~PeerConnectionWrapper() @@ -452,6 +396,17 @@ void PeerConnectionWrapper::onLocalDescription(const Napi::CallbackInfo &info) // Callback mOnLocalDescriptionCallback = std::make_shared(info[0].As()); + + mRtcPeerConnPtr->onLocalDescription([&](const rtc::Description &sdp) { + if (mOnLocalDescriptionCallback) + mOnLocalDescriptionCallback->call([sdp](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + args = { + Napi::String::New(env, std::string(sdp)), + Napi::String::New(env, sdp.typeString())}; + }); + }); } void PeerConnectionWrapper::onLocalCandidate(const Napi::CallbackInfo &info) @@ -467,6 +422,17 @@ void PeerConnectionWrapper::onLocalCandidate(const Napi::CallbackInfo &info) // Callback mOnLocalCandidateCallback = std::make_shared(info[0].As()); + + mRtcPeerConnPtr->onLocalCandidate([&](const rtc::Candidate &candidate) { + if (mOnLocalCandidateCallback) + mOnLocalCandidateCallback->call([candidate](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + args = { + Napi::String::New(env, std::string(candidate)), + Napi::String::New(env, candidate.mid())}; + }); + }); } void PeerConnectionWrapper::onStateChange(const Napi::CallbackInfo &info) @@ -482,6 +448,17 @@ void PeerConnectionWrapper::onStateChange(const Napi::CallbackInfo &info) // Callback mOnStateChangeCallback = std::make_shared(info[0].As()); + + mRtcPeerConnPtr->onStateChange([&](rtc::PeerConnection::State state) { + if (mOnStateChangeCallback) + mOnStateChangeCallback->call([state](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + std::ostringstream stream; + stream << state; + args = {Napi::String::New(env, stream.str())}; + }); + }); } void PeerConnectionWrapper::onGatheringStateChange(const Napi::CallbackInfo &info) @@ -497,6 +474,17 @@ void PeerConnectionWrapper::onGatheringStateChange(const Napi::CallbackInfo &inf // Callback mOnGatheringStateChangeCallback = std::make_shared(info[0].As()); + + mRtcPeerConnPtr->onGatheringStateChange([&](rtc::PeerConnection::GatheringState state) { + if (mOnGatheringStateChangeCallback) + mOnGatheringStateChangeCallback->call([state](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + std::ostringstream stream; + stream << state; + args = {Napi::String::New(env, stream.str())}; + }); + }); } void PeerConnectionWrapper::onDataChannel(const Napi::CallbackInfo &info) @@ -512,4 +500,15 @@ void PeerConnectionWrapper::onDataChannel(const Napi::CallbackInfo &info) // Callback mOnDataChannelCallback = std::make_shared(info[0].As()); + + mRtcPeerConnPtr->onDataChannel([&](std::shared_ptr dc) { + if (mOnDataChannelCallback) + mOnDataChannelCallback->call([dc](Napi::Env env, std::vector &args) { + // This will run in main thread and needs to construct the + // arguments for the call + std::shared_ptr dataChannel = dc; + auto instance = DataChannelWrapper::constructor.New({Napi::External>::New(env, &dataChannel)}); + args = {instance}; + }); + }); } \ No newline at end of file