forked from YeeYoungHan/cppsipstack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTcpPipeThread.cpp
More file actions
206 lines (181 loc) · 6.63 KB
/
Copy pathTcpPipeThread.cpp
File metadata and controls
206 lines (181 loc) · 6.63 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
/*
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (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 "TcpSessionList.h"
#include "TlsFunction.h"
#include "TimeUtility.h"
#include "Log.h"
#include "MemoryDebug.h"
static void DeleteSession( CTcpStackSessionList * pclsSessionList, CTcpStack * pclsStack, int iIndex )
{
pclsStack->m_clsClientMap.Delete( pclsSessionList->m_pclsSession[iIndex].m_strIp.c_str(), pclsSessionList->m_pclsSession[iIndex].m_iPort );
pclsStack->m_pclsCallBack->SessionClosed( &pclsSessionList->m_pclsSession[iIndex] );
pclsSessionList->Delete( iIndex );
}
static void BeforeDelete( CTcpStackSessionList * pclsSessionList, int iIndex, void * pclsArg )
{
CTcpStack * pclsStack = (CTcpStack *)pclsArg;
pclsStack->m_clsClientMap.Delete( pclsSessionList->m_pclsSession[iIndex].m_strIp.c_str(), pclsSessionList->m_pclsSession[iIndex].m_iPort );
pclsStack->m_pclsCallBack->SessionClosed( &pclsSessionList->m_pclsSession[iIndex] );
}
/**
* @ingroup TcpStack
* @brief TCP 세션을 위한 쓰레드 함수
* @param lpParameter CThreadListEntry 객체의 포인터
* @returns 0 을 리턴한다.
*/
THREAD_API TcpPipeThread( LPVOID lpParameter )
{
CTcpThreadInfo * pclsThreadInfo = (CTcpThreadInfo *)lpParameter;
CTcpStackSessionList * pclsSessionList = &pclsThreadInfo->m_clsSessionList;
CTcpStack * pclsStack = pclsThreadInfo->m_pclsStack;
CTcpComm clsTcpComm;
int n, iIndex;
char szPacket[TCP_INIT_BUF_SIZE];
time_t iTime, iDeleteTime;
bool bAccept, bDeleteThreadInfo = false;
CLog::Print( LOG_INFO, "TcpPipeThread started (index=%d)", pclsThreadInfo->m_iIndex );
time( &iDeleteTime );
while( 1 )
{
n = poll( pclsSessionList->m_psttPollFd, pclsSessionList->m_iPoolFdCount, 1000 );
time( &iTime );
if( n <= 0 ) goto LOOP_END;
if( pclsSessionList->m_psttPollFd[0].revents & POLLIN )
{
// 새로운 TCP 세션을 자료구조에 추가한다.
if( CTcpThreadList::RecvCommand( pclsSessionList->m_psttPollFd[0].fd, (char *)&clsTcpComm, sizeof(clsTcpComm) ) == sizeof(clsTcpComm) )
{
if( clsTcpComm.m_hSocket == INVALID_SOCKET )
{
// 종료 신호 수신
bDeleteThreadInfo = true;
break;
}
else
{
bAccept = true;
#ifdef USE_TLS
if( pclsStack->m_clsSetup.m_bUseTls && clsTcpComm.m_bClient == false )
{
if( SSLAccept( clsTcpComm.m_hSocket, &clsTcpComm.m_psttSsl, false, 0, 10000 ) == false )
{
bAccept = false;
}
}
#endif
if( bAccept )
{
iIndex = pclsSessionList->Insert( clsTcpComm );
if( iIndex == -1 )
{
// 새로운 TCP 세션을 자료구조에 실패하면 TCP 세션을 종료하고 오류 처리한다.
clsTcpComm.Close();
if( clsTcpComm.m_bClient )
{
pclsStack->m_clsClientMap.Delete( clsTcpComm.m_szIp, clsTcpComm.m_iPort );
}
}
else if( clsTcpComm.m_bClient )
{
// TCP 클라이언트로 서버에 연결된 경우, TCP 클라이언트 정보를 저장하고 버퍼링된 패킷들을 전송한다.
pclsStack->m_clsClientMap.SetConnected( clsTcpComm.m_szIp, clsTcpComm.m_iPort, pclsThreadInfo->m_iIndex, iIndex );
SEND_PACKET_LIST clsSendPacketList;
SEND_PACKET_LIST::iterator itList;
pclsStack->m_clsClientMap.DeleteSendPacketList( clsTcpComm.m_szIp, clsTcpComm.m_iPort, clsSendPacketList );
for( itList = clsSendPacketList.begin(); itList != clsSendPacketList.end(); ++itList )
{
pclsSessionList->Send( iIndex, (*itList)->c_str(), (*itList)->length() );
delete *itList;
}
}
else
{
// TCP 서버로써 클라이언트가 연결되면 클라이언트 연결시 최초 이벤트 처리를 위한 callback 을 호출한다.
if( pclsStack->m_pclsCallBack->InComingConnected( &pclsSessionList->m_pclsSession[iIndex] ) == false )
{
// 허용되지 않는 TCP 연결이면 TCP 연결 종료시킨다.
CLog::Print( LOG_INFO, "not allowed TCP session(%s:%d)", clsTcpComm.m_szIp, clsTcpComm.m_iPort );
DeleteSession( pclsSessionList, pclsStack, iIndex );
}
else
{
pclsStack->m_clsClientMap.Insert( clsTcpComm.m_szIp, clsTcpComm.m_iPort, pclsThreadInfo->m_iIndex, iIndex );
}
}
}
}
}
--n;
}
if( n == 0 ) goto LOOP_END;
for( iIndex = 1; iIndex < pclsSessionList->m_iPoolFdCount; ++iIndex )
{
if( !(pclsSessionList->m_psttPollFd[iIndex].revents & POLLIN) ) continue;
#ifdef USE_TLS
if( pclsSessionList->m_pclsSession[iIndex].m_psttSsl )
{
pclsSessionList->m_pclsSession[iIndex].m_clsMutex.acquire();
n = SSLRecv( pclsSessionList->m_pclsSession[iIndex].m_psttSsl, szPacket, sizeof(szPacket) );
pclsSessionList->m_pclsSession[iIndex].m_clsMutex.release();
}
else
#endif
{
n = recv( pclsSessionList->m_psttPollFd[iIndex].fd, szPacket, sizeof(szPacket), 0 );
}
if( n <= 0 )
{
DeleteSession( pclsSessionList, pclsStack, iIndex );
continue;
}
if( CLog::GetLevel() & LOG_NETWORK )
{
pclsSessionList->m_pclsSession[iIndex].Log( szPacket, n, false );
}
pclsSessionList->m_pclsSession[iIndex].m_iRecvTime = iTime;
if( pclsStack->m_pclsCallBack->RecvPacket( szPacket, n, &pclsSessionList->m_pclsSession[iIndex] ) == false )
{
DeleteSession( pclsSessionList, pclsStack, iIndex );
continue;
}
}
LOOP_END:
if( ( iDeleteTime + 60 ) < iTime )
{
pclsSessionList->DeleteTimeout( pclsStack->m_clsSetup.m_iTcpRecvTimeout, pclsStack->m_clsSetup.m_iTcpNoPacketTimeout
, BeforeDelete, pclsStack );
iDeleteTime = iTime;
}
}
pclsSessionList->DeleteAll();
CLog::Print( LOG_INFO, "TcpPipeThread terminated (index=%d)", pclsThreadInfo->m_iIndex );
if( bDeleteThreadInfo )
{
pclsThreadInfo->Close();
delete pclsThreadInfo;
}
#ifdef USE_TLS
if( pclsStack->m_clsSetup.m_bUseTls )
{
ERR_remove_thread_state( NULL );
}
#endif
return 0;
}