forked from p308945/MyNetWorkLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySockTaskPool.cpp
More file actions
309 lines (294 loc) · 6.66 KB
/
MySockTaskPool.cpp
File metadata and controls
309 lines (294 loc) · 6.66 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*************************************************************************
> File Name: MySockTaskPool.cpp
> Author: huangyun
> Mail: 895175589@qq.com
> Created Time: Sat 08 Aug 2015 02:31:39 AM
************************************************************************/
#include "MySockTaskPool.h"
#include "MySockTask.h"
#include "MyLock.h"
#include <queue>
#include <deque>
#include <set>
#include "errno.h"
#include "MySockTaskManager.h"
#include <climits>
namespace MyNameSpace
{
class TaskQueue
{
public:
void addTask(MySockTask *task)
{
MyScopeLock scopeLock(mLock);
mTaskQueue.push(task);
}
virtual void add(MySockTask *task) = 0;
void checkQueue()
{
{
MyScopeLock scopeLock(mLock);
while(!mTaskQueue.empty())
{
MySockTask *task = mTaskQueue.front();
mTaskQueueTmp.push(task);
mTaskQueue.pop();
}
}
while(!mTaskQueueTmp.empty())
{
MySockTask *task = mTaskQueueTmp.front();
add(task);
mTaskQueueTmp.pop();
}
}
private:
MyLock mLock;
std::queue<MySockTask*, std::deque<MySockTask*> > mTaskQueue;
std::queue<MySockTask*, std::deque<MySockTask*> > mTaskQueueTmp;
};
class MyIoThread : public MyThread, public TaskQueue
{
public:
MyIoThread(MySockTaskPool *pool, bool j = true) : MyThread(j), mPool(pool)
{
epfd = -1;
}
~MyIoThread()
{
if (epfd > 0)
{
close(epfd);
}
}
void run();
void add(MySockTask *task)
{
epoll_event ev;
ev.events = EPOLLIN|EPOLLOUT|EPOLLPRI|EPOLLERR; //EPOLLOUT在此处设置可能有busy loop现象,但是如果不设置会导致后面频繁设置EPOLLOUT,性能未必高,加上run每次有等待时间,所以还是在此处加上
ev.data.ptr = task;
task->addEpollEvent(epfd, ev);
taskSet.insert(task);
}
void remove(MySockTask *task)
{
epoll_event ev;
ev.events = EPOLLIN|EPOLLOUT|EPOLLPRI|EPOLLERR;
task->delEpollEvent(epfd, ev);
taskSet.erase(task);
}
int getTaskCount()
{
return taskSet.size();
}
bool init()
{
MyThread::init();
int maxCount = mPool->getMaxCoonPerIo();
epfd = epoll_create(maxCount);
if (epfd < 0)
{
std::cerr<<__FUNCTION__<<":"<<__LINE__<<" MyIoThread epoll create failed! "<< std::endl;
return false;
}
epev.resize(maxCount);
return true;
}
private:
MySockTaskPool *mPool;
int epfd;
std::vector<epoll_event> epev;
std::set<MySockTask *> taskSet;
};
void MyIoThread::run()
{
std::set<MySockTask *> taskWillDel;
while(!isFini())
{
checkQueue();
int retCode = epoll_wait(epfd, &epev[0], taskSet.size()/*常数级*/, 50/*ms*/);
for (int i = 0; i < retCode; ++i)
{
if (epev[i].events&EPOLLIN || epev[i].events&EPOLLPRI)
{
MySockTask *task = static_cast<MySockTask*>(epev[i].data.ptr);
int len = task->rcvBuffer();
if (len < 0)
{
taskWillDel.insert(task);
}
else if (len == 0)
{
std::cout<<"client close"<<std::endl;
taskWillDel.insert(task);
}
int getMsgRet = task->getMsg();
if (getMsgRet < 0)
{
taskWillDel.insert(task);
}
}
else if (epev[i].events&EPOLLOUT)
{
MySockTask *task = static_cast<MySockTask*>(epev[i].data.ptr);
int len = task->syncSendBuf();
if (len < 0)
{
taskWillDel.insert(task);
}
}
else if (epev[i].events&EPOLLERR)
{
MySockTask *task = static_cast<MySockTask*>(epev[i].data.ptr);
taskWillDel.insert(task);
}
}
if (!taskWillDel.empty())
{
std::cout<<"delete client size:"<<taskWillDel.size()<<std::endl;
std::set<MySockTask *>::iterator iter = taskWillDel.begin();
for (; iter != taskWillDel.end(); ++iter)
{
mPool->addRecycleThread(*iter);
remove(*iter);
}
taskWillDel.clear();
}
usleep(5000);
}
}
class MyRecycleThread : public MyThread, public TaskQueue
{
public:
MyRecycleThread(MySockTaskPool *pool, bool j = true) : MyThread(j), mPool(pool)
{
}
void run();
void add(MySockTask *task)
{
taskSet.insert(task);
}
void remove(MySockTask *task)
{
taskSet.erase(task);
}
private:
MySockTaskPool *mPool;
std::set<MySockTask *> taskSet;
};
void MyRecycleThread::run()
{
while(!isFini())
{
checkQueue();
if (!taskSet.empty())
{
std::set<MySockTask *>::iterator iter = taskSet.begin();
for (; iter != taskSet.end(); ++iter)
{
if (NULL != *iter)
{
MySockTaskManager::getInstance().removeTask(*iter);
delete *iter;
}
}
taskSet.clear();
}
usleep(10*1000);
}
}
MySockTaskPool::MySockTaskPool(int ioCount, int maxConnPerIo):mIoCount(ioCount), mMaxConnPerIo(maxConnPerIo)
{
}
bool MySockTaskPool::addTask(MySockTask * task)
{
int count = mIoThreadPool.getThreadCOunt();
int minCount = INT_MAX;
int index = -1;
for (int i = 0; i < count; ++i)
{
MyIoThread * thread = dynamic_cast<MyIoThread*>(mIoThreadPool.getThreadByIndex(i));
if (NULL != thread)
{
if (!thread->isStart())
{
if (!thread->start())
{
std::cerr<<__FUNCTION__<<":"<<__LINE__<<"thread start failed! "<< std::endl;
continue;
}
thread->addTask(task);
return true;
}
if ((minCount > thread->getTaskCount()) && (thread->getTaskCount() < mMaxConnPerIo))
{
index = i;
}
/* if (thread->getTaskCount() < mMaxConnPerIo)
{
thread->addTask(task);
return true;
}
else
{
std::cout<<"i: "<<i<<" mMaxConnPerIo: "<<mMaxConnPerIo<<" thread->getTaskCount(): "<<thread->getTaskCount()<<std::endl;
}
*/
}
}
if (-1 == index)
{
std::cerr<<__FUNCTION__<<":"<<__LINE__<<"task add error! "<< std::endl;
return false;
}
MyIoThread * thread = dynamic_cast<MyIoThread*>(mIoThreadPool.getThreadByIndex(index));
if (NULL != thread)
{
thread->addTask(task);
return true;
}
return false;
}
void MySockTaskPool::addRecycleThread(MySockTask * task)
{
mRecycleThread->addTask(task);
}
bool MySockTaskPool::init()
{
mRecycleThread = new MyRecycleThread(this);
if (NULL == mRecycleThread)
{
return false;
}
for (int i = 0; i < mIoCount; ++i)
{
MyIoThread * thread = new MyIoThread(this);
if (NULL == thread || !thread->init())
{
delete mRecycleThread;
mRecycleThread = NULL;
mIoThreadPool.delAllThread();
return false;
}
mIoThreadPool.putThread(thread);
}
return true;
}
void MySockTaskPool::fini()
{
mIoThreadPool.joinAll();
if (NULL != mRecycleThread)
{
mRecycleThread->terminate();
mRecycleThread->join();
}
}
MySockTaskPool::~MySockTaskPool()
{
mIoThreadPool.delAllThread();
if (NULL != mRecycleThread)
{
delete mRecycleThread;
}
}
}