25#ifndef JOIN_CORE_ASYNC_ACCEPTOR_HPP
26#define JOIN_CORE_ASYNC_ACCEPTOR_HPP
36#include <system_error>
44 template <
class Protocol,
class ProactorType>
45 class BasicAsyncStreamAcceptor :
public CompletionHandler
50 using Socket =
typename Protocol::Socket;
59 : _proactor (&proactor)
103 int create (
const Endpoint& endpoint,
int flags = SOCK_CLOEXEC | SOCK_NONBLOCK)
noexcept
105 return _acceptor.
create (endpoint, flags | SOCK_NONBLOCK);
121 while (!_proactor->isProactorThread () &&
pending (_acceptOp.
op));
134 bool flush =
true) noexcept
148 _acceptOp.
remoteLen =
sizeof (
struct sockaddr_storage);
151 flags | SOCK_NONBLOCK,
this);
153 if (_proactor->submit (_acceptOp.
op, flush,
false) == -1)
173 bool flush =
true) noexcept
191 if (_proactor->submit (_acceptOp.
op, flush,
false) == -1)
214 if (_proactor->cancel (_acceptOp.
op,
true,
true) == -1)
222#ifdef JOIN_HAS_IO_URING
227 int flush () noexcept
229 return _proactor->flush (
false);
248 return _acceptor.
opened ();
257 return _acceptor.
family ();
266 return _acceptor.
type ();
284 return _acceptor.
handle ();
295 std::error_code code =
296 (result < 0) ? std::error_code (-result, std::generic_category ()) : std::error_code ();
298 dispatch (op, code, (result > 0) ?
static_cast<size_t> (result) : 0);
336 accept->acceptHandler (std::move (sock), code,
true);
346 handler (std::move (sock), code,
false);
361 std::memory_order_acquire, std::memory_order_relaxed) ||
372 std::memory_order_relaxed);
399 ProactorType* _proactor;
adaptive backoff for busy-wait loops, requires Rdtsc::calibrate ().
Definition backoff.hpp:47
asynchronous stream acceptor class.
Definition protocol.hpp:97
BasicAsyncStreamAcceptor(const BasicAsyncStreamAcceptor &other)=delete
copy constructor.
BasicAsyncAccept< Protocol, ProactorType > AsyncAccept
Definition async_acceptor.hpp:51
void disarmOp(IoOperation::State expected) noexcept
release the accept operation reservation.
Definition async_acceptor.hpp:369
void close() noexcept
close acceptor, cancelling the acceptation in flight.
Definition async_acceptor.hpp:111
int cancel() noexcept
cancel the acceptation in flight, if any.
Definition async_acceptor.hpp:207
ssize_t asyncAcceptMulti(AcceptHandler handler, int flags=SOCK_NONBLOCK|SOCK_CLOEXEC, bool flush=true) noexcept
start an asynchronous multishot acceptation, staying armed until cancelled or failed.
Definition async_acceptor.hpp:172
int type() const noexcept
get the acceptor communication semantic.
Definition async_acceptor.hpp:264
virtual void dispatch(IoOperation &op, const std::error_code &code, size_t size) noexcept
invoke the completion handler of the given operation.
Definition async_acceptor.hpp:317
bool armOp() noexcept
reserve the accept operation for submission.
Definition async_acceptor.hpp:356
typename AsyncAccept::Accept AcceptHandler
Definition async_acceptor.hpp:52
bool opened() const noexcept
check if the acceptor is opened.
Definition async_acceptor.hpp:246
void onCancel(IoOperation &op, int result) override
method called when an operation is cancelled.
Definition async_acceptor.hpp:306
int protocol() const noexcept
get acceptor protocol.
Definition async_acceptor.hpp:273
int create(const Endpoint &endpoint, int flags=SOCK_CLOEXEC|SOCK_NONBLOCK) noexcept
create acceptor.
Definition async_acceptor.hpp:103
Endpoint localEndpoint() const
determine the local endpoint associated with this acceptor.
Definition async_acceptor.hpp:237
BasicAsyncStreamAcceptor(ProactorType &proactor=ProactorThread::proactor())
create the acceptor instance.
Definition async_acceptor.hpp:58
BasicAsyncStreamAcceptor(BasicAsyncStreamAcceptor &&other)=delete
move constructor.
bool pending(const IoOperation &op) const noexcept
check if an operation is in flight or completing.
Definition async_acceptor.hpp:390
void completeAccept(AsyncAccept *accept, const std::error_code &code, size_t handle) noexcept
invoke the accept completion handler.
Definition async_acceptor.hpp:328
void onComplete(IoOperation &op, int result) override
method called when an operation completes.
Definition async_acceptor.hpp:293
int handle() const noexcept
get acceptor native handle.
Definition async_acceptor.hpp:282
BasicAsyncStreamAcceptor & operator=(const BasicAsyncStreamAcceptor &other)=delete
copy assignment operator.
ssize_t asyncAccept(AcceptHandler handler, int flags=SOCK_NONBLOCK|SOCK_CLOEXEC, bool flush=true) noexcept
start an asynchronous acceptation.
Definition async_acceptor.hpp:133
~BasicAsyncStreamAcceptor()
destroy the acceptor instance.
Definition async_acceptor.hpp:92
BasicStreamAcceptor< Protocol > Acceptor
Definition async_acceptor.hpp:48
typename Protocol::Endpoint Endpoint
Definition async_acceptor.hpp:49
typename Protocol::Socket Socket
Definition async_acceptor.hpp:50
bool inFlight(const IoOperation &op) const noexcept
check if an operation is in flight.
Definition async_acceptor.hpp:380
int family() const noexcept
get address family.
Definition async_acceptor.hpp:255
static BasicProactor & proactor()
get the Proactor instance owned by the singleton ProactorThread.
Definition proactor.hpp:1053
basic stream acceptor class.
Definition protocol.hpp:56
bool opened() const noexcept
check if the socket is opened.
Definition acceptor.hpp:237
Endpoint localEndpoint() const
determine the local endpoint associated with this socket.
Definition acceptor.hpp:219
void close() noexcept
close acceptor.
Definition acceptor.hpp:172
int family() const noexcept
get address family.
Definition acceptor.hpp:246
int protocol() const noexcept
get acceptor protocol.
Definition acceptor.hpp:264
int type() const noexcept
get the protocol communication semantic.
Definition acceptor.hpp:255
int handle() const noexcept
get socket native handle.
Definition acceptor.hpp:273
int create(const Endpoint &endpoint, int flags=SOCK_CLOEXEC) noexcept
create acceptor
Definition acceptor.hpp:106
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195
asynchronous accept operation.
Definition async_operation.hpp:64
Function< void(Socket &&, const std::error_code &, bool), 16 > Accept
handler invoked on completion.
Definition async_operation.hpp:69
Endpoint remote
remote endpoint.
Definition async_operation.hpp:78
Accept acceptHandler
handler invoked on completion.
Definition async_operation.hpp:75
IoOperation op
operation submitted to the proactor.
Definition async_operation.hpp:72
socklen_t remoteLen
remote address length.
Definition async_operation.hpp:81
Describes a single asynchronous operation submitted to the Proactor.
Definition io_operation.hpp:48
static IoOperation makeAccept(int fd, sockaddr *addr, socklen_t *addrlen, int flags, CompletionHandler *handler) noexcept
build an accept operation.
Definition io_operation.cpp:110
State
operation lifecycle state.
Definition io_operation.hpp:53
static IoOperation makeAcceptMulti(int fd, int flags, CompletionHandler *handler) noexcept
build a multishot accept operation.
Definition io_operation.cpp:127
std::atomic< State > state
operation state.
Definition io_operation.hpp:404
#define JOIN_LIKELY(x)
Definition utils.hpp:50
#define JOIN_UNLIKELY(x)
Definition utils.hpp:51