tcp_port.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 P2P_BASE_TCP_PORT_H_
  11. #define P2P_BASE_TCP_PORT_H_
  12. #include <list>
  13. #include <memory>
  14. #include <string>
  15. #include "absl/memory/memory.h"
  16. #include "p2p/base/connection.h"
  17. #include "p2p/base/port.h"
  18. #include "rtc_base/async_packet_socket.h"
  19. namespace cricket {
  20. class TCPConnection;
  21. // Communicates using a local TCP port.
  22. //
  23. // This class is designed to allow subclasses to take advantage of the
  24. // connection management provided by this class. A subclass should take of all
  25. // packet sending and preparation, but when a packet is received, it should
  26. // call this TCPPort::OnReadPacket (3 arg) to dispatch to a connection.
  27. class TCPPort : public Port {
  28. public:
  29. static std::unique_ptr<TCPPort> Create(rtc::Thread* thread,
  30. rtc::PacketSocketFactory* factory,
  31. rtc::Network* network,
  32. uint16_t min_port,
  33. uint16_t max_port,
  34. const std::string& username,
  35. const std::string& password,
  36. bool allow_listen) {
  37. // Using `new` to access a non-public constructor.
  38. return absl::WrapUnique(new TCPPort(thread, factory, network, min_port,
  39. max_port, username, password,
  40. allow_listen));
  41. }
  42. ~TCPPort() override;
  43. Connection* CreateConnection(const Candidate& address,
  44. CandidateOrigin origin) override;
  45. void PrepareAddress() override;
  46. int GetOption(rtc::Socket::Option opt, int* value) override;
  47. int SetOption(rtc::Socket::Option opt, int value) override;
  48. int GetError() override;
  49. bool SupportsProtocol(const std::string& protocol) const override;
  50. ProtocolType GetProtocol() const override;
  51. protected:
  52. TCPPort(rtc::Thread* thread,
  53. rtc::PacketSocketFactory* factory,
  54. rtc::Network* network,
  55. uint16_t min_port,
  56. uint16_t max_port,
  57. const std::string& username,
  58. const std::string& password,
  59. bool allow_listen);
  60. // Handles sending using the local TCP socket.
  61. int SendTo(const void* data,
  62. size_t size,
  63. const rtc::SocketAddress& addr,
  64. const rtc::PacketOptions& options,
  65. bool payload) override;
  66. // Accepts incoming TCP connection.
  67. void OnNewConnection(rtc::AsyncPacketSocket* socket,
  68. rtc::AsyncPacketSocket* new_socket);
  69. private:
  70. struct Incoming {
  71. rtc::SocketAddress addr;
  72. rtc::AsyncPacketSocket* socket;
  73. };
  74. void TryCreateServerSocket();
  75. rtc::AsyncPacketSocket* GetIncoming(const rtc::SocketAddress& addr,
  76. bool remove = false);
  77. // Receives packet signal from the local TCP Socket.
  78. void OnReadPacket(rtc::AsyncPacketSocket* socket,
  79. const char* data,
  80. size_t size,
  81. const rtc::SocketAddress& remote_addr,
  82. const int64_t& packet_time_us);
  83. void OnSentPacket(rtc::AsyncPacketSocket* socket,
  84. const rtc::SentPacket& sent_packet) override;
  85. void OnReadyToSend(rtc::AsyncPacketSocket* socket);
  86. void OnAddressReady(rtc::AsyncPacketSocket* socket,
  87. const rtc::SocketAddress& address);
  88. bool allow_listen_;
  89. rtc::AsyncPacketSocket* socket_;
  90. int error_;
  91. std::list<Incoming> incoming_;
  92. friend class TCPConnection;
  93. };
  94. class TCPConnection : public Connection {
  95. public:
  96. // Connection is outgoing unless socket is specified
  97. TCPConnection(TCPPort* port,
  98. const Candidate& candidate,
  99. rtc::AsyncPacketSocket* socket = 0);
  100. ~TCPConnection() override;
  101. int Send(const void* data,
  102. size_t size,
  103. const rtc::PacketOptions& options) override;
  104. int GetError() override;
  105. rtc::AsyncPacketSocket* socket() { return socket_.get(); }
  106. void OnMessage(rtc::Message* pmsg) override;
  107. // Allow test cases to overwrite the default timeout period.
  108. int reconnection_timeout() const { return reconnection_timeout_; }
  109. void set_reconnection_timeout(int timeout_in_ms) {
  110. reconnection_timeout_ = timeout_in_ms;
  111. }
  112. protected:
  113. enum {
  114. MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE,
  115. MSG_TCPCONNECTION_FAILED_CREATE_SOCKET,
  116. };
  117. // Set waiting_for_stun_binding_complete_ to false to allow data packets in
  118. // addition to what Port::OnConnectionRequestResponse does.
  119. void OnConnectionRequestResponse(ConnectionRequest* req,
  120. StunMessage* response) override;
  121. private:
  122. // Helper function to handle the case when Ping or Send fails with error
  123. // related to socket close.
  124. void MaybeReconnect();
  125. void CreateOutgoingTcpSocket();
  126. void ConnectSocketSignals(rtc::AsyncPacketSocket* socket);
  127. void OnConnect(rtc::AsyncPacketSocket* socket);
  128. void OnClose(rtc::AsyncPacketSocket* socket, int error);
  129. void OnReadPacket(rtc::AsyncPacketSocket* socket,
  130. const char* data,
  131. size_t size,
  132. const rtc::SocketAddress& remote_addr,
  133. const int64_t& packet_time_us);
  134. void OnReadyToSend(rtc::AsyncPacketSocket* socket);
  135. std::unique_ptr<rtc::AsyncPacketSocket> socket_;
  136. int error_;
  137. bool outgoing_;
  138. // Guard against multiple outgoing tcp connection during a reconnect.
  139. bool connection_pending_;
  140. // Guard against data packets sent when we reconnect a TCP connection. During
  141. // reconnecting, when a new tcp connection has being made, we can't send data
  142. // packets out until the STUN binding is completed (i.e. the write state is
  143. // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC
  144. // socket, when receiving data packets before that, will trigger OnError which
  145. // will terminate the newly created connection.
  146. bool pretending_to_be_writable_;
  147. // Allow test case to overwrite the default timeout period.
  148. int reconnection_timeout_;
  149. friend class TCPPort;
  150. };
  151. } // namespace cricket
  152. #endif // P2P_BASE_TCP_PORT_H_