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.

199 lines
6.3 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: LgRemoteServerTcpIp.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgRemoteServerTcpIp.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\Version.h"
  16. #include "LgRemoteServerTcpIp.h"
  17. #include "LgRemoteServerTcpIpConnection.h"
  18. #include "LgRemoteServerTcpIpSession.h"
  19. #include "..\..\LgLib\LgImageJPEG.h"
  20. CLgRemoteServerTcpIp::CLgRemoteServerTcpIp()
  21. {
  22. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteServerTcpIp::CLgRemoteServerTcpIp" );
  23. }
  24. CLgRemoteServerTcpIp::~CLgRemoteServerTcpIp()
  25. {
  26. Destroy();
  27. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteServerTcpIp::~CLgRemoteServerTcpIp" );
  28. }
  29. void CLgRemoteServerTcpIp::Destroy()
  30. {
  31. CLgSocketServer<CLgRemoteSocket>::Destroy();
  32. }
  33. CLgSocketServerConnection<CLgRemoteSocket>* CLgRemoteServerTcpIp::OnCreateConnection()
  34. {
  35. // create new connection
  36. CLgRemoteServerTcpIpConnection *pConnection = new CLgRemoteServerTcpIpConnection( this );
  37. if( !pConnection )
  38. {
  39. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::OnCreateConnection"), _T("new CLgRemoteServerTcpIpConnection failed") );
  40. return NULL;
  41. }
  42. // place into pending connection list
  43. if( !m_ConnectionArray.Add( pConnection ) )
  44. {
  45. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::OnCreateConnection"), _T("m_ConnectionArray.Add failed") );
  46. }
  47. return pConnection;
  48. }
  49. void CLgRemoteServerTcpIp::OnDeleteConnection( CLgSocketServerConnection<CLgRemoteSocket>* pConnection )
  50. {
  51. if( !pConnection )
  52. {
  53. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::OnDeleteConnection"), _T("pConnection is NULL") );
  54. return;
  55. }
  56. // cast to valid type
  57. CLgRemoteServerTcpIpConnection *pRemoteConnection = (CLgRemoteServerTcpIpConnection*)pConnection;
  58. // remove from pending connection list (if any)
  59. m_ConnectionArray.Remove( m_ConnectionArray.Find( pRemoteConnection ) );
  60. // delete connection
  61. delete pRemoteConnection;
  62. }
  63. BOOL CLgRemoteServerTcpIp::AssignSession( CLgRemoteServerTcpIpConnection* pConnection, PINT pSessionID, BYTE eConnectionType, BYTE eSessionType )
  64. {
  65. CLgLocker Lock( &m_SessionArray );
  66. // try to assign to existing sessions
  67. for( INT i = 0; i < m_SessionArray.GetCount(); i++ )
  68. if( m_SessionArray[i]->GetSessionID() == *pSessionID )
  69. return m_SessionArray[i]->AddConnection( pConnection, eConnectionType );
  70. // if session is not new one
  71. if( *pSessionID != -1 )
  72. return FALSE;
  73. if( eConnectionType == LG_REMOTE_E_CONNECT_TYPE_MASTER )
  74. {
  75. static INT gSessionID = 0;
  76. // allocate new session
  77. CLgRemoteServerTcpIpSession *pSession = new CLgRemoteServerTcpIpSession( gSessionID, eSessionType );
  78. if( !pSession )
  79. {
  80. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("new CLgRemoteServerTcpIpSession failed") );
  81. return FALSE;
  82. }
  83. // add master connection into session and place session into session list
  84. if( !pSession->AddConnection( pConnection, eConnectionType ) ||
  85. !m_SessionArray.Add( pSession ) )
  86. {
  87. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("pSession->AddConnection or m_SessionArray.Add failed") );
  88. delete pSession;
  89. return FALSE;
  90. }
  91. // store id of new session;
  92. *pSessionID = gSessionID;
  93. // increase session id
  94. gSessionID++;
  95. return TRUE;
  96. }
  97. else
  98. {
  99. // if no master sessions
  100. if( !m_SessionArray.GetCount() )
  101. {
  102. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("There are no any master sessions") );
  103. return FALSE;
  104. }
  105. // default behaviour: taking first one
  106. CLgRemoteServerTcpIpSession *pSession = NULL;
  107. // find appropriate session
  108. CLgLocker Lock( &m_SessionArray );
  109. for( INT i = 0; i < m_SessionArray.GetCount(); i++ )
  110. {
  111. CLgRemoteServerTcpIpSession *pTempSession = m_SessionArray[i];
  112. if( pTempSession->GetSessionType() == eSessionType &&
  113. pTempSession->GetSessionID() == *pSessionID )
  114. {
  115. pSession = pTempSession;
  116. break;
  117. }
  118. }
  119. if( !pSession )
  120. {
  121. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("Can't find appropriate session") );
  122. return FALSE;
  123. }
  124. // add master connection into session and place session into session list
  125. if( !pSession->AddConnection( pConnection, eConnectionType ) )
  126. {
  127. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::AssignSession"), _T("pSession->AddConnection failed") );
  128. return FALSE;
  129. }
  130. // store id of new session;
  131. *pSessionID = pSession->GetSessionID();
  132. return TRUE;
  133. }
  134. return FALSE;
  135. }
  136. BOOL CLgRemoteServerTcpIp::GetSessionList( BYTE eSessionType, LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA* ppSessionInfo, PINT pSessionInfoSize )
  137. {
  138. if( !ppSessionInfo || !pSessionInfoSize )
  139. {
  140. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIp::GetSessionList"), _T("Parameters are wrong (%p,%p)"), ppSessionInfo, pSessionInfoSize );
  141. return FALSE;
  142. }
  143. CLgLocker Lock( &m_SessionArray );
  144. LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA pSessionInfo = NULL;
  145. INT nSessionInfoSize = 0;
  146. // prepare list
  147. for( INT i = 0; i < m_SessionArray.GetCount(); i++ )
  148. {
  149. CLgRemoteServerTcpIpSession *pSession = m_SessionArray[i];
  150. if( pSession->GetSessionType() == eSessionType )
  151. {
  152. // allocate memory for additional info
  153. pSessionInfo = (LG_TCP_PACKET_SESSION_INFO::PSESSION_INFO_DATA) realloc( pSessionInfo, sizeof(LG_TCP_PACKET_SESSION_INFO::SESSION_INFO_DATA) * (1 + nSessionInfoSize) );
  154. if( pSessionInfo )
  155. {
  156. pSessionInfo[nSessionInfoSize].nSessionID = pSession->GetSessionID();
  157. pSessionInfo[nSessionInfoSize].nLength = sizeof(LG_TCP_PACKET_SESSION_INFO::SESSION_INFO_DATA);
  158. nSessionInfoSize ++;
  159. }
  160. }
  161. }
  162. // return parameters
  163. *ppSessionInfo = pSessionInfo;
  164. *pSessionInfoSize = nSessionInfoSize * sizeof(LG_TCP_PACKET_SESSION_INFO::SESSION_INFO_DATA);
  165. return TRUE;
  166. }