2018-08-13 19:15:09 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// 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"
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/MovableValue.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
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
class NativeFd {
|
|
|
|
public:
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
using Raw = int;
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
using Raw = HANDLE;
|
|
|
|
#endif
|
|
|
|
NativeFd() = default;
|
|
|
|
NativeFd(NativeFd &&) = default;
|
|
|
|
NativeFd &operator=(NativeFd &&) = default;
|
|
|
|
explicit NativeFd(Raw raw);
|
2018-09-07 02:41:21 +02:00
|
|
|
NativeFd &operator=(const NativeFd &) = delete;
|
2018-08-13 19:15:09 +02:00
|
|
|
NativeFd(Raw raw, bool nolog);
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
explicit NativeFd(SOCKET raw);
|
|
|
|
#endif
|
|
|
|
~NativeFd();
|
|
|
|
|
|
|
|
explicit operator bool() const;
|
2018-09-10 01:08:12 +02:00
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
static Raw empty_raw();
|
2018-09-10 01:08:12 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Raw raw() const;
|
|
|
|
Raw fd() const;
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
Raw io_handle() const;
|
|
|
|
SOCKET socket() const;
|
|
|
|
#elif TD_PORT_POSIX
|
|
|
|
Raw socket() const;
|
|
|
|
#endif
|
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();
|
|
|
|
Raw release();
|
|
|
|
|
|
|
|
private:
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
MovableValue<Raw, -1> fd_;
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
MovableValue<Raw, INVALID_HANDLE_VALUE> fd_;
|
|
|
|
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
|