25#ifndef JOIN_CORE_ASYNC_ACCEPTOR_HPP
26#define JOIN_CORE_ASYNC_ACCEPTOR_HPP
36#include <system_error>
44 template <
class Protocol,
class Proactor>
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);
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);
adaptive backoff strategy for busy-wait loops.
Definition backoff.hpp:43
asynchronous stream acceptor class.
Definition protocol.hpp:94
void completeAccept(AsyncAccept *accept, const std::error_code &code, size_t handle) noexcept
invoke the accept completion handler.
Definition async_acceptor.hpp:328
BasicAsyncStreamAcceptor & operator=(const BasicAsyncStreamAcceptor &other)=delete
copy assignment operator.
BasicStreamAcceptor< Protocol > Acceptor
Definition async_acceptor.hpp:48
int cancel() noexcept
cancel the acceptation in flight, if any.
Definition async_acceptor.hpp:207
int protocol() const noexcept
get acceptor protocol.
Definition async_acceptor.hpp:273
void onComplete(IoOperation &op, int result) override
method called when an operation completes.
Definition async_acceptor.hpp:293
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
ssize_t asyncAccept(AcceptHandler handler, int flags=SOCK_NONBLOCK|SOCK_CLOEXEC, bool flush=true) noexcept
start an asynchronous acceptation.
Definition async_acceptor.hpp:133
typename Protocol::Endpoint Endpoint
Definition async_acceptor.hpp:49
int family() const noexcept
get address family.
Definition async_acceptor.hpp:255
bool opened() const noexcept
check if the acceptor is opened.
Definition async_acceptor.hpp:246
Endpoint localEndpoint() const
determine the local endpoint associated with this acceptor.
Definition async_acceptor.hpp:237
int create(const Endpoint &endpoint, int flags=SOCK_CLOEXEC|SOCK_NONBLOCK) noexcept
create acceptor.
Definition async_acceptor.hpp:103
BasicAsyncStreamAcceptor(const BasicAsyncStreamAcceptor &other)=delete
copy constructor.
~BasicAsyncStreamAcceptor()
destroy the acceptor instance.
Definition async_acceptor.hpp:92
int handle() const noexcept
get acceptor native handle.
Definition async_acceptor.hpp:282
bool armOp() noexcept
reserve the accept operation for submission.
Definition async_acceptor.hpp:356
void disarmOp(IoOperation::State expected) noexcept
release the accept operation reservation.
Definition async_acceptor.hpp:369
bool pending(const IoOperation &op) const noexcept
check if an operation is in flight or completing.
Definition async_acceptor.hpp:390
BasicAsyncStreamAcceptor(BasicAsyncStreamAcceptor &&other)=delete
move constructor.
BasicAsyncStreamAcceptor(Proactor &proactor=ProactorThread::proactor())
create the acceptor instance.
Definition async_acceptor.hpp:58
int type() const noexcept
get the acceptor communication semantic.
Definition async_acceptor.hpp:264
bool inFlight(const IoOperation &op) const noexcept
check if an operation is in flight.
Definition async_acceptor.hpp:380
typename Protocol::Socket Socket
Definition async_acceptor.hpp:50
typename AsyncAccept::Accept AcceptHandler
Definition async_acceptor.hpp:52
BasicAsyncAccept< Protocol, Proactor > AsyncAccept
Definition async_acceptor.hpp:51
void onCancel(IoOperation &op, int result) override
method called when an operation is cancelled.
Definition async_acceptor.hpp:306
void close() noexcept
close acceptor, cancelling the acceptation in flight.
Definition async_acceptor.hpp:111
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
static BasicProactor & proactor()
get the Proactor instance owned by the singleton ProactorThread.
Definition proactor.hpp:982
basic proactor class.
Definition proactor.hpp:156
int submit(IoOperation &op, bool flush=false, bool sync=false) noexcept
submit an asynchronous operation to the proactor.
Definition proactor.hpp:655
bool isProactorThread() const noexcept
check if the calling thread is the proactor thread.
Definition proactor_epoll_impl.hpp:252
int cancel(IoOperation &op, bool flush=false, bool sync=false) noexcept
cancel an in-flight operation.
Definition proactor.hpp:703
basic stream acceptor class.
Definition protocol.hpp:53
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
Accept acceptHandler
handler invoked on completion.
Definition async_operation.hpp:75
Endpoint remote
remote endpoint.
Definition async_operation.hpp:78
socklen_t remoteLen
remote address length.
Definition async_operation.hpp:81
IoOperation op
operation submitted to the proactor.
Definition async_operation.hpp:72
Describes a single asynchronous operation submitted to the Proactor.
Definition io_operation.hpp:47
static IoOperation makeAccept(int fd, sockaddr *addr, socklen_t *addrlen, int flags, CompletionHandler *handler) noexcept
build an accept operation.
Definition io_operation.cpp:102
State
operation lifecycle state.
Definition io_operation.hpp:52
static IoOperation makeAcceptMulti(int fd, int flags, CompletionHandler *handler) noexcept
build a multishot accept operation.
Definition io_operation.cpp:119
std::atomic< State > state
operation state.
Definition io_operation.hpp:392
#define JOIN_LIKELY(x)
Definition utils.hpp:45
#define JOIN_UNLIKELY(x)
Definition utils.hpp:46