blob: ba5fa88aadb3e2f4d6f4124a934f144d502368eb [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
Yves Gerey665174f2018-06-19 15:03:05 +020019AsyncUDPSocket* AsyncUDPSocket::Create(AsyncSocket* socket,
20 const SocketAddress& bind_address) {
jbauch555604a2016-04-26 03:13:22 -070021 std::unique_ptr<AsyncSocket> owned_socket(socket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022 if (socket->Bind(bind_address) < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010023 RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError();
deadbeef37f5ecf2017-02-27 14:06:41 -080024 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025 }
26 return new AsyncUDPSocket(owned_socket.release());
27}
28
29AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory,
30 const SocketAddress& bind_address) {
31 AsyncSocket* socket =
32 factory->CreateAsyncSocket(bind_address.family(), SOCK_DGRAM);
33 if (!socket)
deadbeef37f5ecf2017-02-27 14:06:41 -080034 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035 return Create(socket, bind_address);
36}
37
Yves Gerey665174f2018-06-19 15:03:05 +020038AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket) : socket_(socket) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039 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
47AsyncUDPSocket::~AsyncUDPSocket() {
Yves Gerey665174f2018-06-19 15:03:05 +020048 delete[] buf_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049}
50
51SocketAddress AsyncUDPSocket::GetLocalAddress() const {
52 return socket_->GetLocalAddress();
53}
54
55SocketAddress AsyncUDPSocket::GetRemoteAddress() const {
56 return socket_->GetRemoteAddress();
57}
58
Yves Gerey665174f2018-06-19 15:03:05 +020059int AsyncUDPSocket::Send(const void* pv,
60 size_t cb,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061 const rtc::PacketOptions& options) {
Qingsi Wang6e641e62018-04-11 20:14:17 -070062 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(),
63 options.info_signaled_after_sent);
Qingsi Wang4ea53b32018-04-16 18:22:31 -070064 CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info);
stefanc1aeaf02015-10-15 07:26:07 -070065 int ret = socket_->Send(pv, cb);
66 SignalSentPacket(this, sent_packet);
67 return ret;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068}
69
Yves Gerey665174f2018-06-19 15:03:05 +020070int AsyncUDPSocket::SendTo(const void* pv,
71 size_t cb,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 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()
Yves Gerey665174f2018-06-19 15:03:05 +0200120 << "] receive failed with error " << socket_->GetError();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 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 Holmer9131efd2016-05-23 18:19:26 +0200126 SignalReadPacket(
127 this, buf_, static_cast<size_t>(len), remote_addr,
128 (timestamp > -1 ? PacketTime(timestamp, 0) : CreatePacketTime(0)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129}
130
131void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) {
132 SignalReadyToSend(this);
133}
134
135} // namespace rtc