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