blob: 237c88d4b1932f55dd9add08583debe3bfcb39b4 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_ASYNC_UDP_SOCKET_H_
12#define RTC_BASE_ASYNC_UDP_SOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <memory>
jbauch555604a2016-04-26 03:13:22 -070016
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/async_packet_socket.h"
18#include "rtc_base/async_socket.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/socket_address.h"
21#include "rtc_base/socket_factory.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace rtc {
24
25// Provides the ability to receive packets asynchronously. Sends are not
26// buffered since it is acceptable to drop packets under high load.
27class AsyncUDPSocket : public AsyncPacketSocket {
28 public:
29 // Binds |socket| and creates AsyncUDPSocket for it. Takes ownership
30 // of |socket|. Returns null if bind() fails (|socket| is destroyed
31 // in that case).
32 static AsyncUDPSocket* Create(AsyncSocket* socket,
33 const SocketAddress& bind_address);
34 // Creates a new socket for sending asynchronous UDP packets using an
35 // asynchronous socket from the given factory.
36 static AsyncUDPSocket* Create(SocketFactory* factory,
37 const SocketAddress& bind_address);
38 explicit AsyncUDPSocket(AsyncSocket* socket);
39 ~AsyncUDPSocket() override;
40
41 SocketAddress GetLocalAddress() const override;
42 SocketAddress GetRemoteAddress() const override;
43 int Send(const void* pv,
44 size_t cb,
45 const rtc::PacketOptions& options) override;
46 int SendTo(const void* pv,
47 size_t cb,
48 const SocketAddress& addr,
49 const rtc::PacketOptions& options) override;
50 int Close() override;
51
52 State GetState() const override;
53 int GetOption(Socket::Option opt, int* value) override;
54 int SetOption(Socket::Option opt, int value) override;
55 int GetError() const override;
56 void SetError(int error) override;
57
58 private:
59 // Called when the underlying socket is ready to be read from.
60 void OnReadEvent(AsyncSocket* socket);
61 // Called when the underlying socket is ready to send.
62 void OnWriteEvent(AsyncSocket* socket);
63
64 std::unique_ptr<AsyncSocket> socket_;
65 char* buf_;
66 size_t size_;
67};
68
69} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070
Steve Anton10542f22019-01-11 09:11:00 -080071#endif // RTC_BASE_ASYNC_UDP_SOCKET_H_