add world.sendPacket and fixed deadlock

This commit is contained in:
ErodeesFleurs 2024-04-02 09:34:06 +08:00
parent 14506b1014
commit be571b5cd0
5 changed files with 17 additions and 2 deletions

View File

@ -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() {

View File

@ -544,6 +544,11 @@ 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);
}
Maybe<Json> WorldServer::receiveMessage(ConnectionId fromConnection, String const& message, JsonArray const& args) {
Maybe<Json> result;
for (auto& p : m_scriptContexts) {
@ -2084,7 +2089,7 @@ float WorldServer::lightLevel(Vec2F const& pos) const {
}
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)
m_dungeonIdBreathable[dungeonId] = *breathable;

View File

@ -104,6 +104,7 @@ public:
void handleIncomingPackets(ConnectionId clientId, List<PacketPtr> const& packets);
List<PacketPtr> getOutgoingPackets(ConnectionId clientId);
void sendPacket(ConnectionId clientId, PacketPtr const& packet);
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<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, world, _1, _2, _3));
callbacks.registerCallbackWithSignature<double>("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());
}

View File

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