TCPClient.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "stdafx.h"
  2. #include "kernel.h"
  3. #include "ThreadPool.hpp"
  4. #include <strsafe.h>
  5. #include "TCPClient.h"
  6. #include "Client2SrvType.h"
  7. const int SOCK_TCP = SOCK_STREAM-1;
  8. const int SOCK_UDP = SOCK_DGRAM-1;
  9. const int AF_IPV4 = 0;
  10. const int AF_IPV6 = 1;
  11. CTCPClient* CTCPClient::m_pTcpClient[TCPCLIENTNUM] = {NULL};
  12. CTCPClient::CTCPClient()
  13. {
  14. m_nMode = AF_IPV4;
  15. m_nSockType = SOCK_TCP;
  16. m_SocketClient.SetInterface(this);
  17. m_bSocket = FALSE;
  18. }
  19. CTCPClient::~CTCPClient()
  20. {
  21. }
  22. void CTCPClient::Release()
  23. {
  24. //chn add
  25. for( int i=0;i<TCPCLIENTNUM;i++ )
  26. {
  27. if( m_pTcpClient[i])
  28. delete m_pTcpClient[i];
  29. m_pTcpClient[i] = NULL;
  30. }
  31. }
  32. // 静态成员函数,提供全局访问的接口
  33. CTCPClient* CTCPClient::GetInstancePtr( int iTCPIndex )
  34. {
  35. if( NULL == m_pTcpClient[iTCPIndex] )
  36. {
  37. m_pTcpClient[iTCPIndex] = new CTCPClient();
  38. }
  39. return m_pTcpClient[iTCPIndex];
  40. }
  41. // 连接服务器;
  42. BOOL CTCPClient::Connection(LPCTSTR strAddr, LPCTSTR strPort)
  43. {
  44. int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
  45. // 客户端开始连接服务器线程;
  46. if ( !m_SocketClient.StartClient(NULL, strAddr, strPort, nFamily, (m_nSockType+1) ) )
  47. {
  48. m_bSocket = FALSE;
  49. //MessageBox(NULL, _T("连接服务器失败!"), "提示", MB_ICONSTOP);
  50. return FALSE;
  51. }
  52. else
  53. {
  54. m_bSocket = TRUE;
  55. CSocketHandle* pSH = (CSocketHandle *)m_SocketClient;
  56. pSH->m_nPendingSize = 0;
  57. memset(pSH->m_PendingBuffer, 0, SOCKET_BUFFSIZE);
  58. SetupMCAST();
  59. return TRUE;
  60. }
  61. }
  62. void CTCPClient::DisConnection()
  63. {
  64. m_SocketClient.Terminate();
  65. }
  66. bool CTCPClient::SetupMCAST()
  67. {
  68. const TCHAR szIPv4MCAST[] = TEXT("239.121.1.2");
  69. const TCHAR szIPv6MCAST[] = TEXT("FF02:0:0:0:0:0:0:1"); // All Nodes local address
  70. bool result = false;
  71. if ( m_nSockType == SOCK_UDP )
  72. {
  73. if ( m_nMode == AF_IPV4 ) {
  74. result = m_SocketClient->AddMembership(szIPv4MCAST, NULL);
  75. } else {
  76. result = m_SocketClient->AddMembership(szIPv6MCAST, NULL);
  77. HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
  78. hr = hr;
  79. }
  80. }
  81. return result;
  82. }
  83. void CTCPClient::GetAddress(const SockAddrIn& addrIn, CString& rString) const
  84. {
  85. TCHAR szIPAddr[MAX_PATH] = { 0 };
  86. CSocketHandle::FormatIP(szIPAddr, MAX_PATH, addrIn);
  87. rString.Format(_T("%s : %d"), szIPAddr, static_cast<int>(static_cast<UINT>(ntohs(addrIn.GetPort()))) );
  88. }
  89. ///////////////////////////////////////////////////////////////////////////////
  90. // 实现ISocketClientHandler的通信方法
  91. void CTCPClient::OnThreadBegin(CSocketHandle* pSH)
  92. {
  93. ASSERT( pSH == m_SocketClient );
  94. (pSH);
  95. CString strAddr;
  96. SockAddrIn sockAddr;
  97. m_SocketClient->GetSockName(sockAddr);
  98. GetAddress( sockAddr, strAddr );
  99. InitializeCriticalSection(&pSH->m_hClient2SrvSection);
  100. //AppendText( _T("Client Running on: %s\r\n"), strAddr);
  101. }
  102. void CTCPClient::OnThreadExit(CSocketHandle* pSH)
  103. {
  104. ASSERT( pSH == m_SocketClient );
  105. DeleteCriticalSection( &pSH->m_hClient2SrvSection );
  106. (pSH);
  107. }
  108. void CTCPClient::OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr)
  109. {
  110. ASSERT( pSH == m_SocketClient );
  111. (pSH);
  112. if( !m_SocketClient->IsOpen() ) return;
  113. m_TcpParse.ProcessData( pSH, pbData, dwCount );
  114. }
  115. void CTCPClient::OnConnectionDropped(CSocketHandle* pSH)
  116. {
  117. ASSERT( pSH == m_SocketClient );
  118. (pSH);
  119. TRACE(_T("======连接服务器断开.\r\n"));
  120. m_bSocket = FALSE;
  121. //AppendText( _T("Connection lost with server. Need restart.\r\n") );
  122. }
  123. void CTCPClient::OnConnectionError(CSocketHandle* pSH, DWORD dwError)
  124. {
  125. ASSERT( pSH == m_SocketClient );
  126. (pSH);
  127. _com_error err(dwError);
  128. m_bSocket = FALSE;
  129. //AppendText( _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() );
  130. }
  131. BOOL CTCPClient::SendNoticeToServer( int iCmd,int iOperateType,CString sUserName,CString sUID,int iVarID )
  132. {
  133. if( m_bSocket )
  134. return m_TcpParse.SendNoticeToServer( m_SocketClient,iCmd,iOperateType,sUserName,sUID,iVarID );
  135. else
  136. return FALSE;
  137. }
  138. BOOL CTCPClient::GetDogInfo()
  139. {
  140. if( m_bSocket )
  141. return m_TcpParse.GetDogInfo( m_SocketClient );
  142. else
  143. return FALSE;
  144. }
  145. BOOL CTCPClient::TestAlarm()
  146. {
  147. if( m_bSocket )
  148. return m_TcpParse.TestAlarm( m_SocketClient );
  149. else
  150. return FALSE;
  151. }
  152. BOOL CTCPClient::ConfigAlarm( CString sUID,int iVarID,int iStatus,int iAlarmIndex )
  153. {
  154. if( m_bSocket )
  155. return m_TcpParse.ConfigAlarm( m_SocketClient,sUID,iVarID,iStatus,iAlarmIndex );
  156. else
  157. return FALSE;
  158. }