PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/3rdParty/include/nio/AsyncSocketExLayer.h

https://gitlab.com/yoage/TTWinClient
C Header | 164 lines | 79 code | 16 blank | 69 comment | 0 complexity | 95545be2d27527e0d1a6c34f005e7d14 MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*CAsyncSocketEx by Tim Kosse (Tim.Kosse@gmx.de)
  2. Version 1.1 (2002-11-01)
  3. --------------------------------------------------------
  4. Introduction:
  5. -------------
  6. CAsyncSocketEx is a replacement for the MFC class CAsyncSocket.
  7. This class was written because CAsyncSocket is not the fastest WinSock
  8. wrapper and it's very hard to add new functionality to CAsyncSocket
  9. derived classes. This class offers the same functionality as CAsyncSocket.
  10. Also, CAsyncSocketEx offers some enhancements which were not possible with
  11. CAsyncSocket without some tricks.
  12. How do I use it?
  13. ----------------
  14. Basically exactly like CAsyncSocket.
  15. To use CAsyncSocketEx, just replace all occurrences of CAsyncSocket in your
  16. code with CAsyncSocketEx, if you did not enhance CAsyncSocket yourself in
  17. any way, you won't have to change anything else in your code.
  18. Why is CAsyncSocketEx faster?
  19. -----------------------------
  20. CAsyncSocketEx is slightly faster when dispatching notification event messages.
  21. First have a look at the way CAsyncSocket works. For each thread that uses
  22. CAsyncSocket, a window is created. CAsyncSocket calls WSAAsyncSelect with
  23. the handle of that window. Until here, CAsyncSocketEx works the same way.
  24. But CAsyncSocket uses only one window message (WM_SOCKET_NOTIFY) for all
  25. sockets within one thread. When the window recieve WM_SOCKET_NOTIFY, wParam
  26. contains the socket handle and the window looks up an CAsyncSocket instance
  27. using a map. CAsyncSocketEx works differently. It's helper window uses a
  28. wide range of different window messages (WM_USER through 0xBFFF) and passes
  29. a different message to WSAAsyncSelect for each socket. When a message in
  30. the specified range is received, CAsyncSocketEx looks up the pointer to a
  31. CAsyncSocketEx instance in an Array using the index of message - WM_USER.
  32. As you can see, CAsyncSocketEx uses the helper window in a more efficient
  33. way, as it don't have to use the slow maps to lookup it's own instance.
  34. Still, speed increase is not very much, but it may be noticeable when using
  35. a lot of sockets at the same time.
  36. Please note that the changes do not affect the raw data throughput rate,
  37. CAsyncSocketEx only dispatches the notification messages faster.
  38. What else does CAsyncSocketEx offer?
  39. ------------------------------------
  40. CAsyncSocketEx offers a flexible layer system. One example is the proxy layer.
  41. Just create an instance of the proxy layer, configure it and add it to the layer
  42. chain of your CAsyncSocketEx instance. After that, you can connect through
  43. proxies.
  44. Benefit: You don't have to change much to use the layer system.
  45. Another layer that is currently in development is the SSL layer to establish
  46. SSL encrypted connections.
  47. License
  48. -------
  49. Feel free to use this class, as long as you don't claim that you wrote it
  50. and this copyright notice stays intact in the source files.
  51. If you use this class in commercial applications, please send a short message
  52. to tim.kosse@gmx.de
  53. */#if !defined(AFX_ASYNCSOCKETEXLAYER_H__90C7FDB6_F3F1_4CC0_B77B_858458A563F3__INCLUDED_)
  54. #define AFX_ASYNCSOCKETEXLAYER_H__90C7FDB6_F3F1_4CC0_B77B_858458A563F3__INCLUDED_
  55. #include "AsyncSocketEx.h" // Hinzugefügt von der Klassenansicht
  56. #if _MSC_VER > 1000
  57. #pragma once
  58. #endif // _MSC_VER > 1000
  59. class CAsyncSocketEx;
  60. class CAsyncSocketExLayer
  61. {
  62. friend CAsyncSocketEx;
  63. friend CAsyncSocketExHelperWindow;
  64. protected:
  65. //Protected constructor so that CAsyncSocketExLayer can't be instantiated
  66. CAsyncSocketExLayer();
  67. virtual ~CAsyncSocketExLayer();
  68. //Notification event handlers
  69. virtual void OnAccept(int nErrorCode);
  70. virtual void OnClose(int nErrorCode);
  71. virtual void OnConnect(int nErrorCode);
  72. virtual void OnReceive(int nErrorCode);
  73. virtual void OnSend(int nErrorCode);
  74. //Operations
  75. virtual BOOL Accept( CAsyncSocketEx& rConnectedSocket, SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL );
  76. virtual void Close();
  77. virtual BOOL Connect(LPCTSTR lpszHostAddress, UINT nHostPort);
  78. virtual BOOL Connect( const SOCKADDR* lpSockAddr, int nSockAddrLen );
  79. virtual BOOL Create(UINT nSocketPort = 0, int nSocketType = SOCK_STREAM,
  80. long lEvent = FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT |
  81. FD_CONNECT | FD_CLOSE,
  82. LPCTSTR lpszSocketAddress = NULL );
  83. virtual BOOL GetPeerName( SOCKADDR* lpSockAddr, int* lpSockAddrLen );
  84. #ifdef _AFX
  85. virtual BOOL GetPeerName( CString& rPeerAddress, UINT& rPeerPort );
  86. #endif
  87. virtual BOOL Listen( int nConnectionBacklog);
  88. virtual int Receive(void* lpBuf, int nBufLen, int nFlags = 0);
  89. virtual int Send(const void* lpBuf, int nBufLen, int nFlags = 0);
  90. virtual BOOL ShutDown( int nHow = sends );
  91. enum { receives = 0, sends = 1, both = 2 };
  92. //Functions that will call next layer
  93. BOOL ShutDownNext( int nHow = sends );
  94. BOOL AcceptNext( CAsyncSocketEx& rConnectedSocket, SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL );
  95. void CloseNext();
  96. BOOL ConnectNext(LPCTSTR lpszHostAddress, UINT nHostPort);
  97. BOOL ConnectNext( const SOCKADDR* lpSockAddr, int nSockAddrLen );
  98. BOOL CreateNext(UINT nSocketPort, int nSocketType, long lEvent, LPCTSTR lpszSocketAddress);
  99. #ifdef _AFX
  100. BOOL GetPeerNameNext( CString& rPeerAddress, UINT& rPeerPort );
  101. #endif
  102. BOOL GetPeerNameNext( SOCKADDR* lpSockAddr, int* lpSockAddrLen );
  103. BOOL ListenNext( int nConnectionBacklog);
  104. int ReceiveNext(void *lpBuf, int nBufLen, int nFlags = 0);
  105. int SendNext(const void *lpBuf, int nBufLen, int nFlags = 0);
  106. CAsyncSocketEx *m_pOwnerSocket;
  107. //Calls OnLayerCallback on owner socket
  108. int DoLayerCallback(int nType, int nParam1, int nParam2);
  109. int GetLayerState();
  110. BOOL TriggerEvent(long lEvent, int nErrorCode, BOOL bPassThrough = FALSE );
  111. enum LayerState
  112. {
  113. notsock,
  114. unconnected,
  115. connecting,
  116. listening,
  117. connected,
  118. closed,
  119. aborted
  120. } ;
  121. private:
  122. //Layer state can't be set directly from derived classes
  123. void SetLayerState(int nLayerState);
  124. int m_nLayerState;
  125. //Called by helper window, dispatches event notification and updated layer state
  126. void CallEvent(int nEvent, int nErrorCode);
  127. int m_nCriticalError;
  128. void Init(CAsyncSocketExLayer *pPrevLayer, CAsyncSocketEx *pOwnerSocket);
  129. CAsyncSocketExLayer *AddLayer(CAsyncSocketExLayer *pLayer, CAsyncSocketEx *pOwnerSocket);
  130. CAsyncSocketExLayer *m_pNextLayer;
  131. CAsyncSocketExLayer *m_pPrevLayer;
  132. struct t_LayerNotifyMsg
  133. {
  134. CAsyncSocketExLayer *pLayer;
  135. long lEvent;
  136. };
  137. };
  138. #endif // !defined(AFX_ASYNCSOCKETEXLAYER_H__90C7FDB6_F3F1_4CC0_B77B_858458A563F3__INCLUDED_)