AsyncSocketServerImpl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #ifndef ASYNCSOCKETSERVERIMPL_H
  2. #define ASYNCSOCKETSERVERIMPL_H
  3. #pragma once
  4. #pragma warning(push)
  5. #pragma warning(disable:4995)
  6. #include <vector>
  7. #include <list>
  8. #include <algorithm>
  9. #pragma warning(pop)
  10. #include "CritSection.h"
  11. #include "SocketHandle.h"
  12. /**
  13. * SocketIOBuffer
  14. * Overlapped I/O buffer
  15. */
  16. class SocketIOBuffer
  17. {
  18. public:
  19. SocketIOBuffer(SOCKET sock)
  20. : _socket(sock)
  21. {
  22. // by default - no allocation
  23. memset(&_Overlapped, 0, sizeof(_Overlapped));
  24. }
  25. explicit SocketIOBuffer(SOCKET sock, size_t size)
  26. : _socket(sock)
  27. {
  28. memset(&_Overlapped, 0, sizeof(_Overlapped));
  29. _data.resize( size );
  30. }
  31. SocketIOBuffer(const SocketIOBuffer& sbuf)
  32. {
  33. Copy(sbuf);
  34. }
  35. SocketIOBuffer& operator=(const SocketIOBuffer& sbuf)
  36. {
  37. return Copy(sbuf);
  38. }
  39. ~SocketIOBuffer()
  40. {
  41. Free();
  42. }
  43. bool IsValid() const { return (_socket != INVALID_SOCKET); }
  44. void ReAlloc( size_t count) { _data.resize(count); }
  45. void Free() { _data.clear(); }
  46. size_t BufferSize() const { return _data.size(); }
  47. SocketIOBuffer& Copy(const SocketIOBuffer& sbuf)
  48. {
  49. if ( this != &sbuf )
  50. {
  51. _socket = sbuf._socket;
  52. _sockAddr = sbuf._sockAddr;
  53. if ( !sbuf._data.empty() ) {
  54. _data.resize( sbuf._data.size() );
  55. memcpy(&_data[0], &(sbuf._data[0]), _data.size());
  56. }
  57. }
  58. return *this;
  59. }
  60. // Quick access operator
  61. operator SOCKET() { return _socket; }
  62. operator SOCKET() const { return _socket; }
  63. operator SockAddrIn&() { return _sockAddr; }
  64. operator const SockAddrIn&() const { return _sockAddr; }
  65. operator LPSOCKADDR() { return _sockAddr; }
  66. operator LPWSAOVERLAPPED() { return &_Overlapped; }
  67. operator LPBYTE() { return &_data[0]; }
  68. bool IsEqual(const SocketIOBuffer& sbuf) const { return (_socket == sbuf._socket); }
  69. bool operator==(const SocketIOBuffer& sbuf) const { return IsEqual( sbuf ); }
  70. bool operator==(SOCKET sock) const { return (_socket == sock); }
  71. bool operator!=(const SocketIOBuffer& sbuf) const { return !IsEqual( sbuf ); }
  72. private:
  73. SOCKET _socket;
  74. SockAddrIn _sockAddr;
  75. WSAOVERLAPPED _Overlapped;
  76. std::vector<unsigned char> _data;
  77. SocketIOBuffer();
  78. };
  79. typedef std::list<SocketIOBuffer> SocketBufferList;
  80. static DWORD WM_ADD_CONNECTION = WM_USER+0x101;
  81. /**
  82. * IASocketServerHandler
  83. * Event handler that ASocketServerImpl<T> must implement
  84. * This class is not required, you can do the same thing as long your class exposes these functions.
  85. * (These functions are not pure to save you some typing)
  86. */
  87. class IASocketServerHandler
  88. {
  89. public:
  90. virtual void OnThreadBegin(CSocketHandle* ) {}
  91. virtual void OnThreadExit(CSocketHandle* ) {}
  92. virtual void OnThreadLoopEnter(CSocketHandle* ) {}
  93. virtual void OnThreadLoopLeave(CSocketHandle* ) {}
  94. virtual void OnAddConnection(CSocketHandle* , SOCKET ) {}
  95. virtual void OnRemoveConnection(CSocketHandle* , SOCKET ) {}
  96. virtual void OnDataReceived(CSocketHandle* , const BYTE* , DWORD , const SockAddrIn& ) {}
  97. virtual void OnConnectionFailure(CSocketHandle*, SOCKET) {}
  98. virtual void OnConnectionDropped(CSocketHandle* ) {}
  99. virtual void OnConnectionError(CSocketHandle* , DWORD ) {}
  100. };
  101. /**
  102. * ASocketServerImpl<T, tBufferSize>
  103. * Because <typename T> may refer to any class of your choosing,
  104. * Server Communication wrapper
  105. */
  106. template <typename T, size_t tBufferSize = 2048>
  107. class ASocketServerImpl
  108. {
  109. typedef ASocketServerImpl<T, tBufferSize> thisClass;
  110. public:
  111. ASocketServerImpl()
  112. : _pInterface(0)
  113. , _thread(0)
  114. , _threadId(0)
  115. {
  116. }
  117. void SetInterface(T* pInterface)
  118. {
  119. ::InterlockedExchangePointer(reinterpret_cast<void**>(&_pInterface), pInterface);
  120. }
  121. operator CSocketHandle*() throw()
  122. {
  123. return( &_socket );
  124. }
  125. CSocketHandle* operator->() throw()
  126. {
  127. return( &_socket );
  128. }
  129. bool IsOpen() const
  130. {
  131. return _socket.IsOpen();
  132. }
  133. bool CreateSocket(LPCTSTR pszHost, LPCTSTR pszServiceName, int nFamily, int nType, UINT uOptions = 0)
  134. {
  135. return _socket.CreateSocket(pszHost, pszServiceName, nFamily, nType, uOptions);
  136. }
  137. void Close()
  138. {
  139. _socket.Close();
  140. }
  141. DWORD Read(LPBYTE lpBuffer, DWORD dwSize, LPSOCKADDR lpAddrIn = NULL, DWORD dwTimeout = INFINITE)
  142. {
  143. return _socket.Read(lpBuffer, dwSize, lpAddrIn, dwTimeout);
  144. }
  145. DWORD Write(const LPBYTE lpBuffer, DWORD dwCount, const LPSOCKADDR lpAddrIn = NULL, DWORD dwTimeout = INFINITE)
  146. {
  147. return _socket.Write(lpBuffer, dwCount, lpAddrIn, dwTimeout);
  148. }
  149. const SocketBufferList& GetSocketList() const
  150. {
  151. // direct access! - use Lock/Unlock to protect
  152. return _sockets;
  153. }
  154. bool Lock()
  155. {
  156. return _critSection.Lock();
  157. }
  158. bool Unlock()
  159. {
  160. return _critSection.Unlock();
  161. }
  162. void ResetConnectionList()
  163. {
  164. AutoThreadSection aSection(&_critSection);
  165. _sockets.clear();
  166. }
  167. size_t GetConnectionCount() const
  168. {
  169. AutoThreadSection aSection(&_critSection);
  170. return _sockets.size();
  171. }
  172. void AddConnection(SOCKET sock)
  173. {
  174. AutoThreadSection aSection(&_critSection);
  175. _sockets.push_back( sock );
  176. }
  177. void RemoveConnection(SOCKET sock)
  178. {
  179. AutoThreadSection aSection(&_critSection);
  180. _sockets.remove( sock );
  181. }
  182. SocketIOBuffer* GetConnectionBuffer(SOCKET sock)
  183. {
  184. AutoThreadSection aSection(&_critSection);
  185. SocketBufferList::iterator iter = std::find(_sockets.begin(), _sockets.end(), sock);
  186. return ((iter != _sockets.end()) ? &(*iter) : NULL);
  187. }
  188. bool CloseConnection(SOCKET sock)
  189. {
  190. return CSocketHandle::ShutdownConnection( sock );
  191. }
  192. void CloseAllConnections();
  193. bool StartServer(LPCTSTR pszHost, LPCTSTR pszServiceName, int nFamily, int nType, UINT uOptions = 0);
  194. void Terminate(DWORD dwTimeout = INFINITE);
  195. static bool IsConnectionDropped(DWORD dwError);
  196. protected:
  197. void Run();
  198. void IoRun();
  199. bool AsyncRead(SocketIOBuffer* pBuffer);
  200. void OnIOComplete(DWORD dwError, SocketIOBuffer* pBuffer, DWORD cbTransferred, DWORD dwFlags);
  201. static DWORD WINAPI SocketServerProc(thisClass* _this);
  202. static DWORD WINAPI SocketServerIOProc(thisClass* _this);
  203. static void WINAPI CompletionRoutine(DWORD dwError, DWORD cbTransferred,
  204. LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags);
  205. T* _pInterface;
  206. HANDLE _thread;
  207. DWORD _threadId;
  208. ThreadSection _critSection;
  209. CSocketHandle _socket;
  210. SocketBufferList _sockets;
  211. };
  212. template <typename T, size_t tBufferSize>
  213. void ASocketServerImpl<T, tBufferSize>::CloseAllConnections()
  214. {
  215. AutoThreadSection aSection(&_critSection);
  216. if ( !_sockets.empty() )
  217. {
  218. // NOTE(elaurentin): this function closes all connections but handles are kept inside of list
  219. // (socket handles are removed by the pooling thread)
  220. SocketBufferList::iterator iter;
  221. for(iter = _sockets.begin(); iter != _sockets.end(); ++iter)
  222. {
  223. CloseConnection( (*iter) );
  224. }
  225. }
  226. }
  227. template <typename T, size_t tBufferSize>
  228. bool ASocketServerImpl<T, tBufferSize>::StartServer(LPCTSTR pszHost, LPCTSTR pszServiceName, int nFamily, int nType, UINT uOptions)
  229. {
  230. // must be closed first...
  231. if ( IsOpen() ) return false;
  232. bool result = false;
  233. result = _socket.CreateSocket(pszHost, pszServiceName, nFamily, nType, uOptions);
  234. if ( result )
  235. {
  236. _thread = AtlCreateThread(SocketServerProc, this);
  237. if ( _thread == NULL )
  238. {
  239. DWORD dwError = GetLastError();
  240. _socket.Close();
  241. SetLastError(dwError);
  242. result = false;
  243. }
  244. }
  245. return result;
  246. }
  247. template <typename T, size_t tBufferSize>
  248. void ASocketServerImpl<T, tBufferSize>::Run()
  249. {
  250. _ASSERTE( _pInterface != NULL && "Need an interface to pass events");
  251. SOCKET sock = _socket.GetSocket();
  252. int type = _socket.GetSocketType();
  253. // Notification: OnThreadBegin
  254. if ( _pInterface != NULL ) {
  255. _pInterface->OnThreadBegin(*this);
  256. }
  257. if (type == SOCK_STREAM)
  258. {
  259. HANDLE ioThread = NULL;
  260. DWORD ioThreadId = 0L;
  261. ioThread = CreateThreadT(0, 0, SocketServerIOProc, this, 0, &ioThreadId);
  262. if ( ioThread == NULL )
  263. {
  264. DWORD dwError = GetLastError();
  265. if ( _pInterface != NULL ) {
  266. _pInterface->OnConnectionError(*this, dwError);
  267. }
  268. }
  269. // In TCP mode, use an I/O thread to process all requests
  270. while( _socket.IsOpen() )
  271. {
  272. SOCKET newSocket = CSocketHandle::WaitForConnection(sock);
  273. if (!_socket.IsOpen())
  274. break;
  275. if (!PostThreadMessage(ioThreadId, WM_ADD_CONNECTION, 0,
  276. static_cast<ULONG_PTR>(newSocket)))
  277. {
  278. // Notification: OnConnectionFailure
  279. if ( _pInterface != NULL ) {
  280. _pInterface->OnConnectionFailure(*this, newSocket);
  281. }
  282. }
  283. }
  284. // close all connections
  285. CloseAllConnections();
  286. // wait for io thread
  287. if ( ioThread != NULL )
  288. {
  289. PostThreadMessage(ioThreadId, WM_QUIT, 0, 0);
  290. WaitForSingleObject(ioThread, INFINITE);
  291. CloseHandle(ioThread);
  292. }
  293. ResetConnectionList();
  294. }
  295. else
  296. {
  297. // UDP mode - let's reuse our thread here
  298. try {
  299. SocketIOBuffer ioBuffer(_socket.GetSocket(), tBufferSize);
  300. AsyncRead( &ioBuffer );
  301. // Save our thread id so we can exit gracefully
  302. _threadId = GetCurrentThreadId();
  303. // Process UDP packets
  304. IoRun();
  305. } catch(std::bad_alloc&) { /* memory exception */
  306. if ( _pInterface != NULL ) {
  307. _pInterface->OnConnectionError(*this, ERROR_NOT_ENOUGH_MEMORY);
  308. }
  309. }
  310. }
  311. // Notification: OnThreadExit
  312. if ( _pInterface != NULL ) {
  313. _pInterface->OnThreadExit(*this);
  314. }
  315. }
  316. template <typename T, size_t tBufferSize>
  317. void ASocketServerImpl<T, tBufferSize>::IoRun()
  318. {
  319. _ASSERTE( _pInterface != NULL && "Need an interface to pass events");
  320. MSG msg;
  321. ::PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
  322. // Notification: OnThreadLoopEnter
  323. if ( _pInterface != NULL ) {
  324. _pInterface->OnThreadLoopEnter(*this);
  325. }
  326. DWORD dwResult;
  327. while( (dwResult = MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE)) != WAIT_FAILED)
  328. {
  329. msg.message = 0;
  330. PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
  331. // exit on WM_QUIT or main socket closed
  332. if ( msg.message == WM_QUIT || !_socket.IsOpen() )
  333. break;
  334. else if ( msg.message == WM_ADD_CONNECTION )
  335. {
  336. SOCKET sock = static_cast<SOCKET>(msg.lParam);
  337. AddConnection(sock);
  338. SocketIOBuffer* pBuffer = GetConnectionBuffer(sock);
  339. _ASSERTE( pBuffer != NULL );
  340. if ( pBuffer != NULL )
  341. {
  342. pBuffer->ReAlloc( tBufferSize );
  343. if (!AsyncRead(pBuffer))
  344. {
  345. // remove and close connection
  346. RemoveConnection( sock );
  347. CloseConnection( sock );
  348. }
  349. else
  350. {
  351. // Notification: OnAddConnection
  352. if ( _pInterface != NULL ) {
  353. _pInterface->OnAddConnection(*this, sock);
  354. }
  355. }
  356. }
  357. }
  358. }
  359. // Notification: OnThreadLoopLeave
  360. if ( _pInterface != NULL ) {
  361. _pInterface->OnThreadLoopLeave(*this);
  362. }
  363. }
  364. template <typename T, size_t tBufferSize>
  365. bool ASocketServerImpl<T, tBufferSize>::AsyncRead(SocketIOBuffer* pBuffer)
  366. {
  367. CSocketHandle sockHandle;
  368. DWORD dwRead;
  369. SocketIOBuffer& sbuf = (*pBuffer);
  370. SOCKET sock = static_cast<SOCKET>(sbuf);
  371. sockHandle.Attach( sock );
  372. int type = sockHandle.GetSocketType();
  373. LPWSAOVERLAPPED lpOverlapped = static_cast<LPWSAOVERLAPPED>(sbuf);
  374. lpOverlapped->hEvent = reinterpret_cast<HANDLE>(this);
  375. lpOverlapped->Pointer = reinterpret_cast<PVOID>(pBuffer);
  376. if (type == SOCK_STREAM) {
  377. // TCP - save current peer address
  378. sockHandle.GetPeerName( sbuf );
  379. dwRead = sockHandle.ReadEx(static_cast<LPBYTE>(sbuf), // buffer
  380. static_cast<DWORD>(sbuf.BufferSize()), // buffer size
  381. NULL, // sockaddr
  382. lpOverlapped, // overlapped
  383. thisClass::CompletionRoutine );
  384. } else {
  385. dwRead = sockHandle.ReadEx(static_cast<LPBYTE>(sbuf), // buffer
  386. static_cast<DWORD>(sbuf.BufferSize()), // buffer size
  387. static_cast<LPSOCKADDR>(sbuf), // sockaddr
  388. lpOverlapped, // overlapped
  389. thisClass::CompletionRoutine );
  390. }
  391. sockHandle.Detach();
  392. return ( dwRead != -1L );
  393. }
  394. template <typename T, size_t tBufferSize>
  395. void ASocketServerImpl<T, tBufferSize>::OnIOComplete(DWORD dwError, SocketIOBuffer* pBuffer, DWORD cbTransferred, DWORD /*dwFlags*/)
  396. {
  397. _ASSERTE( _pInterface != NULL && "Need an interface to pass events");
  398. _ASSERTE( pBuffer != NULL && "Invalid Buffer");
  399. if ( pBuffer != NULL )
  400. {
  401. CSocketHandle sockHandle;
  402. SOCKET sock = static_cast<SOCKET>(*pBuffer);
  403. sockHandle.Attach( sock );
  404. int type = sockHandle.GetSocketType();
  405. if ( dwError == NOERROR )
  406. {
  407. if (type == SOCK_STREAM && cbTransferred == 0L )
  408. {
  409. // connection broken
  410. if ( _pInterface != NULL ) {
  411. _pInterface->OnConnectionDropped(*this);
  412. }
  413. // remove connection
  414. RemoveConnection( sock );
  415. if ( _pInterface != NULL ) {
  416. // Notification: OnRemoveConnection
  417. _pInterface->OnRemoveConnection(*this, sock);
  418. }
  419. }
  420. else
  421. {
  422. // Notification: OnDataReceived
  423. if ( _pInterface != NULL ) {
  424. _pInterface->OnDataReceived(*this,
  425. static_cast<LPBYTE>(*pBuffer), // Data
  426. cbTransferred, // Number of bytes
  427. static_cast<SockAddrIn&>(*pBuffer));
  428. }
  429. // schedule another read for this socket
  430. AsyncRead( pBuffer );
  431. }
  432. }
  433. else
  434. {
  435. for ( ; _pInterface != NULL; )
  436. {
  437. if (IsConnectionDropped( dwError) ) {
  438. // Notification: OnConnectionDropped
  439. if (type == SOCK_STREAM || (dwError == WSAENOTSOCK || dwError == WSAENETDOWN))
  440. {
  441. _pInterface->OnConnectionDropped(*this);
  442. type = -1; // don't schedule other request
  443. break;
  444. }
  445. }
  446. // Notification: OnConnectionError
  447. _pInterface->OnConnectionError(*this, dwError);
  448. break;
  449. }
  450. // Schedule read request
  451. if ((type == SOCK_STREAM || type == SOCK_DGRAM) && _socket.IsOpen() ) {
  452. AsyncRead( pBuffer );
  453. }
  454. }
  455. // Detach or Close this socket (TCP-mode only)
  456. if (type != SOCK_STREAM ||
  457. (type == SOCK_STREAM && cbTransferred != 0L) )
  458. {
  459. sockHandle.Detach();
  460. }
  461. }
  462. }
  463. template <typename T, size_t tBufferSize>
  464. void ASocketServerImpl<T, tBufferSize>::Terminate(DWORD dwTimeout /*= INFINITE*/)
  465. {
  466. _socket.Close();
  467. if ( _thread != NULL )
  468. {
  469. if ( _threadId != 0 ) {
  470. PostThreadMessage(_threadId, WM_QUIT, 0, 0);
  471. }
  472. if ( WaitForSingleObject(_thread, dwTimeout) == WAIT_TIMEOUT ) {
  473. TerminateThread(_thread, 1);
  474. }
  475. CloseHandle(_thread);
  476. _thread = NULL;
  477. _threadId = 0L;
  478. }
  479. }
  480. template <typename T, size_t tBufferSize>
  481. DWORD WINAPI ASocketServerImpl<T, tBufferSize>::SocketServerProc(thisClass* _this)
  482. {
  483. if ( _this != NULL )
  484. {
  485. _this->Run();
  486. }
  487. return 0;
  488. }
  489. template <typename T, size_t tBufferSize>
  490. DWORD WINAPI ASocketServerImpl<T, tBufferSize>::SocketServerIOProc(thisClass* _this)
  491. {
  492. if ( _this != NULL )
  493. {
  494. _this->IoRun();
  495. }
  496. return 0;
  497. }
  498. template <typename T, size_t tBufferSize>
  499. void WINAPI ASocketServerImpl<T, tBufferSize>::CompletionRoutine(DWORD dwError, DWORD cbTransferred,
  500. LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags)
  501. {
  502. thisClass* _this = reinterpret_cast<thisClass*>( lpOverlapped->hEvent );
  503. if ( _this != NULL )
  504. {
  505. SocketIOBuffer* pBuffer = reinterpret_cast<SocketIOBuffer*>(lpOverlapped->Pointer);
  506. _this->OnIOComplete(dwError, pBuffer, cbTransferred, dwFlags);
  507. }
  508. }
  509. template <typename T, size_t tBufferSize>
  510. bool ASocketServerImpl<T, tBufferSize>::IsConnectionDropped(DWORD dwError)
  511. {
  512. // see: winerror.h for definition
  513. switch( dwError )
  514. {
  515. case WSAENOTSOCK:
  516. case WSAENETDOWN:
  517. case WSAENETUNREACH:
  518. case WSAENETRESET:
  519. case WSAECONNABORTED:
  520. case WSAECONNRESET:
  521. case WSAESHUTDOWN:
  522. case WSAEHOSTDOWN:
  523. case WSAEHOSTUNREACH:
  524. return true;
  525. default:
  526. break;
  527. }
  528. return false;
  529. }
  530. #endif //ASYNCSOCKETSERVERIMPL_H