2018-08-13 19:15:09 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-08-13 19:15:09 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/port/config.h"
|
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/optional.h"
|
|
|
|
#include "td/utils/port/detail/NativeFd.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Span.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
|
|
|
class UdpSocketFdImpl;
|
|
|
|
class UdpSocketFdImplDeleter {
|
|
|
|
public:
|
|
|
|
void operator()(UdpSocketFdImpl *impl);
|
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
struct UdpMessage {
|
|
|
|
IPAddress address;
|
|
|
|
BufferSlice data;
|
|
|
|
Status error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UdpSocketFd {
|
|
|
|
public:
|
|
|
|
UdpSocketFd();
|
2021-10-18 13:36:15 +02:00
|
|
|
UdpSocketFd(UdpSocketFd &&) noexcept;
|
|
|
|
UdpSocketFd &operator=(UdpSocketFd &&) noexcept;
|
2018-08-13 19:15:09 +02:00
|
|
|
~UdpSocketFd();
|
|
|
|
|
|
|
|
UdpSocketFd(const UdpSocketFd &) = delete;
|
|
|
|
UdpSocketFd &operator=(const UdpSocketFd &) = delete;
|
|
|
|
|
2021-12-15 18:55:03 +01:00
|
|
|
Result<uint32> maximize_snd_buffer(uint32 max_size = 0);
|
|
|
|
Result<uint32> maximize_rcv_buffer(uint32 max_size = 0);
|
2019-07-06 13:29:15 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
static Result<UdpSocketFd> open(const IPAddress &address) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
PollableFdInfo &get_poll_info();
|
|
|
|
const PollableFdInfo &get_poll_info() const;
|
|
|
|
const NativeFd &get_native_fd() const;
|
|
|
|
|
|
|
|
void close();
|
|
|
|
bool empty() const;
|
|
|
|
|
|
|
|
static bool is_critical_read_error(const Status &status);
|
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
struct OutboundMessage {
|
|
|
|
const IPAddress *to;
|
|
|
|
Slice data;
|
|
|
|
};
|
|
|
|
struct InboundMessage {
|
|
|
|
IPAddress *from;
|
|
|
|
MutableSlice data;
|
|
|
|
Status *error;
|
|
|
|
};
|
|
|
|
|
|
|
|
Status send_message(const OutboundMessage &message, bool &is_sent) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status receive_message(InboundMessage &message, bool &is_received) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
Status send_messages(Span<OutboundMessage> messages, size_t &count) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status receive_messages(MutableSpan<InboundMessage> messages, size_t &count) TD_WARN_UNUSED_RESULT;
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
Result<optional<UdpMessage> > receive();
|
|
|
|
|
|
|
|
void send(UdpMessage message);
|
|
|
|
|
|
|
|
Status flush_send();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2020-06-16 04:10:16 +02:00
|
|
|
static constexpr uint32 DEFAULT_UDP_MAX_SND_BUFFER_SIZE = (1 << 24);
|
|
|
|
static constexpr uint32 DEFAULT_UDP_MAX_RCV_BUFFER_SIZE = (1 << 24);
|
2018-08-13 19:15:09 +02:00
|
|
|
std::unique_ptr<detail::UdpSocketFdImpl, detail::UdpSocketFdImplDeleter> impl_;
|
2018-09-27 03:19:03 +02:00
|
|
|
explicit UdpSocketFd(unique_ptr<detail::UdpSocketFdImpl> impl);
|
2018-08-13 19:15:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|