1 /* 2 * libjingle 3 * Copyright 2004--2005, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 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. 15 * 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. 26 */ 27 28 #include "talk/base/asyncudpsocket.h" 29 #include "talk/base/logging.h" 30 31 namespace talk_base { 32 33 static const int BUF_SIZE = 64 * 1024; 34 35 AsyncUDPSocket* AsyncUDPSocket::Create( 36 AsyncSocket* socket, 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(); 41 return NULL; 42 } 43 return new AsyncUDPSocket(owned_socket.release()); 44 } 45 46 AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory, 47 const SocketAddress& bind_address) { 48 AsyncSocket* socket = 49 factory->CreateAsyncSocket(bind_address.family(), SOCK_DGRAM); 50 if (!socket) 51 return NULL; 52 return Create(socket, bind_address); 53 } 54 55 AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket) 56 : socket_(socket) { 57 ASSERT(socket_); 58 size_ = BUF_SIZE; 59 buf_ = new char[size_]; 60 61 // The socket should start out readable but not writable. 62 socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent); 63 socket_->SignalWriteEvent.connect(this, &AsyncUDPSocket::OnWriteEvent); 64 } 65 66 AsyncUDPSocket::~AsyncUDPSocket() { 67 delete [] buf_; 68 } 69 70 SocketAddress AsyncUDPSocket::GetLocalAddress() const { 71 return socket_->GetLocalAddress(); 72 } 73 74 SocketAddress AsyncUDPSocket::GetRemoteAddress() const { 75 return socket_->GetRemoteAddress(); 76 } 77 78 int AsyncUDPSocket::Send(const void *pv, size_t cb) { 79 return socket_->Send(pv, cb); 80 } 81 82 int AsyncUDPSocket::SendTo( 83 const void *pv, size_t cb, const SocketAddress& addr) { 84 return socket_->SendTo(pv, cb, addr); 85 } 86 87 int AsyncUDPSocket::Close() { 88 return socket_->Close(); 89 } 90 91 AsyncUDPSocket::State AsyncUDPSocket::GetState() const { 92 return STATE_BOUND; 93 } 94 95 int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) { 96 return socket_->GetOption(opt, value); 97 } 98 99 int AsyncUDPSocket::SetOption(Socket::Option opt, int value) { 100 return socket_->SetOption(opt, value); 101 } 102 103 int AsyncUDPSocket::GetError() const { 104 return socket_->GetError(); 105 } 106 107 void AsyncUDPSocket::SetError(int error) { 108 return socket_->SetError(error); 109 } 110 111 void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) { 112 ASSERT(socket_.get() == socket); 113 114 SocketAddress remote_addr; 115 int len = socket_->RecvFrom(buf_, size_, &remote_addr); 116 if (len < 0) { 117 // An error here typically means we got an ICMP error in response to our 118 // send datagram, indicating the remote address was unreachable. 119 // When doing ICE, this kind of thing will often happen. 120 // TODO: Do something better like forwarding the error to the user. 121 SocketAddress local_addr = socket_->GetLocalAddress(); 122 LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString() << "] " 123 << "receive failed with error " << socket_->GetError(); 124 return; 125 } 126 127 // TODO: Make sure that we got all of the packet. 128 // If we did not, then we should resize our buffer to be large enough. 129 SignalReadPacket(this, buf_, (size_t)len, remote_addr); 130 } 131 132 void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) { 133 SignalReadyToSend(this); 134 } 135 136 } // namespace talk_base 137