From 662f12da9624435b1850b424817879f12b7a30b2 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:39:34 +1100 Subject: [PATCH] improvements to universe.sendPacket and world.sendPacket adds necessary clientsMutex lock, and returns false if sending failed --- source/game/StarUniverseServer.cpp | 9 +++++++-- source/game/StarUniverseServer.hpp | 2 +- source/game/StarWorldServer.cpp | 9 ++++++--- source/game/StarWorldServer.hpp | 2 +- .../game/scripting/StarUniverseServerLuaBindings.cpp | 12 ++++-------- .../game/scripting/StarUniverseServerLuaBindings.hpp | 2 +- source/game/scripting/StarWorldLuaBindings.cpp | 6 +++--- source/game/scripting/StarWorldLuaBindings.hpp | 2 +- 8 files changed, 24 insertions(+), 20 deletions(-) diff --git a/source/game/StarUniverseServer.cpp b/source/game/StarUniverseServer.cpp index f2d8b3e..7927aae 100644 --- a/source/game/StarUniverseServer.cpp +++ b/source/game/StarUniverseServer.cpp @@ -473,10 +473,15 @@ bool UniverseServer::updatePlanetType(CelestialCoordinate const& coordinate, Str return false; } -void UniverseServer::sendPacket(ConnectionId clientId, PacketPtr packet) { +bool UniverseServer::sendPacket(ConnectionId clientId, PacketPtr packet) { RecursiveMutexLocker locker(m_mainLock); - if (m_clients.contains(clientId)) + ReadLocker clientsLocker(m_clientsLock); + if (m_clients.contains(clientId)) { + clientsLocker.unlock(); m_connectionServer->sendPackets(clientId, {packet}); + return true; + } + return false; } void UniverseServer::run() { diff --git a/source/game/StarUniverseServer.hpp b/source/game/StarUniverseServer.hpp index eb90a90..804073c 100644 --- a/source/game/StarUniverseServer.hpp +++ b/source/game/StarUniverseServer.hpp @@ -104,7 +104,7 @@ public: bool updatePlanetType(CelestialCoordinate const& coordinate, String const& newType, String const& weatherBiome); - void sendPacket(ConnectionId clientId, PacketPtr packet); + bool sendPacket(ConnectionId clientId, PacketPtr packet); protected: virtual void run(); diff --git a/source/game/StarWorldServer.cpp b/source/game/StarWorldServer.cpp index 95ec8c3..ec144da 100644 --- a/source/game/StarWorldServer.cpp +++ b/source/game/StarWorldServer.cpp @@ -544,9 +544,12 @@ 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); +bool WorldServer::sendPacket(ConnectionId clientId, PacketPtr const& packet) { + if (auto clientInfo = m_clientInfo.ptr(clientId)) { + clientInfo->get()->outgoingPackets.append(packet); + return true; + } + return false; } Maybe WorldServer::receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args) { diff --git a/source/game/StarWorldServer.hpp b/source/game/StarWorldServer.hpp index e1821d5..21b9b48 100644 --- a/source/game/StarWorldServer.hpp +++ b/source/game/StarWorldServer.hpp @@ -104,7 +104,7 @@ public: void handleIncomingPackets(ConnectionId clientId, List const& packets); List getOutgoingPackets(ConnectionId clientId); - void sendPacket(ConnectionId clientId, PacketPtr const& packet); + bool sendPacket(ConnectionId clientId, PacketPtr const& packet); Maybe receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args); diff --git a/source/game/scripting/StarUniverseServerLuaBindings.cpp b/source/game/scripting/StarUniverseServerLuaBindings.cpp index c68f138..479f6b8 100644 --- a/source/game/scripting/StarUniverseServerLuaBindings.cpp +++ b/source/game/scripting/StarUniverseServerLuaBindings.cpp @@ -21,7 +21,7 @@ LuaCallbacks LuaBindings::makeUniverseServerCallbacks(UniverseServer* universe) callbacks.registerCallbackWithSignature("isWorldActive", bind(UniverseServerCallbacks::isWorldActive, universe, _1)); callbacks.registerCallbackWithSignature("activeWorlds", bind(UniverseServerCallbacks::activeWorlds, universe)); callbacks.registerCallbackWithSignature, String, String, LuaVariadic>("sendWorldMessage", bind(UniverseServerCallbacks::sendWorldMessage, universe, _1, _2, _3)); - callbacks.registerCallbackWithSignature("sendPacket", bind(UniverseServerCallbacks::sendPacket, universe, _1, _2, _3)); + callbacks.registerCallbackWithSignature("sendPacket", bind(UniverseServerCallbacks::sendPacket, universe, _1, _2, _3)); callbacks.registerCallbackWithSignature("clientWorld", bind(UniverseServerCallbacks::clientWorld, universe, _1)); return callbacks; @@ -117,21 +117,17 @@ bool LuaBindings::UniverseServerCallbacks::isWorldActive(UniverseServer* univers } StringList LuaBindings::UniverseServerCallbacks::activeWorlds(UniverseServer* universe) { - StringList worlds; - for (WorldId& world : universe->activeWorlds()) - worlds.append(printWorldId(world)); - - return worlds; + return universe->activeWorlds().transformed(printWorldId); } RpcThreadPromise LuaBindings::UniverseServerCallbacks::sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic args) { return universe->sendWorldMessage(parseWorldId(worldId), message, JsonArray::from(std::move(args))); } -void LuaBindings::UniverseServerCallbacks::sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args) { +bool LuaBindings::UniverseServerCallbacks::sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args) { auto packetType = PacketTypeNames.getLeft(packetTypeName); auto packet = createPacket(packetType, args); - universe->sendPacket(clientId, packet); + return universe->sendPacket(clientId, packet); } String LuaBindings::UniverseServerCallbacks::clientWorld(UniverseServer* universe, ConnectionId clientId) { diff --git a/source/game/scripting/StarUniverseServerLuaBindings.hpp b/source/game/scripting/StarUniverseServerLuaBindings.hpp index b5ce8b9..8fe0bf5 100644 --- a/source/game/scripting/StarUniverseServerLuaBindings.hpp +++ b/source/game/scripting/StarUniverseServerLuaBindings.hpp @@ -25,7 +25,7 @@ namespace LuaBindings { bool isWorldActive(UniverseServer* universe, String const& worldId); StringList activeWorlds(UniverseServer* universe); RpcThreadPromise sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic args); - void sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args); + bool sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args); String clientWorld(UniverseServer* universe, ConnectionId clientId); } } diff --git a/source/game/scripting/StarWorldLuaBindings.cpp b/source/game/scripting/StarWorldLuaBindings.cpp index 8287903..13be5ee 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, serverWorld, _1, _2, _3)); + callbacks.registerCallbackWithSignature("sendPacket", bind(ServerWorldCallbacks::sendPacket, serverWorld, _1, _2, _3)); callbacks.registerCallbackWithSignature("skyTime", [serverWorld]() { return serverWorld->sky()->epochTime(); @@ -1187,10 +1187,10 @@ namespace LuaBindings { return context->invoke(function, args); } - void ServerWorldCallbacks::sendPacket(WorldServer* world, ConnectionId clientId, String const& packetType, Json const& packetData) { + bool 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); + return world->sendPacket(clientId, packet); } void WorldDebugCallbacks::debugPoint(Vec2F const& arg1, Color const& arg2) { diff --git a/source/game/scripting/StarWorldLuaBindings.hpp b/source/game/scripting/StarWorldLuaBindings.hpp index 114536d..e3329bf 100644 --- a/source/game/scripting/StarWorldLuaBindings.hpp +++ b/source/game/scripting/StarWorldLuaBindings.hpp @@ -88,7 +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); + bool sendPacket(WorldServer* world, ConnectionId clientId, String const& packetType, Json const& packetData); } namespace WorldDebugCallbacks {