blob: b20281fd17407fbd4d97150998bd8cbf66070ac7 [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
Per Kjellander3daf6962022-11-08 18:23:43 +010016#include <cstdint>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#include <memory>
jbauch555604a2016-04-26 03:13:22 -070018
Per Kjellander3daf6962022-11-08 18:23:43 +010019#include "absl/types/optional.h"
20#include "api/sequence_checker.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/async_packet_socket.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/socket_address.h"
24#include "rtc_base/socket_factory.h"
Per Kjellander3daf6962022-11-08 18:23:43 +010025#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026
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 16:03:14 +020033 // Binds `socket` and creates AsyncUDPSocket for it. Takes ownership
34 // of `socket`. Returns null if bind() fails (`socket` is destroyed
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035 // in that case).
Niels Möllerd0b88792021-08-12 10:32:30 +020036 static AsyncUDPSocket* Create(Socket* socket,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037 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 10:32:30 +020042 explicit AsyncUDPSocket(Socket* socket);
Per Kjellander3daf6962022-11-08 18:23:43 +010043 ~AsyncUDPSocket() = default;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044
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 10:32:30 +020064 void OnReadEvent(Socket* socket);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065 // Called when the underlying socket is ready to send.
Niels Möllerd0b88792021-08-12 10:32:30 +020066 void OnWriteEvent(Socket* socket);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067
Per Kjellander3daf6962022-11-08 18:23:43 +010068 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
Niels Möllerd0b88792021-08-12 10:32:30 +020069 std::unique_ptr<Socket> socket_;
Per Kjellander3daf6962022-11-08 18:23:43 +010070 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 07:52:50 +020073};
74
75} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076
Steve Anton10542f22019-01-11 09:11:00 -080077#endif // RTC_BASE_ASYNC_UDP_SOCKET_H_