Skip to content

Commit

Permalink
server: finish rpc stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Sep 29, 2023
1 parent 842cf7e commit 72065d0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
37 changes: 20 additions & 17 deletions server/src/CNodeResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ bool CNodeResourceImpl::Stop()
uv_loop_close(uvLoop);
delete uvLoop;

vehiclePassengers.clear();
rpcHandlers.clear();

return true;
}

Expand Down Expand Up @@ -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<v8::Local<v8::Value>> args;
args.push_back(GetBaseObjectOrNull(ev.GetTarget()));
args.push_back(V8Helpers::MValueToV8(ev.GetAnswer()));

for (auto handler : handlers)
{
v8::TryCatch tryCatch(isolate);
v8::MaybeLocal<v8::Value> ret = V8Helpers::CallFunctionWithTimeout(handler, context, args);
v8::TryCatch tryCatch(isolate);
v8::MaybeLocal<v8::Value> ret = V8Helpers::CallFunctionWithTimeout(handler, context, args);

if (!tryCatch.HasCaught())
if (!tryCatch.HasCaught())
{
v8::Local<v8::Value> 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<v8::Value> 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);
}
}

Expand Down
26 changes: 16 additions & 10 deletions server/src/bindings/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,27 +272,33 @@ static void OnRpc(const v8::FunctionCallbackInfo<v8::Value>& 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<v8::Value>& 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<v8::Value>& info)
Expand Down
2 changes: 1 addition & 1 deletion shared/V8ResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class V8ResourceImpl : public alt::IResource::Impl
static inline std::unordered_map<alt::IVehicle*, std::unordered_map<uint8_t, alt::IPlayer*>> vehiclePassengers{};

// rpcs
static inline std::unordered_map<std::string, std::vector<v8::Local<v8::Function>>> rpcHandlers{};
static inline std::unordered_map<std::string, v8::Local<v8::Function>> rpcHandlers{};
#endif

protected:
Expand Down

0 comments on commit 72065d0

Please sign in to comment.