Skip to content

Commit b391ea1

Browse files
committed
Implemented "proceed" method of UdpSession
1 parent 9dfb855 commit b391ea1

File tree

9 files changed

+365
-116
lines changed

9 files changed

+365
-116
lines changed

.vscode/settings.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"files.associations": {
3+
"random": "cpp",
4+
"functional": "cpp",
5+
"array": "cpp",
6+
"atomic": "cpp",
7+
"bit": "cpp",
8+
"*.tcc": "cpp",
9+
"cctype": "cpp",
10+
"clocale": "cpp",
11+
"cmath": "cpp",
12+
"cstdarg": "cpp",
13+
"cstddef": "cpp",
14+
"cstdint": "cpp",
15+
"cstdio": "cpp",
16+
"cstdlib": "cpp",
17+
"cstring": "cpp",
18+
"cwchar": "cpp",
19+
"cwctype": "cpp",
20+
"deque": "cpp",
21+
"set": "cpp",
22+
"unordered_map": "cpp",
23+
"vector": "cpp",
24+
"exception": "cpp",
25+
"algorithm": "cpp",
26+
"iterator": "cpp",
27+
"memory": "cpp",
28+
"memory_resource": "cpp",
29+
"numeric": "cpp",
30+
"optional": "cpp",
31+
"string": "cpp",
32+
"string_view": "cpp",
33+
"system_error": "cpp",
34+
"tuple": "cpp",
35+
"type_traits": "cpp",
36+
"utility": "cpp",
37+
"fstream": "cpp",
38+
"initializer_list": "cpp",
39+
"iosfwd": "cpp",
40+
"iostream": "cpp",
41+
"istream": "cpp",
42+
"limits": "cpp",
43+
"new": "cpp",
44+
"ostream": "cpp",
45+
"sstream": "cpp",
46+
"stdexcept": "cpp",
47+
"streambuf": "cpp",
48+
"typeinfo": "cpp"
49+
}
50+
}

server/ClientSession.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,43 @@
1313

1414
namespace simpleApp
1515
{
16-
ClientSession::ClientSession(int& epollfd) : epollfd(epollfd)
16+
ClientSession::ClientSession(int& epollfd, std::string name) : epollfd(epollfd), _name(name)
1717
{
1818

1919
}
2020

2121
ClientSession::~ClientSession()
22+
{
23+
this->sessionClose();
24+
}
25+
26+
std::string ClientSession::getName()
27+
{
28+
return this->_name;
29+
}
30+
31+
void ClientSession::sessionClose()
2232
{
2333
if (this->_socket != -1)
2434
{
2535
epoll_ctl(this->epollfd, EPOLL_CTL_DEL, this->_socket, 0);
2636
shutdown(this->_socket, SHUT_RDWR);
2737
close(this->_socket);
38+
this->_socket = -1;
2839
}
29-
if (this->address != nullptr)
30-
delete this->address;
3140
}
3241

33-
sockaddr_in* ClientSession::getAddress()
42+
std::string addressToString(sockaddr_in& address)
3443
{
35-
return this->address;
44+
uint32_t addressConverted = ntohl(address.sin_addr.s_addr);
45+
auto portConverted = ntohs(address.sin_port);
46+
47+
uint8_t* addressByBytes = reinterpret_cast<uint8_t*>(&addressConverted);
48+
49+
return std::to_string(static_cast<int>(addressByBytes[0])) + std::string(".") +
50+
std::to_string(static_cast<int>(addressByBytes[1])) + std::string(".") +
51+
std::to_string(static_cast<int>(addressByBytes[2])) + std::string(".") +
52+
std::to_string(static_cast<int>(addressByBytes[3])) + std::string(":") +
53+
std::to_string(portConverted);
3654
}
3755
}

server/ClientSession.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <string>
4+
35
#if defined(__linux__)
46
#include <netinet/in.h>
57

@@ -22,13 +24,17 @@ namespace simpleApp
2224
virtual session_result init(socket_t masterSocket, uint16_t port) = 0;
2325
virtual session_result proceed(struct epoll_event& epoll_event) = 0;
2426

25-
sockaddr_in* getAddress();
27+
std::string getName();
2628

2729
protected:
2830
socket_t _socket = -1;
29-
int& epollfd;
30-
sockaddr_in * address = nullptr;
31+
int& epollfd;
32+
std::string _name;
33+
34+
ClientSession(int& epollfd, std::string name = "");
3135

32-
ClientSession(int& epollfd);
36+
void sessionClose();
3337
};
38+
39+
std::string addressToString(sockaddr_in& address);
3440
}

server/Server.cpp

Lines changed: 82 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -231,24 +231,7 @@ namespace simpleApp
231231
else
232232
return false;
233233

234-
235-
std::cout << (event.data.fd == masterTcpSocket ? "TCP" : "UDP");
236-
237-
auto address = clientSession->getAddress();
238-
239-
if(address != nullptr)
240-
{
241-
uint32_t addressConverted = ntohl(address->sin_addr.s_addr);
242-
uint16_t portConverted = ntohs(address->sin_port);
243-
244-
uint8_t* addressByBytes = reinterpret_cast<uint8_t*>(&addressConverted);
245-
std::cout << "(" << static_cast<int>(addressByBytes[0]) << "." <<
246-
static_cast<int>(addressByBytes[1]) << "." <<
247-
static_cast<int>(addressByBytes[2]) << "." <<
248-
static_cast<int>(addressByBytes[3]) << "). ";
249-
}
250-
else
251-
std::cout << ". ";
234+
std::string msg = clientSession->getName() + ". ";
252235

253236
bool toInsert = false;
254237
switch (result.status)
@@ -258,35 +241,35 @@ namespace simpleApp
258241
slaveSocketsMap.insert(clientSession);
259242
break;
260243
case session_status::init_socket_setup_fail:
261-
std::cout << "Socket initialization failed";
244+
msg += "Socket initialization failed";
262245
break;
263-
case session_status::init_udp_listener_fail:
264-
std::cout << "Message receiving from listener failed";
246+
case session_status::init_listener_fail:
247+
msg += "Message receiving from listener failed";
265248
break;
266249
case session_status::init_udp_wrong_header:
267-
std::cout << "Header check failed";
250+
msg += "Header check failed";
268251
break;
269252
case session_status::init_udp_wrong_length:
270-
std::cout << "Message length check failed";
253+
msg += "Message length check failed";
271254
break;
272255
case session_status::init_udp_timer_fail:
273-
std::cout << "Timeout timer setup failed";
256+
msg += "Timeout timer setup failed";
274257
break;
275258
default:
276-
std::cout << "Unknown status returned";
259+
msg += "Unknown status returned";
277260
break;
278261
}
279262

280263
if (result.err != 0)
281-
std::cout << " with code " << result.err << std::endl;
282-
else
283-
std::cout << std::endl;
264+
msg += std::string(" with code ") + std::to_string(result.err);
284265

285266
if (toInsert)
286267
slaveSocketsMap.insert(clientSession);
287268
else
288269
delete clientSession;
289270

271+
std::cout << msg << std::endl;
272+
290273
return true;
291274
};
292275
while (!stopEventHappened)
@@ -298,11 +281,13 @@ namespace simpleApp
298281
if (events[i].data.fd == this->stopObject)
299282
{
300283
eventfd_t decrement = 1;
301-
std::cout << "Stop event happened" << std::endl;
284+
285+
std::string msg = "Stop event happened";
286+
302287
if (eventfd_read(this->stopObject, static_cast<eventfd_t *>(&decrement)) == -1)
303288
{
304-
std::cout << "Stop event decrementation failed with code " << errno << std::endl <<
305-
"Server will stop anyway" << std::endl;
289+
msg += std::string("\nStop event decrementation failed with code ") +
290+
std::to_string(errno) + ". Server will stop anyway";
306291
}
307292
if (masterTcpSocket != -1)
308293
{
@@ -313,6 +298,8 @@ namespace simpleApp
313298
removeFromEPoll(epollfd, masterUdpSocket);
314299
}
315300

301+
std::cout << msg << std::endl;
302+
316303
stopEventHappened = true;
317304
}
318305
else if (initSession(events[i]))
@@ -321,20 +308,74 @@ namespace simpleApp
321308
}
322309
else if (events[i].data.ptr != nullptr)
323310
{
324-
continue;
325-
/*
326311
auto clientSession = reinterpret_cast<ClientSession*>(events[i].data.ptr);
327-
if (clientSession->proceed(events[i]) == -1)
312+
auto result = clientSession->proceed(events[i]);
313+
std::string msg = "";
314+
315+
switch (result.status)
316+
{
317+
case session_status::proceed_msg_send:
318+
case session_status::proceed_udp_connup:
319+
slavesForRemove.erase(clientSession);
320+
break;
321+
322+
case session_status::proceed_disconnect:
323+
slavesForRemove.insert(clientSession);
324+
msg = std::string("Session has been closed by client");
325+
break;
326+
case session_status::proceed_udp_timeout:
327+
slavesForRemove.insert(clientSession);
328+
msg = std::string("Timeout triggered");
329+
break;
330+
case session_status::proceed_udp_client_timeout:
331+
slavesForRemove.insert(clientSession);
332+
msg = std::string("Session has been closed by client because of timeout");
333+
break;
334+
case session_status::proceed_udp_timer_fail:
335+
slavesForRemove.insert(clientSession);
336+
msg = std::string("Session has been closed because of timer failure");
337+
break;
338+
339+
case session_status::proceed_recv_fail:
340+
msg = std::string("RECV failed");
341+
break;
342+
case session_status::proceed_send_fail:
343+
msg = std::string("SEND failed");
344+
break;
345+
case session_status::proceed_udp_wrong_header:
346+
msg = std::string("Received message with wrong header");
347+
break;
348+
case session_status::proceed_unknown_fd:
349+
msg = std::string("Unknown fd connected");
350+
break;
351+
352+
case session_status::proceed_udp_false_timeout:
353+
case session_status::proceed_udp_socket_was_closed:
354+
break;
355+
356+
default:
357+
msg = std::string("Unknown error");
358+
break;
359+
}
360+
361+
if (result.err != 0)
362+
msg += std::string(" Returned code: ") + std::to_string(result.err);
363+
364+
if (msg != std::string(""))
328365
{
329-
slavesForRemove.push_back(clientSession);
330-
}*/
366+
std::cout << clientSession->getName() << ". " << msg << std::endl;
367+
}
368+
331369
}
332370
else
333371
{
334-
std::wcout << L"Found unknown decriptor. Trying to remove from epoll" << std::endl;
372+
std::string msg = "Found unknown decriptor. ";
335373
if (removeFromEPoll(epollfd, events[i].data.fd) == -1)
336-
std::wcout << L"Unknown decriptor removal failed with code " << errno << std::endl;
337-
removeFromEPoll(epollfd, events[i].data.fd);
374+
msg += std::string("Removal failed with code ") + std::to_string(errno);
375+
else
376+
msg += "Removed from epoll successfully";
377+
378+
std::cout << msg << std::endl;
338379
}
339380
}
340381

@@ -345,6 +386,8 @@ namespace simpleApp
345386
}
346387
}
347388

389+
std::cout << "Closing all sessions" << std::endl;
390+
348391
free(events);
349392
for(auto& val: slaveSocketsMap)
350393
delete val;

server/TcpSession.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@
1616

1717
namespace simpleApp
1818
{
19-
TcpSession::TcpSession(int& epollfd) : ClientSession(epollfd)
19+
TcpSession::TcpSession(int& epollfd) : ClientSession(epollfd, "TCP")
2020
{
2121
//TODO
2222
}
2323

2424
session_result TcpSession::init(socket_t masterSocket, uint16_t port)
2525
{
26-
this->address = new sockaddr_in;
26+
struct sockaddr_in address;
2727
socklen_t len = static_cast<socklen_t>(sizeof(sockaddr_in));
2828

29-
this->_socket = accept(masterSocket, (sockaddr *)this->address, &len);
29+
this->_socket = accept(masterSocket, (sockaddr *)&address, &len);
3030
if (this->_socket == -1)
3131
{
32-
delete this->address;
33-
this->address = nullptr;
3432
return session_result {session_status::init_socket_setup_fail, errno};
3533
}
3634

35+
this->_name += std::string(" (") + addressToString(address) + std::string(")");
36+
3737
if (set_nonblock(this->_socket) == -1)
3838
{
3939
auto err = errno;

0 commit comments

Comments
 (0)