From be571b5cd04f3986dc5725b4b04b4f94bd67a16c Mon Sep 17 00:00:00 2001 From: ErodeesFleurs <3353309908@qq.com> Date: Tue, 2 Apr 2024 09:34:06 +0800 Subject: [PATCH 1/2] add world.sendPacket and fixed deadlock --- source/game/StarUniverseServer.cpp | 3 ++- source/game/StarWorldServer.cpp | 7 ++++++- source/game/StarWorldServer.hpp | 1 + source/game/scripting/StarWorldLuaBindings.cpp | 7 +++++++ source/game/scripting/StarWorldLuaBindings.hpp | 1 + 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/source/game/StarUniverseServer.cpp b/source/game/StarUniverseServer.cpp index 9085db9..f2d8b3e 100644 --- a/source/game/StarUniverseServer.cpp +++ b/source/game/StarUniverseServer.cpp @@ -475,7 +475,8 @@ bool UniverseServer::updatePlanetType(CelestialCoordinate const& coordinate, Str void UniverseServer::sendPacket(ConnectionId clientId, PacketPtr packet) { RecursiveMutexLocker locker(m_mainLock); - m_connectionServer->sendPackets(clientId, {packet}); + if (m_clients.contains(clientId)) + m_connectionServer->sendPackets(clientId, {packet}); } void UniverseServer::run() { diff --git a/source/game/StarWorldServer.cpp b/source/game/StarWorldServer.cpp index ad3b555..95ec8c3 100644 --- a/source/game/StarWorldServer.cpp +++ b/source/game/StarWorldServer.cpp @@ -544,6 +544,11 @@ List WorldServer::getOutgoingPackets(ConnectionId clientId) { return std::move(clientInfo->outgoingPackets); } +void WorldServer::sendPacket(ConnectionId clientId, PacketPtr const& packet) { + if (auto const& clientInfo = m_clientInfo.get(clientId)) + clientInfo->outgoingPackets.append(packet); +} + Maybe WorldServer::receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args) { Maybe result; for (auto& p : m_scriptContexts) { @@ -2084,7 +2089,7 @@ float WorldServer::lightLevel(Vec2F const& pos) const { } void WorldServer::setDungeonBreathable(DungeonId dungeonId, Maybe breathable) { - Maybe current = m_dungeonIdBreathable.maybe(dungeonId); + Maybe current = m_dungeonIdBreathable.maybe(dungeonId); if (breathable != current) { if (breathable) m_dungeonIdBreathable[dungeonId] = *breathable; diff --git a/source/game/StarWorldServer.hpp b/source/game/StarWorldServer.hpp index 4c11217..e1821d5 100644 --- a/source/game/StarWorldServer.hpp +++ b/source/game/StarWorldServer.hpp @@ -104,6 +104,7 @@ public: void handleIncomingPackets(ConnectionId clientId, List const& packets); List getOutgoingPackets(ConnectionId clientId); + void sendPacket(ConnectionId clientId, PacketPtr const& packet); Maybe receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args); diff --git a/source/game/scripting/StarWorldLuaBindings.cpp b/source/game/scripting/StarWorldLuaBindings.cpp index cc91ec2..0016589 100644 --- a/source/game/scripting/StarWorldLuaBindings.cpp +++ b/source/game/scripting/StarWorldLuaBindings.cpp @@ -390,6 +390,7 @@ namespace LuaBindings { callbacks.registerCallbackWithSignature>("players", bind(ServerWorldCallbacks::players, world)); callbacks.registerCallbackWithSignature("fidelity", bind(ServerWorldCallbacks::fidelity, world, _1)); callbacks.registerCallbackWithSignature, String, String, LuaVariadic>("callScriptContext", bind(ServerWorldCallbacks::callScriptContext, world, _1, _2, _3)); + callbacks.registerCallbackWithSignature("sendPacket", bind(ServerWorldCallbacks::sendPacket, world, _1, _2, _3)); callbacks.registerCallbackWithSignature("skyTime", [serverWorld]() { return serverWorld->sky()->epochTime(); @@ -1186,6 +1187,12 @@ namespace LuaBindings { return context->invoke(function, args); } + void ServerWorldCallbacks::sendPacket(WorldServer* world, ConnectionId clientId, String const& packetType, Json const& packetData) { + PacketType type = PacketTypeNames.getLeft(packetType); + auto packet = createPacket(type, packetData); + world->sendPacket(clientId, packet); + } + void WorldDebugCallbacks::debugPoint(Vec2F const& arg1, Color const& arg2) { SpatialLogger::logPoint("world", arg1, arg2.toRgba()); } diff --git a/source/game/scripting/StarWorldLuaBindings.hpp b/source/game/scripting/StarWorldLuaBindings.hpp index 010affd..114536d 100644 --- a/source/game/scripting/StarWorldLuaBindings.hpp +++ b/source/game/scripting/StarWorldLuaBindings.hpp @@ -88,6 +88,7 @@ namespace LuaBindings { List players(World* world); LuaString fidelity(World* world, LuaEngine& engine); Maybe callScriptContext(World* world, String const& contextName, String const& function, LuaVariadic const& args); + void sendPacket(WorldServer* world, ConnectionId clientId, String const& packetType, Json const& packetData); } namespace WorldDebugCallbacks { From cf93c2e9d40a87408e02a0c78ee4d38159e9e133 Mon Sep 17 00:00:00 2001 From: ErodeesFleurs <3353309908@qq.com> Date: Tue, 2 Apr 2024 09:42:22 +0800 Subject: [PATCH 2/2] oops --- source/game/scripting/StarWorldLuaBindings.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/game/scripting/StarWorldLuaBindings.cpp b/source/game/scripting/StarWorldLuaBindings.cpp index 0016589..8287903 100644 --- a/source/game/scripting/StarWorldLuaBindings.cpp +++ b/source/game/scripting/StarWorldLuaBindings.cpp @@ -390,7 +390,7 @@ namespace LuaBindings { callbacks.registerCallbackWithSignature>("players", bind(ServerWorldCallbacks::players, world)); callbacks.registerCallbackWithSignature("fidelity", bind(ServerWorldCallbacks::fidelity, world, _1)); callbacks.registerCallbackWithSignature, String, String, LuaVariadic>("callScriptContext", bind(ServerWorldCallbacks::callScriptContext, world, _1, _2, _3)); - callbacks.registerCallbackWithSignature("sendPacket", bind(ServerWorldCallbacks::sendPacket, world, _1, _2, _3)); + callbacks.registerCallbackWithSignature("sendPacket", bind(ServerWorldCallbacks::sendPacket, serverWorld, _1, _2, _3)); callbacks.registerCallbackWithSignature("skyTime", [serverWorld]() { return serverWorld->sky()->epochTime();