diff --git a/src/uvw/pipe.hpp b/src/uvw/pipe.hpp index 9a98c6e2..3000e78e 100644 --- a/src/uvw/pipe.hpp +++ b/src/uvw/pipe.hpp @@ -15,20 +15,6 @@ namespace uvw { -namespace details { - - -enum class UVHandleType: std::underlying_type_t { - UNKNOWN = UV_UNKNOWN_HANDLE, - PIPE = UV_NAMED_PIPE, - TCP = UV_TCP, - UDP = UV_UDP -}; - - -} - - /** * @brief The PipeHandle handle. * @@ -41,8 +27,6 @@ class PipeHandle final: public StreamHandle { { } public: - using Pending = details::UVHandleType; - /** * @brief Creates a new poll handle. * @param args @@ -152,24 +136,19 @@ public: * Steps to be done: * * * Call `pending()`, if it’s greater than zero then proceed. - * * Initialize a handle of the given type, returned by `receive()`. + * * Initialize a handle of the given type, as returned by `receive()`. * * Call `accept(pipe, handle)`. * - * @return + * @return The type of the pending handle. Possible values are: + * + * * `HandleType::PIPE` + * * `HandleType::TCP` + * * `HandleType::UDP` + * * `HandleType::UNKNOWN` */ - Pending receive() noexcept { + HandleType receive() noexcept { auto type = uv_pipe_pending_type(get()); - - switch(type) { - case UV_NAMED_PIPE: - return Pending::PIPE; - case UV_TCP: - return Pending::TCP; - case UV_UDP: - return Pending::UDP; - default: - return Pending::UNKNOWN; - } + return guessHandle(type); } private: diff --git a/src/uvw/util.hpp b/src/uvw/util.hpp index 21bf5d50..80d8fe60 100644 --- a/src/uvw/util.hpp +++ b/src/uvw/util.hpp @@ -12,6 +12,34 @@ namespace uvw { +namespace details { + + +enum class UVHandleType: std::underlying_type_t { + UNKNOWN = UV_UNKNOWN_HANDLE, + ASYNC = UV_ASYNC, + CHECK = UV_CHECK, + FS_EVENT = UV_FS_EVENT, + FS_POLL = UV_FS_POLL, + HANDLE = UV_HANDLE, + IDLE = UV_IDLE, + PIPE = UV_NAMED_PIPE, + POLL = UV_POLL, + PREPARE = UV_PREPARE, + PROCESS = UV_PROCESS, + STREAM = UV_STREAM, + TCP = UV_TCP, + TIMER = UV_TIMER, + TTY = UV_TTY, + UDP = UV_UDP, + SIGNAL = UV_SIGNAL, + FILE = UV_FILE +}; + + +} + + /** * @brief Utility class to handle flags. * @@ -114,9 +142,9 @@ private: * * In particular, It is used for: * - * * FileHandle (that is an alias for `UVTypeWrapper`) - * * OSSocketHandle (that is an alias for `UVTypeWrapper`) - * * OSFileDescriptor (that is an alias for `UVTypeWrapper`) + * * `FileHandle` (that is an alias for `UVTypeWrapper`) + * * `OSSocketHandle` (that is an alias for `UVTypeWrapper`) + * * `OSFileDescriptor` (that is an alias for `UVTypeWrapper`) * * It can be bound to each value of type `T` and it will be implicitly converted * to the underlying type when needed. @@ -141,11 +169,6 @@ private: }; -using FileHandle = UVTypeWrapper; -using OSSocketHandle = UVTypeWrapper; -using OSFileDescriptor = UVTypeWrapper; - - /** * @brief Address representation. * @@ -171,10 +194,78 @@ struct Addr { std::string ip; unsigned int port; }; struct WinSize { int width; int height; }; +using HandleType = details::UVHandleType; + +using FileHandle = UVTypeWrapper; +using OSSocketHandle = UVTypeWrapper; +using OSFileDescriptor = UVTypeWrapper; + using TimeSpec = uv_timespec_t; using Stat = uv_stat_t; using Uid = uv_uid_t; using Gid = uv_gid_t; +/** + * @brief Gets the type of the stream to be used with the given descriptor. + * + * Returns the type of stream that should be used with a given file + * descriptor.
+ * Usually this will be used during initialization to guess the type of the + * stdio streams. + * + * @param file A valid descriptor. + * @return One of the following types: + * + * * `HandleType::UNKNOWN` + * * `HandleType::PIPE` + * * `HandleType::TCP` + * * `HandleType::TTY` + * * `HandleType::UDP` + * * `HandleType::FILE` + */ +HandleType guessHandle(FileHandle file) { + auto type = uv_guess_handle(file); + + switch(type) { + case UV_ASYNC: + return HandleType::ASYNC; + case UV_CHECK: + return HandleType::CHECK; + case UV_FS_EVENT: + return HandleType::FS_EVENT; + case UV_FS_POLL: + return HandleType::FS_POLL; + case UV_HANDLE: + return HandleType::HANDLE; + case UV_IDLE: + return HandleType::IDLE; + case UV_NAMED_PIPE: + return HandleType::PIPE; + case UV_POLL: + return HandleType::POLL; + case UV_PREPARE: + return HandleType::PREPARE; + case UV_PROCESS: + return HandleType::PROCESS; + case UV_STREAM: + return HandleType::STREAM; + case UV_TCP: + return HandleType::TCP; + case UV_TIMER: + return HandleType::TIMER; + case UV_TTY: + return HandleType::TTY; + case UV_UDP: + return HandleType::UDP; + case UV_SIGNAL: + return HandleType::SIGNAL; + case UV_FILE: + return HandleType::FILE; + default: + return HandleType::UNKNOWN; + } +} + + }