工具项目

TcpClient.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : http://www.jessma.org
  6. * Project : https://github.com/ldcsaa
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 75375912, 44636872
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #include "stdafx.h"
  24. #include "TcpClient.h"
  25. #include "../Common/Src/WaitFor.h"
  26. #include <process.h>
  27. const CInitSocket CTcpClient::sm_wsSocket;
  28. BOOL CTcpClient::Start(LPCTSTR lpszRemoteAddress, USHORT usPort, BOOL bAsyncConnect, LPCTSTR lpszBindAddress)
  29. {
  30. if(!CheckParams() || !CheckStarting())
  31. return FALSE;
  32. PrepareStart();
  33. m_ccContext.Reset();
  34. BOOL isOK = FALSE;
  35. HP_SOCKADDR addrRemote, addrBind;
  36. if(CreateClientSocket(lpszRemoteAddress, addrRemote, usPort, lpszBindAddress, addrBind))
  37. {
  38. if(BindClientSocket(addrBind))
  39. {
  40. if(TRIGGER(FirePrepareConnect(m_soClient)) != HR_ERROR)
  41. {
  42. if(ConnectToServer(addrRemote, bAsyncConnect))
  43. {
  44. if(CreateWorkerThread())
  45. isOK = TRUE;
  46. else
  47. SetLastError(SE_WORKER_THREAD_CREATE, __FUNCTION__, ERROR_CREATE_FAILED);
  48. }
  49. else
  50. SetLastError(SE_CONNECT_SERVER, __FUNCTION__, ::WSAGetLastError());
  51. }
  52. else
  53. SetLastError(SE_SOCKET_PREPARE, __FUNCTION__, ENSURE_ERROR_CANCELLED);
  54. }
  55. else
  56. SetLastError(SE_SOCKET_BIND, __FUNCTION__, ::WSAGetLastError());
  57. }
  58. else
  59. SetLastError(SE_SOCKET_CREATE, __FUNCTION__, ::WSAGetLastError());
  60. if(!isOK)
  61. {
  62. m_ccContext.Reset(FALSE);
  63. EXECUTE_RESTORE_ERROR(Stop());
  64. }
  65. return isOK;
  66. }
  67. BOOL CTcpClient::CheckParams()
  68. {
  69. if (((int)m_dwSocketBufferSize > 0) &&
  70. ((int)m_dwFreeBufferPoolSize >= 0) &&
  71. ((int)m_dwFreeBufferPoolHold >= 0) &&
  72. ((int)m_dwKeepAliveTime >= 1000 || m_dwKeepAliveTime == 0) &&
  73. ((int)m_dwKeepAliveInterval >= 1000 || m_dwKeepAliveInterval == 0) )
  74. return TRUE;
  75. SetLastError(SE_INVALID_PARAM, __FUNCTION__, ERROR_INVALID_PARAMETER);
  76. return FALSE;
  77. }
  78. void CTcpClient::PrepareStart()
  79. {
  80. m_itPool.SetItemCapacity((int)m_dwSocketBufferSize);
  81. m_itPool.SetPoolSize((int)m_dwFreeBufferPoolSize);
  82. m_itPool.SetPoolHold((int)m_dwFreeBufferPoolHold);
  83. m_itPool.Prepare();
  84. }
  85. BOOL CTcpClient::CheckStarting()
  86. {
  87. CSpinLock locallock(m_csState);
  88. if(m_enState == SS_STOPPED)
  89. m_enState = SS_STARTING;
  90. else
  91. {
  92. SetLastError(SE_ILLEGAL_STATE, __FUNCTION__, ERROR_INVALID_OPERATION);
  93. return FALSE;
  94. }
  95. return TRUE;
  96. }
  97. BOOL CTcpClient::CheckStoping(DWORD dwCurrentThreadID)
  98. {
  99. if(m_enState != SS_STOPPED)
  100. {
  101. CSpinLock locallock(m_csState);
  102. if(HasStarted())
  103. {
  104. m_enState = SS_STOPPING;
  105. return TRUE;
  106. }
  107. if(dwCurrentThreadID != m_dwWorkerID)
  108. {
  109. while(m_enState != SS_STOPPED)
  110. ::Sleep(30);
  111. }
  112. }
  113. SetLastError(SE_ILLEGAL_STATE, __FUNCTION__, ERROR_INVALID_OPERATION);
  114. return FALSE;
  115. }
  116. BOOL CTcpClient::CreateClientSocket(LPCTSTR lpszRemoteAddress, HP_SOCKADDR& addrRemote, USHORT usPort, LPCTSTR lpszBindAddress, HP_SOCKADDR& addrBind)
  117. {
  118. if(!::GetSockAddrByHostName(lpszRemoteAddress, usPort, addrRemote))
  119. return FALSE;
  120. if(lpszBindAddress && lpszBindAddress[0] != 0)
  121. {
  122. if(!::sockaddr_A_2_IN(lpszBindAddress, 0, addrBind))
  123. return FALSE;
  124. if(addrRemote.family != addrBind.family)
  125. {
  126. ::WSASetLastError(WSAEAFNOSUPPORT);
  127. return FALSE;
  128. }
  129. }
  130. m_soClient = socket(addrRemote.family, SOCK_STREAM, IPPROTO_TCP);
  131. if(m_soClient == INVALID_SOCKET)
  132. return FALSE;
  133. BOOL bOnOff = (m_dwKeepAliveTime > 0 && m_dwKeepAliveInterval > 0);
  134. VERIFY(::SSO_KeepAliveVals(m_soClient, bOnOff, m_dwKeepAliveTime, m_dwKeepAliveInterval) == NO_ERROR);
  135. m_evSocket = ::WSACreateEvent();
  136. ASSERT(m_evSocket != WSA_INVALID_EVENT);
  137. SetRemoteHost(lpszRemoteAddress, usPort);
  138. return TRUE;
  139. }
  140. BOOL CTcpClient::BindClientSocket(const HP_SOCKADDR& addrBind)
  141. {
  142. if(addrBind.IsSpecified() && (::bind(m_soClient, addrBind.Addr(), addrBind.AddrSize()) == SOCKET_ERROR))
  143. return FALSE;
  144. m_dwConnID = ::GenerateConnectionID();
  145. return TRUE;
  146. }
  147. BOOL CTcpClient::ConnectToServer(const HP_SOCKADDR& addrRemote, BOOL bAsyncConnect)
  148. {
  149. BOOL isOK = FALSE;
  150. if(bAsyncConnect)
  151. {
  152. if(::WSAEventSelect(m_soClient, m_evSocket, FD_CONNECT | FD_CLOSE) != SOCKET_ERROR)
  153. {
  154. int rc = ::connect(m_soClient, addrRemote.Addr(), addrRemote.AddrSize());
  155. isOK = (rc == NO_ERROR || (rc == SOCKET_ERROR && ::WSAGetLastError() == WSAEWOULDBLOCK));
  156. }
  157. }
  158. else
  159. {
  160. if(::connect(m_soClient, addrRemote.Addr(), addrRemote.AddrSize()) != SOCKET_ERROR)
  161. {
  162. if(::WSAEventSelect(m_soClient, m_evSocket, FD_READ | FD_WRITE | FD_CLOSE) != SOCKET_ERROR)
  163. {
  164. SetConnected();
  165. if(TRIGGER(FireConnect()) == HR_ERROR)
  166. ::WSASetLastError(ENSURE_ERROR_CANCELLED);
  167. else
  168. isOK = TRUE;
  169. }
  170. }
  171. }
  172. return isOK;
  173. }
  174. BOOL CTcpClient::CreateWorkerThread()
  175. {
  176. m_hWorker = (HANDLE)_beginthreadex(nullptr, 0, WorkerThreadProc, (LPVOID)this, 0, &m_dwWorkerID);
  177. return m_hWorker != nullptr;
  178. }
  179. UINT WINAPI CTcpClient::WorkerThreadProc(LPVOID pv)
  180. {
  181. TRACE("---------------> Client Worker Thread 0x%08X started <---------------\n", ::GetCurrentThreadId());
  182. BOOL bCallStop = TRUE;
  183. CTcpClient* pClient = (CTcpClient*)pv;
  184. HANDLE hEvents[] = {pClient->m_evSocket, pClient->m_evBuffer, pClient->m_evWorker};
  185. pClient->m_rcBuffer.Malloc(pClient->m_dwSocketBufferSize);
  186. while(pClient->HasStarted())
  187. {
  188. DWORD retval = ::WSAWaitForMultipleEvents(3, hEvents, FALSE, WSA_INFINITE, FALSE);
  189. if(retval == WSA_WAIT_EVENT_0)
  190. {
  191. if(!pClient->ProcessNetworkEvent())
  192. break;
  193. }
  194. else if(retval == WSA_WAIT_EVENT_0 + 1)
  195. {
  196. if(!pClient->SendData())
  197. break;
  198. }
  199. else if(retval == WSA_WAIT_EVENT_0 + 2)
  200. {
  201. bCallStop = FALSE;
  202. break;
  203. }
  204. else if(retval == WSA_WAIT_FAILED)
  205. {
  206. pClient->m_ccContext.Reset(TRUE, SO_UNKNOWN, ::WSAGetLastError());
  207. break;
  208. }
  209. else
  210. VERIFY(FALSE);
  211. }
  212. pClient->OnWorkerThreadEnd(::GetCurrentThreadId());
  213. if(bCallStop && pClient->HasStarted())
  214. pClient->Stop();
  215. TRACE("---------------> Client Worker Thread 0x%08X stoped <---------------\n", ::GetCurrentThreadId());
  216. return 0;
  217. }
  218. BOOL CTcpClient::ProcessNetworkEvent()
  219. {
  220. BOOL bContinue = TRUE;
  221. WSANETWORKEVENTS events;
  222. int rc = ::WSAEnumNetworkEvents(m_soClient, m_evSocket, &events);
  223. if(rc == SOCKET_ERROR)
  224. bContinue = HandleError(events);
  225. if(!HasConnected() && bContinue && events.lNetworkEvents & FD_CONNECT)
  226. bContinue = HandleConnect(events);
  227. if(bContinue && events.lNetworkEvents & FD_READ)
  228. bContinue = HandleRead(events);
  229. if(bContinue && events.lNetworkEvents & FD_WRITE)
  230. bContinue = HandleWrite(events);
  231. if(bContinue && events.lNetworkEvents & FD_CLOSE)
  232. bContinue = HandleClose(events);
  233. return bContinue;
  234. }
  235. BOOL CTcpClient::HandleError(WSANETWORKEVENTS& events)
  236. {
  237. int iCode = ::WSAGetLastError();
  238. EnSocketOperation enOperation = SO_UNKNOWN;
  239. if(events.lNetworkEvents & FD_CONNECT)
  240. enOperation = SO_CONNECT;
  241. else if(events.lNetworkEvents & FD_CLOSE)
  242. enOperation = SO_CLOSE;
  243. else if(events.lNetworkEvents & FD_READ)
  244. enOperation = SO_RECEIVE;
  245. else if(events.lNetworkEvents & FD_WRITE)
  246. enOperation = SO_SEND;
  247. VERIFY(::WSAResetEvent(m_evSocket));
  248. m_ccContext.Reset(TRUE, enOperation, iCode);
  249. return FALSE;
  250. }
  251. BOOL CTcpClient::HandleRead(WSANETWORKEVENTS& events)
  252. {
  253. BOOL bContinue = TRUE;
  254. int iCode = events.iErrorCode[FD_READ_BIT];
  255. if(iCode == 0)
  256. bContinue = ReadData();
  257. else
  258. {
  259. m_ccContext.Reset(TRUE, SO_RECEIVE, iCode);
  260. bContinue = FALSE;
  261. }
  262. return bContinue;
  263. }
  264. BOOL CTcpClient::HandleWrite(WSANETWORKEVENTS& events)
  265. {
  266. BOOL bContinue = TRUE;
  267. int iCode = events.iErrorCode[FD_WRITE_BIT];
  268. if(iCode == 0)
  269. bContinue = SendData();
  270. else
  271. {
  272. m_ccContext.Reset(TRUE, SO_SEND, iCode);
  273. bContinue = FALSE;
  274. }
  275. return bContinue;
  276. }
  277. BOOL CTcpClient::HandleConnect(WSANETWORKEVENTS& events)
  278. {
  279. int iCode = events.iErrorCode[FD_CONNECT_BIT];
  280. if(iCode != 0)
  281. {
  282. m_ccContext.Reset(TRUE, SO_CONNECT, iCode);
  283. return FALSE;
  284. }
  285. if(::WSAEventSelect(m_soClient, m_evSocket, FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR)
  286. {
  287. m_ccContext.Reset(TRUE, SO_CONNECT, ::WSAGetLastError());
  288. return FALSE;
  289. }
  290. SetConnected();
  291. if(TRIGGER(FireConnect()) == HR_ERROR)
  292. {
  293. m_ccContext.Reset(FALSE);
  294. return FALSE;
  295. }
  296. return TRUE;
  297. }
  298. BOOL CTcpClient::HandleClose(WSANETWORKEVENTS& events)
  299. {
  300. int iCode = events.iErrorCode[FD_CLOSE_BIT];
  301. if(iCode == 0)
  302. m_ccContext.Reset(TRUE, SO_CLOSE, SE_OK);
  303. else
  304. m_ccContext.Reset(TRUE, SO_CLOSE, iCode);
  305. return FALSE;
  306. }
  307. BOOL CTcpClient::ReadData()
  308. {
  309. while(TRUE)
  310. {
  311. int rc = recv(m_soClient, (char*)(BYTE*)m_rcBuffer, m_dwSocketBufferSize, 0);
  312. if(rc > 0)
  313. {
  314. if(TRIGGER(FireReceive(m_rcBuffer, rc)) == HR_ERROR)
  315. {
  316. TRACE("<C-CNNID: %Iu> OnReceive() event return 'HR_ERROR', connection will be closed !\n", m_dwConnID);
  317. m_ccContext.Reset(TRUE, SO_RECEIVE, ENSURE_ERROR_CANCELLED);
  318. return FALSE;
  319. }
  320. }
  321. else if(rc == SOCKET_ERROR)
  322. {
  323. int code = ::WSAGetLastError();
  324. if(code == WSAEWOULDBLOCK)
  325. break;
  326. else
  327. {
  328. m_ccContext.Reset(TRUE, SO_RECEIVE, code);
  329. return FALSE;
  330. }
  331. }
  332. else if(rc == 0)
  333. {
  334. m_ccContext.Reset(TRUE, SO_CLOSE, SE_OK);
  335. return FALSE;
  336. }
  337. else
  338. ASSERT(FALSE);
  339. }
  340. return TRUE;
  341. }
  342. BOOL CTcpClient::PauseReceive(BOOL bPause)
  343. {
  344. if(!HasConnected())
  345. {
  346. ::SetLastError(ERROR_INVALID_STATE);
  347. return FALSE;
  348. }
  349. if(m_bPaused == bPause)
  350. return TRUE;
  351. if(::WSAEventSelect(m_soClient, m_evSocket, bPause ? 0 : FD_READ | FD_WRITE | FD_CLOSE) != SOCKET_ERROR)
  352. {
  353. m_bPaused = bPause;
  354. return TRUE;
  355. }
  356. return FALSE;
  357. }
  358. BOOL CTcpClient::SendData()
  359. {
  360. BOOL isOK = TRUE;
  361. while(TRUE)
  362. {
  363. TItemPtr itPtr(m_itPool, GetSendBuffer());
  364. if(itPtr.IsValid())
  365. {
  366. ASSERT(!itPtr->IsEmpty());
  367. isOK = DoSendData(itPtr);
  368. if(isOK)
  369. {
  370. if(!itPtr->IsEmpty())
  371. {
  372. CCriSecLock locallock(m_csSend);
  373. m_lsSend.PushFront(itPtr.Detach());
  374. break;
  375. }
  376. }
  377. else
  378. break;
  379. }
  380. else
  381. break;
  382. }
  383. return isOK;
  384. }
  385. TItem* CTcpClient::GetSendBuffer()
  386. {
  387. TItem* pItem = nullptr;
  388. if(m_lsSend.Size() > 0)
  389. {
  390. CCriSecLock locallock(m_csSend);
  391. if(m_lsSend.Size() > 0)
  392. pItem = m_lsSend.PopFront();
  393. }
  394. return pItem;
  395. }
  396. BOOL CTcpClient::DoSendData(TItem* pItem)
  397. {
  398. while(!pItem->IsEmpty())
  399. {
  400. int rc = 0;
  401. {
  402. CCriSecLock locallock(m_csSend);
  403. rc = send(m_soClient, (char*)pItem->Ptr(), min(pItem->Size(), (int)m_dwSocketBufferSize), 0);
  404. if(rc > 0) m_iPending -= rc;
  405. }
  406. if(rc > 0)
  407. {
  408. if(TRIGGER(FireSend(pItem->Ptr(), rc)) == HR_ERROR)
  409. {
  410. TRACE("<C-CNNID: %Iu> OnSend() event should not return 'HR_ERROR' !!\n", m_dwConnID);
  411. ASSERT(FALSE);
  412. }
  413. pItem->Reduce(rc);
  414. }
  415. else if(rc == SOCKET_ERROR)
  416. {
  417. int code = ::WSAGetLastError();
  418. if(code == WSAEWOULDBLOCK)
  419. break;
  420. else
  421. {
  422. m_ccContext.Reset(TRUE, SO_SEND, code);
  423. return FALSE;
  424. }
  425. }
  426. else
  427. ASSERT(FALSE);
  428. }
  429. return TRUE;
  430. }
  431. BOOL CTcpClient::Stop()
  432. {
  433. DWORD dwCurrentThreadID = ::GetCurrentThreadId();
  434. if(!CheckStoping(dwCurrentThreadID))
  435. return FALSE;
  436. WaitForWorkerThreadEnd(dwCurrentThreadID);
  437. if(m_ccContext.bFireOnClose)
  438. FireClose(m_ccContext.enOperation, m_ccContext.iErrorCode);
  439. if(m_evSocket != nullptr)
  440. {
  441. ::WSACloseEvent(m_evSocket);
  442. m_evSocket = nullptr;
  443. }
  444. if(m_soClient != INVALID_SOCKET)
  445. {
  446. shutdown(m_soClient, SD_SEND);
  447. closesocket(m_soClient);
  448. m_soClient = INVALID_SOCKET;
  449. }
  450. Reset();
  451. return TRUE;
  452. }
  453. void CTcpClient::Reset()
  454. {
  455. CCriSecLock locallock(m_csSend);
  456. m_rcBuffer.Free();
  457. m_evBuffer.Reset();
  458. m_evWorker.Reset();
  459. m_lsSend.Clear();
  460. m_itPool.Clear();
  461. m_strHost.Empty();
  462. m_usPort = 0;
  463. m_iPending = 0;
  464. m_bPaused = FALSE;
  465. m_bConnected= FALSE;
  466. m_enState = SS_STOPPED;
  467. }
  468. void CTcpClient::WaitForWorkerThreadEnd(DWORD dwCurrentThreadID)
  469. {
  470. if(m_hWorker != nullptr)
  471. {
  472. if(dwCurrentThreadID != m_dwWorkerID)
  473. {
  474. m_evWorker.Set();
  475. VERIFY(::WaitForSingleObject(m_hWorker, INFINITE) == WAIT_OBJECT_0);
  476. }
  477. ::CloseHandle(m_hWorker);
  478. m_hWorker = nullptr;
  479. m_dwWorkerID = 0;
  480. }
  481. }
  482. BOOL CTcpClient::Send(const BYTE* pBuffer, int iLength, int iOffset)
  483. {
  484. ASSERT(pBuffer && iLength > 0);
  485. if(iOffset != 0) pBuffer += iOffset;
  486. WSABUF buffer;
  487. buffer.len = iLength;
  488. buffer.buf = (char*)pBuffer;
  489. return SendPackets(&buffer, 1);
  490. }
  491. BOOL CTcpClient::DoSendPackets(const WSABUF pBuffers[], int iCount)
  492. {
  493. ASSERT(pBuffers && iCount > 0);
  494. int result = NO_ERROR;
  495. if(pBuffers && iCount > 0)
  496. {
  497. if(HasConnected())
  498. {
  499. CCriSecLock locallock(m_csSend);
  500. if(HasConnected())
  501. result = SendInternal(pBuffers, iCount);
  502. else
  503. result = ERROR_INVALID_STATE;
  504. }
  505. else
  506. result = ERROR_INVALID_STATE;
  507. }
  508. else
  509. result = ERROR_INVALID_PARAMETER;
  510. if(result != NO_ERROR)
  511. ::SetLastError(result);
  512. return (result == NO_ERROR);
  513. }
  514. int CTcpClient::SendInternal(const WSABUF pBuffers[], int iCount)
  515. {
  516. ASSERT(m_iPending >= 0);
  517. int iPending = m_iPending;
  518. BOOL isPending = m_iPending > 0;
  519. for(int i = 0; i < iCount; i++)
  520. {
  521. int iBufLen = pBuffers[i].len;
  522. if(iBufLen > 0)
  523. {
  524. BYTE* pBuffer = (BYTE*)pBuffers[i].buf;
  525. ASSERT(pBuffer);
  526. m_lsSend.Cat(pBuffer, iBufLen);
  527. m_iPending += iBufLen;
  528. }
  529. }
  530. if(!isPending && m_iPending > iPending) m_evBuffer.Set();
  531. return NO_ERROR;
  532. }
  533. BOOL CTcpClient::SendSmallFile(LPCTSTR lpszFileName, const LPWSABUF pHead, const LPWSABUF pTail)
  534. {
  535. CAtlFile file;
  536. CAtlFileMapping<> fmap;
  537. WSABUF szBuf[3];
  538. HRESULT hr = ::MakeSmallFilePackage(lpszFileName, file, fmap, szBuf, pHead, pTail);
  539. if(FAILED(hr))
  540. {
  541. ::SetLastError(HRESULT_CODE(hr));
  542. return FALSE;
  543. }
  544. return SendPackets(szBuf, 3);
  545. }
  546. void CTcpClient::SetLastError(EnSocketError code, LPCSTR func, int ec)
  547. {
  548. TRACE("%s --> Error: %d, EC: %d\n", func, code, ec);
  549. m_enLastError = code;
  550. ::SetLastError(ec);
  551. }
  552. BOOL CTcpClient::GetLocalAddress(TCHAR lpszAddress[], int& iAddressLen, USHORT& usPort)
  553. {
  554. ASSERT(lpszAddress != nullptr && iAddressLen > 0);
  555. return ::GetSocketLocalAddress(m_soClient, lpszAddress, iAddressLen, usPort);
  556. }
  557. void CTcpClient::SetRemoteHost(LPCTSTR lpszHost, USHORT usPort)
  558. {
  559. m_strHost = lpszHost;
  560. m_usPort = usPort;
  561. }
  562. BOOL CTcpClient::GetRemoteHost(TCHAR lpszHost[], int& iHostLen, USHORT& usPort)
  563. {
  564. BOOL isOK = FALSE;
  565. if(m_strHost.IsEmpty())
  566. return isOK;
  567. int iLen = m_strHost.GetLength() + 1;
  568. if(iHostLen >= iLen)
  569. {
  570. memcpy(lpszHost, CA2CT(m_strHost), iLen * sizeof(TCHAR));
  571. usPort = m_usPort;
  572. isOK = TRUE;
  573. }
  574. iHostLen = iLen;
  575. return isOK;
  576. }
  577. BOOL CTcpClient::GetRemoteHost(LPCSTR* lpszHost, USHORT* pusPort)
  578. {
  579. *lpszHost = m_strHost;
  580. if(pusPort != nullptr)
  581. *pusPort = m_usPort;
  582. return !m_strHost.IsEmpty();
  583. }