henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/async_udp_socket.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | |
| 13 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "rtc_base/network/sent_packet.h" |
| 20 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/time_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace rtc { |
| 24 | |
| 25 | static const int BUF_SIZE = 64 * 1024; |
| 26 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 27 | AsyncUDPSocket* AsyncUDPSocket::Create(AsyncSocket* socket, |
| 28 | const SocketAddress& bind_address) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 29 | std::unique_ptr<AsyncSocket> owned_socket(socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | if (socket->Bind(bind_address) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 31 | RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 32 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | } |
| 34 | return new AsyncUDPSocket(owned_socket.release()); |
| 35 | } |
| 36 | |
| 37 | AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory, |
| 38 | const SocketAddress& bind_address) { |
| 39 | AsyncSocket* socket = |
| 40 | factory->CreateAsyncSocket(bind_address.family(), SOCK_DGRAM); |
| 41 | if (!socket) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 42 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | return Create(socket, bind_address); |
| 44 | } |
| 45 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 46 | AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket) : socket_(socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | size_ = BUF_SIZE; |
| 48 | buf_ = new char[size_]; |
| 49 | |
| 50 | // The socket should start out readable but not writable. |
| 51 | socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent); |
| 52 | socket_->SignalWriteEvent.connect(this, &AsyncUDPSocket::OnWriteEvent); |
| 53 | } |
| 54 | |
| 55 | AsyncUDPSocket::~AsyncUDPSocket() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 56 | delete[] buf_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | SocketAddress AsyncUDPSocket::GetLocalAddress() const { |
| 60 | return socket_->GetLocalAddress(); |
| 61 | } |
| 62 | |
| 63 | SocketAddress AsyncUDPSocket::GetRemoteAddress() const { |
| 64 | return socket_->GetRemoteAddress(); |
| 65 | } |
| 66 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 67 | int AsyncUDPSocket::Send(const void* pv, |
| 68 | size_t cb, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | const rtc::PacketOptions& options) { |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 70 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 71 | options.info_signaled_after_sent); |
Qingsi Wang | 4ea53b3 | 2018-04-16 18:22:31 -0700 | [diff] [blame] | 72 | CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 73 | int ret = socket_->Send(pv, cb); |
| 74 | SignalSentPacket(this, sent_packet); |
| 75 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | int AsyncUDPSocket::SendTo(const void* pv, |
| 79 | size_t cb, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | const SocketAddress& addr, |
| 81 | const rtc::PacketOptions& options) { |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 82 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 83 | options.info_signaled_after_sent); |
Qingsi Wang | 4ea53b3 | 2018-04-16 18:22:31 -0700 | [diff] [blame] | 84 | CopySocketInformationToPacketInfo(cb, *this, true, &sent_packet.info); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 85 | int ret = socket_->SendTo(pv, cb, addr); |
| 86 | SignalSentPacket(this, sent_packet); |
| 87 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int AsyncUDPSocket::Close() { |
| 91 | return socket_->Close(); |
| 92 | } |
| 93 | |
| 94 | AsyncUDPSocket::State AsyncUDPSocket::GetState() const { |
| 95 | return STATE_BOUND; |
| 96 | } |
| 97 | |
| 98 | int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) { |
| 99 | return socket_->GetOption(opt, value); |
| 100 | } |
| 101 | |
| 102 | int AsyncUDPSocket::SetOption(Socket::Option opt, int value) { |
| 103 | return socket_->SetOption(opt, value); |
| 104 | } |
| 105 | |
| 106 | int AsyncUDPSocket::GetError() const { |
| 107 | return socket_->GetError(); |
| 108 | } |
| 109 | |
| 110 | void AsyncUDPSocket::SetError(int error) { |
| 111 | return socket_->SetError(error); |
| 112 | } |
| 113 | |
| 114 | void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 115 | RTC_DCHECK(socket_.get() == socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | |
| 117 | SocketAddress remote_addr; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 118 | int64_t timestamp; |
| 119 | int len = socket_->RecvFrom(buf_, size_, &remote_addr, ×tamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | if (len < 0) { |
| 121 | // An error here typically means we got an ICMP error in response to our |
| 122 | // send datagram, indicating the remote address was unreachable. |
| 123 | // When doing ICE, this kind of thing will often happen. |
| 124 | // TODO: Do something better like forwarding the error to the user. |
| 125 | SocketAddress local_addr = socket_->GetLocalAddress(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 126 | RTC_LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString() |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 127 | << "] receive failed with error " << socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
| 131 | // TODO: Make sure that we got all of the packet. |
| 132 | // If we did not, then we should resize our buffer to be large enough. |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 133 | SignalReadPacket(this, buf_, static_cast<size_t>(len), remote_addr, |
| 134 | (timestamp > -1 ? timestamp : TimeMicros())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) { |
| 138 | SignalReadyToSend(this); |
| 139 | } |
| 140 | |
| 141 | } // namespace rtc |