Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
39 changes: 29 additions & 10 deletions src/Client/Systems/Network/ClientNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Systems {
}
}

void receiveEnemyDeath(std::any &any, boost::asio::ip::udp::endpoint &)
void receiveEnemyDeath(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto enemyDeath = std::any_cast<struct msgEnemyDeath_s>(any);
Expand All @@ -43,7 +43,7 @@ namespace Systems {
}
}

void handleStartWave(std::any &any, boost::asio::ip::udp::endpoint &)
void handleStartWave(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto wave = std::any_cast<struct msgStartWave_s>(any);
Expand All @@ -54,7 +54,7 @@ namespace Systems {
Logger::info("Wave started");
}

void receivePlayerInit(std::any &any, boost::asio::ip::udp::endpoint &)
void receivePlayerInit(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto playerInit = std::any_cast<struct msgPlayerInit_s>(any);
Expand All @@ -63,7 +63,7 @@ namespace Systems {
initPlayer(JsonType::DEFAULT_PLAYER, playerInit.playerId);
}

void receiveNewEnemy(std::any &any, boost::asio::ip::udp::endpoint &)
void receiveNewEnemy(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto newEnemy = std::any_cast<struct msgNewEnemy_s>(any);
Expand All @@ -78,16 +78,34 @@ namespace Systems {
Registry::getInstance().getComponents<struct health_s>().insertBack(hp);
}

void receiveNewAllie(std::any &any, boost::asio::ip::udp::endpoint &)
void receiveNewAllie(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto newAllie = std::any_cast<struct msgNewAllie_s>(any);
Types::Position pos = {
static_cast<float>(newAllie.data.pos.x),
static_cast<float>(newAllie.data.pos.y)};

initPlayer(JsonType::DEFAULT_PLAYER, newAllie.data.id, true);
Registry::getInstance().getComponents<Types::Position>().insertBack(pos);
Logger::info("New Ally created with id: " + std::to_string(newAllie.playerId));
initPlayer(JsonType::DEFAULT_PLAYER, newAllie.playerId, true);
}

void receiveRelativePosition(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const struct msgPositionRelativeBroadcast_s &msg =
std::any_cast<struct msgPositionRelativeBroadcast_s>(any);
Types::Position position = {static_cast<float>(msg.pos.x), static_cast<float>(msg.pos.y)};
auto &arrPos = Registry::getInstance().getComponents<Types::Position>();
auto &arrOtherPlayers = Registry::getInstance().getComponents<Types::OtherPlayer>();
auto ids = Registry::getInstance().getEntitiesByComponents(
{typeid(Types::Position), typeid(Types::OtherPlayer)});
auto otherPlayer = std::find_if(ids.begin(), ids.end(), [&arrOtherPlayers, &msg](std::size_t id) {
return arrOtherPlayers[id].constId == msg.playerId;
});

if (otherPlayer == ids.end()) {
return;
}
Logger::trace("Other player id: " + std::to_string(msg.playerId) + " relative position: " + std::to_string(position.x) + " " + std::to_string(position.y));
arrPos[*otherPlayer] += position;
}

void sendPositionRelative(std::size_t /* unused */, std::size_t /* unused */)
Expand Down Expand Up @@ -119,6 +137,7 @@ namespace Systems {
.x = static_cast<char>(static_cast<int>(pos.x - posCached.x)),
.y = static_cast<char>(static_cast<int>(pos.y - posCached.y)),
};
Logger::trace("send pos relative\n\n" + std::to_string(msg.x) + " " + std::to_string(msg.y) + "\n\n");
posCached.x = pos.x;
posCached.y = pos.y;
Nitwork::NitworkClient::getInstance().addPositionRelativeMsg(msg);
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Network/ClientNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Systems {
void receiveNewAllie(std::any &any, boost::asio::ip::udp::endpoint &);
void sendPositionRelative(std::size_t /* unused */, std::size_t /* unused */);
void receiveNewBullet(std::any &any, boost::asio::ip::udp::endpoint &endpoint);
void receiveRelativePosition(std::any &any, boost::asio::ip::udp::endpoint &);
void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint &endpoint);
std::vector<std::function<void(std::size_t, std::size_t)>> getNetworkSystems();
} // namespace Systems
7 changes: 7 additions & 0 deletions src/ECS/ECSCustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ namespace Types {
float y;

NLOHMANN_DEFINE_TYPE_INTRUSIVE(Position, x, y);

Position &operator+=(const Position &pos)
{
x += pos.x;
y += pos.y;
return (*this);
}
};

struct Damage {
Expand Down
2 changes: 2 additions & 0 deletions src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ namespace Systems {
if (otherPlayer) {
Types::OtherPlayer otherPlayerComp(constId);
Registry::getInstance().getComponents<Types::OtherPlayer>().insertBack(otherPlayerComp);
Types::PlayerAllies allie;
Registry::getInstance().getComponents<Types::PlayerAllies>().insertBack(allie);
} else {
Types::Player playerComp = {constId};
Registry::getInstance().getComponents<Types::Player>().insertBack(playerComp);
Expand Down
24 changes: 20 additions & 4 deletions src/Nitwork/ANitwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ namespace Nitwork {
_pool.clear();
}

bool ANitwork::isRunning() const
{
return _isRunning;
}

void ANitwork::startReceiveHandler()
{
if (!_isRunning) {
Expand All @@ -121,7 +126,6 @@ namespace Nitwork {

void ANitwork::callReceiveHandler(const std::string &message)
{
Logger::error("NITWORK: " + message);
startReceiveHandler();
}

Expand Down Expand Up @@ -207,12 +211,18 @@ namespace Nitwork {
return a.first.id < b.first.id;
});
for (auto &action : _actions) {
action.second(action.first.data, action.first.endpoint);
try {
action.second(action.first.data, action.first.endpoint);
} catch (std::exception &e) {
Logger::error("NITWORK: catch action: " + std::string(e.what()));
}
}
_actions.clear();
_inputQueueMutex.unlock();
}
} catch (std::exception &e) {
_inputQueueMutex.unlock();
_isRunning = false;
Logger::fatal("NITWORK: catch input thread: " + std::string(e.what()));
}
});
Expand All @@ -227,7 +237,11 @@ namespace Nitwork {
continue;
}
addPacketToSentPackages(data);
it->second(data);
try {
it->second(data);
} catch (std::exception &e) {
Logger::error("NITWORK: catch action: " + std::string(e.what()));
}
}
}

Expand All @@ -250,6 +264,8 @@ namespace Nitwork {
_outputQueueMutex.unlock();
}
} catch (std::exception &e) {
_outputQueueMutex.unlock();
_isRunning = false;
Logger::fatal("NITWORK: catch output thread: " + std::string(e.what()));
}
});
Expand Down Expand Up @@ -318,7 +334,7 @@ namespace Nitwork {
{
std::lock_guard<std::mutex> lock(_outputQueueMutex);

Logger::info("NITWORK: Adding packet to send of type: " + std::to_string(packet.action));
_outputQueue.emplace_back(packet);
Logger::trace("NITWORK: Adding packet to send of type: " + std::to_string(packet.action));
}
} // namespace Nitwork
2 changes: 2 additions & 0 deletions src/Nitwork/ANitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace Nitwork {

void stop() override;

bool isRunning() const final;

protected:
ANitwork();
// start the NitworkServer
Expand Down
2 changes: 2 additions & 0 deletions src/Nitwork/INitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ namespace Nitwork {

virtual void stop() = 0;

virtual bool isRunning() const = 0;

protected:
INitwork() = default;
// start the NitworkServer config
Expand Down
2 changes: 1 addition & 1 deletion src/Nitwork/Nitwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ PACK(struct packetPositionAbsolute_s {

PACK(struct msgNewAllie_s {
n_magick_t magick;
player_infos_s data;
n_id_t playerId;
});

PACK(struct packetNewAllie_s {
Expand Down
13 changes: 12 additions & 1 deletion src/Nitwork/NitworkClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,22 @@ namespace Nitwork {
}
}
},
{
POSITION_RELATIVE_BROADCAST,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgPositionRelativeBroadcast_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveRelativePosition(any, endpoint);
}
},
},
{
POSITION_ABSOLUTE_BROADCAST,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgPositionAbsolute_s>(handler, header);
handleBody<struct msgPositionAbsoluteBroadcast_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveBroadcastAbsolutePosition(any, endpoint);
Expand Down
41 changes: 39 additions & 2 deletions src/Nitwork/NitworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ namespace Nitwork {
}
/* End Check Methods Section */

void NitworkServer::sendNewAllie(
n_id_t playerId,
struct packetNewAllie_s packetMsgNewAllie,
boost::asio::ip::udp::endpoint &endpoint,
bool butNoOne)
{
packetMsgNewAllie.msg.playerId = playerId;
if (butNoOne) {
Packet packet(
packetMsgNewAllie.action.magick,
std::make_any<struct packetNewAllie_s>(packetMsgNewAllie));
sendToAllClientsButNotOne(packet, endpoint);
} else {
Packet packet(
packetMsgNewAllie.action.magick,
std::make_any<struct packetNewAllie_s>(packetMsgNewAllie),
endpoint);
addPacketToSend(packet);
}
}

/* Handle packet (msg) Section */
void
NitworkServer::handleInitMsg(const std::any & /* unused */, boost::asio::ip::udp::endpoint &endpoint)
Expand All @@ -121,7 +142,22 @@ namespace Nitwork {
return;
}
_endpoints.emplace_back(endpoint);
addPlayerInitMessage(endpoint, static_cast<n_id_t>(_endpoints.size() - 1));
auto playerId = static_cast<n_id_t>(_endpoints.size() - 1);
// Send new Allie to others
addPlayerInitMessage(endpoint, playerId);
struct packetNewAllie_s packetMsgNewAllie = {
.header = {0, 0, 0, 0, 1, 0},
.action = {.magick = NEW_ALLIE},
.msg = {.magick = MAGICK_NEW_ALLIE, .playerId = playerId}
};
Systems::initPlayer(JsonType::DEFAULT_PLAYER, playerId, true);
sendNewAllie(playerId, packetMsgNewAllie, endpoint);
for (const auto &[_, allieId] : _playersIds) {
if (allieId == playerId) {
continue;
}
sendNewAllie(allieId, packetMsgNewAllie, endpoint, false);
}
}

void
Expand All @@ -146,7 +182,8 @@ namespace Nitwork {
Logger::info("Client not connected");
return;
}
auto pos = std::any_cast<struct position_relative_s>(msg);
auto msgData = std::any_cast<struct msgPositionRelative_s>(msg);
auto pos = msgData.pos;
struct packetPositionRelativeBroadcast_s msgPosBroadcast = {
.header =
{.magick1 = HEADER_CODE1,
Expand Down
21 changes: 21 additions & 0 deletions src/Nitwork/NitworkServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ namespace Nitwork {

bool isClientAlreadyConnected(boost::asio::ip::udp::endpoint &endpoint) const;

void sendNewAllie(
n_id_t playerId,
struct packetNewAllie_s packetMsgNewAllie,
boost::asio::ip::udp::endpoint &endpoint,
bool butNoOne = true);

/* BEGIN handle messages methods */
void handleInitMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint);

Expand Down Expand Up @@ -105,6 +111,13 @@ namespace Nitwork {
[this](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) {
handleReadyMsg(msg, endpoint);
}}},
{POSITION_RELATIVE,
{[this](actionHandler &actionHandler, const struct header_s &header) {
handleBody<struct msgPositionRelative_s>(actionHandler, header);
},
[this](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) {
handleRelativePositionMsg(msg, endpoint);
}}},
{LIFE_UPDATE,
{[this](actionHandler &actionHandler, const struct header_s &header) {
handleBody<struct msgLifeUpdate_s>(actionHandler, header);
Expand Down Expand Up @@ -159,6 +172,14 @@ namespace Nitwork {
[this](Packet &packet) {
sendData<struct packetNewBullet_s>(packet);
}},
{NEW_ALLIE,
[this](Packet &packet) {
sendData<struct packetNewAllie_s>(packet);
}},
{POSITION_RELATIVE_BROADCAST,
[this](Packet &packet) {
sendData<struct packetPositionRelativeBroadcast_s>(packet);
}},
{POSITION_ABSOLUTE_BROADCAST,
[this](Packet &packet) {
sendData<struct packetPositionAbsoluteBroadcast_s>(packet);
Expand Down
2 changes: 1 addition & 1 deletion src/main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main(int ac, const char **av)
Systems::SystemManagersDirector::getInstance().addSystemManager(Systems::getECSSystems());
signal(SIGINT, signalHandler);

while (isRunning) {
while (isRunning && Nitwork::NitworkServer::getInstance().isRunning()) {
Systems::SystemManagersDirector::getInstance().getSystemManager(0).updateSystems();
}
Nitwork::NitworkServer::getInstance().stop();
Expand Down