1/*
2 *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_ASYNCUDPSOCKET_H_
12#define WEBRTC_BASE_ASYNCUDPSOCKET_H_
13
14#include "webrtc/base/asyncpacketsocket.h"
15#include "webrtc/base/scoped_ptr.h"
16#include "webrtc/base/socketfactory.h"
17
18namespace rtc {
19
20// Provides the ability to receive packets asynchronously.  Sends are not
21// buffered since it is acceptable to drop packets under high load.
22class AsyncUDPSocket : public AsyncPacketSocket {
23 public:
24  // Binds |socket| and creates AsyncUDPSocket for it. Takes ownership
25  // of |socket|. Returns NULL if bind() fails (|socket| is destroyed
26  // in that case).
27  static AsyncUDPSocket* Create(AsyncSocket* socket,
28                                const SocketAddress& bind_address);
29  // Creates a new socket for sending asynchronous UDP packets using an
30  // asynchronous socket from the given factory.
31  static AsyncUDPSocket* Create(SocketFactory* factory,
32                                const SocketAddress& bind_address);
33  explicit AsyncUDPSocket(AsyncSocket* socket);
34  virtual ~AsyncUDPSocket();
35
36  virtual SocketAddress GetLocalAddress() const;
37  virtual SocketAddress GetRemoteAddress() const;
38  virtual int Send(const void *pv, size_t cb,
39                   const rtc::PacketOptions& options);
40  virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
41                     const rtc::PacketOptions& options);
42  virtual int Close();
43
44  virtual State GetState() const;
45  virtual int GetOption(Socket::Option opt, int* value);
46  virtual int SetOption(Socket::Option opt, int value);
47  virtual int GetError() const;
48  virtual void SetError(int error);
49
50 private:
51  // Called when the underlying socket is ready to be read from.
52  void OnReadEvent(AsyncSocket* socket);
53  // Called when the underlying socket is ready to send.
54  void OnWriteEvent(AsyncSocket* socket);
55
56  scoped_ptr<AsyncSocket> socket_;
57  char* buf_;
58  size_t size_;
59};
60
61}  // namespace rtc
62
63#endif  // WEBRTC_BASE_ASYNCUDPSOCKET_H_
64