RType
networking
server
ServerSocket.hpp
Go to the documentation of this file.
1
/*
2
** EPITECH PROJECT, 2023
3
** R-type
4
** File description:
5
** R-type
6
*/
7
8
#
ifndef
R_TYPE_SERVER_SERVERSOCKET_HPP
9
#
define
R_TYPE_SERVER_SERVERSOCKET_HPP
10
11
#
include
"../shared/USocket.hpp"
12
#
include
<
cstring
>
13
#
include
<
map
>
14
#
include
<
memory
>
15
#
include
<
netinet
/
in
.
h
>
16
#
include
<
string
>
17
#
include
<
vector
>
18
19
class
ServerSocket
:
public
USocket
20
{
21
public
:
22
23
/**
24
* @brief Constructor of ServerSocket
25
*/
26
ServerSocket
();
27
28
/**
29
* @brief Destructor of ServerSocket
30
*/
31
~
ServerSocket
()
override
;
32
33
/**
34
* @brief Init the server socket
35
* @param port
36
*/
37
void
init_server
(
int
port);
38
39
/**
40
* @brief Send a packet to a client
41
* @param packet
42
* @param dest
43
*/
44
void
send
(Packet *packet,
struct
sockaddr_in dest)
override
;
45
46
/**
47
* @brief Send a split packet to a client
48
* @param packet
49
* @param dest
50
*/
51
void
sendPacket
(SplitPacket *packet,
struct
sockaddr_in dest)
override
;
52
53
/**
54
* @brief Broadcast a packet to all clients
55
* @param packet
56
*/
57
void
broadcast
(Packet *packet);
58
59
/**
60
* @brief Receive a packet from a client
61
* @return a tuple with the packet and the size of the packet
62
*/
63
std
::
tuple
<
std
::
unique_ptr
<
Packet
>,
int
>
receive
()
override
;
64
65
/**
66
* @brief Run the server socket
67
*/
68
void
run
()
override
;
69
70
/**
71
* @brief Add a client to the server
72
* @param client
73
*/
74
void
addClient
(
struct
sockaddr_in client);
75
76
/**
77
* @brief Remove a client from the server
78
* @param client
79
*/
80
int
getClientId
(sockaddr_in client);
81
82
/**
83
* @brief Get the client address
84
* @param id
85
* @return the client address
86
*/
87
struct
sockaddr_in getClientAddress(
int
id);
88
89
/**
90
* @brief init_fd_set, init the fd_set
91
*/
92
void
init_fd_set
();
93
94
/**
95
* @brief split and send a packet to a client
96
* @param packet
97
* @param dest
98
*/
99
void
splitAndSend
(Packet *packet,
struct
sockaddr_in dest)
override
;
100
101
/**
102
* @brief receivePacketAndAddToBuffer, receive a packet and add it to the buffer
103
*/
104
void
receivePacketAndAddToBuffer
();
105
106
/**
107
* @brief getClients, get the clients
108
* @return a tuple with the client id, the client address, the client buffer and the client timeout
109
*/
110
[[
nodiscard
]]
std
::
vector
<
111
std
::
tuple
<
int
,
struct
sockaddr_in
,
std
::
vector
<
std
::
tuple
<
std
::
shared_ptr
<
SplitPacket
>,
timeval
>>,
timeval
>> &
112
getClients
();
113
114
/**
115
* @brief manageClientsBuffer, manage the clients buffer
116
* @return a tuple with the packet and the size of the packet
117
*/
118
std
::
tuple
<
std
::
unique_ptr
<
Packet
>,
int
>
manageClientsBuffer
();
119
120
/**
121
* @brief setNewClientConnected, set the new client connected
122
* @param newClientConnected
123
*/
124
void
setNewClientConnected
(
int
newClientConnected);
125
126
/**
127
* @brief getNewClientId, get the new client id
128
*/
129
int
getNewClientId
();
130
131
/**
132
* @brief checkClientsDeconnection, check if a client is disconnected
133
* @return the client id
134
*/
135
int
checkClientsDeconnection
();
136
137
/**
138
* @brief clientDump, dump the clients
139
*/
140
void
clientDump
();
141
142
private
:
143
unsigned
long
long
sockfd;
144
struct
sockaddr_in serv_addr {
145
};
146
std::string lastMessage;
147
std::vector<
148
std::tuple<
int
,
struct
sockaddr_in, std::vector<std::tuple<std::shared_ptr<SplitPacket>, timeval>>, timeval>>
149
clients;
150
struct
sockaddr_in lastClientAddress {
151
};
152
std::unique_ptr<
struct
timeval> timeout;
153
fd_set _readfds;
154
bool
newClientConnected;
155
int
newClientId;
156
int
nextClientId = 1;
157
};
158
159
#
endif
// R_TYPE_SERVER_SERVERSOCKET_HPP
Generated by
1.9.1