blob: d8e60b9a4225509fa8ee3450f6e0ace21bf190ee [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/asyncudpsocket.h"
12#include "rtc_base/checks.h"
13#include "rtc_base/logging.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014
15namespace rtc {
16
17static const int BUF_SIZE = 64 * 1024;
18
19AsyncUDPSocket* AsyncUDPSocket::Create(
20 AsyncSocket* socket,
21 const SocketAddress& bind_address) {
jbauch555604a2016-04-26 03:13:22 -070022 std::unique_ptr<AsyncSocket> owned_socket(socket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023 if (socket->Bind(bind_address) < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010024 RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError();
deadbeef37f5ecf2017-02-27 14:06:41 -080025 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026 }
27 return new AsyncUDPSocket(owned_socket.release());
28}
29
30AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory,
31 const SocketAddress& bind_address) {
32 AsyncSocket* socket =
33 factory->CreateAsyncSocket(bind_address.family(), SOCK_DGRAM);
34 if (!socket)
deadbeef37f5ecf2017-02-27 14:06:41 -080035 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036 return Create(socket, bind_address);
37}
38
39AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket)
40 : socket_(socket) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 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
49AsyncUDPSocket::~AsyncUDPSocket() {
50 delete [] buf_;
51}
52
53SocketAddress AsyncUDPSocket::GetLocalAddress() const {
54 return socket_->GetLocalAddress();
55}
56
57SocketAddress AsyncUDPSocket::GetRemoteAddress() const {
58 return socket_->GetRemoteAddress();
59}
60
61int AsyncUDPSocket::Send(const void *pv, size_t cb,
62 const rtc::PacketOptions& options) {
Qingsi Wang6e641e62018-04-11 20:14:17 -070063 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(),
64 options.info_signaled_after_sent);
Qingsi Wang4ea53b32018-04-16 18:22:31 -070065 CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info);
stefanc1aeaf02015-10-15 07:26:07 -070066 int ret = socket_->Send(pv, cb);
67 SignalSentPacket(this, sent_packet);
68 return ret;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069}
70
71int AsyncUDPSocket::SendTo(const void *pv, size_t cb,
72 const SocketAddress& addr,
73 const rtc::PacketOptions& options) {
Qingsi Wang6e641e62018-04-11 20:14:17 -070074 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(),
75 options.info_signaled_after_sent);
Qingsi Wang4ea53b32018-04-16 18:22:31 -070076 CopySocketInformationToPacketInfo(cb, *this, true, &sent_packet.info);
Qingsi Wang6e641e62018-04-11 20:14:17 -070077 sent_packet.info.remote_socket_address = addr;
stefanc1aeaf02015-10-15 07:26:07 -070078 int ret = socket_->SendTo(pv, cb, addr);
79 SignalSentPacket(this, sent_packet);
80 return ret;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081}
82
83int AsyncUDPSocket::Close() {
84 return socket_->Close();
85}
86
87AsyncUDPSocket::State AsyncUDPSocket::GetState() const {
88 return STATE_BOUND;
89}
90
91int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) {
92 return socket_->GetOption(opt, value);
93}
94
95int AsyncUDPSocket::SetOption(Socket::Option opt, int value) {
96 return socket_->SetOption(opt, value);
97}
98
99int AsyncUDPSocket::GetError() const {
100 return socket_->GetError();
101}
102
103void AsyncUDPSocket::SetError(int error) {
104 return socket_->SetError(error);
105}
106
107void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) {
nisseede5da42017-01-12 05:15:36 -0800108 RTC_DCHECK(socket_.get() == socket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109
110 SocketAddress remote_addr;
Stefan Holmer9131efd2016-05-23 18:19:26 +0200111 int64_t timestamp;
112 int len = socket_->RecvFrom(buf_, size_, &remote_addr, &timestamp);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 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 Bonadei675513b2017-11-09 11:09:25 +0100119 RTC_LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString()
Jonas Olsson45cc8902018-02-13 10:37:07 +0100120 << "] receive failed with error "
121 << socket_->GetError();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 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 Holmer9131efd2016-05-23 18:19:26 +0200127 SignalReadPacket(
128 this, buf_, static_cast<size_t>(len), remote_addr,
129 (timestamp > -1 ? PacketTime(timestamp, 0) : CreatePacketTime(0)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130}
131
132void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) {
133 SignalReadyToSend(this);
134}
135
136} // namespace rtc