diff --git a/src/libutil/include/nix/util/meson.build b/src/libutil/include/nix/util/meson.build index 45b52ff5e..894de0c44 100644 --- a/src/libutil/include/nix/util/meson.build +++ b/src/libutil/include/nix/util/meson.build @@ -65,6 +65,7 @@ headers = files( 'signals.hh', 'signature/local-keys.hh', 'signature/signer.hh', + 'socket.hh', 'sort.hh', 'source-accessor.hh', 'source-path.hh', diff --git a/src/libutil/include/nix/util/socket.hh b/src/libutil/include/nix/util/socket.hh new file mode 100644 index 000000000..30d963ec8 --- /dev/null +++ b/src/libutil/include/nix/util/socket.hh @@ -0,0 +1,61 @@ +#pragma once +///@file + +#include "nix/util/file-descriptor.hh" + +#ifdef _WIN32 +# include +#endif + +namespace nix { + +/** + * Often we want to use `Descriptor`, but Windows makes a slightly + * stronger file descriptor vs socket distinction, at least at the level + * of C types. + */ +using Socket = +#ifdef _WIN32 + SOCKET +#else + int +#endif + ; + +#ifdef _WIN32 +/** + * Windows gives this a different name + */ +# define SHUT_WR SD_SEND +# define SHUT_RDWR SD_BOTH +#endif + +/** + * Convert a `Descriptor` to a `Socket` + * + * This is a no-op except on Windows. + */ +static inline Socket toSocket(Descriptor fd) +{ +#ifdef _WIN32 + return reinterpret_cast(fd); +#else + return fd; +#endif +} + +/** + * Convert a `Socket` to a `Descriptor` + * + * This is a no-op except on Windows. + */ +static inline Descriptor fromSocket(Socket fd) +{ +#ifdef _WIN32 + return reinterpret_cast(fd); +#else + return fd; +#endif +} + +} // namespace nix diff --git a/src/libutil/include/nix/util/unix-domain-socket.hh b/src/libutil/include/nix/util/unix-domain-socket.hh index 6d28b6276..99fd331ce 100644 --- a/src/libutil/include/nix/util/unix-domain-socket.hh +++ b/src/libutil/include/nix/util/unix-domain-socket.hh @@ -3,10 +3,8 @@ #include "nix/util/types.hh" #include "nix/util/file-descriptor.hh" +#include "nix/util/socket.hh" -#ifdef _WIN32 -# include -#endif #include #include @@ -23,55 +21,6 @@ AutoCloseFD createUnixDomainSocket(); */ AutoCloseFD createUnixDomainSocket(const Path & path, mode_t mode); -/** - * Often we want to use `Descriptor`, but Windows makes a slightly - * stronger file descriptor vs socket distinction, at least at the level - * of C types. - */ -using Socket = -#ifdef _WIN32 - SOCKET -#else - int -#endif - ; - -#ifdef _WIN32 -/** - * Windows gives this a different name - */ -# define SHUT_WR SD_SEND -# define SHUT_RDWR SD_BOTH -#endif - -/** - * Convert a `Socket` to a `Descriptor` - * - * This is a no-op except on Windows. - */ -static inline Socket toSocket(Descriptor fd) -{ -#ifdef _WIN32 - return reinterpret_cast(fd); -#else - return fd; -#endif -} - -/** - * Convert a `Socket` to a `Descriptor` - * - * This is a no-op except on Windows. - */ -static inline Descriptor fromSocket(Socket fd) -{ -#ifdef _WIN32 - return reinterpret_cast(fd); -#else - return fd; -#endif -} - /** * Bind a Unix domain socket to a path. */