14#if defined(_WIN32) || defined(_WIN64)
18#ifndef WIN32_LEAN_AND_MEAN
19#define WIN32_LEAN_AND_MEAN
24#define CORIUM_HAS_POSIX_SOCKETS 0
25#elif __has_include(<sys/socket.h>) && __has_include(<sys/un.h>) && __has_include(<unistd.h>) && __has_include(<fcntl.h>)
27#include <sys/socket.h>
30#define CORIUM_HAS_POSIX_SOCKETS 1
32#define CORIUM_HAS_POSIX_SOCKETS 0
53 _boundPath(std::move(other._boundPath)),
54 _isBound(other._isBound)
57 other._isBound =
false;
65 _boundPath = std::move(other._boundPath);
66 _isBound = other._isBound;
68 other._isBound =
false;
76 bool bind(
const std::string& socketPath)
noexcept
80#if CORIUM_HAS_POSIX_SOCKETS
81 ::unlink(socketPath.c_str());
83 _fd = ::socket(AF_UNIX, SOCK_DGRAM, 0);
88 struct sockaddr_un addr{};
89 addr.sun_family = AF_UNIX;
90 std::strncpy(addr.sun_path, socketPath.c_str(),
sizeof(addr.sun_path) - 1);
92 if (
::bind(_fd,
reinterpret_cast<struct sockaddr*
>(&addr),
sizeof(addr)) != 0) {
98 _boundPath = socketPath;
111 bool connect(
const std::string& socketPath,
const std::string& clientPath =
"") noexcept
115#if CORIUM_HAS_POSIX_SOCKETS
116 _fd = ::socket(AF_UNIX, SOCK_DGRAM, 0);
121 if (!clientPath.empty()) {
122 ::unlink(clientPath.c_str());
123 struct sockaddr_un localAddr{};
124 localAddr.sun_family = AF_UNIX;
125 std::strncpy(localAddr.sun_path, clientPath.c_str(),
sizeof(localAddr.sun_path) - 1);
127 if (
::bind(_fd,
reinterpret_cast<struct sockaddr*
>(&localAddr),
sizeof(localAddr)) != 0) {
132 _boundPath = clientPath;
136 struct sockaddr_un remoteAddr{};
137 remoteAddr.sun_family = AF_UNIX;
138 std::strncpy(remoteAddr.sun_path, socketPath.c_str(),
sizeof(remoteAddr.sun_path) - 1);
140 if (
::connect(_fd,
reinterpret_cast<struct sockaddr*
>(&remoteAddr),
sizeof(remoteAddr)) != 0) {
155#if CORIUM_HAS_POSIX_SOCKETS
157 int flags = ::fcntl(_fd, F_GETFL, 0);
162 flags &= ~O_NONBLOCK;
164 ::fcntl(_fd, F_SETFL, flags);
176 int send(
const void* buffer, std::size_t length)
noexcept
178#if CORIUM_HAS_POSIX_SOCKETS
179 if (_fd < 0)
return -1;
180 return static_cast<int>(
::send(_fd, buffer, length, 0));
192 int receive(
void* buffer, std::size_t maxLength)
noexcept
194#if CORIUM_HAS_POSIX_SOCKETS
195 if (_fd < 0)
return -1;
196 return static_cast<int>(::recv(_fd, buffer, maxLength, 0));
207#if CORIUM_HAS_POSIX_SOCKETS
212 if (_isBound && !_boundPath.empty()) {
213 ::unlink(_boundPath.c_str());
221 [[nodiscard]]
bool isOpen() const noexcept {
return _fd >= 0; }
228 std::string _boundPath;
229 bool _isBound{
false};
Low-level RAII abstraction for UNIX Domain Datagram Sockets (AF_UNIX / SOCK_DGRAM)....
Definition DomainSocket.hpp:39
void setNonBlocking(bool nonBlocking) noexcept
Set socket non-blocking mode.
Definition DomainSocket.hpp:153
bool connect(const std::string &socketPath, const std::string &clientPath="") noexcept
Connect to a destination socket as a client.
Definition DomainSocket.hpp:111
DomainSocket() noexcept=default
void close() noexcept
Close socket and unlink bound file if server.
Definition DomainSocket.hpp:205
DomainSocket(DomainSocket &&other) noexcept
Definition DomainSocket.hpp:51
int nativeHandle() const noexcept
Access underlying OS file descriptor.
Definition DomainSocket.hpp:224
DomainSocket & operator=(DomainSocket &&other) noexcept
Definition DomainSocket.hpp:60
bool bind(const std::string &socketPath) noexcept
Bind socket to a filesystem path as a receiving server.
Definition DomainSocket.hpp:76
int send(const void *buffer, std::size_t length) noexcept
Send datagram packet over the connected socket.
Definition DomainSocket.hpp:176
bool isOpen() const noexcept
Check if socket descriptor is open.
Definition DomainSocket.hpp:221
int receive(void *buffer, std::size_t maxLength) noexcept
Receive datagram packet from socket.
Definition DomainSocket.hpp:192
DomainSocket(const DomainSocket &)=delete
DomainSocket & operator=(const DomainSocket &)=delete
Definition DomainSocket.hpp:35