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 | |
| 11 | #include "webrtc/base/asyncudpsocket.h" |
| 12 | #include "webrtc/base/logging.h" |
| 13 | |
| 14 | namespace rtc { |
| 15 | |
| 16 | static const int BUF_SIZE = 64 * 1024; |
| 17 | |
| 18 | AsyncUDPSocket* AsyncUDPSocket::Create( |
| 19 | 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) { |
| 23 | LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError(); |
| 24 | return NULL; |
| 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) |
| 34 | return NULL; |
| 35 | return Create(socket, bind_address); |
| 36 | } |
| 37 | |
| 38 | AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket) |
| 39 | : socket_(socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | size_ = BUF_SIZE; |
| 41 | buf_ = new char[size_]; |
| 42 | |
| 43 | // The socket should start out readable but not writable. |
| 44 | socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent); |
| 45 | socket_->SignalWriteEvent.connect(this, &AsyncUDPSocket::OnWriteEvent); |
| 46 | } |
| 47 | |
| 48 | AsyncUDPSocket::~AsyncUDPSocket() { |
| 49 | delete [] buf_; |
| 50 | } |
| 51 | |
| 52 | SocketAddress AsyncUDPSocket::GetLocalAddress() const { |
| 53 | return socket_->GetLocalAddress(); |
| 54 | } |
| 55 | |
| 56 | SocketAddress AsyncUDPSocket::GetRemoteAddress() const { |
| 57 | return socket_->GetRemoteAddress(); |
| 58 | } |
| 59 | |
| 60 | int AsyncUDPSocket::Send(const void *pv, size_t cb, |
| 61 | const rtc::PacketOptions& options) { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 62 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis()); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 63 | int ret = socket_->Send(pv, cb); |
| 64 | SignalSentPacket(this, sent_packet); |
| 65 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int AsyncUDPSocket::SendTo(const void *pv, size_t cb, |
| 69 | const SocketAddress& addr, |
| 70 | const rtc::PacketOptions& options) { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 71 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis()); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 72 | int ret = socket_->SendTo(pv, cb, addr); |
| 73 | SignalSentPacket(this, sent_packet); |
| 74 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int AsyncUDPSocket::Close() { |
| 78 | return socket_->Close(); |
| 79 | } |
| 80 | |
| 81 | AsyncUDPSocket::State AsyncUDPSocket::GetState() const { |
| 82 | return STATE_BOUND; |
| 83 | } |
| 84 | |
| 85 | int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) { |
| 86 | return socket_->GetOption(opt, value); |
| 87 | } |
| 88 | |
| 89 | int AsyncUDPSocket::SetOption(Socket::Option opt, int value) { |
| 90 | return socket_->SetOption(opt, value); |
| 91 | } |
| 92 | |
| 93 | int AsyncUDPSocket::GetError() const { |
| 94 | return socket_->GetError(); |
| 95 | } |
| 96 | |
| 97 | void AsyncUDPSocket::SetError(int error) { |
| 98 | return socket_->SetError(error); |
| 99 | } |
| 100 | |
| 101 | void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) { |
| 102 | ASSERT(socket_.get() == socket); |
| 103 | |
| 104 | SocketAddress remote_addr; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 105 | int64_t timestamp; |
| 106 | int len = socket_->RecvFrom(buf_, size_, &remote_addr, ×tamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 107 | if (len < 0) { |
| 108 | // An error here typically means we got an ICMP error in response to our |
| 109 | // send datagram, indicating the remote address was unreachable. |
| 110 | // When doing ICE, this kind of thing will often happen. |
| 111 | // TODO: Do something better like forwarding the error to the user. |
| 112 | SocketAddress local_addr = socket_->GetLocalAddress(); |
| 113 | LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString() << "] " |
| 114 | << "receive failed with error " << socket_->GetError(); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | // TODO: Make sure that we got all of the packet. |
| 119 | // 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^] | 120 | SignalReadPacket( |
| 121 | this, buf_, static_cast<size_t>(len), remote_addr, |
| 122 | (timestamp > -1 ? PacketTime(timestamp, 0) : CreatePacketTime(0))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) { |
| 126 | SignalReadyToSend(this); |
| 127 | } |
| 128 | |
| 129 | } // namespace rtc |