-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.cpp
102 lines (91 loc) · 2.06 KB
/
Service.cpp
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
#include "include/Service.hpp"
#include "include/User.hpp"
#include "include/Frame.hpp"
Service::Service(void)
{
_tv.tv_sec = 5;
_tv.tv_usec = 0;
}
Service::Service(Service const& other)
{
*this = other;
}
Service& Service::operator=(Service const& other)
{
if (this == &other)
return *this;
this->_fdRead = other._fdRead;
this->_fdWrite = other._fdWrite;
this->_max = other._max;
this->_res = other._res;
this->_tv = other._tv;
return *this;
}
Service::~Service(void)
{
}
void Service::doSelect(MainServer const& sv)
{
std::map<int, Session*>::const_iterator it;
_res = 0;
FD_ZERO(&_fdRead);
FD_SET(sv.socket(), &_fdRead);
_max = sv.socket();
for (it = sv.users().begin() ; it != sv.users().end() ; ++it)
{
FD_SET(it->first, &_fdRead);
_max = std::max(_max, it->first);
}
_res = select(_max + 1, &_fdRead, NULL, NULL, &_tv);
if (_res == -1)
throw selectException();
}
void Service::doService(MainServer & sv)
{
std::map<int, Session*>::iterator it;
std::map<int, Session*>::iterator temp;
if (_res < 0)
return ;
for (it = sv.users().begin(); it != sv.users().end() ; )
{
temp = it++;
if (FD_ISSET(temp->first, &_fdRead))
{
temp->second->setPing(false);
temp->second->setTime(std::time(0));
sv.handleRead(temp);
}
else
sendPing(sv, temp->second);
}
if (FD_ISSET(sv.socket(), &_fdRead))
sv.handleAccept();
}
/*
* When there is no data for 5 seconds, send Ping Message
*/
void Service::sendPing(MainServer& sv, Session *ss)
{
std::string msg;
std::vector<std::string> v;
if (std::difftime(std::time(0), ss->time()) < 5)
return ;
else if (ss->ping())
{
v.insert(v.end(),"QUIT");
v.insert(v.end(), ":" + std::to_string(ss->soc().sd()) + " client is missing");
Frame::instance()->cmdQuit(ss, v);
sv.handleDecline(sv.users().find(ss->socket()));
return ;
}
msg = "PING ";
msg += ss->user().nick();
msg += "\r\n";
send(ss->soc().sd(), msg.c_str(), msg.length(), 0);
ss->setTime(std::time(0));
ss->setPing(true);
}
const char* Service::selectException::what(void) const throw()
{
return "Select Error\n";
}