Skip to content

Commit

Permalink
fix: client debug (#3264)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah authored Feb 8, 2025
1 parent 7bd52c0 commit 736f302
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7582,7 +7582,7 @@ void Player::sendFightModes() const {
}
}

void Player::sendNetworkMessage(const NetworkMessage &message) const {
void Player::sendNetworkMessage(NetworkMessage &message) const {
if (client) {
client->writeToOutputBuffer(message);
}
Expand Down Expand Up @@ -8388,7 +8388,8 @@ void Player::initializeTaskHunting() {
}

if (client && g_configManager().getBoolean(TASK_HUNTING_ENABLED) && !client->oldProtocol) {
client->writeToOutputBuffer(g_ioprey().getTaskHuntingBaseDate());
auto buffer = g_ioprey().getTaskHuntingBaseDate();
client->writeToOutputBuffer(buffer);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ class Player final : public Creature, public Cylinder, public Bankable {
void resetAsyncOngoingTask(uint64_t flags);
void sendEnterWorld() const;
void sendFightModes() const;
void sendNetworkMessage(const NetworkMessage &message) const;
void sendNetworkMessage(NetworkMessage &message) const;

void receivePing();

Expand Down
8 changes: 8 additions & 0 deletions src/game/scheduling/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ void Dispatcher::stopEvent(uint64_t eventId) {
}
}

void Dispatcher::safeCall(std::function<void(void)> &&f) {
if (dispacherContext.isAsync()) {
addEvent(std::move(f), dispacherContext.taskName);
} else {
f();
}
}

bool DispatcherContext::isOn() {
return OTSYS_TIME() != 0;
}
14 changes: 14 additions & 0 deletions src/game/scheduling/dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ class Dispatcher {
);
}

/**
* @brief Executes an action wrapped in a std::function safely on the dispatcher thread.
*
* This method ensures that the given function is executed on the correct thread (the dispatcher thread).
* If this method is called from a different thread, it will redirect execution to the dispatcher thread,
* using appropriate mechanisms (such as message queues or event loops).
* If called directly from the dispatcher thread, it will execute the function immediately.
*
* @param action The function wrapped in a std::function<void(void)> that should be executed.
*
* @note This method is useful in multi-threaded applications to avoid race conditions or thread context violations.
*/
void safeCall(std::function<void(void)> &&f);

[[nodiscard]] uint64_t getDispatcherCycle() const {
return dispatcherCycle;
}
Expand Down
7 changes: 4 additions & 3 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,10 @@ void ProtocolGame::disconnectClient(const std::string &message) const {
disconnect();
}

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

void ProtocolGame::parsePacket(NetworkMessage &msg) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolgame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ProtocolGame final : public Protocol {
}
void connect(const std::string &playerName, OperatingSystem_t operatingSystem);
void disconnectClient(const std::string &message) const;
void writeToOutputBuffer(const NetworkMessage &msg);
void writeToOutputBuffer(NetworkMessage &msg);

void release() override;

Expand Down

0 comments on commit 736f302

Please sign in to comment.