25#ifndef JOIN_CORE_SOCKET_HPP
26#define JOIN_CORE_SOCKET_HPP
42#include <linux/icmp.h>
51 template <
class Protocol>
55 template <
class P,
class E,
size_t N>
59 using Ptr = std::unique_ptr<BasicSocket<Protocol>>;
61 using TimePoint = std::chrono::steady_clock::time_point;
120 ,
_mode (other._mode)
127 other._protocol = Protocol ();
147 other._protocol = Protocol ();
188 lastError = std::error_code (errno, std::generic_category ());
225 if (
::bind (
_handle, endpoint.addr (), endpoint.length ()) == -1)
227 lastError = std::error_code (errno, std::generic_category ());
240 return (
waitUntil (
true,
false, TimePoint::max ()) == 0);
250 return (
waitUntil (
true,
false, std::chrono::steady_clock::now () + timeout) == 0);
260 return (
waitUntil (
true,
false, deadline) == 0);
269 return (
waitUntil (
false,
true, TimePoint::max ()) == 0);
279 return (
waitUntil (
false,
true, std::chrono::steady_clock::now () + timeout) == 0);
289 return (
waitUntil (
false,
true, deadline) == 0);
302 int flags = ::fcntl (
_handle, F_GETFL, 0);
306 flags = flags | O_NONBLOCK;
310 flags = flags & ~O_NONBLOCK;
313 ::fcntl (
_handle, F_SETFL, flags);
323 struct sockaddr_storage sa;
324 socklen_t sa_len =
sizeof (
struct sockaddr_storage);
326 if (::getsockname (
_handle,
reinterpret_cast<struct sockaddr*
> (&sa), &sa_len) == -1)
331 return Endpoint (
reinterpret_cast<struct sockaddr*
> (&sa), sa_len);
394 int wait (
bool wantRead,
bool wantWrite)
const noexcept
396 return waitUntil (wantRead, wantWrite, TimePoint::max ());
406 int waitFor (
bool wantRead,
bool wantWrite, std::chrono::nanoseconds timeout)
const noexcept
408 return waitUntil (wantRead, wantWrite, std::chrono::steady_clock::now () + timeout);
435 struct timespec ts = {};
436 const struct timespec* remaining =
nullptr;
438 if (deadline != TimePoint::max ())
440 ts =
toTimespec (std::max (deadline - std::chrono::steady_clock::now (), TimePoint::duration::zero ()));
444 int nset = (
handle.fd > -1) ? ::ppoll (&
handle, 1, remaining,
nullptr) : -1;
453 lastError = std::error_code (errno, std::generic_category ());
486 template <
class Protocol>
489 return a.handle () == b.handle ();
498 template <
class Protocol>
501 return a.handle () <=> b.handle ();
asynchronous raw socket class.
Definition protocol.hpp:88
basic socket class.
Definition socket.hpp:53
bool opened() const noexcept
check if the socket is opened.
Definition socket.hpp:338
BasicSocket & operator=(const BasicSocket &other)=delete
copy assignment operator.
virtual int open(const Protocol &protocol=Protocol()) noexcept
open socket using the given protocol.
Definition socket.hpp:168
std::chrono::steady_clock::time_point TimePoint
Definition socket.hpp:61
Mode mode() const noexcept
get the blocking mode of the socket.
Definition socket.hpp:374
void setMode(Mode mode) noexcept
set the socket to the non-blocking or blocking mode.
Definition socket.hpp:296
State
socket states.
Definition socket.hpp:76
@ Disconnected
Definition socket.hpp:80
@ Connecting
Definition socket.hpp:77
@ Disconnecting
Definition socket.hpp:79
@ Closed
Definition socket.hpp:81
@ Connected
Definition socket.hpp:78
Protocol _protocol
protocol.
Definition socket.hpp:477
int wait(bool wantRead, bool wantWrite) const noexcept
wait for the socket handle to become ready.
Definition socket.hpp:394
Mode _mode
socket mode.
Definition socket.hpp:471
virtual void close() noexcept
close the socket.
Definition socket.hpp:203
int family() const noexcept
get socket address family.
Definition socket.hpp:347
Mode
socket modes.
Definition socket.hpp:67
@ Blocking
Definition socket.hpp:68
@ NonBlocking
Definition socket.hpp:69
std::unique_ptr< BasicSocket< Protocol > > Ptr
Definition socket.hpp:59
virtual ~BasicSocket()
destroy the socket instance.
Definition socket.hpp:155
int waitFor(bool wantRead, bool wantWrite, std::chrono::nanoseconds timeout) const noexcept
wait for the socket handle to become ready, giving up after the given duration.
Definition socket.hpp:406
BasicSocket(Mode mode) noexcept
create socket instance specifying the mode.
Definition socket.hpp:96
int waitUntil(bool wantRead, bool wantWrite, TimePoint deadline) const noexcept
wait for the socket handle to become ready, giving up at the given time point.
Definition socket.hpp:418
bool waitReadyRead(std::chrono::nanoseconds timeout) const noexcept
block until new data is available for reading, giving up after the given duration.
Definition socket.hpp:248
virtual int bind(const Endpoint &endpoint) noexcept
assigns the specified endpoint to the socket.
Definition socket.hpp:218
BasicSocket(BasicSocket &&other) noexcept
move constructor.
Definition socket.hpp:118
bool waitReadyRead(TimePoint deadline) const noexcept
block until new data is available for reading, giving up at the given time point.
Definition socket.hpp:258
int type() const noexcept
get the protocol communication semantic.
Definition socket.hpp:356
State _state
socket state.
Definition socket.hpp:468
typename Protocol::Endpoint Endpoint
Definition socket.hpp:60
BasicSocket() noexcept
default constructor.
Definition socket.hpp:87
Endpoint localEndpoint() const noexcept
determine the local endpoint associated with this socket.
Definition socket.hpp:321
bool waitReadyRead() const noexcept
block until new data is available for reading.
Definition socket.hpp:238
int handle() const noexcept
get socket native handle.
Definition socket.hpp:383
bool waitReadyWrite(std::chrono::nanoseconds timeout) const noexcept
block until at least one byte can be written, giving up after the given duration.
Definition socket.hpp:277
bool waitReadyWrite(TimePoint deadline) const noexcept
block until at least one byte can be written, giving up at the given time point.
Definition socket.hpp:287
int protocol() const noexcept
get socket protocol.
Definition socket.hpp:365
int _handle
socket handle.
Definition socket.hpp:474
BasicSocket(const BasicSocket &other)=delete
copy constructor.
bool waitReadyWrite() const noexcept
block until at least one byte can be written.
Definition socket.hpp:267
Definition acceptor.hpp:32
bool operator==(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:196
struct timespec toTimespec(std::chrono::time_point< Clock, Duration > timePoint)
converts time_point to timespec.
Definition utils.hpp:431
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195
std::strong_ordering operator<=>(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
three-way compare endpoints.
Definition endpoint.hpp:208