-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINitwork.hpp
More file actions
122 lines (105 loc) · 3.94 KB
/
INitwork.hpp
File metadata and controls
122 lines (105 loc) · 3.94 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
/*
** EPITECH PROJECT, 2023
** r-type
** File description:
** INitwork
*/
#pragma once
#include <any>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/bind/bind.hpp>
#include <map>
extern "C"
{
#include "Nitwork.h"
}
namespace Nitwork {
using actionHandler = std::function<void(std::any &, boost::asio::ip::udp::endpoint &)>;
using handleBodyT = std::function<void(actionHandler &, const struct header_s &)>;
class SenderData {
public:
SenderData(n_id_t id, boost::asio::ip::udp::endpoint &endpoint, std::any data)
: id(id),
endpoint(endpoint),
data(std::move(data))
{
}
n_id_t id;
boost::asio::ip::udp::endpoint endpoint;
std::any data;
};
class Packet {
public:
Packet(n_actionType_t action, std::any body, const boost::asio::ip::udp::endpoint &endpoint)
: action(action),
body(std::move(body)),
endpoint(endpoint)
{
}
Packet(n_actionType_t action, std::any body) : action(action), body(std::move(body))
{
}
Packet(const Packet &packet, const boost::asio::ip::udp::endpoint &endpoint)
: id(packet.id),
action(packet.action),
body(packet.body),
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 &)>;
class INitwork {
public:
virtual ~INitwork() = default;
INitwork(const INitwork &) = delete;
INitwork(const INitwork &&) = delete;
void operator=(const INitwork &) = delete;
void operator=(const INitwork &&) = delete;
// start the NitworkServer
virtual bool start(int port, int threadNb, int tick, const std::string &ip = "") = 0;
virtual void stop() = 0;
protected:
INitwork() = default;
// start the NitworkServer config
virtual bool startNitworkConfig(int port, const std::string &ip) = 0;
// start the NitworkServer threads (context threads, clock thread, input thread and output
// thread)
virtual bool startNitworkThreads(int threadNb, int tick) = 0;
// start the context threads
virtual bool startContextThreads(int threadNb) = 0;
// start the clock thread
virtual bool startClockThread(int tick) = 0;
// start the input thread inside the context (post)
virtual void startInputHandler() = 0;
// start the output thread inside the context (post)
virtual void startOutputHandler() = 0;
// start receive handler
virtual void startReceiveHandler() = 0;
// handler func for receive handler which handle the header
virtual void
headerHandler(std::size_t bytes_received, const boost::system::error_code &error) = 0;
// handler func for headerHandler which handle the action
virtual void handleBodyAction(
const struct header_s &header,
const boost::asio::ip::udp::endpoint &endpoint) = 0;
// getters
[[nodiscard]] virtual const std::map<enum n_actionType_t, actionSender> &
getActionToSendHandlers() const = 0;
}; // class INitwork
} // namespace Nitwork