asyncudpsocket.cc revision f74420b3285b9fe04a7e00aa3b8c0ab07ea344bc
1f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch/*
2f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * libjingle
3f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * Copyright 2004--2005, Google Inc.
4f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *
5f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * Redistribution and use in source and binary forms, with or without
6f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * modification, are permitted provided that the following conditions are met:
7f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *
8f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *  1. Redistributions of source code must retain the above copyright notice,
9f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *     this list of conditions and the following disclaimer.
10f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *  2. Redistributions in binary form must reproduce the above copyright notice,
11f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *     this list of conditions and the following disclaimer in the documentation
12f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *     and/or other materials provided with the distribution.
13f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *  3. The name of the author may not be used to endorse or promote products
14f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *     derived from this software without specific prior written permission.
15f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch *
16f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch */
27f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
28f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#if defined(_MSC_VER) && _MSC_VER < 1300
29f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#pragma warning(disable:4786)
30f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#endif
31f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
32f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#include "talk/base/asyncudpsocket.h"
33f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#include "talk/base/logging.h"
34f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
35f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochnamespace talk_base {
36f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
37f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochconst int BUF_SIZE = 64 * 1024;
38f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
39f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen MurdochAsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket)
40f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch    : AsyncPacketSocket(socket) {
41f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  ASSERT(socket_ != NULL);
42f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  size_ = BUF_SIZE;
43f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  buf_ = new char[size_];
44f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
45f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  // The socket should start out readable but not writable.
46f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent);
47f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch}
48f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
49f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen MurdochAsyncUDPSocket::~AsyncUDPSocket() {
50f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  delete [] buf_;
51f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch}
52f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
53f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochvoid AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) {
54f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  ASSERT(socket == socket_);
55f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
56f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  SocketAddress remote_addr;
57f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  int len = socket_->RecvFrom(buf_, size_, &remote_addr);
58f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  if (len < 0) {
59f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch    // An error here typically means we got an ICMP error in response to our
60f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch    // send datagram, indicating the remote address was unreachable.
61f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch    // When doing ICE, this kind of thing will often happen.
62f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch    // TODO(juberti): Do something better like forwarding the error to the user.
63f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch    SocketAddress local_addr = socket_->GetLocalAddress();
64f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch    LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToString() << "] "
65f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch                 << "receive failed with error " << socket_->GetError();
66f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch    return;
67f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  }
68f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
69f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  // TODO(juberti): Make sure that we got all of the packet.
70f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  // If we did not, then we should resize our buffer to be large enough.
71f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch  SignalReadPacket(buf_, (size_t)len, remote_addr, this);
72f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch}
73f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch
74f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch}  // namespace talk_base
75