22#include < set>
33#include < vector>
44
5- #if defined(__linux__)
65#include < sys/socket.h>
76#include < sys/types.h>
87#include < netinet/in.h>
98#include < sys/epoll.h>
109#include < sys/eventfd.h>
1110#include < strings.h>
1211
13- #else
14- #error System is not supported
15-
16- #endif
17-
1812#include < unistd.h>
1913
2014#include < simple_lib/common.h>
2115
2216#include " Server.hpp"
23- #include " ClientSession .hpp"
24- #include " UdpSession .hpp"
25- #include " TcpSession .hpp"
17+ #include " Session .hpp"
18+ #include " SessionUdp .hpp"
19+ #include " SessionTcp .hpp"
2620
2721namespace simpleApp
2822{
@@ -148,13 +142,13 @@ namespace simpleApp
148142
149143 Server::~Server ()
150144 {
151- if (this ->stopObject != -1 )
152- close (this ->stopObject );
145+ if (this ->stopEventFd != -1 )
146+ close (this ->stopEventFd );
153147 }
154148
155149 int Server::serverLoop (uint16_t port)
156150 {
157- if (this ->stopObject == -1 )
151+ if (this ->stopEventFd == -1 )
158152 {
159153 std::cout << " Stop event object is not initialized" << std::endl << std::flush;
160154 return -1 ;
@@ -187,7 +181,7 @@ namespace simpleApp
187181 std::cout << " EPoll creation failed with code " << errno << std::endl << std::flush;
188182 isFailed = true ;
189183 }
190- else if (addToEPoll (epollfd, this ->stopObject , &this ->stopObject , EPOLLIN) == -1 )
184+ else if (addToEPoll (epollfd, this ->stopEventFd , &this ->stopEventFd , EPOLLIN) == -1 )
191185 {
192186 std::cout << " EPOLL_CTL_ADD of stop event object failed with code " << errno << std::endl << std::flush;
193187 isFailed = true ;
@@ -230,23 +224,23 @@ namespace simpleApp
230224 const size_t MAX_EVENTS_BUFFER = 10000 ;
231225 epoll_event * events = static_cast <epoll_event *>(calloc (MAX_EVENTS_BUFFER, sizeof (epoll_event)));
232226
233- std::set<ClientSession *> slaveSocketsMap = std::set<ClientSession *>();
227+ std::set<Session *> slaveSocketsMap = std::set<Session *>();
234228
235229 bool stopEventHappened = false ;
236230
237231 auto initSession = [&slaveSocketsMap, &masterTcpSocket, &masterUdpSocket, &epollfd, port](epoll_event& event)
238232 {
239- ClientSession * clientSession;
233+ Session * clientSession;
240234 session_result result;
241235
242236 if (event.data .ptr == &masterTcpSocket)
243237 {
244- clientSession = new TcpSession (epollfd);
238+ clientSession = new SessionTcp (epollfd);
245239 result = clientSession->init (masterTcpSocket, port);
246240 }
247241 else if (event.data .ptr == &masterUdpSocket)
248242 {
249- clientSession = new UdpSession (epollfd);
243+ clientSession = new SessionUdp (epollfd);
250244 result = clientSession->init (masterUdpSocket, port);
251245 }
252246 else
@@ -316,17 +310,17 @@ namespace simpleApp
316310 };
317311 while (!stopEventHappened)
318312 {
319- std::set<ClientSession *> slavesForRemove = std::set<ClientSession *>();
313+ std::set<Session *> slavesForRemove = std::set<Session *>();
320314 int N = epoll_wait (epollfd, events, MAX_EVENTS_BUFFER, -1 );
321315 for (size_t i = 0 ; static_cast <int >(i) < N; i++)
322316 {
323- if (events[i].data .ptr == &this ->stopObject )
317+ if (events[i].data .ptr == &this ->stopEventFd )
324318 {
325319 eventfd_t decrement = 1 ;
326320
327321 std::string msg = " Stop event happened" ;
328322
329- if (eventfd_read (this ->stopObject , static_cast <eventfd_t *>(&decrement)) == -1 )
323+ if (eventfd_read (this ->stopEventFd , static_cast <eventfd_t *>(&decrement)) == -1 )
330324 {
331325 msg += std::string (" \n Stop event decrementation failed with code " ) +
332326 std::to_string (errno) + " . Server will stop anyway" ;
@@ -350,7 +344,7 @@ namespace simpleApp
350344 }
351345 else
352346 {
353- auto clientSession = reinterpret_cast <ClientSession *>(events[i].data .ptr );
347+ auto clientSession = reinterpret_cast <Session *>(events[i].data .ptr );
354348 if (slaveSocketsMap.find (clientSession) == slaveSocketsMap.end ())
355349 {
356350 std::cout << " Unknown event happened" << std::endl;
@@ -464,7 +458,7 @@ namespace simpleApp
464458 delete val;
465459 }
466460
467- removeFromEPoll (epollfd, this ->stopObject );
461+ removeFromEPoll (epollfd, this ->stopEventFd );
468462 }
469463
470464 if (epollfd != -1 )
@@ -485,19 +479,19 @@ namespace simpleApp
485479
486480 int Server::initStop ()
487481 {
488- if (this ->stopObject != -1 )
489- close (this ->stopObject );
482+ if (this ->stopEventFd != -1 )
483+ close (this ->stopEventFd );
490484
491- this ->stopObject = eventfd (0 , EFD_NONBLOCK);
485+ this ->stopEventFd = eventfd (0 , EFD_NONBLOCK);
492486
493- return this ->stopObject == -1 ? errno : 0 ;
487+ return this ->stopEventFd == -1 ? errno : 0 ;
494488 }
495489
496- int Server::stop ()
490+ int Server::raiseStop ()
497491 {
498- if (this ->stopObject != -1 )
492+ if (this ->stopEventFd != -1 )
499493 {
500- if (eventfd_write (this ->stopObject , static_cast <eventfd_t >(1 )) == -1 )
494+ if (eventfd_write (this ->stopEventFd , static_cast <eventfd_t >(1 )) == -1 )
501495 return errno;
502496 }
503497
0 commit comments