forked from YeeYoungHan/cpphttpstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpThreadList.cpp
More file actions
585 lines (503 loc) · 13.6 KB
/
TcpThreadList.cpp
File metadata and controls
585 lines (503 loc) · 13.6 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
* Copyright (C) 2012 Yee Young Han <[email protected]> (http://blog.naver.com/websearch)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "SipPlatformDefine.h"
#include "TcpStack.h"
#include "TcpThread.h"
#include "ServerUtility.h"
#include "TimeUtility.h"
#include "Log.h"
#include "MemoryDebug.h"
CTcpThreadInfo::CTcpThreadInfo() : m_iIndex(0), m_hSend(INVALID_SOCKET), m_hRecv(INVALID_SOCKET)
{
}
CTcpThreadInfo::~CTcpThreadInfo()
{
Close();
}
/**
* @ingroup TcpStack
* @brief 소켓을 종료한다.
*/
void CTcpThreadInfo::Close()
{
if( m_hSend != INVALID_SOCKET )
{
closesocket( m_hSend );
m_hSend = INVALID_SOCKET;
}
if( m_hRecv != INVALID_SOCKET )
{
closesocket( m_hRecv );
m_hRecv = INVALID_SOCKET;
}
}
CTcpThreadList::CTcpThreadList() : m_iMaxSocketPerThread(0), m_iThreadIndex(0), m_pclsStack(NULL)
{
}
CTcpThreadList::~CTcpThreadList()
{
}
/**
* @ingroup TcpStack
* @brief 쓰레드 리스트를 시작한다.
* @param pclsStack CTcpStack 객체
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CTcpThreadList::Create( CTcpStack * pclsStack )
{
if( pclsStack->m_clsSetup.m_iMaxSocketPerThread <= 0 )
{
CLog::Print( LOG_ERROR, "%s m_iMaxSocketPerThread(%d) is invalid", __FUNCTION__, pclsStack->m_clsSetup.m_iMaxSocketPerThread );
return false;
}
// pipe 연산을 수행해야 하므로 1개 추가한다.
m_iMaxSocketPerThread = pclsStack->m_clsSetup.m_iMaxSocketPerThread + 1;
m_pclsStack = pclsStack;
bool bError = false;
m_clsMutex.acquire();
for( int i = 0; i < pclsStack->m_clsSetup.m_iThreadInitCount; ++i )
{
if( AddThread() == false )
{
bError = true;
break;
}
}
m_clsMutex.release();
if( bError )
{
Destroy();
return false;
}
return true;
}
/**
* @ingroup TcpStack
* @brief 쓰레드 리스트를 종료한다.
*/
void CTcpThreadList::Destroy()
{
THREAD_LIST::iterator itTL;
CTcpComm clsTcpComm;
SendCommandAll( (char *)&clsTcpComm, sizeof(clsTcpComm) );
m_clsMutex.acquire();
m_clsList.clear();
m_clsMutex.release();
}
/**
* @ingroup TcpStack
* @brief 쓰레드에 명령을 전송한다.
* @param pszData 명령
* @param iDataLen pszData 길이
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CTcpThreadList::SendCommand( const char * pszData, int iDataLen )
{
bool bRes = false, bFound = false;
THREAD_LIST::iterator itTL;
int iMinCount = 2000000000;
m_clsMutex.acquire();
// 소켓을 최소 사용하는 쓰레드를 검색한다.
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
if( iMinCount > (*itTL)->m_clsSessionList.m_iPoolFdCount )
{
iMinCount = (*itTL)->m_clsSessionList.m_iPoolFdCount;
if( iMinCount == 0 ) break;
}
}
if( iMinCount < m_iMaxSocketPerThread )
{
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
if( iMinCount == (*itTL)->m_clsSessionList.m_iPoolFdCount )
{
bRes = _SendCommand( (*itTL)->m_hSend, pszData, iDataLen );
bFound = true;
break;
}
}
}
if( bFound == false )
{
if( AddThread() )
{
bRes = _SendCommand( m_clsList[m_clsList.size()-1]->m_hSend, pszData, iDataLen );
}
}
m_clsMutex.release();
return bRes;
}
/**
* @ingroup TcpStack
* @brief 지정된 쓰레드 번호의 쓰레드로 명령을 전송한다.
* @param pszData 명령
* @param iDataLen pszData 길이
* @param iThreadIndex 쓰레드 인덱스
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CTcpThreadList::SendCommand( const char * pszData, int iDataLen, int iThreadIndex )
{
bool bRes = false;
THREAD_LIST::iterator itTL;
m_clsMutex.acquire();
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
if( (*itTL)->m_iIndex == iThreadIndex )
{
bRes = _SendCommand( (*itTL)->m_hSend, pszData, iDataLen );
break;
}
}
m_clsMutex.release();
return bRes;
}
/**
* @ingroup TcpStack
* @brief 모든 쓰레드에 명령을 전달한다.
* @param pszData 명령
* @param iDataLen pszData 길이
*/
void CTcpThreadList::SendCommandAll( const char * pszData, int iDataLen )
{
THREAD_LIST::iterator itTL;
m_clsMutex.acquire();
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
_SendCommand( (*itTL)->m_hSend, pszData, iDataLen );
}
m_clsMutex.release();
}
/**
* @ingroup TcpStack
* @brief 명령을 수신한다.
* @param hSocket pipe 수신 핸들
* @param pszData 명령 저장 변수
* @param iDataSize pszData 변수 크기
* @returns 수신한 길이를 리턴한다.
*/
int CTcpThreadList::RecvCommand( Socket hSocket, char * pszData, int iDataSize )
{
int n;
#ifdef WIN32
n = recv( hSocket, pszData, iDataSize, 0 );
#else
n = read( hSocket, pszData, iDataSize );
#endif
return n;
}
/**
* @ingroup TcpStack
* @brief 특정 세션에 TCP 패킷을 전송한다.
* @param iThreadIndex TCP 쓰레드 번호
* @param iSessionIndex TCP 세션 번호
* @param pszPacket 패킷
* @param iPacketLen 패킷 길이
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CTcpThreadList::Send( int iThreadIndex, int iSessionIndex, const char * pszPacket, int iPacketLen )
{
bool bRes = false;
m_clsMutex.acquire();
int iCount = m_clsList.size();
for( int i = 0; i < iCount; ++i )
{
if( m_clsList[i]->m_iIndex == iThreadIndex )
{
bRes = m_clsList[i]->m_clsSessionList.Send( iSessionIndex, pszPacket, iPacketLen );
break;
}
}
m_clsMutex.release();
return bRes;
}
/**
* @ingroup TcpStack
* @brief 모든 세션에 TCP 패킷을 전송한다.
* @param pszPacket 패킷
* @param iPacketLen 패킷 길이
* @param pclsCallBack 세션별로 전송 유무를 결정하는 callback 객체
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CTcpThreadList::SendAll( const char * pszPacket, int iPacketLen, ITcpStackCallBack * pclsCallBack )
{
THREAD_LIST::iterator itTL;
m_clsMutex.acquire();
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
(*itTL)->m_clsSessionList.SendAll( pszPacket, iPacketLen, pclsCallBack );
}
m_clsMutex.release();
return true;
}
/**
* @ingroup TcpStack
* @brief 모든 세션에 TCP 패킷을 전송한다.
* @param pszPacket 패킷
* @param iPacketLen 패킷 길이
* @param pclsCallBack 세션별로 전송 유무를 결정하는 callback 객체
* @param iThreadIndex 전송하지 않을 세션의 쓰레드 인덱스
* @param iSessionIndex 전송하지 않을 세션 인덱스
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CTcpThreadList::SendAllExcept( const char * pszPacket, int iPacketLen, ITcpStackCallBack * pclsCallBack, int iThreadIndex, int iSessionIndex )
{
THREAD_LIST::iterator itTL;
m_clsMutex.acquire();
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
(*itTL)->m_clsSessionList.SendAllExcept( pszPacket, iPacketLen, pclsCallBack, iThreadIndex, iSessionIndex );
}
m_clsMutex.release();
return true;
}
typedef std::list< int > THREAD_INDEX_LIST;
/**
* @ingroup TcpStack
* @brief TCP 클라이언트와 연결되지 않은 쓰레드를 삭제한다.
*/
void CTcpThreadList::DeleteNoUseThread()
{
THREAD_LIST::iterator itTL;
THREAD_INDEX_LIST clsDeleteList;
THREAD_INDEX_LIST::iterator itTIL;
int iUseCount = 0;
m_clsMutex.acquire();
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
if( (*itTL)->m_clsSessionList.m_iPoolFdCount == 1 )
{
clsDeleteList.push_back( (*itTL)->m_iIndex );
}
else
{
++iUseCount;
}
}
m_clsMutex.release();
if( clsDeleteList.size() > 0 )
{
// 초기 실행 쓰레드 개수만큼은 유지되어야 한다.
if( m_pclsStack->m_clsSetup.m_iThreadInitCount > iUseCount )
{
int iDeleteCount = m_pclsStack->m_clsSetup.m_iThreadInitCount - iUseCount;
if( iDeleteCount >= (int)clsDeleteList.size() )
{
clsDeleteList.clear();
}
else
{
THREAD_INDEX_LIST::iterator itNext;
for( itTIL = clsDeleteList.begin(); itTIL != clsDeleteList.end(); )
{
itNext = itTIL;
++itNext;
clsDeleteList.erase( itTIL );
itTIL = itNext;
--iDeleteCount;
if( iDeleteCount == 0 ) break;
}
}
}
for( itTIL = clsDeleteList.begin(); itTIL != clsDeleteList.end(); ++itTIL )
{
DeleteThread( *itTIL );
}
}
}
/**
* @ingroup TcpStack
* @brief 쓰레드 정보를 삭제한다.
* @param iThreadIndex 쓰레드 인덱스
* @returns 성공하면 true 를 리턴하고 성공하면 false 를 리턴한다.
*/
bool CTcpThreadList::DeleteThread( int iThreadIndex )
{
THREAD_LIST::iterator itTL;
CTcpComm clsTcpComm;
bool bRes = false;
SendCommand( (char *)&clsTcpComm, sizeof(clsTcpComm), iThreadIndex );
m_clsMutex.acquire();
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
if( (*itTL)->m_iIndex == iThreadIndex )
{
// 쓰레드에서 자료구조를 사용하므로 쓰레드 종료할 때에 삭제한다.
// delete *itTL;
m_clsList.erase( itTL );
bRes = true;
break;
}
}
if( bRes )
{
int iMaxIndex = 0;
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
if( (*itTL)->m_iIndex > iMaxIndex )
{
iMaxIndex = (*itTL)->m_iIndex;
}
}
m_iThreadIndex = iMaxIndex;
}
m_clsMutex.release();
return bRes;
}
/**
* @ingroup TcpStack
* @brief 쓰레드 정보를 하나의 문자열에 저장한다.
* @param strBuf 쓰레드 정보를 저장할 문자열 변수
*/
void CTcpThreadList::GetString( CMonitorString & strBuf )
{
THREAD_LIST::iterator itTL;
strBuf.Clear();
m_clsMutex.acquire();
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
strBuf.AddRow( (*itTL)->m_clsSessionList.m_iPoolFdCount );
}
m_clsMutex.release();
}
/**
* @ingroup TcpStack
* @brief 쓰레드를 추가한다.
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CTcpThreadList::AddThread()
{
if( m_pclsStack->m_clsSetup.m_iThreadMaxCount != 0 )
{
int iListCount = (int)m_clsList.size();
if( iListCount >= m_pclsStack->m_clsSetup.m_iThreadMaxCount )
{
CLog::Print( LOG_ERROR, "%s thread count(%d) >= max thread count(%d)", __FUNCTION__, iListCount, m_pclsStack->m_clsSetup.m_iThreadMaxCount );
return false;
}
}
Socket arrSocket[2];
CTcpThreadInfo * pclsTcpThreadInfo = new CTcpThreadInfo();
if( pclsTcpThreadInfo == NULL )
{
CLog::Print( LOG_ERROR, "%s new error", __FUNCTION__ );
return false;
}
pclsTcpThreadInfo->m_iIndex = GetThreadIndex();
if( pclsTcpThreadInfo->m_clsSessionList.Init( pclsTcpThreadInfo->m_iIndex, m_iMaxSocketPerThread ) == false )
{
CLog::Print( LOG_ERROR, "%s m_clsSessionList.Init error", __FUNCTION__ );
delete pclsTcpThreadInfo;
return false;
}
if( pipe( arrSocket ) == -1 )
{
CLog::Print( LOG_ERROR, "%s pipe error", __FUNCTION__ );
delete pclsTcpThreadInfo;
return false;
}
pclsTcpThreadInfo->m_hRecv = arrSocket[0];
pclsTcpThreadInfo->m_hSend = arrSocket[1];
pclsTcpThreadInfo->m_clsSessionList.Insert( pclsTcpThreadInfo->m_hRecv );
pclsTcpThreadInfo->m_pclsStack = m_pclsStack;
m_clsList.push_back( pclsTcpThreadInfo );
bool bRes = StartThread( "TcpPipeThread", TcpPipeThread, pclsTcpThreadInfo );
if( bRes == false )
{
DeleteThread( pclsTcpThreadInfo->m_iIndex );
delete pclsTcpThreadInfo;
}
return bRes;
}
/**
* @ingroup TcpStack
* @brief 쓰레드에 명령을 전송한다.
* @param hSocket pipe 전송 핸들
* @param pszData 명령
* @param iDataLen pszData 길이
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CTcpThreadList::_SendCommand( Socket hSocket, const char * pszData, int iDataLen )
{
int n;
#ifdef WIN32
n = send( hSocket, pszData, iDataLen, 0 );
#else
n = write( hSocket, pszData, iDataLen );
#endif
if( n != iDataLen )
{
return false;
}
return true;
}
/**
* @ingroup TcpStack
* @brief 쓰레드 개수를 리턴한다.
* @returns 쓰레드 개수를 리턴한다.
*/
int CTcpThreadList::GetCount()
{
int iCount = 0;
m_clsMutex.acquire();
iCount = m_clsList.size();
m_clsMutex.release();
return iCount;
}
/**
* @ingroup TcpStack
* @brief 새로 사용할 쓰레드 번호를 가져온다.
* @returns 새로 사용할 쓰레드 번호를 리턴한다.
*/
int CTcpThreadList::GetThreadIndex()
{
int iThreadIndex = 0;
++m_iThreadIndex;
if( m_iThreadIndex > 2000000000 )
{
m_iThreadIndex = 1;
}
while( SelectThreadIndex( m_iThreadIndex ) )
{
++m_iThreadIndex;
if( m_iThreadIndex > 2000000000 )
{
m_iThreadIndex = 1;
}
}
iThreadIndex = m_iThreadIndex;
return iThreadIndex;
}
/**
* @ingroup TcpStack
* @brief 쓰레드 번호가 사용중인지 검사한다.
* @param iThreadIndex 쓰레드 번호
* @returns 쓰레드 번호가 사용중이면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CTcpThreadList::SelectThreadIndex( int iThreadIndex )
{
THREAD_LIST::iterator itTL;
for( itTL = m_clsList.begin(); itTL != m_clsList.end(); ++itTL )
{
if( (*itTL)->m_iIndex == iThreadIndex )
{
return true;
}
}
return false;
}