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#if defined(_MSC_VER) && _MSC_VER < 1300
29#pragma warning(disable:4786)
30#endif
31
32#include "talk/base/asyncudpsocket.h"
33#include "talk/base/logging.h"
34
35namespace talk_base {
36
37static const int BUF_SIZE = 64 * 1024;
38
39AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory,
40                                       const SocketAddress& address) {
41  scoped_ptr<AsyncSocket> socket(factory->CreateAsyncSocket(SOCK_DGRAM));
42  if (!socket.get())
43    return NULL;
44  if (socket->Bind(address)) {
45    LOG(LS_INFO) << "Failed to bind UDP socket " << socket->GetError();
46    return NULL;
47  }
48  return new AsyncUDPSocket(socket.release());
49}
50
51AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket)
52    : socket_(socket) {
53  ASSERT(socket_.get() != NULL);
54  size_ = BUF_SIZE;
55  buf_ = new char[size_];
56
57  // The socket should start out readable but not writable.
58  socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent);
59}
60
61AsyncUDPSocket::~AsyncUDPSocket() {
62  delete [] buf_;
63}
64
65SocketAddress AsyncUDPSocket::GetLocalAddress(bool* allocated) const {
66  if (allocated)
67    *allocated = true;
68  return socket_->GetLocalAddress();
69}
70
71SocketAddress AsyncUDPSocket::GetRemoteAddress() const {
72  return socket_->GetRemoteAddress();
73}
74
75int AsyncUDPSocket::Send(const void *pv, size_t cb) {
76  return socket_->Send(pv, cb);
77}
78
79int AsyncUDPSocket::SendTo(
80    const void *pv, size_t cb, const SocketAddress& addr) {
81  return socket_->SendTo(pv, cb, addr);
82}
83
84int AsyncUDPSocket::Close() {
85  return socket_->Close();
86}
87
88Socket::ConnState AsyncUDPSocket::GetState() const {
89  return socket_->GetState();
90}
91
92int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) {
93  return socket_->GetOption(opt, value);
94}
95
96int AsyncUDPSocket::SetOption(Socket::Option opt, int value) {
97  return socket_->SetOption(opt, value);
98}
99
100int AsyncUDPSocket::GetError() const {
101  return socket_->GetError();
102}
103
104void AsyncUDPSocket::SetError(int error) {
105  return socket_->SetError(error);
106}
107
108void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) {
109  ASSERT(socket_.get() == socket);
110
111  SocketAddress remote_addr;
112  int len = socket_->RecvFrom(buf_, size_, &remote_addr);
113  if (len < 0) {
114    // An error here typically means we got an ICMP error in response to our
115    // send datagram, indicating the remote address was unreachable.
116    // When doing ICE, this kind of thing will often happen.
117    // TODO: Do something better like forwarding the error to the user.
118    SocketAddress local_addr = socket_->GetLocalAddress();
119    LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToString() << "] "
120                 << "receive failed with error " << socket_->GetError();
121    return;
122  }
123
124  // TODO: Make sure that we got all of the packet.
125  // If we did not, then we should resize our buffer to be large enough.
126  SignalReadPacket(this, buf_, (size_t)len, remote_addr);
127}
128
129}  // namespace talk_base
130