blob: 0896e50f496a8d76a8994335d612beeb2d997dc6 [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) {
Honghai Zhang82d78622016-05-06 11:29:15 -070063 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis());
stefanc1aeaf02015-10-15 07:26:07 -070064 int ret = socket_->Send(pv, cb);
65 SignalSentPacket(this, sent_packet);
66 return ret;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067}
68
69int AsyncUDPSocket::SendTo(const void *pv, size_t cb,
70 const SocketAddress& addr,
71 const rtc::PacketOptions& options) {
Honghai Zhang82d78622016-05-06 11:29:15 -070072 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis());
stefanc1aeaf02015-10-15 07:26:07 -070073 int ret = socket_->SendTo(pv, cb, addr);
74 SignalSentPacket(this, sent_packet);
75 return ret;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076}
77
78int AsyncUDPSocket::Close() {
79 return socket_->Close();
80}
81
82AsyncUDPSocket::State AsyncUDPSocket::GetState() const {
83 return STATE_BOUND;
84}
85
86int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) {
87 return socket_->GetOption(opt, value);
88}
89
90int AsyncUDPSocket::SetOption(Socket::Option opt, int value) {
91 return socket_->SetOption(opt, value);
92}
93
94int AsyncUDPSocket::GetError() const {
95 return socket_->GetError();
96}
97
98void AsyncUDPSocket::SetError(int error) {
99 return socket_->SetError(error);
100}
101
102void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) {
nisseede5da42017-01-12 05:15:36 -0800103 RTC_DCHECK(socket_.get() == socket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104
105 SocketAddress remote_addr;
Stefan Holmer9131efd2016-05-23 18:19:26 +0200106 int64_t timestamp;
107 int len = socket_->RecvFrom(buf_, size_, &remote_addr, &timestamp);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 if (len < 0) {
109 // An error here typically means we got an ICMP error in response to our
110 // send datagram, indicating the remote address was unreachable.
111 // When doing ICE, this kind of thing will often happen.
112 // TODO: Do something better like forwarding the error to the user.
113 SocketAddress local_addr = socket_->GetLocalAddress();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString()
115 << "] "
116 << "receive failed with error " << socket_->GetError();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117 return;
118 }
119
120 // TODO: Make sure that we got all of the packet.
121 // If we did not, then we should resize our buffer to be large enough.
Stefan Holmer9131efd2016-05-23 18:19:26 +0200122 SignalReadPacket(
123 this, buf_, static_cast<size_t>(len), remote_addr,
124 (timestamp > -1 ? PacketTime(timestamp, 0) : CreatePacketTime(0)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125}
126
127void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) {
128 SignalReadyToSend(this);
129}
130
131} // namespace rtc