25#ifndef JOIN_CORE_SOCKET_HPP
26#define JOIN_CORE_SOCKET_HPP
41#include <linux/icmp.h>
50 template <
class Protocol>
54 template <
class P,
class E,
size_t N>
58 using Ptr = std::unique_ptr<BasicSocket<Protocol>>;
60 using TimePoint = std::chrono::steady_clock::time_point;
119 ,
_mode (other._mode)
126 other._protocol = Protocol ();
146 other._protocol = Protocol ();
187 lastError = std::error_code (errno, std::generic_category ());
224 if (
::bind (
_handle, endpoint.addr (), endpoint.length ()) == -1)
226 lastError = std::error_code (errno, std::generic_category ());
239 return (
waitUntil (
true,
false, TimePoint::max ()) == 0);
249 return (
waitUntil (
true,
false, std::chrono::steady_clock::now () + timeout) == 0);
259 return (
waitUntil (
true,
false, deadline) == 0);
268 return (
waitUntil (
false,
true, TimePoint::max ()) == 0);
278 return (
waitUntil (
false,
true, std::chrono::steady_clock::now () + timeout) == 0);
288 return (
waitUntil (
false,
true, deadline) == 0);
301 int flags = ::fcntl (
_handle, F_GETFL, 0);
305 flags = flags | O_NONBLOCK;
309 flags = flags & ~O_NONBLOCK;
312 ::fcntl (
_handle, F_SETFL, flags);
322 struct sockaddr_storage sa;
323 socklen_t sa_len =
sizeof (
struct sockaddr_storage);
325 if (::getsockname (
_handle,
reinterpret_cast<struct sockaddr*
> (&sa), &sa_len) == -1)
330 return Endpoint (
reinterpret_cast<struct sockaddr*
> (&sa), sa_len);
393 int wait (
bool wantRead,
bool wantWrite)
const noexcept
395 return waitUntil (wantRead, wantWrite, TimePoint::max ());
405 int waitFor (
bool wantRead,
bool wantWrite, std::chrono::nanoseconds timeout)
const noexcept
407 return waitUntil (wantRead, wantWrite, std::chrono::steady_clock::now () + timeout);
434 struct timespec ts = {};
435 const struct timespec* remaining =
nullptr;
437 if (deadline != TimePoint::max ())
439 ts =
toTimespec (std::max (deadline - std::chrono::steady_clock::now (), TimePoint::duration::zero ()));
443 int nset = (
handle.fd > -1) ? ::ppoll (&
handle, 1, remaining,
nullptr) : -1;
452 lastError = std::error_code (errno, std::generic_category ());
485 template <
class Protocol>
488 return a.handle () < b.handle ();
asynchronous raw socket class.
Definition protocol.hpp:85
basic socket class.
Definition socket.hpp:52
bool opened() const noexcept
check if the socket is opened.
Definition socket.hpp:337
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:167
std::chrono::steady_clock::time_point TimePoint
Definition socket.hpp:60
Mode mode() const noexcept
get the blocking mode of the socket.
Definition socket.hpp:373
void setMode(Mode mode) noexcept
set the socket to the non-blocking or blocking mode.
Definition socket.hpp:295
State
socket states.
Definition socket.hpp:75
@ Disconnected
Definition socket.hpp:79
@ Connecting
Definition socket.hpp:76
@ Disconnecting
Definition socket.hpp:78
@ Closed
Definition socket.hpp:80
@ Connected
Definition socket.hpp:77
Protocol _protocol
protocol.
Definition socket.hpp:476
int wait(bool wantRead, bool wantWrite) const noexcept
wait for the socket handle to become ready.
Definition socket.hpp:393
Mode _mode
socket mode.
Definition socket.hpp:470
virtual void close() noexcept
close the socket.
Definition socket.hpp:202
int family() const noexcept
get socket address family.
Definition socket.hpp:346
Mode
socket modes.
Definition socket.hpp:66
@ Blocking
Definition socket.hpp:67
@ NonBlocking
Definition socket.hpp:68
std::unique_ptr< BasicSocket< Protocol > > Ptr
Definition socket.hpp:58
virtual ~BasicSocket()
destroy the socket instance.
Definition socket.hpp:154
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:405
BasicSocket(Mode mode) noexcept
create socket instance specifying the mode.
Definition socket.hpp:95
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:417
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:247
virtual int bind(const Endpoint &endpoint) noexcept
assigns the specified endpoint to the socket.
Definition socket.hpp:217
BasicSocket(BasicSocket &&other) noexcept
move constructor.
Definition socket.hpp:117
bool waitReadyRead(TimePoint deadline) const noexcept
block until new data is available for reading, giving up at the given time point.
Definition socket.hpp:257
int type() const noexcept
get the protocol communication semantic.
Definition socket.hpp:355
State _state
socket state.
Definition socket.hpp:467
typename Protocol::Endpoint Endpoint
Definition socket.hpp:59
BasicSocket() noexcept
default constructor.
Definition socket.hpp:86
Endpoint localEndpoint() const noexcept
determine the local endpoint associated with this socket.
Definition socket.hpp:320
bool waitReadyRead() const noexcept
block until new data is available for reading.
Definition socket.hpp:237
int handle() const noexcept
get socket native handle.
Definition socket.hpp:382
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:276
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:286
int protocol() const noexcept
get socket protocol.
Definition socket.hpp:364
int _handle
socket handle.
Definition socket.hpp:473
BasicSocket(const BasicSocket &other)=delete
copy constructor.
bool waitReadyWrite() const noexcept
block until at least one byte can be written.
Definition socket.hpp:266
Definition acceptor.hpp:32
struct timespec toTimespec(std::chrono::time_point< Clock, Duration > timePoint)
converts time_point to timespec.
Definition utils.hpp:462
bool operator<(const BasicDatagramSocket< Protocol > &a, const BasicDatagramSocket< Protocol > &b) noexcept
compare if socket handle is inferior.
Definition datagram_socket.hpp:394
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195