henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
| 11 | #ifndef WEBRTC_P2P_BASE_TCPPORT_H_ |
| 12 | #define WEBRTC_P2P_BASE_TCPPORT_H_ |
| 13 | |
| 14 | #include <list> |
| 15 | #include <string> |
| 16 | #include "webrtc/p2p/base/port.h" |
| 17 | #include "webrtc/base/asyncpacketsocket.h" |
| 18 | |
| 19 | namespace cricket { |
| 20 | |
| 21 | class TCPConnection; |
| 22 | |
| 23 | // Communicates using a local TCP port. |
| 24 | // |
| 25 | // This class is designed to allow subclasses to take advantage of the |
| 26 | // connection management provided by this class. A subclass should take of all |
| 27 | // packet sending and preparation, but when a packet is received, it should |
| 28 | // call this TCPPort::OnReadPacket (3 arg) to dispatch to a connection. |
| 29 | class TCPPort : public Port { |
| 30 | public: |
| 31 | static TCPPort* Create(rtc::Thread* thread, |
| 32 | rtc::PacketSocketFactory* factory, |
| 33 | rtc::Network* network, |
| 34 | const rtc::IPAddress& ip, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 35 | uint16_t min_port, |
| 36 | uint16_t max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 37 | const std::string& username, |
| 38 | const std::string& password, |
| 39 | bool allow_listen) { |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 40 | TCPPort* port = new TCPPort(thread, factory, network, ip, min_port, |
| 41 | max_port, username, password, allow_listen); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 42 | if (!port->Init()) { |
| 43 | delete port; |
| 44 | port = NULL; |
| 45 | } |
| 46 | return port; |
| 47 | } |
| 48 | virtual ~TCPPort(); |
| 49 | |
| 50 | virtual Connection* CreateConnection(const Candidate& address, |
| 51 | CandidateOrigin origin); |
| 52 | |
| 53 | virtual void PrepareAddress(); |
| 54 | |
| 55 | virtual int GetOption(rtc::Socket::Option opt, int* value); |
| 56 | virtual int SetOption(rtc::Socket::Option opt, int value); |
| 57 | virtual int GetError(); |
| 58 | |
| 59 | protected: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 60 | TCPPort(rtc::Thread* thread, |
| 61 | rtc::PacketSocketFactory* factory, |
| 62 | rtc::Network* network, |
| 63 | const rtc::IPAddress& ip, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 64 | uint16_t min_port, |
| 65 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 66 | const std::string& username, |
| 67 | const std::string& password, |
| 68 | bool allow_listen); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | bool Init(); |
| 70 | |
| 71 | // Handles sending using the local TCP socket. |
| 72 | virtual int SendTo(const void* data, size_t size, |
| 73 | const rtc::SocketAddress& addr, |
| 74 | const rtc::PacketOptions& options, |
| 75 | bool payload); |
| 76 | |
| 77 | // Accepts incoming TCP connection. |
| 78 | void OnNewConnection(rtc::AsyncPacketSocket* socket, |
| 79 | rtc::AsyncPacketSocket* new_socket); |
| 80 | |
| 81 | private: |
| 82 | struct Incoming { |
| 83 | rtc::SocketAddress addr; |
| 84 | rtc::AsyncPacketSocket* socket; |
| 85 | }; |
| 86 | |
| 87 | rtc::AsyncPacketSocket* GetIncoming( |
| 88 | const rtc::SocketAddress& addr, bool remove = false); |
| 89 | |
| 90 | // Receives packet signal from the local TCP Socket. |
| 91 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 92 | const char* data, size_t size, |
| 93 | const rtc::SocketAddress& remote_addr, |
| 94 | const rtc::PacketTime& packet_time); |
| 95 | |
| 96 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 97 | |
| 98 | void OnAddressReady(rtc::AsyncPacketSocket* socket, |
| 99 | const rtc::SocketAddress& address); |
| 100 | |
| 101 | // TODO: Is this still needed? |
| 102 | bool incoming_only_; |
| 103 | bool allow_listen_; |
| 104 | rtc::AsyncPacketSocket* socket_; |
| 105 | int error_; |
| 106 | std::list<Incoming> incoming_; |
| 107 | |
| 108 | friend class TCPConnection; |
| 109 | }; |
| 110 | |
| 111 | class TCPConnection : public Connection { |
| 112 | public: |
| 113 | // Connection is outgoing unless socket is specified |
| 114 | TCPConnection(TCPPort* port, const Candidate& candidate, |
| 115 | rtc::AsyncPacketSocket* socket = 0); |
| 116 | virtual ~TCPConnection(); |
| 117 | |
| 118 | virtual int Send(const void* data, size_t size, |
| 119 | const rtc::PacketOptions& options); |
| 120 | virtual int GetError(); |
| 121 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 122 | rtc::AsyncPacketSocket* socket() { return socket_.get(); } |
| 123 | |
| 124 | void OnMessage(rtc::Message* pmsg); |
| 125 | |
| 126 | // Allow test cases to overwrite the default timeout period. |
| 127 | int reconnection_timeout() const { return reconnection_timeout_; } |
| 128 | void set_reconnection_timeout(int timeout_in_ms) { |
| 129 | reconnection_timeout_ = timeout_in_ms; |
| 130 | } |
| 131 | |
| 132 | protected: |
| 133 | enum { |
| 134 | MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE, |
| 135 | }; |
| 136 | |
| 137 | // Set waiting_for_stun_binding_complete_ to false to allow data packets in |
| 138 | // addition to what Port::OnConnectionRequestResponse does. |
| 139 | virtual void OnConnectionRequestResponse(ConnectionRequest* req, |
| 140 | StunMessage* response); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 141 | |
| 142 | private: |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 143 | // Helper function to handle the case when Ping or Send fails with error |
| 144 | // related to socket close. |
| 145 | void MaybeReconnect(); |
| 146 | |
| 147 | void CreateOutgoingTcpSocket(); |
| 148 | |
| 149 | void ConnectSocketSignals(rtc::AsyncPacketSocket* socket); |
| 150 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 151 | void OnConnect(rtc::AsyncPacketSocket* socket); |
| 152 | void OnClose(rtc::AsyncPacketSocket* socket, int error); |
| 153 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 154 | const char* data, size_t size, |
| 155 | const rtc::SocketAddress& remote_addr, |
| 156 | const rtc::PacketTime& packet_time); |
| 157 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 158 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 159 | rtc::scoped_ptr<rtc::AsyncPacketSocket> socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 160 | int error_; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 161 | bool outgoing_; |
| 162 | |
| 163 | // Guard against multiple outgoing tcp connection during a reconnect. |
| 164 | bool connection_pending_; |
| 165 | |
| 166 | // Guard against data packets sent when we reconnect a TCP connection. During |
| 167 | // reconnecting, when a new tcp connection has being made, we can't send data |
| 168 | // packets out until the STUN binding is completed (i.e. the write state is |
| 169 | // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC |
| 170 | // socket, when receiving data packets before that, will trigger OnError which |
| 171 | // will terminate the newly created connection. |
| 172 | bool pretending_to_be_writable_; |
| 173 | |
| 174 | // Allow test case to overwrite the default timeout period. |
| 175 | int reconnection_timeout_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 176 | |
| 177 | friend class TCPPort; |
| 178 | }; |
| 179 | |
| 180 | } // namespace cricket |
| 181 | |
| 182 | #endif // WEBRTC_P2P_BASE_TCPPORT_H_ |