diff --git a/src/Nitwork/ANitwork.cpp b/src/Nitwork/ANitwork.cpp index 0a7ae12f..1786c724 100644 --- a/src/Nitwork/ANitwork.cpp +++ b/src/Nitwork/ANitwork.cpp @@ -103,6 +103,7 @@ namespace Nitwork { void ANitwork::startReceiveHandler() { + _receiveBuffer.fill(0); _socket.async_receive_from( boost::asio::buffer(_receiveBuffer), _senderEndpoint, @@ -117,10 +118,6 @@ namespace Nitwork { std::size_t bytes_received, const boost::system::error_code &error) { - static int vv = 1; - std::ofstream file("log" + std::to_string(vv++) + std::getenv("TYPE") + ".txt"); - file.write(_receiveBuffer.data(), bytes_received); - std::cout << "abc: " << bytes_received << std::endl; if (bytes_received < sizeof(struct header_s)) { std::cerr << "Error: header not received" << std::endl; @@ -132,28 +129,24 @@ namespace Nitwork { startReceiveHandler(); return; } - try { - auto *header = reinterpret_cast(_receiveBuffer.data()); - if (header->magick1 != HEADER_CODE1 || header->magick2 != HEADER_CODE2) { - std::cerr << "Error: header magick not valid" << std::endl; - startReceiveHandler(); - return; - } - std::cout << "packet id :" << header->id << std::endl; - if (header->nb_action > MAX_NB_ACTION || header->nb_action < 0 || isAlreadyReceived(header->id)) { - std::cerr << "Error: too many actions received or no action or already received" << std::endl; - startReceiveHandler(); - return; - } - _receivedPacketsIds.push_back(header->id); -// handlePacketIdsReceived(*header); - for (int i = 0; i < header->nb_action; i++) { - handleBodyAction(_senderEndpoint); - } + auto *header = reinterpret_cast(_receiveBuffer.data()); + std::cout << "packet id :" << header->id << std::endl; + if (header->magick1 != HEADER_CODE1 || header->magick2 != HEADER_CODE2) { + std::cerr << "Error: header magick not valid" << std::endl; startReceiveHandler(); - } catch (std::exception &e) { - std::cerr << "Error: " << e.what() << std::endl; + return; + } + if (header->nb_action > MAX_NB_ACTION || header->nb_action < 0 || isAlreadyReceived(header->id)) { + std::cerr << "Error: too many actions received or no action or already received" << std::endl; + startReceiveHandler(); + return; } + _receivedPacketsIds.push_back(header->id); +// handlePacketIdsReceived(*header); +// for (int i = 0; i < header->nb_action; i++) { + handleBodyAction(_senderEndpoint); +// } + startReceiveHandler(); } bool ANitwork::isAlreadyReceived(n_id_t id) diff --git a/src/Nitwork/ANitwork.hpp b/src/Nitwork/ANitwork.hpp index 533268fb..5c707d5f 100644 --- a/src/Nitwork/ANitwork.hpp +++ b/src/Nitwork/ANitwork.hpp @@ -80,12 +80,11 @@ namespace Nitwork { void handleBody(const actionHandler &handler) { auto *body = reinterpret_cast( - _receiveBuffer.data() + sizeof(struct header_s)); + _receiveBuffer.data() + sizeof(struct header_s) + sizeof(struct action_s)); handleBodyDatas( handler, *body, - boost::system::error_code(), - sizeof(B)); + boost::system::error_code()); } @@ -110,24 +109,17 @@ namespace Nitwork { template void handleBodyDatas( const actionHandler &handler, - B body, - const boost::system::error_code &error, - const std::size_t bytes_received) + B &body, + const boost::system::error_code &error) { if (error) { std::cerr << "Error: " << error.message() << std::endl; startReceiveHandler(); return; } - if (bytes_received != sizeof(B)) { - std::cerr << "Error: body not received" << std::endl; - startReceiveHandler(); - return; - } std::lock_guard lock(_inputQueueMutex); SenderData senderData(_senderEndpoint, std::any(body)); _actions.emplace_back(senderData, handler); - startReceiveHandler(); } void addPacketToSentPackages(const std::pair, packet_s> &data); @@ -137,7 +129,7 @@ namespace Nitwork { boost::asio::ip::udp::endpoint _endpoint; // endpoint of the Server // The buffer used to receive the actions - std::array _receiveBuffer; // The buffer used to receive the actions + std::array _receiveBuffer = {0}; // The buffer used to receive the actions // std::vector _receiveBuffer; // The buffer used to receive the actions // list of packets' ids receives diff --git a/src/Nitwork/NitworkServer.cpp b/src/Nitwork/NitworkServer.cpp index 447840a5..9f364dc5 100644 --- a/src/Nitwork/NitworkServer.cpp +++ b/src/Nitwork/NitworkServer.cpp @@ -37,8 +37,7 @@ namespace Nitwork { void NitworkServer::handleBodyAction(__attribute__((unused)) const boost::asio::ip::udp::endpoint &endpoint) { - auto *action = reinterpret_cast( - _receiveBuffer.data() + sizeof(struct header_s)); + auto *action = reinterpret_cast(_receiveBuffer.data() + sizeof(struct header_s)); auto it = _actionsHandlers.find(action->magick); if (it == _actionsHandlers.end()) {