TCPClient.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright (c) 2009-2010 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_TCP_CLIENT_HPP
  26. #define CAT_TCP_CLIENT_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. namespace cat {
  33. /*
  34. class TCPClient
  35. Object that represents a TCPClient bound to a single port
  36. ValidClient() : Returns true if the client socket is valid
  37. Connect() : Connects to the given address
  38. DisconnectServer() : Disconnects from the server
  39. PostToServer() : Send a message to the server (will fail if not connected)
  40. OnConnectToServer() : Called when connection is accepted
  41. OnReadFromServer() : Return false to disconnect the server in response to data
  42. OnWriteToServer() : Informs the derived class that data has been sent
  43. OnDisconnectFromServer() : Informs the derived class that the server has disconnected
  44. */
  45. class TCPClient : public ThreadRefObject
  46. {
  47. // Remembers if socket is IPv6 so that user-provided
  48. // addresses can be promoted if necessary.
  49. bool _ipv6;
  50. Socket _socket;
  51. volatile u32 _disconnecting; // Disconnect flag
  52. public:
  53. TCPClient(int priorityLevel);
  54. virtual ~TCPClient();
  55. CAT_INLINE bool Valid() { return _socket != SOCKET_ERROR; }
  56. void Disconnect();
  57. bool Connect(bool onlySupportIPv4, const NetAddr &remoteServerAddress);
  58. public:
  59. bool Post(u8 *data, u32 data_bytes, u32 skip_bytes = 0);
  60. private:
  61. bool ConnectEx(const NetAddr &remoteServerAddress);
  62. bool Read(AsyncBuffer *buffer = 0);
  63. bool Disco(AsyncBuffer *buffer = 0);
  64. private:
  65. bool OnConnectEx(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes);
  66. bool OnRead(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes);
  67. bool OnWrite(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes);
  68. bool OnDisco(ThreadPoolLocalStorage *tls, int error, AsyncBuffer *buffer, u32 bytes);
  69. protected:
  70. virtual void OnConnectToServer(ThreadPoolLocalStorage *tls) = 0;
  71. virtual bool OnReadFromServer(ThreadPoolLocalStorage *tls, u8 *data, u32 bytes) = 0; // false = disconnect
  72. virtual bool OnWriteToServer(ThreadPoolLocalStorage *tls, AsyncBuffer *buffer, u32 bytes) = 0; // true = delete AsyncBuffer object
  73. virtual void OnDisconnectFromServer() = 0;
  74. };
  75. /*
  76. class TCPClientQueued
  77. Base class for a TCP client that needs to queue up data for sending before
  78. a connection has been established. e.g. Uplink for a proxy server.
  79. PostQueuedToServer() : Call in OnConnectToServer() to post the queued messages.
  80. */
  81. class TCPClientQueued : public TCPClient
  82. {
  83. private:
  84. volatile bool _queuing;
  85. Mutex _queueLock;
  86. AsyncBuffer *_queueBuffer;
  87. protected:
  88. void PostQueuedToServer();
  89. public:
  90. TCPClientQueued(int priorityLevel);
  91. virtual ~TCPClientQueued();
  92. bool Post(u8 *data, u32 data_bytes, u32 skip_bytes = 0);
  93. };
  94. } // namespace cat
  95. #endif // CAT_TCP_CLIENT_HPP
粤ICP备19079148号