From 72065d061dc0371619d18f93fb64e5c6a24f0642 Mon Sep 17 00:00:00 2001 From: xLuxy <67131061+xLuxy@users.noreply.github.com> Date: Fri, 29 Sep 2023 12:41:49 +0200 Subject: [PATCH] server: finish rpc stuff --- server/src/CNodeResourceImpl.cpp | 37 +++++++++++++++++--------------- server/src/bindings/Main.cpp | 26 +++++++++++++--------- shared/V8ResourceImpl.h | 2 +- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/server/src/CNodeResourceImpl.cpp b/server/src/CNodeResourceImpl.cpp index 7cd5f3df..624ca765 100644 --- a/server/src/CNodeResourceImpl.cpp +++ b/server/src/CNodeResourceImpl.cpp @@ -114,6 +114,9 @@ bool CNodeResourceImpl::Stop() uv_loop_close(uvLoop); delete uvLoop; + vehiclePassengers.clear(); + rpcHandlers.clear(); + return true; } @@ -245,33 +248,33 @@ void CNodeResourceImpl::HandleVehiclePassengerSeatEvents(const alt::CEvent* ev) void CNodeResourceImpl::HandleClientRpcEvent(const alt::CClientScriptRPCEvent& ev) { - auto handlers = rpcHandlers[ev.GetName()]; + if (!rpcHandlers.contains(ev.GetName())) + return; + + auto handler = rpcHandlers[ev.GetName()]; auto context = GetContext(); std::vector> args; args.push_back(GetBaseObjectOrNull(ev.GetTarget())); args.push_back(V8Helpers::MValueToV8(ev.GetAnswer())); - for (auto handler : handlers) - { - v8::TryCatch tryCatch(isolate); - v8::MaybeLocal ret = V8Helpers::CallFunctionWithTimeout(handler, context, args); + v8::TryCatch tryCatch(isolate); + v8::MaybeLocal ret = V8Helpers::CallFunctionWithTimeout(handler, context, args); - if (!tryCatch.HasCaught()) + if (!tryCatch.HasCaught()) + { + v8::Local returnValue = ret.ToLocalChecked(); + alt::ICore::Instance().TriggerClientRPCAnswer(ev.GetTarget(), ev.GetAnswerID(), V8Helpers::V8ToMValue(returnValue), ""); + } + else + { + std::string errorMessage = "Unknown error"; + if (!tryCatch.Message().IsEmpty()) { - v8::Local returnValue = ret.ToLocalChecked(); - alt::ICore::Instance().TriggerClientRPCAnswer(ev.GetTarget(), ev.GetAnswerID(), V8Helpers::V8ToMValue(returnValue), ""); + errorMessage = *v8::String::Utf8Value(isolate, tryCatch.Message()->Get()); } - else - { - std::string errorMessage = "Unknown error"; - if (!tryCatch.Message().IsEmpty()) - { - errorMessage = *v8::String::Utf8Value(isolate, tryCatch.Message()->Get()); - } - alt::ICore::Instance().TriggerClientRPCAnswer(ev.GetTarget(), ev.GetAnswerID(), V8Helpers::V8ToMValue(v8::Undefined(isolate)), errorMessage); - } + alt::ICore::Instance().TriggerClientRPCAnswer(ev.GetTarget(), ev.GetAnswerID(), V8Helpers::V8ToMValue(v8::Undefined(isolate)), errorMessage); } } diff --git a/server/src/bindings/Main.cpp b/server/src/bindings/Main.cpp index 08dcb33a..e5df7691 100644 --- a/server/src/bindings/Main.cpp +++ b/server/src/bindings/Main.cpp @@ -272,27 +272,33 @@ static void OnRpc(const v8::FunctionCallbackInfo& info) V8_ARG_TO_STRING(1, rpcName); V8_ARG_TO_FUNCTION(2, callback); - resource->rpcHandlers[rpcName].push_back(callback); + V8_CHECK(V8ResourceImpl::rpcHandlers.contains(rpcName), "Handler already registered"); + + V8ResourceImpl::rpcHandlers[rpcName] = callback; } static void OffRpc(const v8::FunctionCallbackInfo& info) { V8_GET_ISOLATE_CONTEXT_RESOURCE(); - V8_CHECK_ARGS_LEN_MIN(2); + V8_CHECK_ARGS_LEN_MIN_MAX(1, 2); V8_ARG_TO_STRING(1, rpcName); - // TODO (xLuxy) Make callback optional so it removes all handlers? - V8_ARG_TO_FUNCTION(2, callback); - auto handlers = V8ResourceImpl::rpcHandlers[rpcName]; - for (auto it = handlers.begin(); it != handlers.end(); ++it) + if (V8ResourceImpl::rpcHandlers.contains(rpcName)) { - if (*it == callback) - handlers.erase(it); - } + if (info[1]->IsFunction()) + { + V8_ARG_TO_FUNCTION(2, callback); + + if (V8ResourceImpl::rpcHandlers[rpcName] == callback) + V8ResourceImpl::rpcHandlers.erase(rpcName); + + return; + } - if (handlers.empty()) V8ResourceImpl::rpcHandlers.erase(rpcName); + } + } static void SetSyncedMeta(const v8::FunctionCallbackInfo& info) diff --git a/shared/V8ResourceImpl.h b/shared/V8ResourceImpl.h index 0aa3dcdf..d33bca7b 100644 --- a/shared/V8ResourceImpl.h +++ b/shared/V8ResourceImpl.h @@ -318,7 +318,7 @@ class V8ResourceImpl : public alt::IResource::Impl static inline std::unordered_map> vehiclePassengers{}; // rpcs - static inline std::unordered_map>> rpcHandlers{}; + static inline std::unordered_map> rpcHandlers{}; #endif protected: