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 | |
| 19 | AsyncUDPSocket* AsyncUDPSocket::Create( |
| 20 | AsyncSocket* socket, |
| 21 | const SocketAddress& bind_address) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 22 | std::unique_ptr<AsyncSocket> owned_socket(socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | if (socket->Bind(bind_address) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 24 | RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 25 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | } |
| 27 | return new AsyncUDPSocket(owned_socket.release()); |
| 28 | } |
| 29 | |
| 30 | AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory, |
| 31 | const SocketAddress& bind_address) { |
| 32 | AsyncSocket* socket = |
| 33 | factory->CreateAsyncSocket(bind_address.family(), SOCK_DGRAM); |
| 34 | if (!socket) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 35 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | return Create(socket, bind_address); |
| 37 | } |
| 38 | |
| 39 | AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket) |
| 40 | : socket_(socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | size_ = BUF_SIZE; |
| 42 | buf_ = new char[size_]; |
| 43 | |
| 44 | // The socket should start out readable but not writable. |
| 45 | socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent); |
| 46 | socket_->SignalWriteEvent.connect(this, &AsyncUDPSocket::OnWriteEvent); |
| 47 | } |
| 48 | |
| 49 | AsyncUDPSocket::~AsyncUDPSocket() { |
| 50 | delete [] buf_; |
| 51 | } |
| 52 | |
| 53 | SocketAddress AsyncUDPSocket::GetLocalAddress() const { |
| 54 | return socket_->GetLocalAddress(); |
| 55 | } |
| 56 | |
| 57 | SocketAddress AsyncUDPSocket::GetRemoteAddress() const { |
| 58 | return socket_->GetRemoteAddress(); |
| 59 | } |
| 60 | |
| 61 | int AsyncUDPSocket::Send(const void *pv, size_t cb, |
| 62 | const rtc::PacketOptions& options) { |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 63 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 64 | options.info_signaled_after_sent); |
Qingsi Wang | 4ea53b3 | 2018-04-16 18:22:31 -0700 | [diff] [blame^] | 65 | CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 66 | int ret = socket_->Send(pv, cb); |
| 67 | SignalSentPacket(this, sent_packet); |
| 68 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int AsyncUDPSocket::SendTo(const void *pv, size_t cb, |
| 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() |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 120 | << "] receive failed with error " |
| 121 | << socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
| 125 | // TODO: Make sure that we got all of the packet. |
| 126 | // 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] | 127 | SignalReadPacket( |
| 128 | this, buf_, static_cast<size_t>(len), remote_addr, |
| 129 | (timestamp > -1 ? PacketTime(timestamp, 0) : CreatePacketTime(0))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) { |
| 133 | SignalReadyToSend(this); |
| 134 | } |
| 135 | |
| 136 | } // namespace rtc |