-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNitworkServer.hpp
More file actions
189 lines (162 loc) · 8.06 KB
/
NitworkServer.hpp
File metadata and controls
189 lines (162 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
** EPITECH PROJECT, 2023
** r-type
** File description:
** NitworkServer
*/
#pragma once
#include "ANitwork.hpp"
#include "ServerNetwork.hpp"
namespace Nitwork {
class NitworkServer : public ANitwork {
public:
~NitworkServer() override = default;
NitworkServer(const NitworkServer &) = delete;
NitworkServer(const NitworkServer &&) = delete;
void operator=(const NitworkServer &) = delete;
void operator=(const NitworkServer &&) = delete;
static NitworkServer &getInstance();
bool startServer(
int port,
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);
void addLifeUpdateMessage(
boost::asio::ip::udp::endpoint &endpoint,
n_id_t playerId,
const struct health_s &life);
void addEnemyDeathMessage(n_id_t enemyId);
void addNewEnemyMessage(
boost::asio::ip::udp::endpoint &endpoint,
const struct enemy_infos_s &enemyInfos);
void addPlayerInitMessage(boost::asio::ip::udp::endpoint &endpoint, n_id_t playerId);
void broadcastNewBulletMsg(
const struct msgNewBullet_s &msg,
boost::asio::ip::udp::endpoint &senderEndpoint);
void broadcastAbsolutePositionMsg(
const struct position_absolute_s &pos,
boost::asio::ip::udp::endpoint &senderEndpoint);
n_id_t getPlayerId(const boost::asio::ip::udp::endpoint &endpoint) const;
private:
NitworkServer() = default;
std::unordered_map<boost::asio::ip::udp::endpoint, n_id_t> _playersIds;
bool startNitworkConfig(int port, const std::string &ip) final;
void sendToAllClients(const Packet &packet);
void sendToAllClientsButNotOne(const Packet &packet, boost::asio::ip::udp::endpoint &endpoint);
void handleBodyAction(
const struct header_s &header,
const boost::asio::ip::udp::endpoint &endpoint) final;
[[nodiscard]] const std::map<enum n_actionType_t, actionSender> &
getActionToSendHandlers() const final;
bool isClientAlreadyConnected(boost::asio::ip::udp::endpoint &endpoint) const;
void sendNewAllie(
n_id_t playerId,
struct packetNewAllie_s packetMsgNewAllie,
boost::asio::ip::udp::endpoint &endpoint,
bool butNoOne = true);
/* BEGIN handle messages methods */
void handleInitMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint);
void handleReadyMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint);
void handleRelativePositionMsg(const std::any &msg, boost::asio::ip::udp::endpoint &endpoint);
/* END handle messages methods */
// 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
// maps that will be used to handle the actions, in order to send or receive them
std::map<enum n_actionType_t, std::pair<handleBodyT, actionHandler>> _actionsHandlers = {
{INIT,
{[this](actionHandler &actionHandler, const struct header_s &header) {
handleBody<struct msgInit_s>(actionHandler, header);
},
[this](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) {
handleInitMsg(msg, endpoint);
}}},
{READY,
{[this](actionHandler &actionHandler, const struct header_s &header) {
handleBody<struct msgReady_s>(actionHandler, header);
},
[this](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) {
handleReadyMsg(msg, endpoint);
}}},
{POSITION_RELATIVE,
{[this](actionHandler &actionHandler, const struct header_s &header) {
handleBody<struct msgPositionRelative_s>(actionHandler, header);
},
[this](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) {
handleRelativePositionMsg(msg, endpoint);
}}},
{LIFE_UPDATE,
{[this](actionHandler &actionHandler, const struct header_s &header) {
handleBody<struct msgLifeUpdate_s>(actionHandler, header);
},
[](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) {
Systems::handleLifeUpdateMsg(msg, endpoint);
}}},
{ENEMY_DEATH,
{[this](actionHandler &actionHandler, const struct header_s &header) {
handleBody<struct msgEnemyDeath_s>(actionHandler, header);
},
[](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) {
Systems::handleClientEnemyDeath(msg, endpoint);
}}},
{NEW_BULLET,
{[this](actionHandler &actionHandler, const struct header_s &header) {
handleBody<struct msgNewBullet_s>(actionHandler, header);
},
[](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveNewBulletMsg(msg, endpoint);
}}},
{POSITION_ABSOLUTE,
{[this](actionHandler &actionHandler, const struct header_s &header) {
handleBody<struct msgPositionAbsolute_s>(actionHandler, header);
},
[](std::any &msg, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveAbsolutePositionMsg(msg, endpoint);
}}},
};
std::map<enum n_actionType_t, actionSender> _actionToSendHandlers = {
{
INIT, [this](Packet &packet) {
sendData<struct packetMsgPlayerInit_s>(packet);
}, },
{LIFE_UPDATE,
[this](Packet &packet) {
sendData<struct packetLifeUpdate_s>(packet);
}},
{START_WAVE,
[this](Packet &packet) {
sendData<struct packetMsgStartWave_s>(packet);
}},
{ENEMY_DEATH,
[this](Packet &packet) {
sendData<struct packetEnemyDeath_s>(packet);
}},
{NEW_ENEMY,
[this](Packet &packet) {
sendData<struct packetNewEnemy_s>(packet);
}},
{NEW_BULLET,
[this](Packet &packet) {
sendData<struct packetNewBullet_s>(packet);
}},
{NEW_ALLIE,
[this](Packet &packet) {
sendData<struct packetNewAllie_s>(packet);
}},
{POSITION_RELATIVE_BROADCAST,
[this](Packet &packet) {
sendData<struct packetPositionRelativeBroadcast_s>(packet);
}},
{POSITION_ABSOLUTE_BROADCAST,
[this](Packet &packet) {
sendData<struct packetPositionAbsoluteBroadcast_s>(packet);
}},
};
};
} // namespace Nitwork