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 Proactor,
size_t OpCount>
50 class BasicAsyncRawSocket :
public BasicAsyncSocket<Protocol, Proactor, 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 (
char* data,
size_t maxSize, uint16_t index,
ReadHandler handler,
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 (
const char* data,
size_t size, uint16_t index,
WriteHandler handler,
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);
463 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)))
512 if (read->msg.msg_name !=
nullptr)
514 from =
Endpoint (
static_cast<const struct sockaddr*
> (read->msg.msg_name), read->msg.msg_namelen);
519 if (read->readFromHandler)
521 read->readFromHandler (result, data, size, from,
true);
525 read->readHandler (result, data, size,
true);
531 ReadHandler handler = std::move (read->readHandler);
538 fromHandler (result, data, size, from,
false);
542 handler (result, data, size,
false);
562 handler (code, size);
uint32_t getIndex(void *p) const noexcept
get the index of a chunk in the pool.
Definition allocator.hpp:498
asynchronous raw socket class.
Definition protocol.hpp:85
AsyncRead * allocateRead() noexcept
allocate a read operation in the arena.
Definition async_raw_socket.hpp:570
BasicAsyncWrite< Protocol, Proactor > AsyncWrite
Definition async_raw_socket.hpp:57
BasicAsyncRawSocket(const BasicAsyncRawSocket &other)=delete
copy constructor.
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 Socket::Option Option
Definition async_raw_socket.hpp:55
int canRead() const noexcept
get the number of readable bytes.
Definition async_raw_socket.hpp:151
BasicAsyncRawSocket & operator=(const BasicAsyncRawSocket &other)=delete
copy assignment operator.
BasicAsyncRawSocket(Socket &&sock, Proactor &proactor=ProactorThread::proactor())
create the socket instance adopting an already opened socket.
Definition async_raw_socket.hpp:77
int bindToDevice(const std::string &device) noexcept
assign the specified device to the socket.
Definition async_raw_socket.hpp:142
typename AsyncWrite::Write WriteHandler
Definition async_raw_socket.hpp:61
typename Protocol::Socket Socket
Definition async_raw_socket.hpp:53
typename Protocol::Endpoint Endpoint
Definition async_raw_socket.hpp:54
ssize_t asyncWrite(const char *data, size_t size, WriteHandler handler, bool flush=true, bool link=false) noexcept
start an asynchronous write.
Definition async_raw_socket.hpp:300
void completeWrite(AsyncWrite *write, const std::error_code &code, size_t size) noexcept
invoke the write completion handler.
Definition async_raw_socket.hpp:551
ssize_t asyncReadMulti(uint16_t group, ReadHandler handler, bool flush=true) noexcept
start an asynchronous multishot read, staying armed until cancelled or failed.
Definition async_raw_socket.hpp:259
typename AsyncRead::Read ReadHandler
Definition async_raw_socket.hpp:58
typename AsyncWrite::Connect ConnectHandler
Definition async_raw_socket.hpp:60
ssize_t asyncRead(char *data, size_t maxSize, ReadHandler handler, bool flush=true, bool link=false) noexcept
start an asynchronous read.
Definition async_raw_socket.hpp:165
int bind(const Endpoint &endpoint) noexcept
assign the specified endpoint to the socket.
Definition async_raw_socket.hpp:132
~BasicAsyncRawSocket()
destroy the socket instance.
Definition async_raw_socket.hpp:122
BasicAsyncRawSocket(Proactor &proactor=ProactorThread::proactor())
create the socket instance.
Definition async_raw_socket.hpp:67
typename AsyncRead::ReadFrom ReadFromHandler
Definition async_raw_socket.hpp:59
int cancelConnect() noexcept
cancel the connect operation in flight, if any.
Definition async_raw_socket.hpp:391
BasicAsyncRead< Protocol, Proactor > AsyncRead
Definition async_raw_socket.hpp:56
BasicAsyncRawSocket(BasicAsyncRawSocket &&other) noexcept
move constructor.
Definition async_raw_socket.hpp:99
int setOption(Option option, int value) noexcept
set the given option to the given value.
Definition async_raw_socket.hpp:412
void completeConnect(AsyncWrite *connect, const std::error_code &code) noexcept
invoke the connect completion handler.
Definition async_raw_socket.hpp:451
AsyncWrite * allocateWrite() noexcept
allocate a write operation in the arena.
Definition async_raw_socket.hpp:579
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
basic asynchronous socket class.
Definition protocol.hpp:82
Socket _socket
Definition async_socket.hpp:629
void close() noexcept
Definition async_socket.hpp:171
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:432
void releaseOp(Op *operation) noexcept
Definition async_socket.hpp:525
int cancelOp(IoOperation *op) noexcept
Definition async_socket.hpp:588
OpArena _arena
Definition async_socket.hpp:632
Op * allocateOp() noexcept
Definition async_socket.hpp:504
BasicAsyncSocket & operator=(const BasicAsyncSocket &other)=delete
copy assignment operator.
std::array< std::atomic< IoOperation * >, _opCount > _ops
Definition async_socket.hpp:635
BasicProactor * _proactor
Definition async_socket.hpp:626
void resumeAll() noexcept
Definition async_socket.hpp:546
BasicAsyncSocket(BasicProactor &proactor=ProactorThread::proactor())
Definition async_socket.hpp:76
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
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
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:99
iovec iov
read scatter gather entry.
Definition async_operation.hpp:111
msghdr msg
read message header.
Definition async_operation.hpp:108
Read readHandler
handler invoked on completion.
Definition async_operation.hpp:102
Function< void(const std::error_code &, const char *, size_t, const Endpoint &, bool), 16 > ReadFrom
handler invoked on completion, reporting the endpoint the data are coming from.
Definition async_operation.hpp:96
asynchronous write operation.
Definition async_operation.hpp:119
Function< void(const std::error_code &), 16 > Connect
handler invoked on connection completion.
Definition async_operation.hpp:121
Function< void(const std::error_code &, size_t), 16 > Write
handler invoked on completion.
Definition async_operation.hpp:124
Write writeHandler
handler invoked on completion.
Definition async_operation.hpp:133
msghdr msg
write message header.
Definition async_operation.hpp:136
iovec iov
write scatter gather entry.
Definition async_operation.hpp:139
IoOperation op
operation submitted to the proactor.
Definition async_operation.hpp:127
Describes a single asynchronous operation submitted to the Proactor.
Definition io_operation.hpp:47
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:263
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:189
static IoOperation makeRecvMulti(int fd, uint16_t group, int flags, CompletionHandler *handler) noexcept
build a multishot receive operation.
Definition io_operation.cpp:298
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:208
static IoOperation makeRecvmsg(int fd, msghdr *msg, int flags, CompletionHandler *handler, bool linked=false) noexcept
build a receive-message operation.
Definition io_operation.cpp:227
std::atomic< State > state
operation state.
Definition io_operation.hpp:392
uint8_t code
operation code.
Definition io_operation.hpp:389
Opcode
operation code.
Definition io_operation.hpp:63
#define JOIN_LIKELY(x)
Definition utils.hpp:45
#define JOIN_UNLIKELY(x)
Definition utils.hpp:46