25#ifndef JOIN_CORE_ASYNC_RAW_SOCKET_HPP
26#define JOIN_CORE_ASYNC_RAW_SOCKET_HPP
36#include <system_error>
49 template <
class Protocol,
class ProactorType,
size_t OpCount>
50 class BasicAsyncRawSocket :
public BasicAsyncSocket<Protocol, ProactorType, OpCount>
53 using Socket =
typename Protocol::Socket;
55 using Option =
typename Socket::Option;
134 return this->
_socket.bind (endpoint);
144 return this->
_socket.bindToDevice (device);
153 return this->
_socket.canRead ();
166 bool link =
false) noexcept
182 read->
iov.iov_base = data;
183 read->
iov.iov_len = maxSize;
184 read->
msg.msg_name =
nullptr;
185 read->
msg.msg_namelen = 0;
186 read->
msg.msg_iov = &read->
iov;
187 read->
msg.msg_iovlen = 1;
188 read->
msg.msg_control =
nullptr;
189 read->
msg.msg_controllen = 0;
190 read->
msg.msg_flags = 0;
204 return static_cast<ssize_t
> (index);
207#ifdef JOIN_HAS_IO_URING
218 ssize_t asyncReadFixed (
ReadHandler handler,
char* data,
size_t maxSize, uint16_t index,
bool flush =
true,
219 bool link =
false) noexcept
234 read->readHandler = std::move (handler);
248 return static_cast<ssize_t
> (slot);
288 return static_cast<ssize_t
> (index);
301 bool link =
false) noexcept
317 write->
iov.iov_base =
const_cast<char*
> (data);
318 write->
iov.iov_len = size;
319 write->
msg.msg_name =
nullptr;
320 write->
msg.msg_namelen = 0;
321 write->
msg.msg_iov = &write->
iov;
322 write->
msg.msg_iovlen = 1;
323 write->
msg.msg_control =
nullptr;
324 write->
msg.msg_controllen = 0;
325 write->
msg.msg_flags = 0;
339 return static_cast<ssize_t
> (index);
342#ifdef JOIN_HAS_IO_URING
353 ssize_t asyncWriteFixed (
WriteHandler handler,
const char* data,
size_t size, uint16_t index,
bool flush =
true,
354 bool link =
false) noexcept
369 write->writeHandler = std::move (handler);
383 return static_cast<ssize_t
> (slot);
393 for (
auto& slot : this->
_ops)
395 IoOperation* op = slot.load (std::memory_order_acquire);
414 return this->
_socket.setOption (option, value);
461 this->
_socket._state = Socket::Connected;
480 std::error_code result = code;
481 const char* data =
nullptr;
486 data =
static_cast<const char*
> (read->op.data.stream.buf);
490 data =
static_cast<const char*
> (read->op.data.rw.buf);
494 data =
static_cast<const char*
> (read->msg.msg_iov->iov_base);
498 if (this->
_socket.type () == SOCK_STREAM)
505 else if (
JOIN_UNLIKELY (!result && (read->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
512 if (read->msg.msg_name !=
nullptr)
514 from =
Endpoint (
static_cast<const struct sockaddr*
> (read->msg.msg_name), read->msg.msg_namelen);
517 const char* control =
nullptr;
518 size_t controlSize = 0;
520 if ((read->msg.msg_control !=
nullptr) && (read->msg.msg_controllen > 0))
522 control =
static_cast<const char*
> (read->msg.msg_control);
523 controlSize = read->msg.msg_controllen;
528 if (read->readFromHandler)
530 read->readFromHandler (result, data, size, from, control, controlSize,
true);
534 read->readHandler (result, data, size,
true);
540 ReadHandler handler = std::move (read->readHandler);
547 fromHandler (result, data, size, from, control, controlSize,
false);
551 handler (result, data, size,
false);
571 handler (code, size);
asynchronous raw socket class.
Definition protocol.hpp:88
BasicAsyncRawSocket(BasicAsyncRawSocket &&other) noexcept
move constructor.
Definition async_raw_socket.hpp:99
void completeWrite(AsyncWrite *write, const std::error_code &code, size_t size) noexcept
invoke the write completion handler.
Definition async_raw_socket.hpp:560
int canRead() const noexcept
get the number of readable bytes.
Definition async_raw_socket.hpp:151
typename Socket::Option Option
Definition async_raw_socket.hpp:55
ssize_t asyncRead(ReadHandler handler, char *data, size_t maxSize, bool flush=true, bool link=false) noexcept
start an asynchronous read.
Definition async_raw_socket.hpp:165
BasicAsyncRead< Protocol, ProactorType > AsyncRead
Definition async_raw_socket.hpp:56
BasicAsyncWrite< Protocol, ProactorType > AsyncWrite
Definition async_raw_socket.hpp:57
typename AsyncWrite::Write WriteHandler
Definition async_raw_socket.hpp:61
int cancelConnect() noexcept
cancel the connect operation in flight, if any.
Definition async_raw_socket.hpp:391
void completeConnect(AsyncWrite *connect, const std::error_code &code) noexcept
invoke the connect completion handler.
Definition async_raw_socket.hpp:451
int setOption(Option option, int value) noexcept
set the given option to the given value.
Definition async_raw_socket.hpp:412
AsyncWrite * allocateWrite() noexcept
allocate a write operation in the allocator.
Definition async_raw_socket.hpp:588
BasicAsyncRawSocket(Socket &&sock, ProactorType &proactor=ProactorThread::proactor())
create the socket instance adopting an already opened socket.
Definition async_raw_socket.hpp:77
BasicAsyncRawSocket(ProactorType &proactor=ProactorThread::proactor())
create the socket instance.
Definition async_raw_socket.hpp:67
~BasicAsyncRawSocket()
destroy the socket instance.
Definition async_raw_socket.hpp:122
int bindToDevice(const std::string &device) noexcept
assign the specified device to the socket.
Definition async_raw_socket.hpp:142
ssize_t asyncWrite(WriteHandler handler, const char *data, size_t size, bool flush=true, bool link=false) noexcept
start an asynchronous write.
Definition async_raw_socket.hpp:300
ssize_t asyncReadMulti(ReadHandler handler, uint16_t group, bool flush=true) noexcept
start an asynchronous multishot read, staying armed until cancelled or failed.
Definition async_raw_socket.hpp:259
void completeRead(AsyncRead *read, const std::error_code &code, size_t size) noexcept
invoke the read completion handler.
Definition async_raw_socket.hpp:478
typename AsyncRead::Read ReadHandler
Definition async_raw_socket.hpp:58
BasicAsyncRawSocket & operator=(const BasicAsyncRawSocket &other)=delete
copy assignment operator.
AsyncRead * allocateRead() noexcept
allocate a read operation in the allocator.
Definition async_raw_socket.hpp:579
typename Protocol::Endpoint Endpoint
Definition async_raw_socket.hpp:54
BasicAsyncRawSocket(const BasicAsyncRawSocket &other)=delete
copy constructor.
int bind(const Endpoint &endpoint) noexcept
assign the specified endpoint to the socket.
Definition async_raw_socket.hpp:132
typename Protocol::Socket Socket
Definition async_raw_socket.hpp:53
typename AsyncRead::ReadFrom ReadFromHandler
Definition async_raw_socket.hpp:59
void dispatch(IoOperation &op, const std::error_code &code, size_t size) noexcept override
invoke the completion handler of the given operation, then release it.
Definition async_raw_socket.hpp:424
typename AsyncWrite::Connect ConnectHandler
Definition async_raw_socket.hpp:60
basic asynchronous socket class.
Definition protocol.hpp:85
virtual void dispatch(IoOperation &op, const std::error_code &code, size_t size) noexcept
invoke the completion handler of the given operation, then release it.
Definition async_socket.hpp:446
Socket _socket
Definition async_socket.hpp:657
void close() noexcept
Definition async_socket.hpp:171
BasicAsyncSocket(BasicProactor &proactor=ProactorThread::proactor())
Definition async_socket.hpp:76
std::array< std::atomic< IoOperation * >, _opCount > _ops
Definition async_socket.hpp:663
void releaseOperation(Op *operation) noexcept
Definition async_socket.hpp:539
void resumeAll() noexcept
Definition async_socket.hpp:566
int cancelOperation(IoOperation &op) noexcept
Definition async_socket.hpp:616
BasicAsyncSocket & operator=(const BasicAsyncSocket &other)=delete
copy assignment operator.
Op * allocateOperation() noexcept
Definition async_socket.hpp:518
BasicProactor * _proactor
Definition async_socket.hpp:654
OpAllocator _allocator
Definition async_socket.hpp:660
static BasicProactor & proactor()
get the Proactor instance owned by the singleton ProactorThread.
Definition proactor.hpp:1053
int submit(IoOperation &op, bool flush=false, bool sync=false) noexcept
submit an asynchronous operation to the proactor.
Definition proactor.hpp:741
uint32_t getIndex(void *p) const noexcept
get the index of a chunk in the pool.
Definition slab_allocator.hpp:455
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 read operation.
Definition async_operation.hpp:89
Read readHandler
handler invoked on completion.
Definition async_operation.hpp:104
Function< void(const std::error_code &, const char *, size_t, bool), 16 > Read
handler invoked on completion.
Definition async_operation.hpp:93
IoOperation op
operation submitted to the proactor.
Definition async_operation.hpp:101
msghdr msg
read message header.
Definition async_operation.hpp:110
Function< void(const std::error_code &, const char *, size_t, const Endpoint &, const char *, size_t, bool), 16 > ReadFrom
handler invoked on completion, reporting the endpoint the data are coming from and the control messag...
Definition async_operation.hpp:96
iovec iov
read scatter gather entry.
Definition async_operation.hpp:113
asynchronous write operation.
Definition async_operation.hpp:121
msghdr msg
write message header.
Definition async_operation.hpp:138
Function< void(const std::error_code &, size_t), 16 > Write
handler invoked on completion.
Definition async_operation.hpp:126
Write writeHandler
handler invoked on completion.
Definition async_operation.hpp:135
iovec iov
write scatter gather entry.
Definition async_operation.hpp:141
Function< void(const std::error_code &), 16 > Connect
handler invoked on connection completion.
Definition async_operation.hpp:123
IoOperation op
operation submitted to the proactor.
Definition async_operation.hpp:129
Describes a single asynchronous operation submitted to the Proactor.
Definition io_operation.hpp:48
static IoOperation makeSendmsg(int fd, const msghdr *msg, int flags, CompletionHandler *handler, bool linked=false) noexcept
build a send-message operation.
Definition io_operation.cpp:271
static IoOperation makeReadFixed(int fd, void *buf, uint32_t len, uint16_t index, CompletionHandler *handler, bool linked=false) noexcept
build a fixed-buffer read operation.
Definition io_operation.cpp:197
static IoOperation makeRecvMulti(int fd, uint16_t group, int flags, CompletionHandler *handler) noexcept
build a multishot receive operation.
Definition io_operation.cpp:306
static IoOperation makeWriteFixed(int fd, const void *buf, uint32_t len, uint16_t index, CompletionHandler *handler, bool linked=false) noexcept
build a fixed-buffer write operation.
Definition io_operation.cpp:216
static IoOperation makeRecvmsg(int fd, msghdr *msg, int flags, CompletionHandler *handler, bool linked=false) noexcept
build a receive-message operation.
Definition io_operation.cpp:235
std::atomic< State > state
operation state.
Definition io_operation.hpp:404
uint8_t code
operation code.
Definition io_operation.hpp:401
Opcode
operation code.
Definition io_operation.hpp:64
#define JOIN_LIKELY(x)
Definition utils.hpp:50
#define JOIN_UNLIKELY(x)
Definition utils.hpp:51