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