henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_ASYNC_UDP_SOCKET_H_ |
| 12 | #define RTC_BASE_ASYNC_UDP_SOCKET_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 16 | #include <cstdint> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 17 | #include <memory> |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 18 | |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
| 20 | #include "api/sequence_checker.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/async_packet_socket.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 22 | #include "rtc_base/socket.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "rtc_base/socket_address.h" |
| 24 | #include "rtc_base/socket_factory.h" |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 25 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 26 | |
| 27 | namespace 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. |
| 31 | class AsyncUDPSocket : public AsyncPacketSocket { |
| 32 | public: |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 33 | // Binds `socket` and creates AsyncUDPSocket for it. Takes ownership |
| 34 | // of `socket`. Returns null if bind() fails (`socket` is destroyed |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 35 | // in that case). |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 36 | static AsyncUDPSocket* Create(Socket* socket, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 37 | 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öller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 42 | explicit AsyncUDPSocket(Socket* socket); |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 43 | ~AsyncUDPSocket() = default; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 44 | |
| 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öller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 64 | void OnReadEvent(Socket* socket); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 65 | // Called when the underlying socket is ready to send. |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 66 | void OnWriteEvent(Socket* socket); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 67 | |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 68 | RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_; |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 69 | std::unique_ptr<Socket> socket_; |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 70 | 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 Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 76 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 77 | #endif // RTC_BASE_ASYNC_UDP_SOCKET_H_ |