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.

141 lines
4.5 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. #include "rtc_base/async_udp_socket.h"
  11. #include <stdint.h>
  12. #include <string>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/logging.h"
  15. #include "rtc_base/network/sent_packet.h"
  16. #include "rtc_base/third_party/sigslot/sigslot.h"
  17. #include "rtc_base/time_utils.h"
  18. namespace rtc {
  19. static const int BUF_SIZE = 64 * 1024;
  20. AsyncUDPSocket* AsyncUDPSocket::Create(AsyncSocket* socket,
  21. const SocketAddress& bind_address) {
  22. std::unique_ptr<AsyncSocket> owned_socket(socket);
  23. if (socket->Bind(bind_address) < 0) {
  24. RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError();
  25. return nullptr;
  26. }
  27. return new AsyncUDPSocket(owned_socket.release());
  28. }
  29. AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory,
  30. const SocketAddress& bind_address) {
  31. AsyncSocket* socket =
  32. factory->CreateAsyncSocket(bind_address.family(), SOCK_DGRAM);
  33. if (!socket)
  34. return nullptr;
  35. return Create(socket, bind_address);
  36. }
  37. AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket) : socket_(socket) {
  38. size_ = BUF_SIZE;
  39. buf_ = new char[size_];
  40. // The socket should start out readable but not writable.
  41. socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent);
  42. socket_->SignalWriteEvent.connect(this, &AsyncUDPSocket::OnWriteEvent);
  43. }
  44. AsyncUDPSocket::~AsyncUDPSocket() {
  45. delete[] buf_;
  46. }
  47. SocketAddress AsyncUDPSocket::GetLocalAddress() const {
  48. return socket_->GetLocalAddress();
  49. }
  50. SocketAddress AsyncUDPSocket::GetRemoteAddress() const {
  51. return socket_->GetRemoteAddress();
  52. }
  53. int AsyncUDPSocket::Send(const void* pv,
  54. size_t cb,
  55. const rtc::PacketOptions& options) {
  56. rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(),
  57. options.info_signaled_after_sent);
  58. CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info);
  59. int ret = socket_->Send(pv, cb);
  60. SignalSentPacket(this, sent_packet);
  61. return ret;
  62. }
  63. int AsyncUDPSocket::SendTo(const void* pv,
  64. size_t cb,
  65. const SocketAddress& addr,
  66. const rtc::PacketOptions& options) {
  67. rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(),
  68. options.info_signaled_after_sent);
  69. CopySocketInformationToPacketInfo(cb, *this, true, &sent_packet.info);
  70. int ret = socket_->SendTo(pv, cb, addr);
  71. SignalSentPacket(this, sent_packet);
  72. return ret;
  73. }
  74. int AsyncUDPSocket::Close() {
  75. return socket_->Close();
  76. }
  77. AsyncUDPSocket::State AsyncUDPSocket::GetState() const {
  78. return STATE_BOUND;
  79. }
  80. int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) {
  81. return socket_->GetOption(opt, value);
  82. }
  83. int AsyncUDPSocket::SetOption(Socket::Option opt, int value) {
  84. return socket_->SetOption(opt, value);
  85. }
  86. int AsyncUDPSocket::GetError() const {
  87. return socket_->GetError();
  88. }
  89. void AsyncUDPSocket::SetError(int error) {
  90. return socket_->SetError(error);
  91. }
  92. void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) {
  93. RTC_DCHECK(socket_.get() == socket);
  94. SocketAddress remote_addr;
  95. int64_t timestamp;
  96. int len = socket_->RecvFrom(buf_, size_, &remote_addr, &timestamp);
  97. if (len < 0) {
  98. // An error here typically means we got an ICMP error in response to our
  99. // send datagram, indicating the remote address was unreachable.
  100. // When doing ICE, this kind of thing will often happen.
  101. // TODO: Do something better like forwarding the error to the user.
  102. SocketAddress local_addr = socket_->GetLocalAddress();
  103. RTC_LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString()
  104. << "] receive failed with error " << socket_->GetError();
  105. return;
  106. }
  107. // TODO: Make sure that we got all of the packet.
  108. // If we did not, then we should resize our buffer to be large enough.
  109. SignalReadPacket(this, buf_, static_cast<size_t>(len), remote_addr,
  110. (timestamp > -1 ? timestamp : TimeMicros()));
  111. }
  112. void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) {
  113. SignalReadyToSend(this);
  114. }
  115. } // namespace rtc