3
3
4
4
using namespace tpt ;
5
5
6
- UnixSocket::UnixSocket ()
7
- {
8
- this ->ip_address = " 127.0.0.1" ;
9
- this ->port = 8000 ;
10
- this ->max_connections = 10 ;
11
- this ->logger = ConsoleLogger ();
12
-
13
- LOG_INFO (logger, " Creating socket ..." );
14
- this ->server_socket = socket (AF_INET, SOCK_STREAM, 0 );
15
- if (this ->server_socket < 0 )
16
- {
17
- perror (" Socket failed" );
18
- std::cout << " Error code: " + errno << std::endl;
19
- exit (EXIT_FAILURE);
20
- }
21
- LOG_INFO (logger, " Socket created!" );
22
-
23
- this ->server_address .sin_family = AF_INET;
24
- this ->server_address .sin_port = htons (this ->port );
25
- this ->server_address .sin_addr .s_addr = inet_addr (this ->ip_address .c_str ());
26
-
27
- Utils::fillIPBlacklist (this ->ip_blacklist );
28
- }
29
-
30
- UnixSocket::UnixSocket (ConsoleLogger logger)
31
- {
32
- this ->ip_address = " 127.0.0.1" ;
33
- this ->port = 8000 ;
34
- this ->max_connections = 10 ;
35
- this ->logger = logger;
36
-
37
- LOG_INFO (logger, " Creating socket ..." );
38
- this ->server_socket = socket (AF_INET, SOCK_STREAM, 0 );
39
- if (this ->server_socket < 0 )
40
- {
41
- perror (" Socket failed" );
42
- std::cout << " Error code: " + errno << std::endl;
43
- exit (EXIT_FAILURE);
44
- }
45
- LOG_INFO (logger, " Socket created!" );
46
-
47
- this ->server_address .sin_family = AF_INET;
48
- this ->server_address .sin_port = htons (this ->port );
49
- this ->server_address .sin_addr .s_addr = inet_addr (this ->ip_address .c_str ());
50
-
51
- Utils::fillIPBlacklist (this ->ip_blacklist );
52
- }
53
-
54
- UnixSocket::UnixSocket (ConsoleLogger logger, unsigned int port)
55
- {
56
- this ->ip_address = " 127.0.0.1" ;
57
- this ->port = port;
58
- this ->max_connections = 10 ;
59
- this ->logger = logger;
6
+ UnixSocket::UnixSocket () : UnixSocket(ConsoleLogger()) {}
60
7
61
- LOG_INFO (logger, " Creating socket ..." );
62
- this ->server_socket = socket (AF_INET, SOCK_STREAM, 0 );
63
- if (this ->server_socket < 0 )
64
- {
65
- perror (" Socket failed" );
66
- std::cout << " Error code: " + errno << std::endl;
67
- exit (EXIT_FAILURE);
68
- }
69
- LOG_INFO (logger, " Socket created!" );
70
-
71
- this ->server_address .sin_family = AF_INET;
72
- this ->server_address .sin_port = htons (this ->port );
73
- this ->server_address .sin_addr .s_addr = inet_addr (this ->ip_address .c_str ());
8
+ UnixSocket::UnixSocket (ConsoleLogger logger) : UnixSocket(logger, " 127.0.0.1" , 8000 , 10 ) {}
74
9
75
- Utils::fillIPBlacklist (this ->ip_blacklist );
76
- }
10
+ UnixSocket::UnixSocket (ConsoleLogger logger, unsigned int port) : UnixSocket(logger, " 127.0.0.1" , port, 10 ) {}
77
11
78
- UnixSocket::UnixSocket (ConsoleLogger logger, std::string ip_address, unsigned int port)
79
- {
80
- this ->ip_address = ip_address;
81
- this ->port = port;
82
- this ->max_connections = 10 ;
83
- this ->logger = logger;
84
-
85
- LOG_INFO (logger, " Creating socket ..." );
86
- this ->server_socket = socket (AF_INET, SOCK_STREAM, 0 );
87
- if (this ->server_socket < 0 )
88
- {
89
- perror (" Socket failed" );
90
- std::cout << " Error code: " + errno << std::endl;
91
- exit (EXIT_FAILURE);
92
- }
93
- LOG_INFO (logger, " Socket created!" );
94
-
95
- this ->server_address .sin_family = AF_INET;
96
- this ->server_address .sin_port = htons (this ->port );
97
- this ->server_address .sin_addr .s_addr = inet_addr (this ->ip_address .c_str ());
98
-
99
- Utils::fillIPBlacklist (this ->ip_blacklist );
100
- }
12
+ UnixSocket::UnixSocket (ConsoleLogger logger, std::string ip_address, unsigned int port) : UnixSocket(logger, ip_address, port, 10 ) {}
101
13
102
14
UnixSocket::UnixSocket (ConsoleLogger logger, std::string ip_address, unsigned int port, unsigned int max_connections)
103
15
{
@@ -110,9 +22,7 @@ UnixSocket::UnixSocket(ConsoleLogger logger, std::string ip_address, unsigned in
110
22
this ->server_socket = socket (AF_INET, SOCK_STREAM, 0 );
111
23
if (this ->server_socket < 0 )
112
24
{
113
- perror (" Socket failed" );
114
- std::cout << " Error code: " + errno << std::endl;
115
- exit (EXIT_FAILURE);
25
+ throw SocketCreationException ();
116
26
}
117
27
LOG_INFO (logger, " Socket created!" );
118
28
@@ -129,21 +39,16 @@ void UnixSocket::bindSocket()
129
39
LOG_INFO (logger, " Binding socket ..." );
130
40
if ((bind (this ->server_socket , (struct sockaddr *)&this ->server_address , sizeof (this ->server_address ))) < 0 )
131
41
{
132
- perror (" Bind failed" );
133
- std::cout << " Error code: " + errno << std::endl;
134
- exit (EXIT_FAILURE);
42
+ throw SocketBindingException ();
135
43
}
136
- this ->setSocketTimeout (this ->server_socket , 5 );
137
44
LOG_INFO (logger, " Binding done! Listening to connections ..." );
138
45
}
139
46
140
47
void UnixSocket::listenToConnections ()
141
48
{
142
49
if ((listen (this ->server_socket , this ->max_connections )) < 0 )
143
50
{
144
- perror (" Listen failed" );
145
- std::cout << " Error code: " + errno << std::endl;
146
- exit (1 );
51
+ throw SocketListenException ();
147
52
}
148
53
}
149
54
@@ -152,12 +57,11 @@ void UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
152
57
struct sockaddr_storage client_addr_storage;
153
58
socklen_t client_addr_size = sizeof (client_addr_storage);
154
59
60
+ this ->setSocketTimeout (this ->server_socket , 5 );
155
61
client_socket = accept (this ->server_socket , (struct sockaddr *)&client_addr_storage, &client_addr_size);
156
62
if (client_socket < 0 )
157
63
{
158
- perror (" Accept failed" );
159
- std::cout << " Error code: " << std::to_string (errno) << std::endl;
160
- exit (EXIT_FAILURE);
64
+ throw SocketAcceptException ();
161
65
}
162
66
163
67
// Assuming client_address is meant to store the result
@@ -198,16 +102,17 @@ ssize_t UnixSocket::receiveData(SOCKET client_socket, char *buffer, unsigned int
198
102
ssize_t data = recv (client_socket, (void *)buffer, buffer_size, 0 );
199
103
if (data < 0 )
200
104
{
201
- perror (" Receive error" );
202
- std::cout << " Error code: " + errno << std::endl;
203
- exit (1 );
105
+ throw SocketReceiveException ();
204
106
}
205
107
return data;
206
108
}
207
109
208
110
void UnixSocket::sendData (SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags)
209
111
{
210
- send (client_socket, buffer, buffer_size, flags);
112
+ if (send (client_socket, buffer, buffer_size, flags) == -1 )
113
+ {
114
+ throw SocketSendException ();
115
+ }
211
116
}
212
117
213
118
void UnixSocket::closeSocket ()
@@ -221,30 +126,24 @@ void UnixSocket::closeSocket()
221
126
LOG_INFO (logger, " Closing socket ..." );
222
127
if (shutdown (this ->server_socket , SHUT_RDWR) == -1 )
223
128
{
224
- perror (" An error occurred while shutting down the socket: " );
225
- std::cout << " Error code: " + errno << std::endl;
226
- exit (EXIT_FAILURE);
129
+ throw SocketCloseException ();
227
130
}
228
131
if (close (this ->server_socket ) == 0 )
229
132
{
230
133
LOG_INFO (logger, " Socket closed!" );
231
- exit (EXIT_SUCCESS) ;
134
+ return ;
232
135
}
233
136
else
234
137
{
235
- perror (" An error occurred while closing the socket: " );
236
- std::cout << " Error code: " + errno << std::endl;
237
- exit (EXIT_FAILURE);
138
+ throw SocketCloseException ();
238
139
}
239
140
}
240
141
241
142
void UnixSocket::closeSocket (SOCKET client_socket)
242
143
{
243
144
if (shutdown (client_socket, SHUT_RDWR) == -1 )
244
145
{
245
- perror (" An error occurred while shutting down the socket: " );
246
- std::cout << " Error code: " + errno << std::endl;
247
- exit (EXIT_FAILURE);
146
+ throw SocketCloseException ();
248
147
}
249
148
close (client_socket);
250
149
0 commit comments