Merge pull request #55 from ErodeesFleurs/sp

fixed the deadlock problem of universe.sendPacket and added world.sendPacket function
This commit is contained in:
Kae 2024-04-02 13:11:42 +11:00 committed by GitHub
commit 40698751e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 17 additions and 2 deletions

View File

@ -475,6 +475,7 @@ bool UniverseServer::updatePlanetType(CelestialCoordinate const& coordinate, Str
void UniverseServer::sendPacket(ConnectionId clientId, PacketPtr packet) { void UniverseServer::sendPacket(ConnectionId clientId, PacketPtr packet) {
RecursiveMutexLocker locker(m_mainLock); RecursiveMutexLocker locker(m_mainLock);
if (m_clients.contains(clientId))
m_connectionServer->sendPackets(clientId, {packet}); m_connectionServer->sendPackets(clientId, {packet});
} }

View File

@ -544,6 +544,11 @@ List<PacketPtr> WorldServer::getOutgoingPackets(ConnectionId clientId) {
return std::move(clientInfo->outgoingPackets); 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<Json> WorldServer::receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args) { Maybe<Json> WorldServer::receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args) {
Maybe<Json> result; Maybe<Json> result;
for (auto& p : m_scriptContexts) { for (auto& p : m_scriptContexts) {
@ -2084,7 +2089,7 @@ float WorldServer::lightLevel(Vec2F const& pos) const {
} }
void WorldServer::setDungeonBreathable(DungeonId dungeonId, Maybe<bool> breathable) { void WorldServer::setDungeonBreathable(DungeonId dungeonId, Maybe<bool> breathable) {
Maybe<float> current = m_dungeonIdBreathable.maybe(dungeonId); Maybe<bool> current = m_dungeonIdBreathable.maybe(dungeonId);
if (breathable != current) { if (breathable != current) {
if (breathable) if (breathable)
m_dungeonIdBreathable[dungeonId] = *breathable; m_dungeonIdBreathable[dungeonId] = *breathable;

View File

@ -104,6 +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);
Maybe<Json> receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args); Maybe<Json> receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args);

View File

@ -390,6 +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<double>("skyTime", [serverWorld]() { callbacks.registerCallbackWithSignature<double>("skyTime", [serverWorld]() {
return serverWorld->sky()->epochTime(); return serverWorld->sky()->epochTime();
@ -1186,6 +1187,12 @@ 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) {
PacketType type = PacketTypeNames.getLeft(packetType);
auto packet = createPacket(type, packetData);
world->sendPacket(clientId, packet);
}
void WorldDebugCallbacks::debugPoint(Vec2F const& arg1, Color const& arg2) { void WorldDebugCallbacks::debugPoint(Vec2F const& arg1, Color const& arg2) {
SpatialLogger::logPoint("world", arg1, arg2.toRgba()); SpatialLogger::logPoint("world", arg1, arg2.toRgba());
} }

View File

@ -88,6 +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);
} }
namespace WorldDebugCallbacks { namespace WorldDebugCallbacks {