forked from YeeYoungHan/cpphttpstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpClientThread.cpp
More file actions
168 lines (144 loc) · 4.75 KB
/
TcpClientThread.cpp
File metadata and controls
168 lines (144 loc) · 4.75 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
/*
* 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 "SipTcp.h"
#include "Log.h"
#include "MemoryDebug.h"
/**
* @ingroup TcpStack
* @brief TCP 클라이언트 쓰레드 실행 인자
*/
class CTcpClientArg
{
public:
CTcpClientArg()
{
}
~CTcpClientArg()
{
}
/** TCP 서버 IP 주소 */
std::string m_strIp;
/** TCP 서버 포트 번호 */
int m_iPort;
/** TCP stack */
CTcpStack * m_pclsStack;
};
/**
* @ingroup TcpStack
* @brief TCP 연결 Thread
* @param lpParameter CTcpClientArg 객체
* @returns 0 을 리턴한다.
*/
THREAD_API TcpClientThread( LPVOID lpParameter )
{
CTcpClientArg * pclsArg = (CTcpClientArg *)lpParameter;
CLog::Print( LOG_INFO, "TcpClientThread started (%s:%d)", pclsArg->m_strIp.c_str(), pclsArg->m_iPort );
Socket hConn = TcpConnect( pclsArg->m_strIp.c_str(), pclsArg->m_iPort, pclsArg->m_pclsStack->m_clsSetup.m_iTcpConnectTimeout );
if( hConn == INVALID_SOCKET )
{
CLog::Print( LOG_ERROR, "%s TcpConnect(%s:%d) error(%d)", __FUNCTION__, pclsArg->m_strIp.c_str(), pclsArg->m_iPort, GetError() );
pclsArg->m_pclsStack->m_clsClientMap.Delete( pclsArg->m_strIp.c_str(), pclsArg->m_iPort );
}
else
{
CTcpComm clsTcpComm;
bool bAccept = true;
if( pclsArg->m_pclsStack->m_clsSetup.m_bUseTls )
{
if( SSLConnect( hConn, &clsTcpComm.m_psttSsl ) == false )
{
CLog::Print( LOG_ERROR, "%s SSLConnect(%s:%d) error", __FUNCTION__, pclsArg->m_strIp.c_str(), pclsArg->m_iPort );
closesocket( hConn );
pclsArg->m_pclsStack->m_clsClientMap.Delete( pclsArg->m_strIp.c_str(), pclsArg->m_iPort );
bAccept = false;
}
}
if( bAccept )
{
if( pclsArg->m_pclsStack->m_clsSetup.m_bUseThreadPipe )
{
clsTcpComm.m_hSocket = hConn;
snprintf( clsTcpComm.m_szIp, sizeof(clsTcpComm.m_szIp), "%s", pclsArg->m_strIp.c_str() );
clsTcpComm.m_iPort = pclsArg->m_iPort;
clsTcpComm.m_bClient = true;
if( pclsArg->m_pclsStack->m_clsThreadList.SendCommand( (char *)&clsTcpComm, sizeof(clsTcpComm) ) == false )
{
CLog::Print( LOG_ERROR, "%s m_clsThreadList.SendCommand error", __FUNCTION__ );
pclsArg->m_pclsStack->m_clsClientMap.Delete( pclsArg->m_strIp.c_str(), pclsArg->m_iPort );
closesocket( hConn );
}
}
else
{
CTcpNoPipeThreadArg * pclsNewArg = new CTcpNoPipeThreadArg();
if( pclsNewArg == NULL )
{
CLog::Print( LOG_ERROR, "%s new error", __FUNCTION__ );
closesocket( hConn );
}
else
{
pclsNewArg->m_hSocket = hConn;
pclsNewArg->m_strIp = pclsArg->m_strIp;
pclsNewArg->m_iPort = pclsArg->m_iPort;
pclsNewArg->m_pclsStack = pclsArg->m_pclsStack;
pclsNewArg->m_bClient = true;
if( StartThread( "TcpNoPipeThread", TcpNoPipeThread, pclsNewArg ) == false )
{
CLog::Print( LOG_ERROR, "%s StartThread error", __FUNCTION__ );
closesocket( hConn );
}
}
pclsArg->m_pclsStack->m_clsClientMap.Delete( pclsArg->m_strIp.c_str(), pclsArg->m_iPort );
}
}
}
CLog::Print( LOG_INFO, "TcpClientThread terminated (%s:%d)", pclsArg->m_strIp.c_str(), pclsArg->m_iPort );
delete pclsArg;
if( pclsArg->m_pclsStack->m_clsSetup.m_bUseTls )
{
ERR_remove_thread_state( NULL );
}
return 0;
}
/**
* @ingroup TcpStack
* @brief TCP 연결 Thread 를 시작한다.
* @param pszIp TCP 서버 IP 주소
* @param iPort TCP 서버 포트 번호
* @param pclsStack TCP stack
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool StartTcpClientThread( const char * pszIp, int iPort, CTcpStack * pclsStack )
{
CTcpClientArg * pclsArg = new CTcpClientArg();
if( pclsArg == NULL ) return false;
pclsArg->m_strIp = pszIp;
pclsArg->m_iPort = iPort;
pclsArg->m_pclsStack = pclsStack;
if( StartThread( "TcpClientThread", TcpClientThread, pclsArg ) == false )
{
delete pclsArg;
return false;
}
return true;
}