13#if defined(_MSC_VER) && (_WIN32_WINNT >= _WIN32_WINNT_WIN10)
25#if defined(_WIN32) && defined(AF_UNIX) || defined(__unix__) || defined(__APPLE__)
94 explicit UnixSocket(std::string_view path, std::size_t bufferSize = 512);
140 void listen(
int backlog = SOMAXCONN) const;
161 size_t write(std::string_view data) const;
170 size_t read(
char* buffer, std::
size_t len) const;
180 static_assert(std::is_trivially_copyable_v<T>,
"UnixSocket::read<T>() only supports trivially copyable types");
182 const int len = ::recv(
_sockFd,
reinterpret_cast<char*
>(&value),
184 static_cast<int>(
sizeof(T)),
261 void setOption(
int level,
int optName,
int value)
const;
276 [[nodiscard]]
int getOption(
int level,
int optName)
const;
307 static_cast<int>(
_buffer.size()),
316 return {
_buffer.data(),
static_cast<size_t>(len)};
Exception class for socket-related errors in jsocketpp.
Represents socket-related errors in the jsocketpp library.
Definition SocketException.hpp:58
SOCKADDR_UN _addr
Address structure for Unix domain sockets.
Definition UnixSocket.hpp:288
static bool isPathInUse(std::string_view path)
Checks whether a UNIX domain socket path is currently in use (i.e., a process is listening on it).
Definition UnixSocket.cpp:202
UnixSocket & operator=(const UnixSocket &)=delete
Copy assignment operator deleted to prevent copying.
int getOption(int level, int optName) const
Retrieve the value of a socket option for the Unix domain socket.
Definition UnixSocket.cpp:256
void bind()
Binds the socket.
Definition UnixSocket.cpp:76
std::string _socketPath
Path for the Unix domain socket.
Definition UnixSocket.hpp:287
void close()
Closes the socket.
Definition UnixSocket.cpp:113
UnixSocket(std::string_view path, std::size_t bufferSize=512)
Constructs a UnixSocket and connects or binds to the specified path.
Definition UnixSocket.cpp:8
UnixSocket()
Default constructor for internal use (e.g., accept()).
Definition UnixSocket.hpp:282
void setTimeout(int millis) const
Sets a timeout for socket operations.
Definition UnixSocket.cpp:185
T read()
Reads a trivially copyable type from the socket.
Definition UnixSocket.hpp:178
void setOption(int level, int optName, int value) const
Set a socket option on the Unix domain socket.
Definition UnixSocket.cpp:244
void connect()
Connects the socket.
Definition UnixSocket.cpp:104
SOCKET _sockFd
Underlying socket file descriptor.
Definition UnixSocket.hpp:285
bool _isListening
True if the socket is listening for connections.
Definition UnixSocket.hpp:286
std::string getSocketPath() const
Returns the path of the Unix domain socket.
Definition UnixSocket.hpp:211
size_t read(char *buffer, std::size_t len) const
Reads data from the socket into a buffer.
Definition UnixSocket.cpp:151
void listen(int backlog=SOMAXCONN) const
Marks the socket as a passive socket to accept incoming connections.
Definition UnixSocket.cpp:95
~UnixSocket() noexcept
Destructor.
Definition UnixSocket.cpp:25
void setNonBlocking(bool nonBlocking) const
Sets the socket to non-blocking or blocking mode.
Definition UnixSocket.cpp:166
size_t write(std::string_view data) const
Writes data to the socket.
Definition UnixSocket.cpp:136
bool isValid() const
Checks if the socket is valid (open).
Definition UnixSocket.hpp:205
UnixSocket accept() const
Accepts an incoming connection.
Definition UnixSocket.cpp:122
std::vector< char > _buffer
Internal buffer for read operations.
Definition UnixSocket.hpp:289
UnixSocket(const UnixSocket &)=delete
Copy constructor deleted to prevent copying.
Common platform and utility includes for jsocketpp.
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:13
std::string SocketErrorMessage(int error, bool gaiStrerror=false)
Returns a human-readable error message for a socket error code.
Definition common.cpp:5
constexpr SOCKET INVALID_SOCKET
Definition common.hpp:220
sockaddr_un SOCKADDR_UN
Definition common.hpp:222
int GetSocketError()
Definition common.hpp:202
int SOCKET
Definition common.hpp:219
constexpr SOCKET SOCKET_ERROR
Definition common.hpp:221