Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.1 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgRemoteClientTcpIp.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgRemoteClientTcpIp.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\Version.h"
  16. #include "LgRemoteClientTcpIp.h"
  17. CLgRemoteClientTcpIp::CLgRemoteClientTcpIp()
  18. {
  19. m_pHostAddress[0] = 0;
  20. m_nHostPort = 0;
  21. m_eConnectionType = LG_REMOTE_E_CONNECT_TYPE_SLAVE;
  22. m_eSessionType = LG_REMOTE_E_SESSION_TYPE_UNKNOWN;
  23. m_nSessionID = -1;
  24. m_nConnectionID = -1;
  25. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteClientTcpIp::CLgRemoteClientTcpIp" );
  26. }
  27. CLgRemoteClientTcpIp::~CLgRemoteClientTcpIp()
  28. {
  29. Destroy();
  30. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteClientTcpIp::~CLgRemoteClientTcpIp" );
  31. }
  32. BOOL CLgRemoteClientTcpIp::Create( LPCTSTR lpszHostAddress, UINT nHostPort, BYTE eSessionType, BYTE eConnectionType )
  33. {
  34. TCHAR pLocalHostAddress[MAX_PATH];
  35. // use current machine adress
  36. if( !lpszHostAddress )
  37. {
  38. // repoint to another buffer
  39. lpszHostAddress = pLocalHostAddress;
  40. // get local address
  41. if( gethostname( (PCHAR)lpszHostAddress, MAX_PATH ) )
  42. {
  43. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::Create"), _T("gethostname failed") );
  44. return FALSE;
  45. }
  46. }
  47. // store properties
  48. m_eConnectionType = eConnectionType;
  49. m_eSessionType = eSessionType;
  50. _tcscpy( m_pHostAddress, lpszHostAddress );
  51. m_nHostPort = nHostPort;
  52. if( !CLgSocketClient<CLgRemoteSocket>::Create( lpszHostAddress, nHostPort ) )
  53. {
  54. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::Create"), _T("CLgSocketClient<CLgRemoteSocket>::Create failed") );
  55. return FALSE;
  56. }
  57. switch( eConnectionType )
  58. {
  59. case LG_REMOTE_E_CONNECT_TYPE_MASTER:
  60. // send connect packet
  61. if( !SendServerConnect( m_eSessionType, m_nSessionID, m_eConnectionType, m_nConnectionID ) )
  62. {
  63. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::Create"), _T("SendServerConnect failed") );
  64. }
  65. break;
  66. case LG_REMOTE_E_CONNECT_TYPE_SLAVE:
  67. // send connect packet
  68. if( !SendServerSessionList( m_eSessionType, NULL, 0 ) )
  69. {
  70. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::Create"), _T("SendServerSessionList failed") );
  71. }
  72. break;
  73. }
  74. return TRUE;
  75. }
  76. void CLgRemoteClientTcpIp::Destroy()
  77. {
  78. // unlink interface
  79. CLgRemoteInterfaceInfo::Unlink();
  80. // destroy socket
  81. CLgSocketClient<CLgRemoteSocket>::Destroy();
  82. m_pHostAddress[0] = 0;
  83. m_nHostPort = 0;
  84. }
  85. void CLgRemoteClientTcpIp::OnConnect( int nErrorCode )
  86. {
  87. if( nErrorCode )
  88. {
  89. // perform reconnect
  90. if( !CLgSocketClient<CLgRemoteSocket>::Create( m_pHostAddress, m_nHostPort ) )
  91. {
  92. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnConnect"), _T("CLgSocketClient<CLgRemoteSocket>::Create failed") );
  93. }
  94. CLgSocketClient<CLgRemoteSocket>::OnConnect( nErrorCode );
  95. return;
  96. }
  97. switch( m_eConnectionType )
  98. {
  99. case LG_REMOTE_E_CONNECT_TYPE_MASTER:
  100. // send connect packet
  101. if( !SendServerConnect( m_eSessionType, m_nSessionID, m_eConnectionType, m_nConnectionID ) )
  102. {
  103. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnConnect"), _T("SendServerConnect failed") );
  104. }
  105. break;
  106. case LG_REMOTE_E_CONNECT_TYPE_SLAVE:
  107. // send connect packet
  108. if( !SendServerSessionList( m_eSessionType, NULL, 0 ) )
  109. {
  110. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnConnect"), _T("SendServerSessionList failed") );
  111. }
  112. break;
  113. }
  114. // call base class handler
  115. CLgSocketClient<CLgRemoteSocket>::OnConnect( nErrorCode );
  116. }
  117. void CLgRemoteClientTcpIp::OnClose( int nErrorCode )
  118. {
  119. // perform reconnect
  120. CLgSocketClient<CLgRemoteSocket>::Create( m_pHostAddress, m_nHostPort );
  121. CLgSocketClient<CLgRemoteSocket>::OnClose( nErrorCode );
  122. OnRemoteInfoConnect( FALSE );
  123. }
  124. void CLgRemoteClientTcpIp::OnPacketConnect( PLG_TCP_PACKET_CONNECT pReceivePacket )
  125. {
  126. m_nSessionID = pReceivePacket->nSessionID;
  127. m_nConnectionID = pReceivePacket->nConnectionID;
  128. // notify interface about connection
  129. if( !OnRemoteInfoConnect( m_nSessionID != -1 && m_nConnectionID != -1 ) )
  130. {
  131. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnPacketConnect"), _T("OnRemoteInfoConnect failed") );
  132. }
  133. }
  134. void CLgRemoteClientTcpIp::OnPacketSessionInfo( PLG_TCP_PACKET_SESSION_INFO pReceivePacket )
  135. {
  136. // error situation
  137. if( pReceivePacket->eSessionType != m_eSessionType ||
  138. // no active sessions
  139. pReceivePacket->nLength <= sizeof(LG_TCP_PACKET_SESSION_INFO) )
  140. {
  141. // perform 1 second pause
  142. ::WaitForSingleObject( CLgThread::GetThreadStopEvent(), 1000UL );
  143. // make aditional request
  144. if( !SendServerSessionList( m_eSessionType, NULL, 0 ) )
  145. {
  146. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnPacketSessionInfo"), _T("SendServerSessionList failed") );
  147. }
  148. return;
  149. }
  150. LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA pSessionInfoData = (LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA)&pReceivePacket[1];
  151. // send connect packet
  152. if( !SendServerConnect( m_eSessionType, pSessionInfoData->nSessionID, m_eConnectionType, m_nConnectionID ) )
  153. {
  154. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::OnPacketSessionInfo"), _T("SendServerConnect failed") );
  155. }
  156. }
  157. BOOL CLgRemoteClientTcpIp::DoRemoteInfoTransfer()
  158. {
  159. LARGE_INTEGER liRX, liTX;
  160. if( !CLgRemoteSocket::GetTransferInfo( &liTX, &liRX ) )
  161. {
  162. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::DoRemoteInfoTransfer"), _T("CLgRemoteSocket::GetTransferInfo failed") );
  163. return FALSE;
  164. }
  165. if( !OnRemoteInfoTransfer( &liTX, &liRX ) )
  166. {
  167. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteClientTcpIp::DoRemoteInfoTransfer"), _T("OnRemoteInfoTransfer failed") );
  168. }
  169. return TRUE;
  170. }