-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathANitwork.hpp
More file actions
188 lines (165 loc) · 8.25 KB
/
ANitwork.hpp
File metadata and controls
188 lines (165 loc) · 8.25 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
/*
** EPITECH PROJECT, 2023
** r-type
** File description:
** ANitwork
*/
#pragma once
#include <condition_variable>
#include <iostream>
#include <list>
#include <mutex>
#include <unordered_map>
#include "INitwork.hpp"
#include "Logger.hpp"
namespace Nitwork {
constexpr int MAX_PACKET_SIZE = 1024;
class ANitwork : public INitwork {
public:
~ANitwork() override = default;
ANitwork(const ANitwork &) = delete;
ANitwork(const ANitwork &&) = delete;
void operator=(const ANitwork &) = delete;
void operator=(const ANitwork &&) = delete;
void stop() override;
bool isRunning() const final;
protected:
ANitwork();
// start the NitworkServer
bool start(int port, int threadNb, int tick, const std::string &ip = "") final;
// send data to the endpoint with the given data
template <typename T>
void sendData(Packet &packet)
{
n_id_t id = getPacketId(packet.endpoint);
if (packet.body.type() != typeid(T)) {
Logger::error("NITWORK: Invalid type");
return;
}
if constexpr (sizeof(T) > MAX_PACKET_SIZE) {
Logger::error("NITWORK: Package too big");
return;
}
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)),
packet.endpoint,
[](const boost::system::error_code &error, std::size_t bytes_sent) {
Logger::debug("NITWORK: Package sent");
if (error) {
Logger::error("NITWORK: " + std::string(error.message()));
return;
}
if (bytes_sent != sizeof(T)) {
Logger::error("NITWORK: Package not sent");
return;
}
});
}
/* 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);
void addPacketToSend(const Packet &);
void handlePacketIdsReceived(const struct header_s &header);
void startReceiveHandler() final;
// handler func for receive handler which handle the header
template <typename B>
void handleBody(const actionHandler &handler, const struct header_s &header)
{
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
auto *body = reinterpret_cast<B *>(
_receiveBuffer.data() + sizeof(struct header_s) + sizeof(struct action_s));
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
handleBodyDatas<B>(handler, header, *body, boost::system::error_code());
}
private:
// start the NitworkServer threads (context threads, clock thread, input thread and output
// thread)
bool startNitworkThreads(int threadNb, int tick) final;
// start the context threads
bool startContextThreads(int threadNb) final;
// start the clock thread
bool startClockThread(int tick) final;
// start the input thread inside the context (post)
void startInputHandler() final;
// start the output thread inside the context (post)
void startOutputHandler() final;
// send package
void sendPackages(const std::map<enum n_actionType_t, actionSender> &);
// start receive handler
void headerHandler(std::size_t bytes_received, const boost::system::error_code &error) final;
// check if the packet has already been received
bool isAlreadyReceived(n_id_t id, const boost::asio::ip::udp::endpoint &endpoint);
// call startReceiveHandler method by displaying a message
void callReceiveHandler(const std::string &message);
// handler func for receive handler which handle the action
template <typename B>
void handleBodyDatas(
const actionHandler &handler,
const struct header_s &header,
B &body,
const boost::system::error_code &error)
{
if (error) {
Logger::error("NITWORK: " + std::string(error.message()));
startReceiveHandler();
return;
}
std::lock_guard<std::mutex> lock(_inputQueueMutex);
SenderData senderData(header.id, _senderEndpoint, std::any(body));
_actions.emplace_back(senderData, handler);
}
void addPacketToSentPackages(Packet &data);
protected:
// NOLINTBEGIN(cppcoreguidelines-non-private-member-variables-in-classes)
boost::asio::io_context _context; // The main context
boost::asio::ip::udp::socket
_socket; // The socket which will be used to send and receive the actions
std::list<boost::asio::ip::udp::endpoint> _endpoints;
// 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
// std::vector<n_id_t> _receivedPacketsIds; // A list of packets' ids receives
std::unordered_map<boost::asio::ip::udp::endpoint, std::vector<n_id_t>> _receivedPacketsIdsMap;
std::unordered_map<boost::asio::ip::udp::endpoint, n_id_t> _packetsIds;
std::unordered_map<boost::asio::ip::udp::endpoint, std::list<Packet>>
_packetsSent; // A list of packets' ids receives
// Mutexes shared
std::mutex _receivedPacketsIdsMutex; // Mutex for the received packets ids
std::mutex _outputQueueMutex; // Mutex for the output queue
std::mutex _packetsSentMutex; // Mutex for the packets sent
// NOLINTEND(cppcoreguidelines-non-private-member-variables-in-classes)
private:
bool _isRunning = false; // A boolean to know if the NitworkServer is running
n_id_t _packetId; // The packet id
std::mutex _inputQueueMutex; // Mutex for the input queue
std::mutex _tickMutex; // Mutex for the tick
std::condition_variable _tickConvVar; // Condition variable for the tick
// Threads
std::vector<std::thread> _pool; // The context threads pool
std::thread _clockThread; // The clock thread
// Endpoints
boost::asio::ip::udp::endpoint _senderEndpoint; // The sender endpoint
// Body handler vars
std::list<std::pair<
SenderData,
const actionHandler &>>
_actions; // A list of actions which will be handled by the second context
std::list<Packet> _outputQueue; // A queue of actions which will be sent to the clients
}; // class INitwork
} // namespace Nitwork