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