blob: 232d264ab10c74f1ccd5fc7606d4787dc0acf420 [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
11#include "webrtc/base/asyncudpsocket.h"
12#include "webrtc/base/logging.h"
13
14namespace rtc {
15
16static const int BUF_SIZE = 64 * 1024;
17
18AsyncUDPSocket* AsyncUDPSocket::Create(
19 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) {
23 LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError();
24 return NULL;
25 }
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)
34 return NULL;
35 return Create(socket, bind_address);
36}
37
38AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket)
39 : socket_(socket) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 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
48AsyncUDPSocket::~AsyncUDPSocket() {
49 delete [] buf_;
50}
51
52SocketAddress AsyncUDPSocket::GetLocalAddress() const {
53 return socket_->GetLocalAddress();
54}
55
56SocketAddress AsyncUDPSocket::GetRemoteAddress() const {
57 return socket_->GetRemoteAddress();
58}
59
60int AsyncUDPSocket::Send(const void *pv, size_t cb,
61 const rtc::PacketOptions& options) {
Honghai Zhang82d78622016-05-06 11:29:15 -070062 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis());
stefanc1aeaf02015-10-15 07:26:07 -070063 int ret = socket_->Send(pv, cb);
64 SignalSentPacket(this, sent_packet);
65 return ret;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066}
67
68int AsyncUDPSocket::SendTo(const void *pv, size_t cb,
69 const SocketAddress& addr,
70 const rtc::PacketOptions& options) {
Honghai Zhang82d78622016-05-06 11:29:15 -070071 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis());
stefanc1aeaf02015-10-15 07:26:07 -070072 int ret = socket_->SendTo(pv, cb, addr);
73 SignalSentPacket(this, sent_packet);
74 return ret;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075}
76
77int AsyncUDPSocket::Close() {
78 return socket_->Close();
79}
80
81AsyncUDPSocket::State AsyncUDPSocket::GetState() const {
82 return STATE_BOUND;
83}
84
85int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) {
86 return socket_->GetOption(opt, value);
87}
88
89int AsyncUDPSocket::SetOption(Socket::Option opt, int value) {
90 return socket_->SetOption(opt, value);
91}
92
93int AsyncUDPSocket::GetError() const {
94 return socket_->GetError();
95}
96
97void AsyncUDPSocket::SetError(int error) {
98 return socket_->SetError(error);
99}
100
101void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) {
102 ASSERT(socket_.get() == socket);
103
104 SocketAddress remote_addr;
Stefan Holmer9131efd2016-05-23 18:19:26 +0200105 int64_t timestamp;
106 int len = socket_->RecvFrom(buf_, size_, &remote_addr, &timestamp);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107 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 Holmer9131efd2016-05-23 18:19:26 +0200120 SignalReadPacket(
121 this, buf_, static_cast<size_t>(len), remote_addr,
122 (timestamp > -1 ? PacketTime(timestamp, 0) : CreatePacketTime(0)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123}
124
125void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) {
126 SignalReadyToSend(this);
127}
128
129} // namespace rtc