Skip to content

Commit

Permalink
re-write signals
Browse files Browse the repository at this point in the history
  • Loading branch information
murat-dogan committed Mar 3, 2021
1 parent 1c7d571 commit 2176323
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 121 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 62 additions & 63 deletions src/data-channel-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,69 +35,6 @@ Napi::Object DataChannelWrapper::Init(Napi::Env env, Napi::Object exports)
DataChannelWrapper::DataChannelWrapper(const Napi::CallbackInfo &info) : Napi::ObjectWrap<DataChannelWrapper>(info)
{
mDataChannelPtr = *(info[0].As<Napi::External<std::shared_ptr<rtc::DataChannel>>>().Data());

// Signals
mDataChannelPtr->onOpen([&]() {
if (mOnOpenCallback)
mOnOpenCallback->call([](Napi::Env env, std::vector<napi_value> &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<napi_value> &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<napi_value> &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<napi_value> &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<napi_value> &args) {
// This will run in main thread and needs to construct the
// arguments for the call
args = {};
});
});

mDataChannelPtr->onMessage([&](const std::variant<rtc::binary, std::string> &message) {
if (mOnMessageCallback)
mOnMessageCallback->call([message](Napi::Env env, std::vector<napi_value> &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<std::string>(message))
{
args = {Napi::String::New(env, std::get<std::string>(message))};
}
else
{
args = {Napi::Buffer<std::byte>::Copy(env, std::get<rtc::binary>(message).data(), std::get<rtc::binary>(message).size())};
}
});
});
}

DataChannelWrapper::~DataChannelWrapper()
Expand Down Expand Up @@ -324,6 +261,15 @@ void DataChannelWrapper::onOpen(const Napi::CallbackInfo &info)

// Callback
mOnOpenCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mDataChannelPtr->onOpen([&]() {
if (mOnOpenCallback)
mOnOpenCallback->call([](Napi::Env env, std::vector<napi_value> &args) {
// This will run in main thread and needs to construct the
// arguments for the call
args = {};
});
});
}

void DataChannelWrapper::onClosed(const Napi::CallbackInfo &info)
Expand All @@ -339,6 +285,15 @@ void DataChannelWrapper::onClosed(const Napi::CallbackInfo &info)

// Callback
mOnClosedCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mDataChannelPtr->onClosed([&]() {
if (mOnClosedCallback)
mOnClosedCallback->call([](Napi::Env env, std::vector<napi_value> &args) {
// This will run in main thread and needs to construct the
// arguments for the call
args = {};
});
});
}

void DataChannelWrapper::onError(const Napi::CallbackInfo &info)
Expand All @@ -354,6 +309,15 @@ void DataChannelWrapper::onError(const Napi::CallbackInfo &info)

// Callback
mOnErrorCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mDataChannelPtr->onError([&](const std::string &error) {
if (mOnErrorCallback)
mOnErrorCallback->call([error](Napi::Env env, std::vector<napi_value> &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)
Expand All @@ -369,6 +333,15 @@ void DataChannelWrapper::onAvailable(const Napi::CallbackInfo &info)

// Callback
mOnAvailableCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mDataChannelPtr->onAvailable([&]() {
if (mOnAvailableCallback)
mOnAvailableCallback->call([](Napi::Env env, std::vector<napi_value> &args) {
// This will run in main thread and needs to construct the
// arguments for the call
args = {};
});
});
}

void DataChannelWrapper::onBufferedAmountLow(const Napi::CallbackInfo &info)
Expand All @@ -384,6 +357,15 @@ void DataChannelWrapper::onBufferedAmountLow(const Napi::CallbackInfo &info)

// Callback
mOnBufferedAmountLowCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mDataChannelPtr->onBufferedAmountLow([&]() {
if (mOnBufferedAmountLowCallback)
mOnBufferedAmountLowCallback->call([](Napi::Env env, std::vector<napi_value> &args) {
// This will run in main thread and needs to construct the
// arguments for the call
args = {};
});
});
}

void DataChannelWrapper::onMessage(const Napi::CallbackInfo &info)
Expand All @@ -399,4 +381,21 @@ void DataChannelWrapper::onMessage(const Napi::CallbackInfo &info)

// Callback
mOnMessageCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mDataChannelPtr->onMessage([&](const std::variant<rtc::binary, std::string> &message) {
if (mOnMessageCallback)
mOnMessageCallback->call([message](Napi::Env env, std::vector<napi_value> &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<std::string>(message))
{
args = {Napi::String::New(env, std::get<std::string>(message))};
}
else
{
args = {Napi::Buffer<std::byte>::Copy(env, std::get<rtc::binary>(message).data(), std::get<rtc::binary>(message).size())};
}
});
});
}
111 changes: 55 additions & 56 deletions src/peer-connection-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<napi_value> &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<napi_value> &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<napi_value> &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<napi_value> &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<rtc::DataChannel> dc) {
if (mOnDataChannelCallback)
mOnDataChannelCallback->call([dc](Napi::Env env, std::vector<napi_value> &args) {
// This will run in main thread and needs to construct the
// arguments for the call
std::shared_ptr<rtc::DataChannel> dataChannel = dc;
auto instance = DataChannelWrapper::constructor.New({Napi::External<std::shared_ptr<rtc::DataChannel>>::New(env, &dataChannel)});
args = {instance};
});
});
}

PeerConnectionWrapper::~PeerConnectionWrapper()
Expand Down Expand Up @@ -452,6 +396,17 @@ void PeerConnectionWrapper::onLocalDescription(const Napi::CallbackInfo &info)

// Callback
mOnLocalDescriptionCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mRtcPeerConnPtr->onLocalDescription([&](const rtc::Description &sdp) {
if (mOnLocalDescriptionCallback)
mOnLocalDescriptionCallback->call([sdp](Napi::Env env, std::vector<napi_value> &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)
Expand All @@ -467,6 +422,17 @@ void PeerConnectionWrapper::onLocalCandidate(const Napi::CallbackInfo &info)

// Callback
mOnLocalCandidateCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mRtcPeerConnPtr->onLocalCandidate([&](const rtc::Candidate &candidate) {
if (mOnLocalCandidateCallback)
mOnLocalCandidateCallback->call([candidate](Napi::Env env, std::vector<napi_value> &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)
Expand All @@ -482,6 +448,17 @@ void PeerConnectionWrapper::onStateChange(const Napi::CallbackInfo &info)

// Callback
mOnStateChangeCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mRtcPeerConnPtr->onStateChange([&](rtc::PeerConnection::State state) {
if (mOnStateChangeCallback)
mOnStateChangeCallback->call([state](Napi::Env env, std::vector<napi_value> &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)
Expand All @@ -497,6 +474,17 @@ void PeerConnectionWrapper::onGatheringStateChange(const Napi::CallbackInfo &inf

// Callback
mOnGatheringStateChangeCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mRtcPeerConnPtr->onGatheringStateChange([&](rtc::PeerConnection::GatheringState state) {
if (mOnGatheringStateChangeCallback)
mOnGatheringStateChangeCallback->call([state](Napi::Env env, std::vector<napi_value> &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)
Expand All @@ -512,4 +500,15 @@ void PeerConnectionWrapper::onDataChannel(const Napi::CallbackInfo &info)

// Callback
mOnDataChannelCallback = std::make_shared<ThreadSafeCallback>(info[0].As<Napi::Function>());

mRtcPeerConnPtr->onDataChannel([&](std::shared_ptr<rtc::DataChannel> dc) {
if (mOnDataChannelCallback)
mOnDataChannelCallback->call([dc](Napi::Env env, std::vector<napi_value> &args) {
// This will run in main thread and needs to construct the
// arguments for the call
std::shared_ptr<rtc::DataChannel> dataChannel = dc;
auto instance = DataChannelWrapper::constructor.New({Napi::External<std::shared_ptr<rtc::DataChannel>>::New(env, &dataChannel)});
args = {instance};
});
});
}

0 comments on commit 2176323

Please sign in to comment.