Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
084422c
NITWORK: Fix packet id doesn't need to change if he's resent
romainpanno Oct 15, 2023
c29f846
FORMAT-AUTO: automatic format on pull request #100
github-actions[bot] Oct 15, 2023
8c2b377
NITWORK: Fix codeRabbit requests
romainpanno Oct 15, 2023
32246e5
Merge branch 'fix/Nitwork-dont-change-resent-packet-id' of github.com…
romainpanno Oct 15, 2023
591a295
NITWORK: Fix logger info when handling ready
romainpanno Oct 15, 2023
8d0741d
FORMAT-AUTO: automatic format on pull request #100
github-actions[bot] Oct 15, 2023
57749ff
RFC: Update rfc for ready handling
romainpanno Oct 15, 2023
57c1e95
Merge branch 'fix/Nitwork-dont-change-resent-packet-id' of github.com…
romainpanno Oct 15, 2023
aaab4a5
NITWORK: Fix codeRabbit requests
romainpanno Oct 15, 2023
8d477b4
FORMAT-AUTO: automatic format on pull request #100
github-actions[bot] Oct 15, 2023
0f101f5
NITWORK: Set 4 clients for max clients
romainpanno Oct 15, 2023
1372d77
Merge branch 'fix/Nitwork-dont-change-resent-packet-id' of github.com…
romainpanno Oct 15, 2023
6585df1
NITWORK: Add argument of playerNb in server r-type
romainpanno Oct 15, 2023
dd7b3af
FORMAT-AUTO: automatic format on pull request #100
github-actions[bot] Oct 15, 2023
ea53960
NITWORK: Fix client_main.cpp to use cpp types and better args checks
romainpanno Oct 15, 2023
91d58ff
Merge branch 'fix/Nitwork-dont-change-resent-packet-id' of github.com…
romainpanno Oct 15, 2023
3062a04
SERVER-NETWORK: Fix codeRabbit requests
romainpanno Oct 15, 2023
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
3 changes: 2 additions & 1 deletion docs/network/rfc/RFC.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ Table of Contents
2.2.2.2. READY

The Client must send a ready action to enter/start the game.
The Server respond to this action with a ready action.
The Server respond to this action by sending a start wave action, only if
all the clients are ready

2.2.2.2.1. Client

Expand Down
6 changes: 1 addition & 5 deletions src/Nitwork/ANitwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ namespace Nitwork {
"NITWORK: packet not found: " + std::to_string(header.last_id_received - index));
continue;
}
packet->setIsResend(true);
addPacketToSend(*packet);
}
}
Expand Down Expand Up @@ -313,11 +314,6 @@ namespace Nitwork {
return lastId;
}

const boost::asio::ip::udp::endpoint &ANitwork::getEndpointSender()
{
return _senderEndpoint;
}

void ANitwork::addPacketToSend(const Packet &packet)
{
std::lock_guard<std::mutex> lock(_outputQueueMutex);
Expand Down
35 changes: 18 additions & 17 deletions src/Nitwork/ANitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ namespace Nitwork {
void operator=(const ANitwork &) = delete;
void operator=(const ANitwork &&) = delete;

void stop() override;

protected:
ANitwork();
// start the NitworkServer
bool start(int port, int threadNb, int tick, const std::string &ip = "") override;
bool start(int port, int threadNb, int tick, const std::string &ip = "") final;

void stop() override;
// send data to the endpoint with the given data
template <typename T>
void sendData(Packet &packet)
Expand All @@ -43,17 +46,19 @@ namespace Nitwork {
Logger::error("NITWORK: Package too big");
return;
}
packet.id = id;
T data = std::any_cast<T>(packet.body);
auto header = static_cast<struct header_s>(data.header);
header = {
HEADER_CODE1,
getIdsReceived(packet.endpoint),
getLastIdsReceived(packet.endpoint),
id,
header.nb_action,
HEADER_CODE2};
data.header = header;
T data = std::any_cast<T>(packet.body);
if (!packet.getIsResend()) {
packet.id = id;
auto oldHeader = static_cast<struct header_s>(data.header);
struct header_s newHeader = {
HEADER_CODE1,
getIdsReceived(packet.endpoint),
getLastIdsReceived(packet.endpoint),
id,
oldHeader.nb_action,
HEADER_CODE2};
data.header = newHeader;
}

_socket.async_send_to(
boost::asio::buffer(&data, sizeof(T)),
Expand All @@ -71,14 +76,10 @@ namespace Nitwork {
});
}

protected:
ANitwork();

/* Getters / Setters */
n_idsReceived_t getIdsReceived(const boost::asio::ip::udp::endpoint &endpoint);
n_id_t getLastIdsReceived(const boost::asio::ip::udp::endpoint &endpoint);
n_id_t getPacketId(const boost::asio::ip::udp::endpoint &endpoint);
const boost::asio::ip::udp::endpoint &getEndpointSender();
void addPacketToSend(const Packet &);
void handlePacketIdsReceived(const struct header_s &header);

Expand Down
11 changes: 11 additions & 0 deletions src/Nitwork/INitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,22 @@ namespace Nitwork {
endpoint(endpoint)
{
}
[[nodiscard]] bool getIsResend() const
{
return _isResend;
}
void setIsResend(bool value)
{
_isResend = value;
}

n_id_t id = 0;
n_actionType_t action;
std::any body;
boost::asio::ip::udp::endpoint endpoint;

private:
bool _isResend = false;
};

using actionSender = std::function<void(Packet &)>;
Expand Down
1 change: 0 additions & 1 deletion src/Nitwork/Nitwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#define ONE_SECOND 1000
#define DEFAULT_THREAD_NB 4
#define MAX_NB_ACTION 16
#define MAX_CLIENTS 4
#define HEADER_CODE1 '\x01'
#define HEADER_CODE2 '\x03'

Expand Down
2 changes: 1 addition & 1 deletion src/Nitwork/NitworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Nitwork {
return _instance;
}

bool NitworkClient::start(int port, int threadNb, int tick, const std::string &ip)
bool NitworkClient::startClient(int port, const std::string &ip, int threadNb, int tick)
{
return ANitwork::start(port, threadNb, tick, ip);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Nitwork/NitworkClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ namespace Nitwork {
static NitworkClient &getInstance();

using ANitwork::start;
bool start(
bool startClient(
int port,
int threadNb = DEFAULT_THREAD_NB,
int tick = TICKS_PER_SECOND,
const std::string &ip = "") final;
const std::string &ip,
int threadNb = DEFAULT_THREAD_NB,
int tick = TICKS_PER_SECOND);

// Messages creation methods
void addInitMsg();
Expand Down
11 changes: 8 additions & 3 deletions src/Nitwork/NitworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ namespace Nitwork {
return _instance;
}

bool NitworkServer::start(int port, int threadNb, int tick, const std::string &ip)
bool NitworkServer::startServer(int port, int nbPlayer, int threadNb, int tick)
{
return ANitwork::start(port, threadNb, tick, ip);
_maxNbPlayer = nbPlayer;
return ANitwork::start(port, threadNb, tick, "");
}

bool NitworkServer::startNitworkConfig(int port, const std::string & /* unused */)
Expand Down Expand Up @@ -111,7 +112,7 @@ namespace Nitwork {
void
NitworkServer::handleInitMsg(const std::any & /* unused */, boost::asio::ip::udp::endpoint &endpoint)
{
if (_endpoints.size() >= MAX_CLIENTS) {
if (_endpoints.size() >= _maxNbPlayer) {
std::cerr << "Too many clients, can't add an other one" << std::endl;
return;
}
Expand All @@ -130,6 +131,10 @@ namespace Nitwork {
Logger::info("Client not connected");
return;
}
if (_endpoints.size() < _maxNbPlayer) {
Logger::info("A new client is ready, waiting for others");
return;
}
addStarWaveMessage(endpoint, Types::Enemy::getEnemyNb());
Systems::SystemManagersDirector::getInstance().getSystemManager(0).addSystem(Systems::initWave);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Nitwork/NitworkServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace Nitwork {

static NitworkServer &getInstance();

bool start(
bool startServer(
int port,
int threadNb = DEFAULT_THREAD_NB,
int tick = TICKS_PER_SECOND,
const std::string &ip = "") final;
int nbPlayer,
int threadNb = DEFAULT_THREAD_NB,
int tick = TICKS_PER_SECOND);

/* Messages creation methods */
void addStarWaveMessage(boost::asio::ip::udp::endpoint &endpoint, n_id_t enemyId);
Expand Down Expand Up @@ -84,6 +84,7 @@ namespace Nitwork {
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static NitworkServer _instance; // instance of the NitworkServer (singleton)
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
unsigned int _maxNbPlayer = 0; // max number of players
std::list<boost::asio::ip::udp::endpoint>
_endpoints; // A vector of endpoints which will be used to send the actions to the clients
// and identify them
Expand Down
43 changes: 13 additions & 30 deletions src/main_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@
** main
*/

// #include "NitworkClient.hpp"
//
// int main()
//{
// const int port = 4242;
// Nitwork::NitworkClient::getInstance().start(port);
//
// Nitwork::NitworkClient::getInstance().addInitMsg();
// Nitwork::NitworkClient::getInstance().addInitMsg();
// Nitwork::NitworkClient::getInstance().addInitMsg();
// Nitwork::NitworkClient::getInstance().addInitMsg();
// Nitwork::NitworkClient::getInstance().addReadyMsg();
// while (true) {};
// return 0;
// }

#include <cstddef>
#include "Logger.hpp"
#include "NitworkClient.hpp"
#include "Registry.hpp"
Expand All @@ -30,30 +13,30 @@

constexpr int EXIT_EPITECH = 84;

static bool checkArgs(int ac, char **av)
static bool isNumber(const std::string &str)
{
return std::all_of(str.begin(), str.end(), ::isdigit);
}

static bool checkArgs(int ac, const char **av)
{
if (ac != 3) {
Logger::error("Usage: ./r-type_client <ip> <port>");
return false;
}
for (int i = 0; av[2][i] != '\0'; i++) {
if (av[2][i] < '0' || av[2][i] > '9') {
Logger::error("Invalid port");
return false;
}
}
if (std::stoi(av[2]) < 0 || std::stoi(av[2]) > 65535) {
Logger::error("Invalid port");
const std::vector<std::string> args(av + 1, av + ac);
if (args[0].empty()) {
Logger::error("Invalid ip");
return false;
}
if (av[1][0] == '\0') {
Logger::error("Invalid ip");
if (!isNumber(args[1]) || std::stoi(args[1]) < 0 || std::stoi(args[1]) > 65535) {
Logger::error("Invalid port");
return false;
}
return true;
}

int main(int ac, char **av)
int main(int ac, const char **av)
{
#ifndef NDEBUG
Registry::getInstance().getLogger().setLogLevel(Logger::LogLevel::Debug);
Expand All @@ -64,7 +47,7 @@ int main(int ac, char **av)
}
auto &sceneManager = Scene::SceneManager::getInstance();
if (!Nitwork::NitworkClient::getInstance()
.start(std::stoi(av[2]), DEFAULT_THREAD_NB, TICKS_PER_SECOND, av[1])) {
.startClient(std::stoi(av[2]), av[1], DEFAULT_THREAD_NB, TICKS_PER_SECOND)) {
return EXIT_EPITECH;
}
Nitwork::NitworkClient::getInstance().addInitMsg();
Expand Down
26 changes: 14 additions & 12 deletions src/main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ static void signalHandler(int signum)
signal(SIGINT, SIG_DFL);
}

static bool isNumber(const std::string &str)
{
return std::all_of(str.begin(), str.end(), ::isdigit);
}

static bool checkArgs(int ac, const char **av)
{
if (ac != 2) {
Logger::error("Usage: ./r-type_server <port>");
return false;
}
if (av[1][0] == '\0') {
Logger::error("Invalid ip");
if (ac != 3) {
Logger::error("Usage: ./r-type_server <port> <playerNb>");
return false;
}
for (int i = 0; av[1][i] != '\0'; i++) {
if (av[1][i] < '0' || av[1][i] > '9') {
Logger::error("Invalid port");
const std::vector<std::string> args(av + 1, av + ac);
for (const auto &arg : args) {
if (!isNumber(arg)) {
Logger::error("Invalid argument: " + arg);
return false;
}
}
if (std::stoi(av[1]) < PORT_MIN || std::stoi(av[1]) > PORT_MAX) {
Logger::error("Invalid port");
if (std::stoi(args[0]) < PORT_MIN || std::stoi(args[0]) > PORT_MAX || std::stoi(args[1]) < 1) {
Logger::error("Invalid port or playerNb");
return false;
}
return true;
Expand All @@ -51,7 +53,7 @@ int main(int ac, const char **av)
return EXIT_EPITECH;
}
Logger::info("Starting Server...");
if (!Nitwork::NitworkServer::getInstance().start(std::stoi(av[1]))) {
if (!Nitwork::NitworkServer::getInstance().startServer(std::stoi(av[1]), std::stoi(av[2]))) {
return EXIT_EPITECH;
}
Systems::SystemManagersDirector::getInstance().addSystemManager(Systems::getECSSystems());
Expand Down