henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 78 | int AsyncUDPSocket::Send(const void *pv, size_t cb, |
| 79 | const talk_base::PacketOptions& options) { |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | return socket_->Send(pv, cb); |
| 81 | } |
| 82 | |
mallinath@webrtc.org | 391247d | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 83 | int AsyncUDPSocket::SendTo(const void *pv, size_t cb, |
mallinath@webrtc.org | f5e5b3a | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 84 | const SocketAddress& addr, |
| 85 | const talk_base::PacketOptions& options) { |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | return socket_->SendTo(pv, cb, addr); |
| 87 | } |
| 88 | |
| 89 | int AsyncUDPSocket::Close() { |
| 90 | return socket_->Close(); |
| 91 | } |
| 92 | |
| 93 | AsyncUDPSocket::State AsyncUDPSocket::GetState() const { |
| 94 | return STATE_BOUND; |
| 95 | } |
| 96 | |
| 97 | int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) { |
| 98 | return socket_->GetOption(opt, value); |
| 99 | } |
| 100 | |
| 101 | int AsyncUDPSocket::SetOption(Socket::Option opt, int value) { |
| 102 | return socket_->SetOption(opt, value); |
| 103 | } |
| 104 | |
| 105 | int AsyncUDPSocket::GetError() const { |
| 106 | return socket_->GetError(); |
| 107 | } |
| 108 | |
| 109 | void AsyncUDPSocket::SetError(int error) { |
| 110 | return socket_->SetError(error); |
| 111 | } |
| 112 | |
| 113 | void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) { |
| 114 | ASSERT(socket_.get() == socket); |
| 115 | |
| 116 | SocketAddress remote_addr; |
| 117 | int len = socket_->RecvFrom(buf_, size_, &remote_addr); |
| 118 | if (len < 0) { |
| 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(); |
| 126 | return; |
| 127 | } |
| 128 | |
| 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. |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 131 | SignalReadPacket(this, buf_, static_cast<size_t>(len), remote_addr, |
| 132 | CreatePacketTime(0)); |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) { |
| 136 | SignalReadyToSend(this); |
| 137 | } |
| 138 | |
| 139 | } // namespace talk_base |