blob: b3655a8067a6a13ab5aaa60b25f0d0eea020a9e0 [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,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000035 uint16 min_port,
36 uint16 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();
58
59 protected:
pkasting@chromium.org332331f2014-11-06 20:19:22 +000060 TCPPort(rtc::Thread* thread,
61 rtc::PacketSocketFactory* factory,
62 rtc::Network* network,
63 const rtc::IPAddress& ip,
64 uint16 min_port,
65 uint16 max_port,
66 const std::string& username,
67 const std::string& password,
68 bool allow_listen);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069 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
111class 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
122 rtc::AsyncPacketSocket* socket() { return socket_; }
123
124 private:
125 void OnConnect(rtc::AsyncPacketSocket* socket);
126 void OnClose(rtc::AsyncPacketSocket* socket, int error);
127 void OnReadPacket(rtc::AsyncPacketSocket* socket,
128 const char* data, size_t size,
129 const rtc::SocketAddress& remote_addr,
130 const rtc::PacketTime& packet_time);
131 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
132
133 rtc::AsyncPacketSocket* socket_;
134 int error_;
135
136 friend class TCPPort;
137};
138
139} // namespace cricket
140
141#endif // WEBRTC_P2P_BASE_TCPPORT_H_