-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommonserver.cpp
213 lines (173 loc) · 4.53 KB
/
commonserver.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "../commonserver.h"
#include <sys/time.h>
#define MAX_THREAD_NUM 64
static void * __Thread_Run(void *arg);
void CPktHandleThread::Init(ITcpServerDataHandle * pDataHandle)
{
m_pDataHandle = pDataHandle;
m_bThreadRun = true;
pthread_mutex_init(&m_ThreadMutex, NULL);
pthread_cond_init(&m_ThreadCond, NULL);
pthread_create(&m_ThreadId, NULL, __Thread_Run, this);
}
void CPktHandleThread::Stop()
{
if (m_bThreadRun)
{
m_bThreadRun = false;
pthread_join(m_ThreadId, NULL);
}
}
void CPktHandleThread::AddPkt(const PktData_t & PktData)
{
pthread_mutex_lock(&m_ThreadMutex);
m_PktQueue.push(PktData);
pthread_cond_signal(&m_ThreadCond);
pthread_mutex_unlock(&m_ThreadMutex);
}
void CPktHandleThread::HandlePkt()
{
while (m_bThreadRun)
{
int nSize = 0;
{
pthread_mutex_lock(&m_ThreadMutex);
nSize = m_PktQueue.size();
pthread_mutex_unlock(&m_ThreadMutex);
while ((nSize == 0) && m_bThreadRun)
{
pthread_mutex_lock(&m_ThreadMutex);
struct timeval nowtime;
gettimeofday(&nowtime, NULL);
struct timespec abstime;
abstime.tv_nsec = nowtime.tv_usec * 1000;
abstime.tv_sec = nowtime.tv_sec + 1;
pthread_cond_timedwait(&m_ThreadCond, &m_ThreadMutex, &abstime);
nSize = m_PktQueue.size();
pthread_mutex_unlock(&m_ThreadMutex);
}
}
while (nSize-- > 0)
{
pthread_mutex_lock(&m_ThreadMutex);
PktData_t PktData = m_PktQueue.front();
m_PktQueue.pop();
pthread_mutex_unlock(&m_ThreadMutex);
m_pDataHandle->OnClientDataReceived(PktData.first, PktData.second->GetDataPos(), PktData.second->GetBytes());
}
}
pthread_exit(NULL);
}
void * __Thread_Run(void *arg)
{
CPktHandleThread * pPktHandleThread = (CPktHandleThread *)arg;
pPktHandleThread->HandlePkt();
return 0;
}
CCommonServer::~CCommonServer()
{
if (m_nThreadNum > 0)
{
delete [] m_Threads;
}
}
void CCommonServer::SetDataHandle(ITcpServerDataHandle * pDataHandle)
{
CTcpServer::SetDataHandle(pDataHandle);
}
bool CCommonServer::Start(unsigned int IP, unsigned short Port)
{
return CTcpServer::Start(IP, Port);
}
bool CCommonServer::Stop()
{
return CTcpServer::Stop();
}
bool CCommonServer::Send(SocketClientData_t sClient, const char *pData, int nDataLen)
{
return CTcpServer::Send(sClient, pData, nDataLen);
}
bool CCommonServer::CloseClient(SocketClientData_t sClient)
{
return CTcpServer::CloseClient(sClient);
}
void CCommonServer::SetConnectionTimeout(int nTimeout)
{
CTcpServer::SetConnectionTimeout(nTimeout);
}
void CCommonServer::SetThreadNum(int nThreadNum)
{
if ((nThreadNum <= 0) || (nThreadNum > MAX_THREAD_NUM))
{
return;
}
m_nThreadNum = nThreadNum;
m_Threads = new CPktHandleThread[m_nThreadNum]();
for (int i = 0; i < m_nThreadNum; ++i)
{
m_Threads[i].Init(m_pDataHandle);
}
}
void CCommonServer::OnClientDisconnected(SocketClientData_t sClient, int nErrorCode)
{
CTcpServer::OnClientDisconnected(sClient, nErrorCode);
__RemoveContent(sClient);
}
void CCommonServer::OnClientDataReceived(SocketClientData_t sClient, const char * pData, int nDataLen)
{
__AddContent(sClient, pData, nDataLen);
}
void CCommonServer::OnClientRecvError(SocketClientData_t sClient, int nErrorCode)
{
CTcpServer::OnClientRecvError(sClient, nErrorCode);
__RemoveContent(sClient);
}
void CCommonServer::OnClientSendError(SocketClientData_t sClient, int nErrorCode)
{
CTcpServer::OnClientSendError(sClient, nErrorCode);
__RemoveContent(sClient);
}
void CCommonServer::OnClientTimeout(SocketClientData_t sClient)
{
CTcpServer::OnClientTimeout(sClient);
__RemoveContent(sClient);
}
void CCommonServer::__AddContent(const SocketClientData_t & sClient, const char * pData, int nDataLen)
{
std::string & strData = m_Contents[sClient];
strData.insert(strData.end(), pData, pData+nDataLen);
while (strData.size() > 4)
{
int nPktLen = *(int*)&strData[0];
if(nPktLen <= 0) //abnormal
{
strData.clear();
break;
}
if ((int)strData.size() >= (nPktLen + 4))
{
__HandlePkt(sClient, strData.c_str(), nPktLen + 4);
strData = strData.substr(nPktLen+4);
}
else
{
break;
}
}
}
void CCommonServer::__RemoveContent(const SocketClientData_t & sClient)
{
m_Contents.erase(sClient);
}
void CCommonServer::__HandlePkt(const SocketClientData_t & sClient, const char * pData, int nDataLen)
{
if (m_nThreadNum == 0)
{
m_pDataHandle->OnClientDataReceived(sClient, pData, nDataLen);
}
else
{
COutputBuffer::Pointer pOutputBuffer(new COutputBuffer(pData, nDataLen));
m_Threads[((unsigned int)__sync_fetch_and_add(&m_nCount, 1))%m_nThreadNum].AddPkt(std::make_pair(sClient, pOutputBuffer));
}
}