-
Notifications
You must be signed in to change notification settings - Fork 4
/
CommServer.h
92 lines (69 loc) · 2.41 KB
/
CommServer.h
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
/*
* File: CommonServer.h
* Author: gwm <wenmao@eastmoney.com>
*
* Created on 2014年4月4日, 上午9:55
*/
#ifndef COMMONSERVER_H
#define COMMONSERVER_H
#include "TcpEpollServer.h"
namespace CommLib {
template<class ClientType>
class CommonServer : public TcpEpollServerImp {
public:
CommonServer(
boost::shared_ptr<Epoll> epoll,
boost::shared_ptr<MemPool> mempool)
: TcpEpollServerImp(epoll, "CommonServer", "check", 5), mempool_(mempool) {
}
typedef std::list<boost::shared_ptr<TcpEpollSockImp> >::iterator pClientList;
private:
virtual boost::shared_ptr<TcpEpollSockImp> MakeNewClient(int socket) {
return boost::shared_ptr<TcpEpollSockImp>(new ClientType(socket, mempool_, this));
}
virtual void OnAddClient(boost::shared_ptr<TcpEpollSockImp> sock) {
CAutoLock lock(lock_);
ClientList_.push_back(sock);
}
virtual void OnClientClose(boost::shared_ptr<TcpEpollSockImp> sock) {
CAutoLock lock(lock_);
std::list<boost::shared_ptr<TcpEpollSockImp> >::iterator iter = ClientList_.begin();
for (; iter != ClientList_.end(); iter++) {
if (sock == *iter)
ClientList_.erase(iter);
}
};
void Check() {
CAutoLock lock(lock_);
std::list<boost::shared_ptr<TcpEpollSockImp> >::iterator iter = ClientList_.begin();
for (; iter != ClientList_.end();) {
if (!(*iter)->CheckValid()) {
OnCloseClient((*iter));
ClientList_.erase(iter++);
continue;
}
iter++;
}
}
private:
boost::shared_ptr<MemPool> mempool_;
CMutexLock lock_;
std::list<boost::shared_ptr<TcpEpollSockImp> > ClientList_;
};
class CommSockImp : public TcpEpollSockImp {
public:
CommSockImp(int sock, boost::shared_ptr<MemPool> pMemPool
, TcpEpollServerImp* pEServ, int timeOut = 15
);
private:
virtual int OnRecvPack(AllocPack* pack) = 0;
int OnRecv();
void SetLastRecvTime();
bool CheckValid();
private:
boost::shared_ptr<MemPool> pMemPool_;
Time LastRecvTime_;
TimeSpan TimeOut_;
};
}
#endif /* COMMONSERVER_H */