TCPConnection.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. Copyright (c) 2009 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_THREAD_POOL_SOCKETS_HPP
  26. #define CAT_THREAD_POOL_SOCKETS_HPP
  27. /*
  28. Windows version of thread pool sockets with IO Completion Ports
  29. Included from <cat/net/ThreadPoolSockets.hpp>
  30. Do not include directly
  31. */
  32. #include <MSWSock.h>
  33. #include <cat/port/WindowsInclude.hpp>
  34. namespace cat {
  35. /*
  36. class TCPConnection
  37. Object that represents a TCPServer's connection from a TCPClient
  38. Object is instantiated just before accepting a connection
  39. DisconnectClient() : Disconnect the client
  40. PostToClient() : Send a message to the client
  41. ValidServerConnection() : Returns true iff the connection is valid
  42. OnConnectFromClient() : Return false to deny this connection
  43. OnReadFromClient() : Return false to disconnect the client in response to a message
  44. OnWriteToClient() : Informs the derived class that data has been sent
  45. OnDisconectFromClient() : Informs the derived class that the client has disconnected
  46. */
  47. class TCPConnection : public ThreadRefObject
  48. {
  49. friend class TCPServer;
  50. friend class ThreadPool;
  51. public:
  52. TCPConnection();
  53. virtual ~TCPConnection();
  54. bool ValidServerConnection();
  55. void DisconnectClient();
  56. bool PostToClient(void *buffer, u32 bytes);
  57. protected:
  58. virtual bool OnConnectFromClient(const NetAddr &remoteClientAddress) = 0; // false = disconnect
  59. virtual bool OnReadFromClient(u8 *data, u32 bytes) = 0; // false = disconnect
  60. virtual void OnWriteToClient(u32 bytes) = 0;
  61. virtual void OnDisconnectFromClient() = 0;
  62. private:
  63. Socket _socket;
  64. LPFN_DISCONNECTEX _lpfnDisconnectEx;
  65. TypedOverlapped *_recvOv;
  66. volatile u32 _disconnecting;
  67. private:
  68. bool AcceptConnection(Socket listenSocket, Socket acceptSocket,
  69. LPFN_DISCONNECTEX lpfnDisconnectEx, const NetAddr &acceptAddress,
  70. const NetAddr &remoteClientAddress);
  71. bool QueueWSARecv();
  72. void OnWSARecvComplete(int error, u32 bytes);
  73. bool QueueWSASend(TypedOverlapped *sendOv, u32 bytes);
  74. void OnWSASendComplete(int error, u32 bytes);
  75. bool QueueDisconnectEx();
  76. void OnDisconnectExComplete(int error);
  77. };
  78. /*
  79. class TCPClient
  80. Object that represents a TCPClient bound to a single port
  81. ValidClient() : Returns true iff the client socket is valid
  82. Connect() : Connects to the given address
  83. DisconnectServer() : Disconnects from the server
  84. PostToServer() : Send a message to the server (will fail if not connected)
  85. OnConnectToServer() : Called when connection is accepted
  86. OnReadFromServer() : Return false to disconnect the server in response to data
  87. OnWriteToServer() : Informs the derived class that data has been sent
  88. OnDisconnectFromServer() : Informs the derived class that the server has disconnected
  89. */
  90. class TCPClient : public ThreadRefObject
  91. {
  92. friend class ThreadPool;
  93. public:
  94. TCPClient();
  95. virtual ~TCPClient();
  96. bool ValidClient();
  97. bool Connect(const NetAddr &remoteServerAddress);
  98. void DisconnectServer();
  99. bool PostToServer(void *buffer, u32 bytes);
  100. protected:
  101. virtual void OnConnectToServer() = 0;
  102. virtual bool OnReadFromServer(u8 *data, u32 bytes) = 0; // false = disconnect
  103. virtual void OnWriteToServer(u32 bytes) = 0;
  104. virtual void OnDisconnectFromServer() = 0;
  105. private:
  106. Socket _socket;
  107. TypedOverlapped *_recvOv;
  108. volatile u32 _disconnecting;
  109. bool _ipv6;
  110. private:
  111. bool QueueConnectEx(const NetAddr &remoteServerAddress);
  112. void OnConnectExComplete(int error);
  113. bool QueueWSARecv();
  114. void OnWSARecvComplete(int error, u32 bytes);
  115. bool QueueWSASend(TypedOverlapped *sendOv, u32 bytes);
  116. void OnWSASendComplete(int error, u32 bytes);
  117. bool QueueDisconnectEx();
  118. void OnDisconnectExComplete(int error);
  119. };
  120. /*
  121. class TCPClientQueued
  122. Base class for a TCP client that needs to queue up data for sending before
  123. a connection has been established. e.g. Uplink for a proxy server.
  124. PostQueuedToServer() : Call in OnConnectToServer() to post the queued messages.
  125. */
  126. class TCPClientQueued : public TCPClient
  127. {
  128. private:
  129. volatile bool _queuing;
  130. Mutex _queueLock;
  131. void *_queueBuffer;
  132. u32 _queueBytes;
  133. protected:
  134. void PostQueuedToServer();
  135. public:
  136. TCPClientQueued();
  137. virtual ~TCPClientQueued();
  138. bool PostToServer(void *buffer, u32 bytes);
  139. };
  140. /*
  141. class UDPEndpoint
  142. Object that represents a UDP endpoint bound to a single port
  143. */
  144. class UDPEndpoint : public ThreadRefObject
  145. {
  146. friend class ThreadPool;
  147. public:
  148. UDPEndpoint();
  149. virtual ~UDPEndpoint();
  150. bool Valid();
  151. Port GetPort();
  152. // Is6() result is only valid AFTER Bind()
  153. CAT_INLINE bool Is6() { return _ipv6; }
  154. // For servers: Bind() with ignoreUnreachable = true ((default))
  155. // For clients: Bind() with ignoreUnreachable = false and call this
  156. // after the first packet from the server is received.
  157. bool IgnoreUnreachable();
  158. void Close(); // Invalidates this object
  159. bool Bind(Port port = 0, bool ignoreUnreachable = true);
  160. bool QueueWSARecvFrom();
  161. // If Is6() == true, the address must be promoted to IPv6
  162. // before calling Post() with addr.PromoteTo6()
  163. bool Post(const NetAddr &addr, void *data, u32 bytes);
  164. protected:
  165. virtual void OnRead(ThreadPoolLocalStorage *tls, const NetAddr &addr, u8 *data, u32 bytes) = 0; // false = close
  166. virtual void OnWrite(u32 bytes) = 0;
  167. virtual void OnClose() = 0;
  168. virtual void OnUnreachable(const NetAddr &addr) {} // Only IP is valid
  169. private:
  170. Socket _socket;
  171. Port _port;
  172. volatile u32 _closing;
  173. bool _ipv6;
  174. private:
  175. bool QueueWSARecvFrom(RecvFromOverlapped *recvOv);
  176. void OnWSARecvFromComplete(ThreadPoolLocalStorage *tls, int error, RecvFromOverlapped *recvOv, u32 bytes);
  177. bool QueueWSASendTo(const NetAddr &addr, TypedOverlapped *sendOv, u32 bytes);
  178. void OnWSASendToComplete(int error, u32 bytes);
  179. };
  180. } // namespace cat
  181. #endif // CAT_THREAD_POOL_SOCKETS_HPP
粤ICP备19079148号