improvements to universe.sendPacket and world.sendPacket

adds necessary clientsMutex lock, and returns false if sending failed
This commit is contained in:
Kae 2024-04-02 13:39:34 +11:00
parent 40698751e7
commit 662f12da96
8 changed files with 24 additions and 20 deletions

View File

@ -473,10 +473,15 @@ bool UniverseServer::updatePlanetType(CelestialCoordinate const& coordinate, Str
return false; return false;
} }
void UniverseServer::sendPacket(ConnectionId clientId, PacketPtr packet) { bool UniverseServer::sendPacket(ConnectionId clientId, PacketPtr packet) {
RecursiveMutexLocker locker(m_mainLock); 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}); m_connectionServer->sendPackets(clientId, {packet});
return true;
}
return false;
} }
void UniverseServer::run() { void UniverseServer::run() {

View File

@ -104,7 +104,7 @@ public:
bool updatePlanetType(CelestialCoordinate const& coordinate, String const& newType, String const& weatherBiome); bool updatePlanetType(CelestialCoordinate const& coordinate, String const& newType, String const& weatherBiome);
void sendPacket(ConnectionId clientId, PacketPtr packet); bool sendPacket(ConnectionId clientId, PacketPtr packet);
protected: protected:
virtual void run(); virtual void run();

View File

@ -544,9 +544,12 @@ List<PacketPtr> WorldServer::getOutgoingPackets(ConnectionId clientId) {
return std::move(clientInfo->outgoingPackets); return std::move(clientInfo->outgoingPackets);
} }
void WorldServer::sendPacket(ConnectionId clientId, PacketPtr const& packet) { bool WorldServer::sendPacket(ConnectionId clientId, PacketPtr const& packet) {
if (auto const& clientInfo = m_clientInfo.get(clientId)) if (auto clientInfo = m_clientInfo.ptr(clientId)) {
clientInfo->outgoingPackets.append(packet); clientInfo->get()->outgoingPackets.append(packet);
return true;
}
return false;
} }
Maybe<Json> WorldServer::receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args) { Maybe<Json> WorldServer::receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args) {

View File

@ -104,7 +104,7 @@ public:
void handleIncomingPackets(ConnectionId clientId, List<PacketPtr> const& packets); void handleIncomingPackets(ConnectionId clientId, List<PacketPtr> const& packets);
List<PacketPtr> getOutgoingPackets(ConnectionId clientId); 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); Maybe<Json> receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args);

View File

@ -21,7 +21,7 @@ LuaCallbacks LuaBindings::makeUniverseServerCallbacks(UniverseServer* universe)
callbacks.registerCallbackWithSignature<bool, String>("isWorldActive", bind(UniverseServerCallbacks::isWorldActive, universe, _1)); callbacks.registerCallbackWithSignature<bool, String>("isWorldActive", bind(UniverseServerCallbacks::isWorldActive, universe, _1));
callbacks.registerCallbackWithSignature<StringList>("activeWorlds", bind(UniverseServerCallbacks::activeWorlds, universe)); 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<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)); callbacks.registerCallbackWithSignature<String, ConnectionId>("clientWorld", bind(UniverseServerCallbacks::clientWorld, universe, _1));
return callbacks; return callbacks;
@ -117,21 +117,17 @@ bool LuaBindings::UniverseServerCallbacks::isWorldActive(UniverseServer* univers
} }
StringList LuaBindings::UniverseServerCallbacks::activeWorlds(UniverseServer* universe) { StringList LuaBindings::UniverseServerCallbacks::activeWorlds(UniverseServer* universe) {
StringList worlds; return universe->activeWorlds().transformed(printWorldId);
for (WorldId& world : universe->activeWorlds())
worlds.append(printWorldId(world));
return worlds;
} }
RpcThreadPromise<Json> LuaBindings::UniverseServerCallbacks::sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic<Json> args) { 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))); 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 packetType = PacketTypeNames.getLeft(packetTypeName);
auto packet = createPacket(packetType, args); auto packet = createPacket(packetType, args);
universe->sendPacket(clientId, packet); return universe->sendPacket(clientId, packet);
} }
String LuaBindings::UniverseServerCallbacks::clientWorld(UniverseServer* universe, ConnectionId clientId) { String LuaBindings::UniverseServerCallbacks::clientWorld(UniverseServer* universe, ConnectionId clientId) {

View File

@ -25,7 +25,7 @@ namespace LuaBindings {
bool isWorldActive(UniverseServer* universe, String const& worldId); bool isWorldActive(UniverseServer* universe, String const& worldId);
StringList activeWorlds(UniverseServer* universe); StringList activeWorlds(UniverseServer* universe);
RpcThreadPromise<Json> sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic<Json> args); 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); String clientWorld(UniverseServer* universe, ConnectionId clientId);
} }
} }

View File

@ -390,7 +390,7 @@ namespace LuaBindings {
callbacks.registerCallbackWithSignature<List<EntityId>>("players", bind(ServerWorldCallbacks::players, world)); callbacks.registerCallbackWithSignature<List<EntityId>>("players", bind(ServerWorldCallbacks::players, world));
callbacks.registerCallbackWithSignature<LuaString, LuaEngine&>("fidelity", bind(ServerWorldCallbacks::fidelity, world, _1)); 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<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]() { callbacks.registerCallbackWithSignature<double>("skyTime", [serverWorld]() {
return serverWorld->sky()->epochTime(); return serverWorld->sky()->epochTime();
@ -1187,10 +1187,10 @@ namespace LuaBindings {
return context->invoke(function, args); 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); PacketType type = PacketTypeNames.getLeft(packetType);
auto packet = createPacket(type, packetData); auto packet = createPacket(type, packetData);
world->sendPacket(clientId, packet); return world->sendPacket(clientId, packet);
} }
void WorldDebugCallbacks::debugPoint(Vec2F const& arg1, Color const& arg2) { void WorldDebugCallbacks::debugPoint(Vec2F const& arg1, Color const& arg2) {

View File

@ -88,7 +88,7 @@ namespace LuaBindings {
List<EntityId> players(World* world); List<EntityId> players(World* world);
LuaString fidelity(World* world, LuaEngine& engine); LuaString fidelity(World* world, LuaEngine& engine);
Maybe<LuaValue> callScriptContext(World* world, String const& contextName, String const& function, LuaVariadic<LuaValue> const& args); 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 { namespace WorldDebugCallbacks {