2018-08-13 19:15:09 +02:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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"
|
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
|
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;
|
|
|
|
NativeFd(NativeFd &&) = default;
|
|
|
|
NativeFd &operator=(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;
|
2018-08-13 19:15:09 +02:00
|
|
|
~NativeFd();
|
|
|
|
|
|
|
|
explicit operator bool() const;
|
2018-09-10 01:08:12 +02:00
|
|
|
|
2018-09-11 15:27:20 +02:00
|
|
|
static Fd empty_fd();
|
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
|
|
|
|
|
|
|
private:
|
|
|
|
#if TD_PORT_POSIX
|
2018-09-11 15:27:20 +02:00
|
|
|
MovableValue<Fd, -1> fd_;
|
2018-08-13 19:15:09 +02:00
|
|
|
#elif TD_PORT_WINDOWS
|
2018-09-11 15:27:20 +02:00
|
|
|
MovableValue<Fd, INVALID_HANDLE_VALUE> fd_;
|
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
|