jsocketpp 1.0
A cross-platform C++20 socket library.
Loading...
Searching...
No Matches
UnixSocket.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "common.hpp"
9
10#include <string_view>
11
12// Enable AF_UNIX support on Windows 10+ (version 1803, build 17134) only
13#if defined(_MSC_VER) && (_WIN32_WINNT >= _WIN32_WINNT_WIN10)
14// https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/
15#include <afunix.h> // Windows 10+ AF_UNIX support
16#define AF_UNIX 1
17#endif
18
19#include "SocketException.hpp"
20#include "common.hpp"
21
22namespace jsocketpp
23{
24
25#if defined(_WIN32) && defined(AF_UNIX) || defined(__unix__) || defined(__APPLE__)
26
87{
88 public:
94 explicit UnixSocket(std::string_view path, std::size_t bufferSize = 512);
95
99 UnixSocket(const UnixSocket&) = delete;
100
104 UnixSocket& operator=(const UnixSocket&) = delete;
105
110 UnixSocket(UnixSocket&& other) noexcept;
111
117 UnixSocket& operator=(UnixSocket&& other) noexcept;
118
124 ~UnixSocket() noexcept;
125
130 void bind();
131
140 void listen(int backlog = SOMAXCONN) const;
141
147 UnixSocket accept() const;
148
153 void connect();
154
161 size_t write(std::string_view data) const;
162
170 size_t read(char* buffer, std::size_t len) const;
171
178 template <typename T> T read()
179 {
180 static_assert(std::is_trivially_copyable_v<T>, "UnixSocket::read<T>() only supports trivially copyable types");
181 T value;
182 const int len = ::recv(_sockFd, reinterpret_cast<char*>(&value),
183#ifdef _WIN32
184 static_cast<int>(sizeof(T)),
185#else
186 sizeof(T),
187#endif
188 0);
189 if (len == SOCKET_ERROR)
191 if (len == 0)
192 throw SocketException(0, "Connection closed by remote socket.");
193 return value;
194 }
195
199 void close();
200
205 [[nodiscard]] bool isValid() const { return this->_sockFd != INVALID_SOCKET; }
206
211 [[nodiscard]] std::string getSocketPath() const { return this->_socketPath; }
212
222 void setNonBlocking(bool nonBlocking) const;
223
228 void setTimeout(int millis) const;
229
240 static bool isPathInUse(std::string_view path);
241
261 void setOption(int level, int optName, int value) const;
262
276 [[nodiscard]] int getOption(int level, int optName) const;
277
278 protected:
283
284 private:
286 bool _isListening = false;
287 std::string _socketPath;
289 std::vector<char> _buffer;
290};
291
303template <> inline std::string UnixSocket::read()
304{
305 const auto len = static_cast<int>(recv(_sockFd, _buffer.data(),
306#ifdef _WIN32
307 static_cast<int>(_buffer.size()),
308#else
309 _buffer.size(),
310#endif
311 0));
312 if (len == SOCKET_ERROR)
314 if (len == 0)
315 throw SocketException(0, "Connection closed by remote socket.");
316 return {_buffer.data(), static_cast<size_t>(len)};
317}
318
319#endif
320
321} // namespace jsocketpp
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