blob: 568dc65f3d40f48a3a78aeb0e704f4a13191fffb [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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
19namespace cricket {
20
21class 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.
29class 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öm0c4e06b2015-10-07 12:23:21 +020035 uint16_t min_port,
36 uint16_t max_port,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037 const std::string& username,
38 const std::string& password,
39 bool allow_listen) {
pkasting@chromium.org332331f2014-11-06 20:19:22 +000040 TCPPort* port = new TCPPort(thread, factory, network, ip, min_port,
41 max_port, username, password, allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042 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();
Honghai Zhangf9945b22015-12-15 12:20:13 -080058 virtual bool SupportsProtocol(const std::string& protocol) const {
59 return protocol == TCP_PROTOCOL_NAME || protocol == SSLTCP_PROTOCOL_NAME;
60 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061
62 protected:
pkasting@chromium.org332331f2014-11-06 20:19:22 +000063 TCPPort(rtc::Thread* thread,
64 rtc::PacketSocketFactory* factory,
65 rtc::Network* network,
66 const rtc::IPAddress& ip,
Peter Boström0c4e06b2015-10-07 12:23:21 +020067 uint16_t min_port,
68 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000069 const std::string& username,
70 const std::string& password,
71 bool allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072 bool Init();
73
74 // Handles sending using the local TCP socket.
75 virtual int SendTo(const void* data, size_t size,
76 const rtc::SocketAddress& addr,
77 const rtc::PacketOptions& options,
78 bool payload);
79
80 // Accepts incoming TCP connection.
81 void OnNewConnection(rtc::AsyncPacketSocket* socket,
82 rtc::AsyncPacketSocket* new_socket);
83
84 private:
85 struct Incoming {
86 rtc::SocketAddress addr;
87 rtc::AsyncPacketSocket* socket;
88 };
89
90 rtc::AsyncPacketSocket* GetIncoming(
91 const rtc::SocketAddress& addr, bool remove = false);
92
93 // Receives packet signal from the local TCP Socket.
94 void OnReadPacket(rtc::AsyncPacketSocket* socket,
95 const char* data, size_t size,
96 const rtc::SocketAddress& remote_addr,
97 const rtc::PacketTime& packet_time);
98
99 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
100
101 void OnAddressReady(rtc::AsyncPacketSocket* socket,
102 const rtc::SocketAddress& address);
103
104 // TODO: Is this still needed?
105 bool incoming_only_;
106 bool allow_listen_;
107 rtc::AsyncPacketSocket* socket_;
108 int error_;
109 std::list<Incoming> incoming_;
110
111 friend class TCPConnection;
112};
113
114class TCPConnection : public Connection {
115 public:
116 // Connection is outgoing unless socket is specified
117 TCPConnection(TCPPort* port, const Candidate& candidate,
118 rtc::AsyncPacketSocket* socket = 0);
119 virtual ~TCPConnection();
120
121 virtual int Send(const void* data, size_t size,
122 const rtc::PacketOptions& options);
123 virtual int GetError();
124
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700125 rtc::AsyncPacketSocket* socket() { return socket_.get(); }
126
127 void OnMessage(rtc::Message* pmsg);
128
129 // Allow test cases to overwrite the default timeout period.
130 int reconnection_timeout() const { return reconnection_timeout_; }
131 void set_reconnection_timeout(int timeout_in_ms) {
132 reconnection_timeout_ = timeout_in_ms;
133 }
134
135 protected:
136 enum {
137 MSG_TCPCONNECTION_DELAYED_ONCLOSE = Connection::MSG_FIRST_AVAILABLE,
138 };
139
140 // Set waiting_for_stun_binding_complete_ to false to allow data packets in
141 // addition to what Port::OnConnectionRequestResponse does.
142 virtual void OnConnectionRequestResponse(ConnectionRequest* req,
143 StunMessage* response);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000144
145 private:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700146 // Helper function to handle the case when Ping or Send fails with error
147 // related to socket close.
148 void MaybeReconnect();
149
150 void CreateOutgoingTcpSocket();
151
152 void ConnectSocketSignals(rtc::AsyncPacketSocket* socket);
153
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000154 void OnConnect(rtc::AsyncPacketSocket* socket);
155 void OnClose(rtc::AsyncPacketSocket* socket, int error);
156 void OnReadPacket(rtc::AsyncPacketSocket* socket,
157 const char* data, size_t size,
158 const rtc::SocketAddress& remote_addr,
159 const rtc::PacketTime& packet_time);
160 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
161
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700162 rtc::scoped_ptr<rtc::AsyncPacketSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 int error_;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700164 bool outgoing_;
165
166 // Guard against multiple outgoing tcp connection during a reconnect.
167 bool connection_pending_;
168
169 // Guard against data packets sent when we reconnect a TCP connection. During
170 // reconnecting, when a new tcp connection has being made, we can't send data
171 // packets out until the STUN binding is completed (i.e. the write state is
172 // set to WRITABLE again by Connection::OnConnectionRequestResponse). IPC
173 // socket, when receiving data packets before that, will trigger OnError which
174 // will terminate the newly created connection.
175 bool pretending_to_be_writable_;
176
177 // Allow test case to overwrite the default timeout period.
178 int reconnection_timeout_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000179
180 friend class TCPPort;
181};
182
183} // namespace cricket
184
185#endif // WEBRTC_P2P_BASE_TCPPORT_H_