port_interface.h 5.0 KB

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