Skip to content

Commit

Permalink
NITWORK: Fix read bug
Browse files Browse the repository at this point in the history
PATCH
  • Loading branch information
romainpanno committed Oct 3, 2023
1 parent 7277c8e commit 5c26e17
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
41 changes: 17 additions & 24 deletions src/Nitwork/ANitwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ namespace Nitwork {

void ANitwork::startReceiveHandler()
{
_receiveBuffer.fill(0);
_socket.async_receive_from(
boost::asio::buffer(_receiveBuffer),
_senderEndpoint,
Expand All @@ -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;
Expand All @@ -132,28 +129,24 @@ namespace Nitwork {
startReceiveHandler();
return;
}
try {
auto *header = reinterpret_cast<struct header_s *>(_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<struct header_s *>(_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)
Expand Down
18 changes: 5 additions & 13 deletions src/Nitwork/ANitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ namespace Nitwork {
void handleBody(const actionHandler &handler)
{
auto *body = reinterpret_cast<B *>(
_receiveBuffer.data() + sizeof(struct header_s));
_receiveBuffer.data() + sizeof(struct header_s) + sizeof(struct action_s));
handleBodyDatas<B>(
handler,
*body,
boost::system::error_code(),
sizeof(B));
boost::system::error_code());
}


Expand All @@ -110,24 +109,17 @@ namespace Nitwork {
template <typename B>
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<std::mutex> lock(_inputQueueMutex);
SenderData senderData(_senderEndpoint, std::any(body));
_actions.emplace_back(senderData, handler);
startReceiveHandler();
}
void addPacketToSentPackages(const std::pair<boost::asio::ip::basic_endpoint<boost::asio::ip::udp>, packet_s> &data);

Expand All @@ -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<char, MAX_PACKET_SIZE> _receiveBuffer; // The buffer used to receive the actions
std::array<char, MAX_PACKET_SIZE> _receiveBuffer = {0}; // The buffer used to receive the actions
// std::vector<char> _receiveBuffer; // The buffer used to receive the actions

// list of packets' ids receives
Expand Down
3 changes: 1 addition & 2 deletions src/Nitwork/NitworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ namespace Nitwork {

void NitworkServer::handleBodyAction(__attribute__((unused)) const boost::asio::ip::udp::endpoint &endpoint)
{
auto *action = reinterpret_cast<struct action_s *>(
_receiveBuffer.data() + sizeof(struct header_s));
auto *action = reinterpret_cast<struct action_s *>(_receiveBuffer.data() + sizeof(struct header_s));
auto it = _actionsHandlers.find(action->magick);

if (it == _actionsHandlers.end()) {
Expand Down

0 comments on commit 5c26e17

Please sign in to comment.