Skip to content

Commit

Permalink
NITWORK: Add missing call of function that receive datas
Browse files Browse the repository at this point in the history
PATCH
  • Loading branch information
romainpanno committed Sep 29, 2023
1 parent d7f8b4c commit 30b26e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
19 changes: 10 additions & 9 deletions src/Nitwork/Nitwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ namespace Nitwork {
std::cerr << "Error: server config failed" << std::endl;
return false;
}
startInputHandler();
startOutputHandler();
if (!startServerThreads(threadNb, tick)) {
std::cerr << "Error: server threads failed" << std::endl;
return false;
}
startInputHandler();
startOutputHandler();
startReceiveHandler();
std::cout << "Server started on port " << port << " on " << boost::asio::ip::host_name() << " with ip " << _endpoint.address().to_string() << std::endl;
} catch (std::exception &e) {
std::cerr << "Nitwork Error : " << e.what() << std::endl;
Expand Down Expand Up @@ -97,7 +98,9 @@ namespace Nitwork {
std::cout << std::endl << "Starting input handler" << std::endl;
try {
while (true) {
std::cout << "Waiting for input" << std::endl;
_tickConvVar.wait(lockTick);
std::cout << "Input received" << std::endl;
for (auto &action : _actions) {
action.second(action.first.data, action.first.endpoint);
}
Expand All @@ -109,7 +112,7 @@ namespace Nitwork {
}

void Nitwork::startOutputHandler() {
boost::asio::post([this]() {
boost::asio::post(_context, [this]() {
std::unique_lock<std::mutex> lockQueue(_outputQueueMutex);
std::unique_lock<std::mutex> lockTick(_tickMutex);
std::size_t size = 0;
Expand All @@ -136,23 +139,21 @@ namespace Nitwork {

/* Receive Section */
bool Nitwork::startReceiveHandler() {
boost::asio::ip::udp::endpoint endpoint;

std::cout << "Starting receive handler" << std::endl;
_socket.async_receive_from(
boost::asio::buffer(&_headerPacket, HEADER_SIZE),
endpoint,
_clientEndpoint,
boost::bind(
&Nitwork::headerHandler,
this,
endpoint,
boost::asio::placeholders::bytes_transferred,
boost::asio::placeholders::error
)
)
);
}

void Nitwork::headerHandler(const boost::asio::ip::udp::endpoint &endpoint, const std::size_t bytes_received, const boost::system::error_code& error) {
void Nitwork::headerHandler(std::size_t bytes_received, const boost::system::error_code& error) {
if (error) {
std::cerr << "Error: " << error.message() << std::endl;
startReceiveHandler();
Expand All @@ -170,7 +171,7 @@ namespace Nitwork {
}
std::cout << "header received" << std::endl;
for (int i = 0; i < _headerPacket.nb_action; i++) {
handleBodyAction(endpoint);
handleBodyAction(_clientEndpoint);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Nitwork/Nitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Nitwork {
// start receive handler
bool startReceiveHandler();
// handler func for receive handler which handle the header
void headerHandler(const boost::asio::ip::udp::endpoint &endpoint, const std::size_t bytes_received, const boost::system::error_code& error);
void headerHandler(std::size_t bytes_received, const boost::system::error_code& error);
// handler func for headerHandler which handle the action
void handleBodyAction(const boost::asio::ip::udp::endpoint &endpoint);
// handler func for handleBodyAction which handle the body
Expand Down Expand Up @@ -166,6 +166,7 @@ namespace Nitwork {
struct action_s _actionPacket = { NO_ACTION }; // A packet which will be used to receive the action
struct msgInit_s _initPacket = { 'N' }; // A packet which will be used to receive the init message
struct msgReady_s _readyPacket = { 'N' }; // A packet which will be used to receive the ready message
boost::asio::ip::udp::endpoint _clientEndpoint; // An endpoint which will be used to receive the actions

// Actions ids
std::array<id_t, MAX_NB_ACTION> _ids{}; // An array of ids which will be used to identify the actions
Expand Down

0 comments on commit 30b26e4

Please sign in to comment.