2018-08-13 19:15:09 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/port/config.h"
|
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/common.h"
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-09-10 01:08:12 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/StringBuilder.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
namespace td {
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
extern int VERBOSITY_NAME(fd);
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
class NativeFd {
|
|
|
|
public:
|
|
|
|
#if TD_PORT_POSIX
|
2018-09-11 15:27:20 +02:00
|
|
|
using Fd = int;
|
|
|
|
using Socket = int;
|
2018-08-13 19:15:09 +02:00
|
|
|
#elif TD_PORT_WINDOWS
|
2018-09-11 15:27:20 +02:00
|
|
|
using Fd = HANDLE;
|
|
|
|
using Socket = SOCKET;
|
2018-08-13 19:15:09 +02:00
|
|
|
#endif
|
|
|
|
NativeFd() = default;
|
2018-09-11 15:27:20 +02:00
|
|
|
explicit NativeFd(Fd fd);
|
|
|
|
NativeFd(Fd fd, bool nolog);
|
2018-08-13 19:15:09 +02:00
|
|
|
#if TD_PORT_WINDOWS
|
2018-09-11 15:27:20 +02:00
|
|
|
explicit NativeFd(Socket socket);
|
2018-08-13 19:15:09 +02:00
|
|
|
#endif
|
2018-09-11 15:27:20 +02:00
|
|
|
NativeFd(const NativeFd &) = delete;
|
|
|
|
NativeFd &operator=(const NativeFd &) = delete;
|
2021-10-18 13:36:15 +02:00
|
|
|
NativeFd(NativeFd &&other) noexcept;
|
|
|
|
NativeFd &operator=(NativeFd &&other) noexcept;
|
2018-08-13 19:15:09 +02:00
|
|
|
~NativeFd();
|
|
|
|
|
2022-10-09 18:30:18 +02:00
|
|
|
explicit operator bool() const noexcept;
|
2018-09-10 01:08:12 +02:00
|
|
|
|
2018-09-11 15:27:20 +02:00
|
|
|
Fd fd() const;
|
|
|
|
Socket socket() const;
|
2018-09-10 01:08:12 +02:00
|
|
|
|
|
|
|
Status set_is_blocking(bool is_blocking) const;
|
|
|
|
|
2018-09-10 16:05:12 +02:00
|
|
|
Status set_is_blocking_unsafe(bool is_blocking) const; // may drop other Fd flags on non-Windows
|
|
|
|
|
2018-09-10 16:47:28 +02:00
|
|
|
Status duplicate(const NativeFd &to) const;
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void close();
|
2018-09-11 15:27:20 +02:00
|
|
|
Fd release();
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2019-07-31 11:18:48 +02:00
|
|
|
Status validate() const;
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
private:
|
2020-01-30 20:11:04 +01:00
|
|
|
static Fd empty_fd();
|
|
|
|
|
|
|
|
Fd fd_ = empty_fd();
|
|
|
|
#if TD_PORT_WINDOWS
|
2018-08-13 19:15:09 +02:00
|
|
|
bool is_socket_{false};
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &sb, const NativeFd &fd);
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
} // namespace td
|