You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef RTC_BASE_ASYNC_PACKET_SOCKET_H_
  11. #define RTC_BASE_ASYNC_PACKET_SOCKET_H_
  12. #include "rtc_base/constructor_magic.h"
  13. #include "rtc_base/dscp.h"
  14. #include "rtc_base/network/sent_packet.h"
  15. #include "rtc_base/socket.h"
  16. #include "rtc_base/system/rtc_export.h"
  17. #include "rtc_base/third_party/sigslot/sigslot.h"
  18. #include "rtc_base/time_utils.h"
  19. namespace rtc {
  20. // This structure holds the info needed to update the packet send time header
  21. // extension, including the information needed to update the authentication tag
  22. // after changing the value.
  23. struct PacketTimeUpdateParams {
  24. PacketTimeUpdateParams();
  25. PacketTimeUpdateParams(const PacketTimeUpdateParams& other);
  26. ~PacketTimeUpdateParams();
  27. int rtp_sendtime_extension_id = -1; // extension header id present in packet.
  28. std::vector<char> srtp_auth_key; // Authentication key.
  29. int srtp_auth_tag_len = -1; // Authentication tag length.
  30. int64_t srtp_packet_index = -1; // Required for Rtp Packet authentication.
  31. };
  32. // This structure holds meta information for the packet which is about to send
  33. // over network.
  34. struct RTC_EXPORT PacketOptions {
  35. PacketOptions();
  36. explicit PacketOptions(DiffServCodePoint dscp);
  37. PacketOptions(const PacketOptions& other);
  38. ~PacketOptions();
  39. DiffServCodePoint dscp = DSCP_NO_CHANGE;
  40. // When used with RTP packets (for example, webrtc::PacketOptions), the value
  41. // should be 16 bits. A value of -1 represents "not set".
  42. int64_t packet_id = -1;
  43. PacketTimeUpdateParams packet_time_params;
  44. // PacketInfo is passed to SentPacket when signaling this packet is sent.
  45. PacketInfo info_signaled_after_sent;
  46. };
  47. // Provides the ability to receive packets asynchronously. Sends are not
  48. // buffered since it is acceptable to drop packets under high load.
  49. class RTC_EXPORT AsyncPacketSocket : public sigslot::has_slots<> {
  50. public:
  51. enum State {
  52. STATE_CLOSED,
  53. STATE_BINDING,
  54. STATE_BOUND,
  55. STATE_CONNECTING,
  56. STATE_CONNECTED
  57. };
  58. AsyncPacketSocket();
  59. ~AsyncPacketSocket() override;
  60. // Returns current local address. Address may be set to null if the
  61. // socket is not bound yet (GetState() returns STATE_BINDING).
  62. virtual SocketAddress GetLocalAddress() const = 0;
  63. // Returns remote address. Returns zeroes if this is not a client TCP socket.
  64. virtual SocketAddress GetRemoteAddress() const = 0;
  65. // Send a packet.
  66. virtual int Send(const void* pv, size_t cb, const PacketOptions& options) = 0;
  67. virtual int SendTo(const void* pv,
  68. size_t cb,
  69. const SocketAddress& addr,
  70. const PacketOptions& options) = 0;
  71. // Close the socket.
  72. virtual int Close() = 0;
  73. // Returns current state of the socket.
  74. virtual State GetState() const = 0;
  75. // Get/set options.
  76. virtual int GetOption(Socket::Option opt, int* value) = 0;
  77. virtual int SetOption(Socket::Option opt, int value) = 0;
  78. // Get/Set current error.
  79. // TODO: Remove SetError().
  80. virtual int GetError() const = 0;
  81. virtual void SetError(int error) = 0;
  82. // Emitted each time a packet is read. Used only for UDP and
  83. // connected TCP sockets.
  84. sigslot::signal5<AsyncPacketSocket*,
  85. const char*,
  86. size_t,
  87. const SocketAddress&,
  88. // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
  89. // timestamp by value.
  90. const int64_t&>
  91. SignalReadPacket;
  92. // Emitted each time a packet is sent.
  93. sigslot::signal2<AsyncPacketSocket*, const SentPacket&> SignalSentPacket;
  94. // Emitted when the socket is currently able to send.
  95. sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
  96. // Emitted after address for the socket is allocated, i.e. binding
  97. // is finished. State of the socket is changed from BINDING to BOUND
  98. // (for UDP and server TCP sockets) or CONNECTING (for client TCP
  99. // sockets).
  100. sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
  101. // Emitted for client TCP sockets when state is changed from
  102. // CONNECTING to CONNECTED.
  103. sigslot::signal1<AsyncPacketSocket*> SignalConnect;
  104. // Emitted for client TCP sockets when state is changed from
  105. // CONNECTED to CLOSED.
  106. sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
  107. // Used only for listening TCP sockets.
  108. sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
  109. private:
  110. RTC_DISALLOW_COPY_AND_ASSIGN(AsyncPacketSocket);
  111. };
  112. void CopySocketInformationToPacketInfo(size_t packet_size_bytes,
  113. const AsyncPacketSocket& socket_from,
  114. bool is_connectionless,
  115. rtc::PacketInfo* info);
  116. } // namespace rtc
  117. #endif // RTC_BASE_ASYNC_PACKET_SOCKET_H_