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.

175 lines
5.5 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: LgRemoteServerTcpIpSession.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgRemoteServerTcpIpSession.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\Version.h"
  16. #include "LgRemoteServerTcpIpSession.h"
  17. CLgRemoteServerTcpIpSession::CLgRemoteServerTcpIpSession( INT nSessionID, BYTE eSessionType ) :
  18. m_nSessionID( nSessionID ),
  19. m_eSessionType( eSessionType )
  20. {
  21. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteServerTcpIpSession::CLgRemoteServerTcpIpSession" );
  22. }
  23. CLgRemoteServerTcpIpSession::~CLgRemoteServerTcpIpSession()
  24. {
  25. SessionToConnectionMasterLinkP::Unlink();
  26. SessionToConnectionSlaveLinkP::Unlink();
  27. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgRemoteServerTcpIpSession::~CLgRemoteServerTcpIpSession" );
  28. }
  29. BOOL CLgRemoteServerTcpIpSession::AddConnection( CLgRemoteServerTcpIpConnection* pConnection, BYTE eConnectionType )
  30. {
  31. if( !pConnection )
  32. return FALSE;
  33. switch( eConnectionType )
  34. {
  35. case LG_REMOTE_E_CONNECT_TYPE_MASTER:
  36. return SessionToConnectionMasterLinkP::Link( pConnection );
  37. case LG_REMOTE_E_CONNECT_TYPE_SLAVE:
  38. return SessionToConnectionSlaveLinkP::Link( pConnection );
  39. }
  40. return FALSE;
  41. }
  42. BOOL CLgRemoteServerTcpIpSession::RemoveConnection( CLgRemoteServerTcpIpConnection* pConnection )
  43. {
  44. if( !pConnection )
  45. return FALSE;
  46. if( pConnection->IsMaster() )
  47. {
  48. if( SessionToConnectionMasterLinkP::Unlink( pConnection ) )
  49. return TRUE;
  50. }
  51. else
  52. {
  53. if( SessionToConnectionSlaveLinkP::Unlink( pConnection ) )
  54. return TRUE;
  55. }
  56. return FALSE;
  57. }
  58. BOOL CLgRemoteServerTcpIpSession::Dispatch( CLgRemoteServerTcpIpConnection* pFromConnection, PLG_TCP_PACKET pPacket )
  59. {
  60. if( !pFromConnection )
  61. return FALSE;
  62. if( pFromConnection->IsMaster() )
  63. {
  64. // protect link
  65. CLgLocker Lock( &SessionToConnectionSlaveLinkP::GetLock() );
  66. // dispatch packet
  67. switch( pPacket->bType )
  68. {
  69. case LG_TCP_PACKET::TYPE_VIDEO:
  70. {
  71. INT nConnectionID = ((PLG_TCP_PACKET_VIDEO)pPacket)->nConnectionID;
  72. // find and send into exact connection
  73. BOOL bFound = FALSE;
  74. for( INT i = 0; i < SessionToConnectionSlaveLinkP::GetCount(); i++ )
  75. {
  76. CLgRemoteServerTcpIpConnection *pToConnection = *SessionToConnectionSlaveLinkP::GetAt(i);
  77. if( pToConnection->GetConnectionID() == nConnectionID )
  78. {
  79. if( pToConnection->Send( pPacket, pPacket->nLength ) == -1 )
  80. {
  81. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("pToConnection->Send 1 failed") );
  82. }
  83. // mark that connection found
  84. bFound = TRUE;
  85. break;
  86. }
  87. }
  88. if( !bFound )
  89. {
  90. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("Connection %d was not found for LG_TCP_PACKET::TYPE_VIDEO"), nConnectionID );
  91. }
  92. }
  93. break;
  94. case LG_TCP_PACKET::TYPE_REPORT:
  95. {
  96. INT nConnectionID = ((PLG_TCP_PACKET_REPORT)pPacket)->nConnectionID;
  97. // find and send into exact connection
  98. BOOL bFound = FALSE;
  99. for( INT i = 0; i < SessionToConnectionSlaveLinkP::GetCount(); i++ )
  100. {
  101. CLgRemoteServerTcpIpConnection *pToConnection = *SessionToConnectionSlaveLinkP::GetAt(i);
  102. if( pToConnection->GetConnectionID() == nConnectionID )
  103. {
  104. if( pToConnection->Send( pPacket, pPacket->nLength ) == -1 )
  105. {
  106. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("pToConnection->Send 2 failed") );
  107. }
  108. // mark that connection found
  109. bFound = TRUE;
  110. break;
  111. }
  112. }
  113. if( !bFound )
  114. {
  115. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("Connection %d was not found for LG_TCP_PACKET::TYPE_REPORT"), nConnectionID );
  116. }
  117. }
  118. break;
  119. default:
  120. // broadcast
  121. {
  122. for( INT i = 0; i < SessionToConnectionSlaveLinkP::GetCount(); i++ )
  123. {
  124. CLgRemoteServerTcpIpConnection *pToConnection = *SessionToConnectionSlaveLinkP::GetAt(i);
  125. if( pToConnection->Send( pPacket, pPacket->nLength ) == -1 )
  126. {
  127. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("pToConnection->Send 3 failed") );
  128. }
  129. }
  130. }
  131. break;
  132. }
  133. }
  134. else
  135. {
  136. // protect link
  137. CLgLocker Lock( &SessionToConnectionMasterLinkP::GetLock() );
  138. // dispatch into master connection only
  139. if( !SessionToConnectionMasterLinkP::GetCount() )
  140. return FALSE;
  141. // substitute connection id
  142. switch( pPacket->bType )
  143. {
  144. case LG_TCP_PACKET::TYPE_VIDEO: ((PLG_TCP_PACKET_VIDEO)pPacket)->nConnectionID = pFromConnection->GetConnectionID(); break;
  145. case LG_TCP_PACKET::TYPE_REPORT:((PLG_TCP_PACKET_REPORT)pPacket)->nConnectionID = pFromConnection->GetConnectionID(); break;
  146. }
  147. // master connection pointer
  148. CLgRemoteServerTcpIpConnection *pToConnection = *SessionToConnectionMasterLinkP::GetAt(0);
  149. if( pToConnection->Send( pPacket, pPacket->nLength ) == -1 )
  150. {
  151. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgRemoteServerTcpIpSession::Dispatch"), _T("pToConnection->Send 4 failed") );
  152. }
  153. }
  154. return TRUE;
  155. }