|
| 1 | +#include "ice-udp-mux-listener-wrapper.h" |
| 2 | + |
| 3 | +#include "plog/Log.h" |
| 4 | + |
| 5 | +#include <cctype> |
| 6 | +#include <sstream> |
| 7 | + |
| 8 | +Napi::FunctionReference IceUdpMuxListenerWrapper::constructor = Napi::FunctionReference(); |
| 9 | +std::unordered_set<IceUdpMuxListenerWrapper *> IceUdpMuxListenerWrapper::instances; |
| 10 | + |
| 11 | +void IceUdpMuxListenerWrapper::StopAll() |
| 12 | +{ |
| 13 | + PLOG_DEBUG << "IceUdpMuxListenerWrapper StopAll() called"; |
| 14 | + auto copy(instances); |
| 15 | + for (auto inst : copy) |
| 16 | + inst->doCleanup(); |
| 17 | +} |
| 18 | + |
| 19 | +Napi::Object IceUdpMuxListenerWrapper::Init(Napi::Env env, Napi::Object exports) |
| 20 | +{ |
| 21 | + Napi::HandleScope scope(env); |
| 22 | + |
| 23 | + Napi::Function func = DefineClass( |
| 24 | + env, |
| 25 | + "IceUdpMuxListener", |
| 26 | + { |
| 27 | + InstanceMethod("stop", &IceUdpMuxListenerWrapper::stop), |
| 28 | + InstanceMethod("onUnhandledStunRequest", &IceUdpMuxListenerWrapper::onUnhandledStunRequest), |
| 29 | + InstanceMethod("port", &IceUdpMuxListenerWrapper::port), |
| 30 | + InstanceMethod("address", &IceUdpMuxListenerWrapper::address) |
| 31 | + }); |
| 32 | + |
| 33 | + // If this is not the first call, we don't want to reassign the constructor (hot-reload problem) |
| 34 | + if(constructor.IsEmpty()) |
| 35 | + { |
| 36 | + constructor = Napi::Persistent(func); |
| 37 | + constructor.SuppressDestruct(); |
| 38 | + } |
| 39 | + |
| 40 | + exports.Set("IceUdpMuxListener", func); |
| 41 | + return exports; |
| 42 | +} |
| 43 | + |
| 44 | +IceUdpMuxListenerWrapper::IceUdpMuxListenerWrapper(const Napi::CallbackInfo &info) : Napi::ObjectWrap<IceUdpMuxListenerWrapper>(info) |
| 45 | +{ |
| 46 | + PLOG_DEBUG << "IceUdpMuxListenerWrapper Constructor called"; |
| 47 | + Napi::Env env = info.Env(); |
| 48 | + int length = info.Length(); |
| 49 | + |
| 50 | + // We expect (Number, String?) as param |
| 51 | + if (length > 0 && info[0].IsNumber()) { |
| 52 | + // Port |
| 53 | + mPort = info[0].As<Napi::Number>().ToNumber().Uint32Value(); |
| 54 | + } else { |
| 55 | + Napi::TypeError::New(env, "Port (Number) and optional Address (String) expected").ThrowAsJavaScriptException(); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (length > 1 && info[1].IsString()) { |
| 60 | + // Address |
| 61 | + mAddress = info[1].As<Napi::String>().ToString(); |
| 62 | + } |
| 63 | + |
| 64 | + iceUdpMuxListenerPtr = std::make_unique<rtc::IceUdpMuxListener>(mPort, mAddress); |
| 65 | + instances.insert(this); |
| 66 | +} |
| 67 | + |
| 68 | +IceUdpMuxListenerWrapper::~IceUdpMuxListenerWrapper() |
| 69 | +{ |
| 70 | + PLOG_DEBUG << "IceUdpMuxListenerWrapper Destructor called"; |
| 71 | + doCleanup(); |
| 72 | +} |
| 73 | + |
| 74 | +void IceUdpMuxListenerWrapper::doCleanup() |
| 75 | +{ |
| 76 | + PLOG_DEBUG << "IceUdpMuxListenerWrapper::doCleanup() called"; |
| 77 | + |
| 78 | + if (iceUdpMuxListenerPtr) |
| 79 | + { |
| 80 | + iceUdpMuxListenerPtr->stop(); |
| 81 | + iceUdpMuxListenerPtr.reset(); |
| 82 | + } |
| 83 | + |
| 84 | + mOnUnhandledStunRequestCallback.reset(); |
| 85 | + instances.erase(this); |
| 86 | +} |
| 87 | + |
| 88 | +Napi::Value IceUdpMuxListenerWrapper::port(const Napi::CallbackInfo &info) |
| 89 | +{ |
| 90 | + Napi::Env env = info.Env(); |
| 91 | + |
| 92 | + return Napi::Number::New(env, mPort); |
| 93 | +} |
| 94 | + |
| 95 | +Napi::Value IceUdpMuxListenerWrapper::address(const Napi::CallbackInfo &info) |
| 96 | +{ |
| 97 | + Napi::Env env = info.Env(); |
| 98 | + |
| 99 | + if (!mAddress.has_value()) { |
| 100 | + return env.Undefined(); |
| 101 | + } |
| 102 | + |
| 103 | + return Napi::String::New(env, mAddress.value()); |
| 104 | +} |
| 105 | + |
| 106 | +void IceUdpMuxListenerWrapper::stop(const Napi::CallbackInfo &info) |
| 107 | +{ |
| 108 | + PLOG_DEBUG << "IceUdpMuxListenerWrapper::stop() called"; |
| 109 | + doCleanup(); |
| 110 | +} |
| 111 | + |
| 112 | +void IceUdpMuxListenerWrapper::onUnhandledStunRequest(const Napi::CallbackInfo &info) |
| 113 | +{ |
| 114 | + PLOG_DEBUG << "IceUdpMuxListenerWrapper::onUnhandledStunRequest() called"; |
| 115 | + Napi::Env env = info.Env(); |
| 116 | + int length = info.Length(); |
| 117 | + |
| 118 | + if (!iceUdpMuxListenerPtr) |
| 119 | + { |
| 120 | + Napi::Error::New(env, "IceUdpMuxListenerWrapper::onUnhandledStunRequest() called on destroyed IceUdpMuxListener").ThrowAsJavaScriptException(); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if (length < 1 || !info[0].IsFunction()) |
| 125 | + { |
| 126 | + Napi::TypeError::New(env, "Function expected").ThrowAsJavaScriptException(); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + // Callback |
| 131 | + mOnUnhandledStunRequestCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>()); |
| 132 | + |
| 133 | + iceUdpMuxListenerPtr->OnUnhandledStunRequest([&](rtc::IceUdpMuxRequest request) |
| 134 | + { |
| 135 | + PLOG_DEBUG << "IceUdpMuxListenerWrapper::onUnhandledStunRequest() IceUdpMuxCallback call(1)"; |
| 136 | + |
| 137 | + if (mOnUnhandledStunRequestCallback) { |
| 138 | + mOnUnhandledStunRequestCallback->call([request = std::move(request)](Napi::Env env, std::vector<napi_value> &args) { |
| 139 | + Napi::Object reqObj = Napi::Object::New(env); |
| 140 | + reqObj.Set("ufrag", request.remoteUfrag.c_str()); |
| 141 | + reqObj.Set("host", request.remoteAddress.c_str()); |
| 142 | + reqObj.Set("port", request.remotePort); |
| 143 | + |
| 144 | + args = {reqObj}; |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + PLOG_DEBUG << "IceUdpMuxListenerWrapper::onUnhandledStunRequest() IceUdpMuxCallback call(2)"; |
| 149 | + }); |
| 150 | +} |
0 commit comments