blob: b20281fd17407fbd4d97150998bd8cbf66070ac7 [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
Per Kjellander3daf6962022-11-08 17:23:4316#include <cstdint>
Henrik Kjellanderec78f1c2017-06-29 05:52:5017#include <memory>
jbauch555604a2016-04-26 10:13:2218
Per Kjellander3daf6962022-11-08 17:23:4319#include "absl/types/optional.h"
20#include "api/sequence_checker.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/async_packet_socket.h"
Yves Gerey988cc082018-10-23 10:03:0122#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 17:11:0023#include "rtc_base/socket_address.h"
24#include "rtc_base/socket_factory.h"
Per Kjellander3daf6962022-11-08 17:23:4325#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5026
27namespace rtc {
28
29// Provides the ability to receive packets asynchronously. Sends are not
30// buffered since it is acceptable to drop packets under high load.
31class AsyncUDPSocket : public AsyncPacketSocket {
32 public:
Artem Titov96e3b992021-07-26 14:03:1433 // Binds `socket` and creates AsyncUDPSocket for it. Takes ownership
34 // of `socket`. Returns null if bind() fails (`socket` is destroyed
Henrik Kjellanderec78f1c2017-06-29 05:52:5035 // in that case).
Niels Möllerd0b88792021-08-12 08:32:3036 static AsyncUDPSocket* Create(Socket* socket,
Henrik Kjellanderec78f1c2017-06-29 05:52:5037 const SocketAddress& bind_address);
38 // Creates a new socket for sending asynchronous UDP packets using an
39 // asynchronous socket from the given factory.
40 static AsyncUDPSocket* Create(SocketFactory* factory,
41 const SocketAddress& bind_address);
Niels Möllerd0b88792021-08-12 08:32:3042 explicit AsyncUDPSocket(Socket* socket);
Per Kjellander3daf6962022-11-08 17:23:4343 ~AsyncUDPSocket() = default;
Henrik Kjellanderec78f1c2017-06-29 05:52:5044
45 SocketAddress GetLocalAddress() const override;
46 SocketAddress GetRemoteAddress() const override;
47 int Send(const void* pv,
48 size_t cb,
49 const rtc::PacketOptions& options) override;
50 int SendTo(const void* pv,
51 size_t cb,
52 const SocketAddress& addr,
53 const rtc::PacketOptions& options) override;
54 int Close() override;
55
56 State GetState() const override;
57 int GetOption(Socket::Option opt, int* value) override;
58 int SetOption(Socket::Option opt, int value) override;
59 int GetError() const override;
60 void SetError(int error) override;
61
62 private:
63 // Called when the underlying socket is ready to be read from.
Niels Möllerd0b88792021-08-12 08:32:3064 void OnReadEvent(Socket* socket);
Henrik Kjellanderec78f1c2017-06-29 05:52:5065 // Called when the underlying socket is ready to send.
Niels Möllerd0b88792021-08-12 08:32:3066 void OnWriteEvent(Socket* socket);
Henrik Kjellanderec78f1c2017-06-29 05:52:5067
Per Kjellander3daf6962022-11-08 17:23:4368 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
Niels Möllerd0b88792021-08-12 08:32:3069 std::unique_ptr<Socket> socket_;
Per Kjellander3daf6962022-11-08 17:23:4370 static constexpr int BUF_SIZE = 64 * 1024;
71 char buf_[BUF_SIZE] RTC_GUARDED_BY(sequence_checker_);
72 absl::optional<int64_t> socket_time_offset_ RTC_GUARDED_BY(sequence_checker_);
Henrik Kjellanderec78f1c2017-06-29 05:52:5073};
74
75} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:2676
Steve Anton10542f22019-01-11 17:11:0077#endif // RTC_BASE_ASYNC_UDP_SOCKET_H_