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.

201 lines
4.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: 10/11/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgSocketServer.h
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of LgSocketServer.h
  13. //----------------------------------------------------------------------------
  14. #pragma warning( disable : 4786 )
  15. #ifndef __LG__SOCKET__SERVER__H__INCLUDED__
  16. #define __LG__SOCKET__SERVER__H__INCLUDED__
  17. #if _MSC_VER > 1000
  18. #pragma once
  19. #endif
  20. #include "..\..\LgLib\Thread.h"
  21. #include "..\..\LgLib\Templ.h"
  22. #include "..\..\LgLib\LgDebug.h"
  23. #include "LgSocket.h"
  24. #include "LgSocketServerConnection.h"
  25. template<class BASE_SOCKET>
  26. class CLgSocketServer :
  27. public BASE_SOCKET
  28. {
  29. public:
  30. CLgSocketServer() { m_hConnectionEvent = NULL; }
  31. virtual ~CLgSocketServer() { Destroy(); }
  32. public:
  33. BOOL Create( UINT nHostPort )
  34. {
  35. // create event
  36. m_hConnectionEvent = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  37. if( !m_hConnectionEvent )
  38. return FALSE;
  39. // create socket
  40. if( !BASE_SOCKET::Create( nHostPort ) )
  41. return FALSE;
  42. // run sockets listening
  43. if( !BASE_SOCKET::Listen() )
  44. return FALSE;
  45. return TRUE;
  46. }
  47. virtual void Destroy()
  48. {
  49. // close socket
  50. Close();
  51. // destroy all incoming sockets
  52. CLgLocker locker( &m_SocketArray );
  53. for( INT i = 0; i < m_SocketArray.GetCount(); i++ )
  54. delete m_SocketArray[i];
  55. m_SocketArray.RemoveAll();
  56. // close notification event
  57. if( m_hConnectionEvent )
  58. {
  59. ::CloseHandle( m_hConnectionEvent );
  60. m_hConnectionEvent = NULL;
  61. }
  62. }
  63. public:
  64. //{{AFX_VIRTUAL(CLgSocketServer)
  65. public:
  66. virtual void OnAccept(int nErrorCode)
  67. {
  68. CLgSocketServerConnection<BASE_SOCKET> *pSocket = OnCreateConnection();
  69. if( !pSocket )
  70. return;
  71. SOCKADDR_IN SockAddr;
  72. INT SockAddrLen = sizeof(SockAddr);
  73. if( !Accept( *pSocket, (SOCKADDR*)&SockAddr, &SockAddrLen ) )
  74. {
  75. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgSocketServer<BASE_SOCKET>::OnAccept"), _T("Accept failed") );
  76. delete pSocket;
  77. return;
  78. }
  79. CLgLocker locker( &m_SocketArray );
  80. m_SocketArray.Add( pSocket );
  81. BASE_SOCKET::OnAccept(nErrorCode);
  82. pSocket->OnConnect( &SockAddr );
  83. }
  84. //}}AFX_VIRTUAL
  85. //{{AFX_MSG(CLgSocketServer)
  86. //}}AFX_MSG
  87. protected:
  88. virtual void OnDeleteConnection( CLgSocketServerConnection<BASE_SOCKET>* pConnection )
  89. {
  90. delete pConnection;
  91. }
  92. virtual CLgSocketServerConnection<BASE_SOCKET>* OnCreateConnection()
  93. {
  94. return new CLgSocketServerConnection<BASE_SOCKET>( this );
  95. }
  96. private:
  97. friend class CLgSocketServerConnection<BASE_SOCKET>;
  98. BOOL OnCloseConnection( CLgSocketServerConnection<BASE_SOCKET>* pConnection )
  99. {
  100. if( !pConnection )
  101. return FALSE;
  102. // do not call CLgSocketServerConnection<BASE_SOCKET>::Close() here! it will cause deadlock
  103. pConnection->DeletePending();
  104. return ::SetEvent( m_hConnectionEvent );
  105. }
  106. private:
  107. virtual DWORD 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. void DoDeleteConnection()
  132. {
  133. CLgLocker locker( &m_SocketArray );
  134. for( INT i = 0; i < m_SocketArray.GetCount(); )
  135. {
  136. if( m_SocketArray[i]->GetSocketHandle() == INVALID_SOCKET ||
  137. m_SocketArray[i]->IsDeletePending() )
  138. {
  139. OnDeleteConnection( m_SocketArray[i] );
  140. m_SocketArray.Remove( i );
  141. }
  142. else
  143. i++;
  144. }
  145. }
  146. private:
  147. HANDLE m_hConnectionEvent;
  148. protected:
  149. typedef CLgArrayP<CLgSocketServerConnection<BASE_SOCKET>*> CSSConnectionArray;
  150. CSSConnectionArray& GetConnectionArray() { return m_SocketArray; }
  151. private:
  152. CSSConnectionArray m_SocketArray;
  153. };
  154. // Do not edit the following lines, which are needed by ClassWizard.
  155. #if 0
  156. BEGIN_MESSAGE_MAP(CLgSocketServer, CSocket)
  157. //{{AFX_MSG_MAP(CLgSocketServer)
  158. //}}AFX_MSG_MAP
  159. END_MESSAGE_MAP()
  160. #endif // 0
  161. //{{AFX_INSERT_LOCATION}}
  162. #endif
  163. #pragma warning( default : 4786 )