3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/asyncudpsocket.h"
29 #include "talk/base/logging.h"
33 static const int BUF_SIZE = 64 * 1024;
35 AsyncUDPSocket* AsyncUDPSocket::Create(
37 const SocketAddress& bind_address) {
38 scoped_ptr<AsyncSocket> owned_socket(socket);
39 if (socket->Bind(bind_address) < 0) {
40 LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError();
43 return new AsyncUDPSocket(owned_socket.release());
46 AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory,
47 const SocketAddress& bind_address) {
49 factory->CreateAsyncSocket(bind_address.family(), SOCK_DGRAM);
52 return Create(socket, bind_address);
55 AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket)
59 buf_ = new char[size_];
61 // The socket should start out readable but not writable.
62 socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent);
63 socket_->SignalWriteEvent.connect(this, &AsyncUDPSocket::OnWriteEvent);
66 AsyncUDPSocket::~AsyncUDPSocket() {
70 SocketAddress AsyncUDPSocket::GetLocalAddress() const {
71 return socket_->GetLocalAddress();
74 SocketAddress AsyncUDPSocket::GetRemoteAddress() const {
75 return socket_->GetRemoteAddress();
78 int AsyncUDPSocket::Send(const void *pv, size_t cb,
79 const talk_base::PacketOptions& options) {
80 return socket_->Send(pv, cb);
83 int AsyncUDPSocket::SendTo(const void *pv, size_t cb,
84 const SocketAddress& addr,
85 const talk_base::PacketOptions& options) {
86 return socket_->SendTo(pv, cb, addr);
89 int AsyncUDPSocket::Close() {
90 return socket_->Close();
93 AsyncUDPSocket::State AsyncUDPSocket::GetState() const {
97 int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) {
98 return socket_->GetOption(opt, value);
101 int AsyncUDPSocket::SetOption(Socket::Option opt, int value) {
102 return socket_->SetOption(opt, value);
105 int AsyncUDPSocket::GetError() const {
106 return socket_->GetError();
109 void AsyncUDPSocket::SetError(int error) {
110 return socket_->SetError(error);
113 void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) {
114 ASSERT(socket_.get() == socket);
116 SocketAddress remote_addr;
117 int len = socket_->RecvFrom(buf_, size_, &remote_addr);
119 // An error here typically means we got an ICMP error in response to our
120 // send datagram, indicating the remote address was unreachable.
121 // When doing ICE, this kind of thing will often happen.
122 // TODO: Do something better like forwarding the error to the user.
123 SocketAddress local_addr = socket_->GetLocalAddress();
124 LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString() << "] "
125 << "receive failed with error " << socket_->GetError();
129 // TODO: Make sure that we got all of the packet.
130 // If we did not, then we should resize our buffer to be large enough.
131 SignalReadPacket(this, buf_, static_cast<size_t>(len), remote_addr,
132 CreatePacketTime(0));
135 void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) {
136 SignalReadyToSend(this);
139 } // namespace talk_base