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, |
| 35 | int min_port, int max_port, |
| 36 | const std::string& username, |
| 37 | const std::string& password, |
| 38 | bool allow_listen) { |
| 39 | TCPPort* port = new TCPPort(thread, factory, network, |
| 40 | ip, min_port, max_port, |
| 41 | username, password, allow_listen); |
| 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: |
| 60 | TCPPort(rtc::Thread* thread, rtc::PacketSocketFactory* factory, |
| 61 | rtc::Network* network, const rtc::IPAddress& ip, |
| 62 | int min_port, int max_port, const std::string& username, |
| 63 | const std::string& password, bool allow_listen); |
| 64 | bool Init(); |
| 65 | |
| 66 | // Handles sending using the local TCP socket. |
| 67 | virtual int SendTo(const void* data, size_t size, |
| 68 | const rtc::SocketAddress& addr, |
| 69 | const rtc::PacketOptions& options, |
| 70 | bool payload); |
| 71 | |
| 72 | // Accepts incoming TCP connection. |
| 73 | void OnNewConnection(rtc::AsyncPacketSocket* socket, |
| 74 | rtc::AsyncPacketSocket* new_socket); |
| 75 | |
| 76 | private: |
| 77 | struct Incoming { |
| 78 | rtc::SocketAddress addr; |
| 79 | rtc::AsyncPacketSocket* socket; |
| 80 | }; |
| 81 | |
| 82 | rtc::AsyncPacketSocket* GetIncoming( |
| 83 | const rtc::SocketAddress& addr, bool remove = false); |
| 84 | |
| 85 | // Receives packet signal from the local TCP Socket. |
| 86 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 87 | const char* data, size_t size, |
| 88 | const rtc::SocketAddress& remote_addr, |
| 89 | const rtc::PacketTime& packet_time); |
| 90 | |
| 91 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 92 | |
| 93 | void OnAddressReady(rtc::AsyncPacketSocket* socket, |
| 94 | const rtc::SocketAddress& address); |
| 95 | |
| 96 | // TODO: Is this still needed? |
| 97 | bool incoming_only_; |
| 98 | bool allow_listen_; |
| 99 | rtc::AsyncPacketSocket* socket_; |
| 100 | int error_; |
| 101 | std::list<Incoming> incoming_; |
| 102 | |
| 103 | friend class TCPConnection; |
| 104 | }; |
| 105 | |
| 106 | class TCPConnection : public Connection { |
| 107 | public: |
| 108 | // Connection is outgoing unless socket is specified |
| 109 | TCPConnection(TCPPort* port, const Candidate& candidate, |
| 110 | rtc::AsyncPacketSocket* socket = 0); |
| 111 | virtual ~TCPConnection(); |
| 112 | |
| 113 | virtual int Send(const void* data, size_t size, |
| 114 | const rtc::PacketOptions& options); |
| 115 | virtual int GetError(); |
| 116 | |
| 117 | rtc::AsyncPacketSocket* socket() { return socket_; } |
| 118 | |
| 119 | private: |
| 120 | void OnConnect(rtc::AsyncPacketSocket* socket); |
| 121 | void OnClose(rtc::AsyncPacketSocket* socket, int error); |
| 122 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 123 | const char* data, size_t size, |
| 124 | const rtc::SocketAddress& remote_addr, |
| 125 | const rtc::PacketTime& packet_time); |
| 126 | void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 127 | |
| 128 | rtc::AsyncPacketSocket* socket_; |
| 129 | int error_; |
| 130 | |
| 131 | friend class TCPPort; |
| 132 | }; |
| 133 | |
| 134 | } // namespace cricket |
| 135 | |
| 136 | #endif // WEBRTC_P2P_BASE_TCPPORT_H_ |