diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2024-04-02 13:39:34 +1100 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2024-04-02 13:39:34 +1100 |
commit | 662f12da9624435b1850b424817879f12b7a30b2 (patch) | |
tree | d43af674451d2247b91b2e3b9fa3f3e4b4181423 | |
parent | 40698751e79d119e1ae37ea605b320961d6b597a (diff) |
improvements to universe.sendPacket and world.sendPacket
adds necessary clientsMutex lock, and returns false if sending failed
-rw-r--r-- | source/game/StarUniverseServer.cpp | 9 | ||||
-rw-r--r-- | source/game/StarUniverseServer.hpp | 2 | ||||
-rw-r--r-- | source/game/StarWorldServer.cpp | 9 | ||||
-rw-r--r-- | source/game/StarWorldServer.hpp | 2 | ||||
-rw-r--r-- | source/game/scripting/StarUniverseServerLuaBindings.cpp | 12 | ||||
-rw-r--r-- | source/game/scripting/StarUniverseServerLuaBindings.hpp | 2 | ||||
-rw-r--r-- | source/game/scripting/StarWorldLuaBindings.cpp | 6 | ||||
-rw-r--r-- | 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<PacketPtr> 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<Json> 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<PacketPtr> const& packets); List<PacketPtr> getOutgoingPackets(ConnectionId clientId); - void sendPacket(ConnectionId clientId, PacketPtr const& packet); + bool sendPacket(ConnectionId clientId, PacketPtr const& packet); Maybe<Json> 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<bool, String>("isWorldActive", bind(UniverseServerCallbacks::isWorldActive, universe, _1)); callbacks.registerCallbackWithSignature<StringList>("activeWorlds", bind(UniverseServerCallbacks::activeWorlds, universe)); callbacks.registerCallbackWithSignature<RpcThreadPromise<Json>, String, String, LuaVariadic<Json>>("sendWorldMessage", bind(UniverseServerCallbacks::sendWorldMessage, universe, _1, _2, _3)); - callbacks.registerCallbackWithSignature<void, ConnectionId, String, Json>("sendPacket", bind(UniverseServerCallbacks::sendPacket, universe, _1, _2, _3)); + callbacks.registerCallbackWithSignature<bool, ConnectionId, String, Json>("sendPacket", bind(UniverseServerCallbacks::sendPacket, universe, _1, _2, _3)); callbacks.registerCallbackWithSignature<String, ConnectionId>("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<Json> LuaBindings::UniverseServerCallbacks::sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic<Json> 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<Json> sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic<Json> 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<List<EntityId>>("players", bind(ServerWorldCallbacks::players, world)); callbacks.registerCallbackWithSignature<LuaString, LuaEngine&>("fidelity", bind(ServerWorldCallbacks::fidelity, world, _1)); callbacks.registerCallbackWithSignature<Maybe<LuaValue>, String, String, LuaVariadic<LuaValue>>("callScriptContext", bind(ServerWorldCallbacks::callScriptContext, world, _1, _2, _3)); - callbacks.registerCallbackWithSignature<void, ConnectionId, String, Json>("sendPacket", bind(ServerWorldCallbacks::sendPacket, serverWorld, _1, _2, _3)); + callbacks.registerCallbackWithSignature<bool, ConnectionId, String, Json>("sendPacket", bind(ServerWorldCallbacks::sendPacket, serverWorld, _1, _2, _3)); callbacks.registerCallbackWithSignature<double>("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<EntityId> players(World* world); LuaString fidelity(World* world, LuaEngine& engine); Maybe<LuaValue> callScriptContext(World* world, String const& contextName, String const& function, LuaVariadic<LuaValue> 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 { |