2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
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.
11 #ifndef WEBRTC_P2P_BASE_TCPPORT_H_
12 #define WEBRTC_P2P_BASE_TCPPORT_H_
16 #include "webrtc/p2p/base/port.h"
17 #include "webrtc/base/asyncpacketsocket.h"
23 // Communicates using a local TCP port.
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 {
31 static TCPPort* Create(rtc::Thread* thread,
32 rtc::PacketSocketFactory* factory,
33 rtc::Network* network,
34 const rtc::IPAddress& ip,
37 const std::string& username,
38 const std::string& password,
40 TCPPort* port = new TCPPort(thread, factory, network, ip, min_port,
41 max_port, username, password, allow_listen);
50 virtual Connection* CreateConnection(const Candidate& address,
51 CandidateOrigin origin);
53 virtual void PrepareAddress();
55 virtual int GetOption(rtc::Socket::Option opt, int* value);
56 virtual int SetOption(rtc::Socket::Option opt, int value);
57 virtual int GetError();
60 TCPPort(rtc::Thread* thread,
61 rtc::PacketSocketFactory* factory,
62 rtc::Network* network,
63 const rtc::IPAddress& ip,
66 const std::string& username,
67 const std::string& password,
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,
77 // Accepts incoming TCP connection.
78 void OnNewConnection(rtc::AsyncPacketSocket* socket,
79 rtc::AsyncPacketSocket* new_socket);
83 rtc::SocketAddress addr;
84 rtc::AsyncPacketSocket* socket;
87 rtc::AsyncPacketSocket* GetIncoming(
88 const rtc::SocketAddress& addr, bool remove = false);
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);
96 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
98 void OnAddressReady(rtc::AsyncPacketSocket* socket,
99 const rtc::SocketAddress& address);
101 // TODO: Is this still needed?
104 rtc::AsyncPacketSocket* socket_;
106 std::list<Incoming> incoming_;
108 friend class TCPConnection;
111 class TCPConnection : public Connection {
113 // Connection is outgoing unless socket is specified
114 TCPConnection(TCPPort* port, const Candidate& candidate,
115 rtc::AsyncPacketSocket* socket = 0);
116 virtual ~TCPConnection();
118 virtual int Send(const void* data, size_t size,
119 const rtc::PacketOptions& options);
120 virtual int GetError();
122 rtc::AsyncPacketSocket* socket() { return socket_; }
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);
133 rtc::AsyncPacketSocket* socket_;
136 friend class TCPPort;
139 } // namespace cricket
141 #endif // WEBRTC_P2P_BASE_TCPPORT_H_