134 other._addrInfoPtr =
nullptr;
135 other._selectedAddrInfo =
nullptr;
136 other._localAddrLen = 0;
155 _buffer = std::move(other._buffer);
159 other._addrInfoPtr =
nullptr;
160 other._selectedAddrInfo =
nullptr;
161 other._localAddrLen = 0;
200 void connect(
int timeoutMillis = -1);
221 [[nodiscard]]
size_t write(std::string_view message)
const;
231 [[nodiscard]]
size_t write(std::string_view message, std::string_view host,
Port port)
const;
255 static_assert(std::is_trivially_copyable_v<T>,
256 "DatagramSocket::read<T>() only supports trivially copyable types");
262 const int n = ::recv(
_sockFd,
reinterpret_cast<char*
>(&value),
sizeof(T), 0);
269 throw SocketException(0,
"Partial read: did not receive a complete T object.");
281 template <
typename T> T
recvFrom(std::string* senderAddr,
Port* senderPort)
283 static_assert(std::is_trivially_copyable_v<T>,
"recvFrom<T>() only supports trivially copyable types");
285 sockaddr_storage srcAddr{};
286 socklen_t srcLen =
sizeof(srcAddr);
288 const auto n = ::recvfrom(
_sockFd,
reinterpret_cast<char*
>(&value),
sizeof(T), 0,
289 reinterpret_cast<sockaddr*
>(&srcAddr), &srcLen);
296 char hostBuf[NI_MAXHOST], portBuf[NI_MAXSERV];
297 if (getnameinfo(
reinterpret_cast<sockaddr*
>(&srcAddr), srcLen, hostBuf,
sizeof(hostBuf), portBuf,
298 sizeof(portBuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0)
301 *senderAddr = hostBuf;
303 *senderPort =
static_cast<Port>(std::stoul(portBuf));
349 void setOption(
int level,
int optName,
int value)
const;
358 [[nodiscard]]
int getOption(
int level,
int optName)
const;
426 _buffer.resize(_buffer.size());
427 const auto n = ::recv(_sockFd, _buffer.data(),
429 static_cast<int>(_buffer.size()),
438 return {_buffer.data(),
static_cast<size_t>(n)};
450 sockaddr_storage srcAddr{};
451 socklen_t srcLen =
sizeof(srcAddr);
452 const auto n = ::recvfrom(_sockFd, _buffer.data(),
454 static_cast<int>(_buffer.size()),
458 0,
reinterpret_cast<sockaddr*
>(&srcAddr), &srcLen);
465 char hostBuf[NI_MAXHOST], portBuf[NI_MAXSERV];
466 if (getnameinfo(
reinterpret_cast<sockaddr*
>(&srcAddr), srcLen, hostBuf,
sizeof(hostBuf), portBuf,
sizeof(portBuf),
467 NI_NUMERICHOST | NI_NUMERICSERV) == 0)
470 *senderAddr = hostBuf;
472 *senderPort =
static_cast<Port>(std::stoul(portBuf));
481 return {_buffer.data(),
static_cast<size_t>(n)};
UDP datagram packet class for jsocketpp.
Represents a UDP datagram packet, encapsulating both payload and addressing information.
Definition DatagramPacket.hpp:48
void cleanupAndThrow(int errorCode)
Cleans up datagram socket resources and throws a SocketException.
Definition DatagramSocket.cpp:80
T recvFrom(std::string *senderAddr, Port *senderPort)
Receive a datagram into a trivially copyable type.
Definition DatagramSocket.hpp:281
void setOption(int level, int optName, int value) const
Set a socket option (integer value).
Definition DatagramSocket.cpp:446
DatagramSocket & operator=(DatagramSocket &&other) noexcept
Move assignment operator.
Definition DatagramSocket.hpp:145
~DatagramSocket() noexcept
Destructor. Closes the socket and frees resources.
Definition DatagramSocket.cpp:89
DatagramSocket(const DatagramSocket &)=delete
Copy constructor (deleted).
void connect(int timeoutMillis=-1)
Connects the datagram socket to the remote host and port with optional timeout handling.
Definition DatagramSocket.cpp:123
socklen_t _localAddrLen
Length of local address.
Definition DatagramSocket.hpp:410
void setTimeout(int millis) const
Sets the socket's receive timeout.
Definition DatagramSocket.cpp:425
size_t read(DatagramPacket &packet, bool resizeBuffer=true) const
Receives a UDP datagram and fills the provided DatagramPacket.
Definition DatagramSocket.cpp:343
T read()
Read a trivially copyable type from the socket (connected UDP).
Definition DatagramSocket.hpp:253
int getOption(int level, int optName) const
Get a socket option (integer value).
Definition DatagramSocket.cpp:458
void bind() const
Bind the datagram socket to the configured port set in the constructor.
Definition DatagramSocket.cpp:101
std::string getLocalSocketAddress() const
Get the local socket's address as a string (ip:port).
Definition DatagramSocket.cpp:477
sockaddr_storage _localAddr
Local address structure.
Definition DatagramSocket.hpp:409
bool isConnected() const
Check if the datagram socket is connected to a remote host.
Definition DatagramSocket.hpp:381
DatagramSocket(Port port, std::size_t bufferSize=2048)
Constructs a UDP socket optionally bound to a local port.
Definition DatagramSocket.cpp:6
void close()
Close the datagram socket.
Definition DatagramSocket.cpp:390
DatagramSocket(DatagramSocket &&other) noexcept
Move constructor.
Definition DatagramSocket.hpp:128
void enableBroadcast(bool enable) const
Enable or disable the ability to send broadcast packets.
Definition DatagramSocket.cpp:508
DatagramSocket & operator=(const DatagramSocket &)=delete
Copy assignment operator (deleted).
void setNonBlocking(bool nonBlocking) const
Set the socket to non-blocking or blocking mode.
Definition DatagramSocket.cpp:406
Port _port
Port number the socket is bound to (if applicable).
Definition DatagramSocket.hpp:412
addrinfo * _selectedAddrInfo
Selected address info for connection.
Definition DatagramSocket.hpp:406
bool _isConnected
True if the socket is connected to a remote host.
Definition DatagramSocket.hpp:413
bool isValid() const
Check if the datagram socket is valid (open).
Definition DatagramSocket.hpp:370
SOCKET _sockFd
Underlying socket file descriptor.
Definition DatagramSocket.hpp:404
std::vector< char > _buffer
Internal buffer for read operations.
Definition DatagramSocket.hpp:411
addrinfo * _addrInfoPtr
Address info pointer for bind/connect.
Definition DatagramSocket.hpp:405
size_t write(const DatagramPacket &packet) const
Write data to a remote host using a DatagramPacket.
Definition DatagramSocket.cpp:232
Represents socket-related errors in the jsocketpp library.
Definition SocketException.hpp:58
Common platform and utility includes for jsocketpp.
std::uint16_t Port
Type alias representing a TCP or UDP port number (1–65535).
Definition common.hpp:315
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
int GetSocketError()
Definition common.hpp:202
int SOCKET
Definition common.hpp:219
constexpr SOCKET SOCKET_ERROR
Definition common.hpp:221