Skip to content

Commit 6585df1

Browse files
committed
NITWORK: Add argument of playerNb in server r-type
MINOR
1 parent 1372d77 commit 6585df1

File tree

9 files changed

+41
-36
lines changed

9 files changed

+41
-36
lines changed

src/Nitwork/ANitwork.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,6 @@ namespace Nitwork {
314314
return lastId;
315315
}
316316

317-
const boost::asio::ip::udp::endpoint &ANitwork::getEndpointSender()
318-
{
319-
return _senderEndpoint;
320-
}
321-
322317
void ANitwork::addPacketToSend(const Packet &packet)
323318
{
324319
std::lock_guard<std::mutex> lock(_outputQueueMutex);

src/Nitwork/ANitwork.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ namespace Nitwork {
2626
void operator=(const ANitwork &) = delete;
2727
void operator=(const ANitwork &&) = delete;
2828

29+
void stop() override;
30+
protected:
31+
ANitwork();
2932
// start the NitworkServer
30-
bool start(int port, int threadNb, int tick, const std::string &ip = "") override;
33+
bool start(int port, int threadNb, int tick, const std::string &ip = "") final;
3134

32-
void stop() override;
3335
// send data to the endpoint with the given data
3436
template <typename T>
3537
void sendData(Packet &packet)
@@ -73,14 +75,10 @@ namespace Nitwork {
7375
});
7476
}
7577

76-
protected:
77-
ANitwork();
78-
7978
/* Getters / Setters */
8079
n_idsReceived_t getIdsReceived(const boost::asio::ip::udp::endpoint &endpoint);
8180
n_id_t getLastIdsReceived(const boost::asio::ip::udp::endpoint &endpoint);
8281
n_id_t getPacketId(const boost::asio::ip::udp::endpoint &endpoint);
83-
const boost::asio::ip::udp::endpoint &getEndpointSender();
8482
void addPacketToSend(const Packet &);
8583
void handlePacketIdsReceived(const struct header_s &header);
8684

src/Nitwork/Nitwork.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#define ONE_SECOND 1000
1717
#define DEFAULT_THREAD_NB 4
1818
#define MAX_NB_ACTION 16
19-
#define MAX_CLIENTS 4
2019
#define HEADER_CODE1 '\x01'
2120
#define HEADER_CODE2 '\x03'
2221

src/Nitwork/NitworkClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Nitwork {
2222
return _instance;
2323
}
2424

25-
bool NitworkClient::start(int port, int threadNb, int tick, const std::string &ip)
25+
bool NitworkClient::startClient(int port, const std::string &ip, int threadNb, int tick)
2626
{
2727
return ANitwork::start(port, threadNb, tick, ip);
2828
}

src/Nitwork/NitworkClient.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ namespace Nitwork {
2222
static NitworkClient &getInstance();
2323

2424
using ANitwork::start;
25-
bool start(
25+
bool startClient(
2626
int port,
27+
const std::string &ip,
2728
int threadNb = DEFAULT_THREAD_NB,
28-
int tick = TICKS_PER_SECOND,
29-
const std::string &ip = "") final;
29+
int tick = TICKS_PER_SECOND);
3030

3131
// Messages creation methods
3232
void addInitMsg();

src/Nitwork/NitworkServer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ namespace Nitwork {
2222
return _instance;
2323
}
2424

25-
bool NitworkServer::start(int port, int threadNb, int tick, const std::string &ip)
25+
bool NitworkServer::startServer(
26+
int port,
27+
int nbPlayer,
28+
int threadNb,
29+
int tick)
2630
{
27-
return ANitwork::start(port, threadNb, tick, ip);
31+
_maxNbPlayer = nbPlayer;
32+
return ANitwork::start(port, threadNb, tick, "");
2833
}
2934

3035
bool NitworkServer::startNitworkConfig(int port, const std::string & /* unused */)
@@ -111,7 +116,7 @@ namespace Nitwork {
111116
void
112117
NitworkServer::handleInitMsg(const std::any & /* unused */, boost::asio::ip::udp::endpoint &endpoint)
113118
{
114-
if (_endpoints.size() >= MAX_CLIENTS) {
119+
if (_endpoints.size() >= _maxNbPlayer) {
115120
std::cerr << "Too many clients, can't add an other one" << std::endl;
116121
return;
117122
}
@@ -130,7 +135,7 @@ namespace Nitwork {
130135
Logger::info("Client not connected");
131136
return;
132137
}
133-
if (_endpoints.size() < MAX_CLIENTS) {
138+
if (_endpoints.size() < _maxNbPlayer) {
134139
Logger::info("A new client is ready, waiting for others");
135140
return;
136141
}

src/Nitwork/NitworkServer.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ namespace Nitwork {
2121

2222
static NitworkServer &getInstance();
2323

24-
bool start(
24+
bool startServer(
2525
int port,
26+
int nbPlayer,
2627
int threadNb = DEFAULT_THREAD_NB,
27-
int tick = TICKS_PER_SECOND,
28-
const std::string &ip = "") final;
28+
int tick = TICKS_PER_SECOND);
2929

3030
/* Messages creation methods */
3131
void addStarWaveMessage(boost::asio::ip::udp::endpoint &endpoint, n_id_t enemyId);
@@ -84,6 +84,7 @@ namespace Nitwork {
8484
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
8585
static NitworkServer _instance; // instance of the NitworkServer (singleton)
8686
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
87+
unsigned int _maxNbPlayer = 0; // max number of players
8788
std::list<boost::asio::ip::udp::endpoint>
8889
_endpoints; // A vector of endpoints which will be used to send the actions to the clients
8990
// and identify them

src/main_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int main(int ac, char **av)
6464
}
6565
auto &sceneManager = Scene::SceneManager::getInstance();
6666
if (!Nitwork::NitworkClient::getInstance()
67-
.start(std::stoi(av[2]), DEFAULT_THREAD_NB, TICKS_PER_SECOND, av[1])) {
67+
.startClient(std::stoi(av[2]), av[1], DEFAULT_THREAD_NB, TICKS_PER_SECOND)) {
6868
return EXIT_EPITECH;
6969
}
7070
Nitwork::NitworkClient::getInstance().addInitMsg();

src/main_server.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,31 @@ static void signalHandler(int signum)
1818
signal(SIGINT, SIG_DFL);
1919
}
2020

21-
static bool checkArgs(int ac, const char **av)
21+
static bool isNumber(const std::string &str)
2222
{
23-
if (ac != 2) {
24-
Logger::error("Usage: ./r-type_server <port>");
25-
return false;
23+
for (auto c : str) {
24+
if (c < '0' || c > '9') {
25+
return false;
26+
}
2627
}
27-
if (av[1][0] == '\0') {
28-
Logger::error("Invalid ip");
28+
return true;
29+
}
30+
31+
static bool checkArgs(int ac, const char **av)
32+
{
33+
if (ac != 3) {
34+
Logger::error("Usage: ./r-type_server <port> <playerNb>");
2935
return false;
3036
}
31-
for (int i = 0; av[1][i] != '\0'; i++) {
32-
if (av[1][i] < '0' || av[1][i] > '9') {
33-
Logger::error("Invalid port");
37+
const std::vector<std::string> args(av + 1, av + ac);
38+
for (const auto &arg : args) {
39+
if (!isNumber(arg)) {
40+
Logger::error("Invalid argument");
3441
return false;
3542
}
3643
}
37-
if (std::stoi(av[1]) < PORT_MIN || std::stoi(av[1]) > PORT_MAX) {
38-
Logger::error("Invalid port");
44+
if (std::stoi(args[0]) < PORT_MIN || std::stoi(args[0]) > PORT_MAX || std::stoi(args[1]) < 1) {
45+
Logger::error("Invalid port or playerNb");
3946
return false;
4047
}
4148
return true;
@@ -51,7 +58,7 @@ int main(int ac, const char **av)
5158
return EXIT_EPITECH;
5259
}
5360
Logger::info("Starting Server...");
54-
if (!Nitwork::NitworkServer::getInstance().start(std::stoi(av[1]))) {
61+
if (!Nitwork::NitworkServer::getInstance().startServer(std::stoi(av[1]), std::stoi(av[2]))) {
5562
return EXIT_EPITECH;
5663
}
5764
Systems::SystemManagersDirector::getInstance().addSystemManager(Systems::getECSSystems());

0 commit comments

Comments
 (0)