Skip to content

Commit 736f302

Browse files
authored
fix: client debug (#3264)
1 parent 7bd52c0 commit 736f302

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

src/creatures/players/player.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7582,7 +7582,7 @@ void Player::sendFightModes() const {
75827582
}
75837583
}
75847584

7585-
void Player::sendNetworkMessage(const NetworkMessage &message) const {
7585+
void Player::sendNetworkMessage(NetworkMessage &message) const {
75867586
if (client) {
75877587
client->writeToOutputBuffer(message);
75887588
}
@@ -8388,7 +8388,8 @@ void Player::initializeTaskHunting() {
83888388
}
83898389

83908390
if (client && g_configManager().getBoolean(TASK_HUNTING_ENABLED) && !client->oldProtocol) {
8391-
client->writeToOutputBuffer(g_ioprey().getTaskHuntingBaseDate());
8391+
auto buffer = g_ioprey().getTaskHuntingBaseDate();
8392+
client->writeToOutputBuffer(buffer);
83928393
}
83938394
}
83948395

src/creatures/players/player.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ class Player final : public Creature, public Cylinder, public Bankable {
928928
void resetAsyncOngoingTask(uint64_t flags);
929929
void sendEnterWorld() const;
930930
void sendFightModes() const;
931-
void sendNetworkMessage(const NetworkMessage &message) const;
931+
void sendNetworkMessage(NetworkMessage &message) const;
932932

933933
void receivePing();
934934

src/game/scheduling/dispatcher.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ void Dispatcher::stopEvent(uint64_t eventId) {
251251
}
252252
}
253253

254+
void Dispatcher::safeCall(std::function<void(void)> &&f) {
255+
if (dispacherContext.isAsync()) {
256+
addEvent(std::move(f), dispacherContext.taskName);
257+
} else {
258+
f();
259+
}
260+
}
261+
254262
bool DispatcherContext::isOn() {
255263
return OTSYS_TIME() != 0;
256264
}

src/game/scheduling/dispatcher.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ class Dispatcher {
119119
);
120120
}
121121

122+
/**
123+
* @brief Executes an action wrapped in a std::function safely on the dispatcher thread.
124+
*
125+
* This method ensures that the given function is executed on the correct thread (the dispatcher thread).
126+
* If this method is called from a different thread, it will redirect execution to the dispatcher thread,
127+
* using appropriate mechanisms (such as message queues or event loops).
128+
* If called directly from the dispatcher thread, it will execute the function immediately.
129+
*
130+
* @param action The function wrapped in a std::function<void(void)> that should be executed.
131+
*
132+
* @note This method is useful in multi-threaded applications to avoid race conditions or thread context violations.
133+
*/
134+
void safeCall(std::function<void(void)> &&f);
135+
122136
[[nodiscard]] uint64_t getDispatcherCycle() const {
123137
return dispatcherCycle;
124138
}

src/server/network/protocol/protocolgame.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,10 @@ void ProtocolGame::disconnectClient(const std::string &message) const {
902902
disconnect();
903903
}
904904

905-
void ProtocolGame::writeToOutputBuffer(const NetworkMessage &msg) {
906-
auto out = getOutputBuffer(msg.getLength());
907-
out->append(msg);
905+
void ProtocolGame::writeToOutputBuffer(NetworkMessage &msg) {
906+
g_dispatcher().safeCall([self = getThis(), msg = std::move(msg)] {
907+
self->getOutputBuffer(msg.getLength())->append(msg);
908+
});
908909
}
909910

910911
void ProtocolGame::parsePacket(NetworkMessage &msg) {

src/server/network/protocol/protocolgame.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class ProtocolGame final : public Protocol {
117117
}
118118
void connect(const std::string &playerName, OperatingSystem_t operatingSystem);
119119
void disconnectClient(const std::string &message) const;
120-
void writeToOutputBuffer(const NetworkMessage &msg);
120+
void writeToOutputBuffer(NetworkMessage &msg);
121121

122122
void release() override;
123123

0 commit comments

Comments
 (0)