async_packet_socket.h 5.1 KB

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