blob: 5d738ffe9474271c1087dcda3634158d0ffe7ff2 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0011#ifndef RTC_BASE_ASYNC_UDP_SOCKET_H_
12#define RTC_BASE_ASYNC_UDP_SOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Yves Gerey988cc082018-10-23 10:03:0114#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#include <memory>
jbauch555604a2016-04-26 10:13:2217
Steve Anton10542f22019-01-11 17:11:0018#include "rtc_base/async_packet_socket.h"
Yves Gerey988cc082018-10-23 10:03:0119#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 17:11:0020#include "rtc_base/socket_address.h"
21#include "rtc_base/socket_factory.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5022
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:
Artem Titov96e3b992021-07-26 14:03:1429 // Binds `socket` and creates AsyncUDPSocket for it. Takes ownership
30 // of `socket`. Returns null if bind() fails (`socket` is destroyed
Henrik Kjellanderec78f1c2017-06-29 05:52:5031 // in that case).
Niels Möllerd0b88792021-08-12 08:32:3032 static AsyncUDPSocket* Create(Socket* socket,
Henrik Kjellanderec78f1c2017-06-29 05:52:5033 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);
Niels Möllerd0b88792021-08-12 08:32:3038 explicit AsyncUDPSocket(Socket* socket);
Henrik Kjellanderec78f1c2017-06-29 05:52:5039 ~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.
Niels Möllerd0b88792021-08-12 08:32:3060 void OnReadEvent(Socket* socket);
Henrik Kjellanderec78f1c2017-06-29 05:52:5061 // Called when the underlying socket is ready to send.
Niels Möllerd0b88792021-08-12 08:32:3062 void OnWriteEvent(Socket* socket);
Henrik Kjellanderec78f1c2017-06-29 05:52:5063
Niels Möllerd0b88792021-08-12 08:32:3064 std::unique_ptr<Socket> socket_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5065 char* buf_;
66 size_t size_;
67};
68
69} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:2670
Steve Anton10542f22019-01-11 17:11:0071#endif // RTC_BASE_ASYNC_UDP_SOCKET_H_