henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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_PORTINTERFACE_H_ |
| 12 | #define WEBRTC_P2P_BASE_PORTINTERFACE_H_ |
| 13 | |
| 14 | #include <string> |
| 15 | |
| 16 | #include "webrtc/p2p/base/transport.h" |
stefan | c1aeaf0 | 2015-10-15 14:26:07 | [diff] [blame] | 17 | #include "webrtc/base/asyncpacketsocket.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 18 | #include "webrtc/base/socketaddress.h" |
| 19 | |
| 20 | namespace rtc { |
| 21 | class Network; |
| 22 | struct PacketOptions; |
| 23 | } |
| 24 | |
| 25 | namespace cricket { |
| 26 | class Connection; |
| 27 | class IceMessage; |
| 28 | class StunMessage; |
| 29 | |
| 30 | enum ProtocolType { |
| 31 | PROTO_UDP, |
| 32 | PROTO_TCP, |
| 33 | PROTO_SSLTCP, |
| 34 | PROTO_LAST = PROTO_SSLTCP |
| 35 | }; |
| 36 | |
| 37 | // Defines the interface for a port, which represents a local communication |
| 38 | // mechanism that can be used to create connections to similar mechanisms of |
| 39 | // the other client. Various types of ports will implement this interface. |
| 40 | class PortInterface { |
| 41 | public: |
| 42 | virtual ~PortInterface() {} |
| 43 | |
| 44 | virtual const std::string& Type() const = 0; |
| 45 | virtual rtc::Network* Network() const = 0; |
| 46 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 47 | // Methods to set/get ICE role and tiebreaker values. |
| 48 | virtual void SetIceRole(IceRole role) = 0; |
| 49 | virtual IceRole GetIceRole() const = 0; |
| 50 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 51 | virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0; |
| 52 | virtual uint64_t IceTiebreaker() const = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 53 | |
| 54 | virtual bool SharedSocket() const = 0; |
| 55 | |
Honghai Zhang | f9945b2 | 2015-12-15 20:20:13 | [diff] [blame] | 56 | virtual bool SupportsProtocol(const std::string& protocol) const = 0; |
| 57 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 58 | // PrepareAddress will attempt to get an address for this port that other |
| 59 | // clients can send to. It may take some time before the address is ready. |
| 60 | // Once it is ready, we will send SignalAddressReady. If errors are |
| 61 | // preventing the port from getting an address, it may send |
| 62 | // SignalAddressError. |
| 63 | virtual void PrepareAddress() = 0; |
| 64 | |
| 65 | // Returns the connection to the given address or NULL if none exists. |
| 66 | virtual Connection* GetConnection( |
| 67 | const rtc::SocketAddress& remote_addr) = 0; |
| 68 | |
| 69 | // Creates a new connection to the given address. |
| 70 | enum CandidateOrigin { ORIGIN_THIS_PORT, ORIGIN_OTHER_PORT, ORIGIN_MESSAGE }; |
| 71 | virtual Connection* CreateConnection( |
| 72 | const Candidate& remote_candidate, CandidateOrigin origin) = 0; |
| 73 | |
| 74 | // Functions on the underlying socket(s). |
| 75 | virtual int SetOption(rtc::Socket::Option opt, int value) = 0; |
| 76 | virtual int GetOption(rtc::Socket::Option opt, int* value) = 0; |
| 77 | virtual int GetError() = 0; |
| 78 | |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 79 | virtual ProtocolType GetProtocol() const = 0; |
| 80 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 81 | virtual const std::vector<Candidate>& Candidates() const = 0; |
| 82 | |
| 83 | // Sends the given packet to the given address, provided that the address is |
| 84 | // that of a connection or an address that has sent to us already. |
| 85 | virtual int SendTo(const void* data, size_t size, |
| 86 | const rtc::SocketAddress& addr, |
| 87 | const rtc::PacketOptions& options, bool payload) = 0; |
| 88 | |
| 89 | // Indicates that we received a successful STUN binding request from an |
| 90 | // address that doesn't correspond to any current connection. To turn this |
| 91 | // into a real connection, call CreateConnection. |
| 92 | sigslot::signal6<PortInterface*, const rtc::SocketAddress&, |
| 93 | ProtocolType, IceMessage*, const std::string&, |
| 94 | bool> SignalUnknownAddress; |
| 95 | |
| 96 | // Sends a response message (normal or error) to the given request. One of |
| 97 | // these methods should be called as a response to SignalUnknownAddress. |
| 98 | // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse. |
| 99 | virtual void SendBindingResponse(StunMessage* request, |
| 100 | const rtc::SocketAddress& addr) = 0; |
| 101 | virtual void SendBindingErrorResponse( |
| 102 | StunMessage* request, const rtc::SocketAddress& addr, |
| 103 | int error_code, const std::string& reason) = 0; |
| 104 | |
| 105 | // Signaled when this port decides to delete itself because it no longer has |
| 106 | // any usefulness. |
| 107 | sigslot::signal1<PortInterface*> SignalDestroyed; |
| 108 | |
| 109 | // Signaled when Port discovers ice role conflict with the peer. |
| 110 | sigslot::signal1<PortInterface*> SignalRoleConflict; |
| 111 | |
| 112 | // Normally, packets arrive through a connection (or they result signaling of |
| 113 | // unknown address). Calling this method turns off delivery of packets |
| 114 | // through their respective connection and instead delivers every packet |
| 115 | // through this port. |
| 116 | virtual void EnablePortPackets() = 0; |
| 117 | sigslot::signal4<PortInterface*, const char*, size_t, |
| 118 | const rtc::SocketAddress&> SignalReadPacket; |
| 119 | |
stefan | c1aeaf0 | 2015-10-15 14:26:07 | [diff] [blame] | 120 | // Emitted each time a packet is sent on this port. |
Stefan Holmer | 55674ff | 2016-01-14 14:49:16 | [diff] [blame] | 121 | sigslot::signal1<const rtc::SentPacket&> SignalSentPacket; |
stefan | c1aeaf0 | 2015-10-15 14:26:07 | [diff] [blame] | 122 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 123 | virtual std::string ToString() const = 0; |
| 124 | |
| 125 | protected: |
| 126 | PortInterface() {} |
| 127 | }; |
| 128 | |
| 129 | } // namespace cricket |
| 130 | |
| 131 | #endif // WEBRTC_P2P_BASE_PORTINTERFACE_H_ |