forked from YeeYoungHan/cpphttpstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpNoPipeThread.cpp
More file actions
159 lines (134 loc) · 4.66 KB
/
TcpNoPipeThread.cpp
File metadata and controls
159 lines (134 loc) · 4.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
/*
* 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 "TlsFunction.h"
#include "TimeUtility.h"
#include "Log.h"
#include "MemoryDebug.h"
/**
* @ingroup TcpStack
* @brief pipe 를 사용하지 않는 TCP 세션 쓰레드 함수 - m_bUseThreadPipe 가 false 일 때에 사용된다.
* @param lpParameter CThreadListEntry 객체의 포인터
* @returns 0 을 리턴한다.
*/
THREAD_API TcpNoPipeThread( LPVOID lpParameter )
{
CTcpNoPipeThreadArg * pclsArg = (CTcpNoPipeThreadArg *)lpParameter;
CTcpStack * pclsStack = pclsArg->m_pclsStack;
CTcpSessionInfo clsSessionInfo;
int iDiffTime, n;
bool bAccept = true;
char szPacket[TCP_INIT_BUF_SIZE];
time_t iTime;
CLog::Print( LOG_INFO, "TcpNoPipeThread started" );
clsSessionInfo.m_hSocket = pclsArg->m_hSocket;
clsSessionInfo.m_strIp = pclsArg->m_strIp;
clsSessionInfo.m_iPort = pclsArg->m_iPort;
time( &clsSessionInfo.m_iConnectTime );
if( pclsStack->m_clsSetup.m_bUseTls && pclsArg->m_bClient == false )
{
if( SSLAccept( clsSessionInfo.m_hSocket, &clsSessionInfo.m_psttSsl, false, 0, 10000 ) == false )
{
bAccept = false;
}
}
delete pclsArg;
if( bAccept )
{
if( pclsStack->m_pclsCallBack->InComingConnected( &clsSessionInfo ) == false )
{
// 허용되지 않는 TCP 연결이면 TCP 연결 종료시킨다.
CLog::Print( LOG_INFO, "not allowed TCP session(%s:%d)", clsSessionInfo.m_strIp.c_str(), clsSessionInfo.m_iPort );
}
else
{
if( pclsStack->m_clsSessionMap.Insert( clsSessionInfo.m_strIp.c_str(), clsSessionInfo.m_iPort, &clsSessionInfo ) == false )
{
CLog::Print( LOG_ERROR, "%s m_clsSessionMap.Insert(%s:%d) error", __FUNCTION__, clsSessionInfo.m_strIp.c_str(), clsSessionInfo.m_iPort );
}
else
{
CLog::Print( LOG_INFO, "TcpNoPipeThread (index=%d)", clsSessionInfo.m_iThreadIndex );
pollfd sttPoll[1];
TcpSetPollIn( sttPoll[0], clsSessionInfo.m_hSocket );
while( pclsStack->m_bStop == false )
{
n = poll( sttPoll, 1, 1000 );
if( n <= 0 )
{
time( &iTime );
if( clsSessionInfo.m_iRecvTime != 0 )
{
iDiffTime = (int)(iTime - clsSessionInfo.m_iRecvTime);
if( iDiffTime >= pclsStack->m_clsSetup.m_iTcpRecvTimeout )
{
CLog::Print( LOG_ERROR, "%s client(%s:%d) recv timeout", __FUNCTION__, clsSessionInfo.m_strIp.c_str(), clsSessionInfo.m_iPort );
break;
}
}
else
{
iDiffTime = (int)(iTime - clsSessionInfo.m_iConnectTime);
if( iDiffTime >= pclsStack->m_clsSetup.m_iTcpNoPacketTimeout )
{
CLog::Print( LOG_ERROR, "%s client(%s:%d) no packet timeout", __FUNCTION__, clsSessionInfo.m_strIp.c_str(), clsSessionInfo.m_iPort );
break;
}
}
continue;
}
if( clsSessionInfo.m_psttSsl )
{
clsSessionInfo.m_clsMutex.acquire();
n = SSLRecv( clsSessionInfo.m_psttSsl, szPacket, sizeof(szPacket) );
clsSessionInfo.m_clsMutex.release();
}
else
{
n = recv( clsSessionInfo.m_hSocket, szPacket, sizeof(szPacket), 0 );
}
if( n <= 0 )
{
break;
}
if( CLog::GetLevel() & LOG_NETWORK )
{
clsSessionInfo.Log( szPacket, n, false );
}
time( &clsSessionInfo.m_iRecvTime );
if( pclsStack->m_pclsCallBack->RecvPacket( szPacket, n, &clsSessionInfo ) == false )
{
break;
}
}
pclsStack->m_clsSessionMap.Delete( clsSessionInfo.m_strIp.c_str(), clsSessionInfo.m_iPort );
}
}
}
closesocket( clsSessionInfo.m_hSocket );
pclsStack->m_pclsCallBack->SessionClosed( &clsSessionInfo );
clsSessionInfo.Clear();
CLog::Print( LOG_INFO, "TcpNoPipeThread terminated (index=%d)", clsSessionInfo.m_iThreadIndex );
if( pclsStack->m_clsSetup.m_bUseTls )
{
ERR_remove_thread_state( NULL );
}
return 0;
}