Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: task context is being sent empty #2871

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/creatures/combat/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2067,10 +2067,12 @@ bool ConditionFeared::executeCondition(std::shared_ptr<Creature> creature, int32
}

if (getFleePath(creature, currentPos, listDir)) {
g_dispatcher().addEvent([id = creature->getID(), listDir] {
g_game().forcePlayerAutoWalk(id, listDir);
},
"ConditionFeared::executeCondition");
g_dispatcher().addEvent(
[id = creature->getID(), listDir] {
g_game().forcePlayerAutoWalk(id, listDir);
},
"Game::forcePlayerAutoWalk"
);

g_logger().debug("[ConditionFeared::executeCondition] Walking Scheduled");
}
Expand Down
38 changes: 23 additions & 15 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,23 @@ void Creature::addEventWalk(bool firstStep) {
return;
}

g_dispatcher().context().tryAddEvent([ticks, self = getCreature()]() {
// Take first step right away, but still queue the next
if (ticks == 1) {
g_game().checkCreatureWalk(self->getID());
}
g_dispatcher().context().tryAddEvent(
[ticks, self = getCreature()]() {
// Take first step right away, but still queue the next
if (ticks == 1) {
g_game().checkCreatureWalk(self->getID());
}

self->eventWalk = g_dispatcher().scheduleEvent(
static_cast<uint32_t>(ticks),
[creatureId = self->getID()] { g_game().checkCreatureWalk(creatureId); }, "Game::checkCreatureWalk"
);
});
self->eventWalk = g_dispatcher().scheduleEvent(
static_cast<uint32_t>(ticks),
[creatureId = self->getID()] {
g_game().checkCreatureWalk(creatureId);
},
"Game::checkCreatureWalk"
);
},
"Game::checkCreatureWalk"
);
}

void Creature::stopEventWalk() {
Expand Down Expand Up @@ -825,10 +831,12 @@ bool Creature::dropCorpse(std::shared_ptr<Creature> lastHitCreature, std::shared
auto isReachable = g_game().map.getPathMatching(player->getPosition(), dirList, FrozenPathingConditionCall(corpse->getPosition()), fpp);

if (player->checkAutoLoot(monster->isRewardBoss()) && isReachable) {
g_dispatcher().addEvent([player, corpseContainer, corpsePosition = corpse->getPosition()] {
g_game().playerQuickLootCorpse(player, corpseContainer, corpsePosition);
},
"Game::playerQuickLootCorpse");
g_dispatcher().addEvent(
[player, corpseContainer, corpsePosition = corpse->getPosition()] {
g_game().playerQuickLootCorpse(player, corpseContainer, corpsePosition);
},
"Game::playerQuickLootCorpse"
);
}
}
}
Expand Down Expand Up @@ -1061,7 +1069,7 @@ void Creature::goToFollowCreature_async(std::function<void()> &&onComplete) {
});

if (onComplete) {
g_dispatcher().context().addEvent(std::move(onComplete));
g_dispatcher().context().addEvent(std::move(onComplete), "Creature::onThink");
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7841,9 +7841,8 @@ bool Player::canAutoWalk(const Position &toPosition, const std::function<void()>
// Check if can walk to the toPosition and send event to use function
std::vector<Direction> listDir;
if (getPathTo(toPosition, listDir, 0, 1, true, true)) {
g_dispatcher().addEvent([creatureId = getID(), listDir] { g_game().playerAutoWalk(creatureId, listDir); }, __FUNCTION__);

std::shared_ptr<Task> task = createPlayerTask(delay, function, __FUNCTION__);
g_dispatcher().addEvent([creatureId = getID(), listDir] { g_game().playerAutoWalk(creatureId, listDir); }, "Game::playerAutoWalk");
const auto &task = createPlayerTask(delay, function, "Player::canAutoWalk");
setNextWalkActionTask(task);
return true;
} else {
Expand Down
163 changes: 109 additions & 54 deletions src/game/game.cpp

Large diffs are not rendered by default.

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

void DispatcherContext::addEvent(std::function<void(void)> &&f) const {
g_dispatcher().addEvent(std::move(f), taskName);
void DispatcherContext::addEvent(std::function<void(void)> &&f, std::string_view context) const {
g_dispatcher().addEvent(std::move(f), context);
}

void DispatcherContext::tryAddEvent(std::function<void(void)> &&f) const {
void DispatcherContext::tryAddEvent(std::function<void(void)> &&f, std::string_view context) const {
if (!f) {
return;
}

if (isAsync()) {
g_dispatcher().addEvent(std::move(f), taskName);
g_dispatcher().addEvent(std::move(f), context);
} else {
f();
}
Expand Down
6 changes: 3 additions & 3 deletions src/game/scheduling/dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ struct DispatcherContext {
}

// postpone the event
void addEvent(std::function<void(void)> &&f) const;
void addEvent(std::function<void(void)> &&f, std::string_view context) const;

// if the context is async, the event will be postponed, if not, it will be executed immediately.
void tryAddEvent(std::function<void(void)> &&f) const;
void tryAddEvent(std::function<void(void)> &&f, std::string_view context) const;

private:
void reset() {
Expand All @@ -70,7 +70,7 @@ struct DispatcherContext {

DispatcherType type = DispatcherType::None;
TaskGroup group = TaskGroup::ThreadPool;
std::string_view taskName = "";
std::string_view taskName;

friend class Dispatcher;
};
Expand Down
4 changes: 2 additions & 2 deletions src/lua/functions/core/game/game_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ int GameFunctions::luaGameGetPlayers(lua_State* L) {
int GameFunctions::luaGameLoadMap(lua_State* L) {
// Game.loadMap(path)
const std::string &path = getString(L, 1);
g_dispatcher().addEvent([path]() { g_game().loadMap(path); }, "GameFunctions::luaGameLoadMap");
g_dispatcher().addEvent([path]() { g_game().loadMap(path); }, "Game::loadMap");
return 0;
}

int GameFunctions::luaGameloadMapChunk(lua_State* L) {
// Game.loadMapChunk(path, position, remove)
const std::string &path = getString(L, 1);
const Position &position = getPosition(L, 2);
g_dispatcher().addEvent([path, position]() { g_game().loadMap(path, position); }, "GameFunctions::luaGameloadMapChunk");
g_dispatcher().addEvent([path, position]() { g_game().loadMap(path, position); }, "Game::loadMap");
luanluciano93 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}

Expand Down
13 changes: 8 additions & 5 deletions src/map/mapcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,14 @@ std::shared_ptr<Tile> MapCache::getOrCreateTileFromCache(const std::unique_ptr<F
tile->setFlag(static_cast<TileFlags_t>(cachedTile->flags));

// add zone synchronously
g_dispatcher().context().tryAddEvent([tile, pos] {
for (const auto &zone : Zone::getZones(pos)) {
tile->addZone(zone);
}
});
g_dispatcher().context().tryAddEvent(
[tile, pos] {
for (const auto &zone : Zone::getZones(pos)) {
tile->addZone(zone);
}
},
"Zone::getZones"
);

floor->setTile(x, y, tile);

Expand Down
16 changes: 10 additions & 6 deletions src/server/network/protocol/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ bool Protocol::sendRecvMessageCallback(NetworkMessage &msg) {
return false;
}

g_dispatcher().addEvent([&msg, protocolWeak = std::weak_ptr<Protocol>(shared_from_this())]() {
if (auto protocol = protocolWeak.lock()) {
if (auto protocolConnection = protocol->getConnection()) {
protocol->parsePacket(msg);
protocolConnection->resumeWork();
g_dispatcher().addEvent(
[&msg, protocolWeak = std::weak_ptr<Protocol>(shared_from_this())]() {
if (auto protocol = protocolWeak.lock()) {
if (auto protocolConnection = protocol->getConnection()) {
protocol->parsePacket(msg);
protocolConnection->resumeWork();
}
}
} }, "Protocol::sendRecvMessageCallback");
},
"Protocol::sendRecvMessageCallback"
);

return true;
}
Expand Down
26 changes: 14 additions & 12 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5040,18 +5040,20 @@ void ProtocolGame::updateCoinBalance() {
return;
}

g_dispatcher().addEvent([playerId = player->getID()] {
const auto &threadPlayer = g_game().getPlayerByID(playerId);
if (threadPlayer && threadPlayer->getAccount()) {
const auto [coins, errCoin] = threadPlayer->getAccount()->getCoins(enumToValue(CoinType::Normal));
const auto [transferCoins, errTCoin] = threadPlayer->getAccount()->getCoins(enumToValue(CoinType::Transferable));

threadPlayer->coinBalance = coins;
threadPlayer->coinTransferableBalance = transferCoins;
threadPlayer->sendCoinBalance();
}
},
"ProtocolGame::updateCoinBalance");
g_dispatcher().addEvent(
[playerId = player->getID()] {
const auto &threadPlayer = g_game().getPlayerByID(playerId);
if (threadPlayer && threadPlayer->getAccount()) {
const auto [coins, errCoin] = threadPlayer->getAccount()->getCoins(enumToValue(CoinType::Normal));
const auto [transferCoins, errTCoin] = threadPlayer->getAccount()->getCoins(enumToValue(CoinType::Transferable));

threadPlayer->coinBalance = coins;
threadPlayer->coinTransferableBalance = transferCoins;
threadPlayer->sendCoinBalance();
}
},
"ProtocolGame::updateCoinBalance"
);
}

void ProtocolGame::sendMarketLeave() {
Expand Down
10 changes: 6 additions & 4 deletions src/server/network/protocol/protocollogin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ void ProtocolLogin::onRecvFirstMessage(NetworkMessage &msg) {
return;
}

g_dispatcher().addEvent([self = std::static_pointer_cast<ProtocolLogin>(shared_from_this()), accountDescriptor, password] {
self->getCharacterList(accountDescriptor, password);
},
"ProtocolLogin::getCharacterList");
g_dispatcher().addEvent(
[self = std::static_pointer_cast<ProtocolLogin>(shared_from_this()), accountDescriptor, password] {
self->getCharacterList(accountDescriptor, password);
},
"ProtocolLogin::getCharacterList"
);
}
20 changes: 12 additions & 8 deletions src/server/network/protocol/protocolstatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ void ProtocolStatus::onRecvFirstMessage(NetworkMessage &msg) {
// XML info protocol
case 0xFF: {
if (msg.getString(4) == "info") {
g_dispatcher().addEvent([self = std::static_pointer_cast<ProtocolStatus>(shared_from_this())] {
self->sendStatusString();
},
"ProtocolStatus::sendStatusString");
g_dispatcher().addEvent(
[self = std::static_pointer_cast<ProtocolStatus>(shared_from_this())] {
self->sendStatusString();
},
"ProtocolStatus::sendStatusString"
);
return;
}
break;
Expand All @@ -60,10 +62,12 @@ void ProtocolStatus::onRecvFirstMessage(NetworkMessage &msg) {
if (requestedInfo & REQUEST_PLAYER_STATUS_INFO) {
characterName = msg.getString();
}
g_dispatcher().addEvent([self = std::static_pointer_cast<ProtocolStatus>(shared_from_this()), requestedInfo, characterName] {
self->sendInfo(requestedInfo, characterName);
},
"ProtocolStatus::sendInfo");
g_dispatcher().addEvent(
[self = std::static_pointer_cast<ProtocolStatus>(shared_from_this()), requestedInfo, characterName] {
self->sendInfo(requestedInfo, characterName);
},
"ProtocolStatus::sendInfo"
);

return;
}
Expand Down
10 changes: 5 additions & 5 deletions src/server/signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ void Signals::asyncWait() {
void Signals::dispatchSignalHandler(int signal) {
switch (signal) {
case SIGINT: // Shuts the server down
g_dispatcher().addEvent(sigintHandler, "sigintHandler");
g_dispatcher().addEvent(sigintHandler, "Signals::sigintHandler");
break;
case SIGTERM: // Shuts the server down
g_dispatcher().addEvent(sigtermHandler, "sigtermHandler");
g_dispatcher().addEvent(sigtermHandler, "Signals::sigtermHandler");
break;
#ifndef _WIN32
case SIGHUP: // Reload config/data
g_dispatcher().addEvent(sighupHandler, "sighupHandler");
g_dispatcher().addEvent(sighupHandler, "Signals::sighupHandler");
break;
case SIGUSR1: // Saves game state
g_dispatcher().addEvent(sigusr1Handler, "sigusr1Handler");
g_dispatcher().addEvent(sigusr1Handler, "Signals::sigusr1Handler");
break;
#else
case SIGBREAK: // Shuts the server down
g_dispatcher().addEvent(sigbreakHandler, "sigbreakHandler");
g_dispatcher().addEvent(sigbreakHandler, "Signals::sigbreakHandler");
// hold the thread until other threads end
inject<ThreadPool>().shutdown();
break;
Expand Down
Loading