async_tcp_socket.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_ASYNC_TCP_SOCKET_H_
  11. #define RTC_BASE_ASYNC_TCP_SOCKET_H_
  12. #include <stddef.h>
  13. #include <memory>
  14. #include "rtc_base/async_packet_socket.h"
  15. #include "rtc_base/async_socket.h"
  16. #include "rtc_base/buffer.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/socket.h"
  19. #include "rtc_base/socket_address.h"
  20. namespace rtc {
  21. // Simulates UDP semantics over TCP. Send and Recv packet sizes
  22. // are preserved, and drops packets silently on Send, rather than
  23. // buffer them in user space.
  24. class AsyncTCPSocketBase : public AsyncPacketSocket {
  25. public:
  26. AsyncTCPSocketBase(AsyncSocket* socket, bool listen, size_t max_packet_size);
  27. ~AsyncTCPSocketBase() override;
  28. // Pure virtual methods to send and recv data.
  29. int Send(const void* pv,
  30. size_t cb,
  31. const rtc::PacketOptions& options) override = 0;
  32. virtual void ProcessInput(char* data, size_t* len) = 0;
  33. // Signals incoming connection.
  34. virtual void HandleIncomingConnection(AsyncSocket* socket) = 0;
  35. SocketAddress GetLocalAddress() const override;
  36. SocketAddress GetRemoteAddress() const override;
  37. int SendTo(const void* pv,
  38. size_t cb,
  39. const SocketAddress& addr,
  40. const rtc::PacketOptions& options) override;
  41. int Close() override;
  42. State GetState() const override;
  43. int GetOption(Socket::Option opt, int* value) override;
  44. int SetOption(Socket::Option opt, int value) override;
  45. int GetError() const override;
  46. void SetError(int error) override;
  47. protected:
  48. // Binds and connects |socket| and creates AsyncTCPSocket for
  49. // it. Takes ownership of |socket|. Returns null if bind() or
  50. // connect() fail (|socket| is destroyed in that case).
  51. static AsyncSocket* ConnectSocket(AsyncSocket* socket,
  52. const SocketAddress& bind_address,
  53. const SocketAddress& remote_address);
  54. int FlushOutBuffer();
  55. // Add data to |outbuf_|.
  56. void AppendToOutBuffer(const void* pv, size_t cb);
  57. // Helper methods for |outpos_|.
  58. bool IsOutBufferEmpty() const { return outbuf_.size() == 0; }
  59. void ClearOutBuffer() { outbuf_.Clear(); }
  60. private:
  61. // Called by the underlying socket
  62. void OnConnectEvent(AsyncSocket* socket);
  63. void OnReadEvent(AsyncSocket* socket);
  64. void OnWriteEvent(AsyncSocket* socket);
  65. void OnCloseEvent(AsyncSocket* socket, int error);
  66. std::unique_ptr<AsyncSocket> socket_;
  67. bool listen_;
  68. Buffer inbuf_;
  69. Buffer outbuf_;
  70. size_t max_insize_;
  71. size_t max_outsize_;
  72. RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase);
  73. };
  74. class AsyncTCPSocket : public AsyncTCPSocketBase {
  75. public:
  76. // Binds and connects |socket| and creates AsyncTCPSocket for
  77. // it. Takes ownership of |socket|. Returns null if bind() or
  78. // connect() fail (|socket| is destroyed in that case).
  79. static AsyncTCPSocket* Create(AsyncSocket* socket,
  80. const SocketAddress& bind_address,
  81. const SocketAddress& remote_address);
  82. AsyncTCPSocket(AsyncSocket* socket, bool listen);
  83. ~AsyncTCPSocket() override {}
  84. int Send(const void* pv,
  85. size_t cb,
  86. const rtc::PacketOptions& options) override;
  87. void ProcessInput(char* data, size_t* len) override;
  88. void HandleIncomingConnection(AsyncSocket* socket) override;
  89. private:
  90. RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket);
  91. };
  92. } // namespace rtc
  93. #endif // RTC_BASE_ASYNC_TCP_SOCKET_H_