blob: 4b47007ed79949d16405ee558d4cebfc6110b367 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000034 ~AsyncUDPSocket() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000036 SocketAddress GetLocalAddress() const override;
37 SocketAddress GetRemoteAddress() const override;
38 int Send(const void* pv,
39 size_t cb,
40 const rtc::PacketOptions& options) override;
41 int SendTo(const void* pv,
42 size_t cb,
43 const SocketAddress& addr,
44 const rtc::PacketOptions& options) override;
45 int Close() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000047 State GetState() const override;
48 int GetOption(Socket::Option opt, int* value) override;
49 int SetOption(Socket::Option opt, int value) override;
50 int GetError() const override;
51 void SetError(int error) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052
53 private:
54 // Called when the underlying socket is ready to be read from.
55 void OnReadEvent(AsyncSocket* socket);
56 // Called when the underlying socket is ready to send.
57 void OnWriteEvent(AsyncSocket* socket);
58
59 scoped_ptr<AsyncSocket> socket_;
60 char* buf_;
61 size_t size_;
62};
63
64} // namespace rtc
65
66#endif // WEBRTC_BASE_ASYNCUDPSOCKET_H_