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.

168 lines
3.7 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: LgSocketServer.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgSocketServer.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include <Winsock2.h>
  16. #include "LgSocketServer.h"
  17. #include "..\Version.h"
  18. /*
  19. template<class BASE_SOCKET>
  20. CLgSocketServer<BASE_SOCKET>::CLgSocketServer()
  21. {
  22. m_hConnectionEvent = NULL;
  23. }
  24. template<class BASE_SOCKET>
  25. CLgSocketServer<BASE_SOCKET>::~CLgSocketServer()
  26. {
  27. Destroy();
  28. }
  29. // Do not edit the following lines, which are needed by ClassWizard.
  30. #if 0
  31. BEGIN_MESSAGE_MAP(CLgSocketServer, CSocket)
  32. //{{AFX_MSG_MAP(CLgSocketServer)
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. #endif // 0
  36. template<class BASE_SOCKET>
  37. BOOL CLgSocketServer<BASE_SOCKET>::Create( UINT nHostPort )
  38. {
  39. // create socket
  40. if( !CLgSocket::Create( nHostPort ) )
  41. return FALSE;
  42. // run sockets listening
  43. if( !CLgSocket::Listen() )
  44. return FALSE;
  45. // create event
  46. m_hConnectionEvent = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  47. if( !m_hConnectionEvent )
  48. return FALSE;
  49. return TRUE;
  50. }
  51. template<class BASE_SOCKET>
  52. void CLgSocketServer<BASE_SOCKET>::Destroy()
  53. {
  54. // close socket
  55. Close();
  56. // destroy all incoming sockets
  57. CLgLocker locker( &m_SocketArray );
  58. for( INT i = 0; i < m_SocketArray.GetCount(); i++ )
  59. delete m_SocketArray[i];
  60. m_SocketArray.RemoveAll();
  61. // close notification event
  62. if( m_hConnectionEvent )
  63. {
  64. ::CloseHandle( m_hConnectionEvent );
  65. m_hConnectionEvent = NULL;
  66. }
  67. }
  68. template<class BASE_SOCKET>
  69. void CLgSocketServer<BASE_SOCKET>::OnAccept( int nErrorCode )
  70. {
  71. CLgSocketServerConnection<BASE_SOCKET> *pSocket = OnCreateConnection();
  72. if( !pSocket )
  73. return;
  74. if( !Accept( *pSocket ) )
  75. {
  76. delete pSocket;
  77. return;
  78. }
  79. CLgLocker locker( &m_SocketArray );
  80. m_SocketArray.Add( pSocket );
  81. BASE_SOCKET::OnAccept(nErrorCode);
  82. }
  83. template<class BASE_SOCKET>
  84. BOOL CLgSocketServer<BASE_SOCKET>::DeleteConnection( CLgSocketServerConnection<BASE_SOCKET>* pConnection )
  85. {
  86. if( !pConnection )
  87. return FALSE;
  88. pConnection->Close();
  89. return ::SetEvent( m_hConnectionEvent );
  90. }
  91. template<class BASE_SOCKET>
  92. void CLgSocketServer<BASE_SOCKET>::DoDeleteConnection()
  93. {
  94. CLgLocker locker( &m_SocketArray );
  95. for( INT i = 0; i < m_SocketArray.GetCount(); )
  96. {
  97. if( m_SocketArray[i]->GetSocketHandle() == INVALID_SOCKET )
  98. {
  99. OnDeleteConnection( m_SocketArray[i] );
  100. m_SocketArray.Remove( i );
  101. }
  102. else
  103. i++;
  104. }
  105. }
  106. template<class BASE_SOCKET>
  107. DWORD CLgSocketServer<BASE_SOCKET>::ThreadProc()
  108. {
  109. HANDLE hEvents[] = { CLgThread::GetThreadStopEvent(), BASE_SOCKET::GetSocketEventHandle(), m_hConnectionEvent };
  110. while( TRUE )
  111. {
  112. // wait for events
  113. switch( ::WaitForMultipleObjects( 3, hEvents, FALSE, INFINITE ) )
  114. {
  115. default:
  116. case WAIT_OBJECT_0:
  117. goto EXIT_THREAD;
  118. case WAIT_OBJECT_0+1:
  119. // handle events
  120. DoHandleEvents();
  121. continue;
  122. case WAIT_OBJECT_0+2:
  123. // handle connection closure
  124. DoDeleteConnection();
  125. continue;
  126. }
  127. }
  128. EXIT_THREAD:
  129. return 0UL;
  130. }
  131. template<class BASE_SOCKET>
  132. CLgSocketServerConnection<BASE_SOCKET>* CLgSocketServer<BASE_SOCKET>::OnCreateConnection()
  133. {
  134. return new CLgSocketServerConnection<BASE_SOCKET>( this );
  135. }
  136. template<class BASE_SOCKET>
  137. void CLgSocketServer<BASE_SOCKET>::OnDeleteConnection( CLgSocketServerConnection<BASE_SOCKET>* pConnection )
  138. {
  139. delete pConnection;
  140. }
  141. */