join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
async_stream_socket.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_ASYNC_STREAM_SOCKET_HPP
26#define JOIN_CORE_ASYNC_STREAM_SOCKET_HPP
27
28// libjoin.
31
32// C++.
33#include <system_error>
34#include <utility>
35
36namespace join
37{
41 template <class Protocol, class Proactor, size_t OpCount>
42 class BasicAsyncStreamSocket : public BasicAsyncRawSocket<Protocol, Proactor, OpCount>
43 {
44 public:
45 using Socket = typename Protocol::Socket;
46 using Endpoint = typename Protocol::Endpoint;
49
55 : BasicAsyncRawSocket<Protocol, Proactor, OpCount> (proactor)
56 {
57 }
58
65 : BasicAsyncRawSocket<Protocol, Proactor, OpCount> (std::move (sock), proactor)
66 {
67 }
68
74
81
86 BasicAsyncStreamSocket (BasicAsyncStreamSocket&& other) noexcept = default;
87
94
101 int asyncConnect (const Endpoint& endpoint, ConnectHandler handler) noexcept
102 {
103 if (JOIN_UNLIKELY (!this->_arena.hasBackend ()))
104 {
106 return -1;
107 }
108
109 if (this->_socket.connected () || this->_socket.connecting ())
110 {
111 lastError = make_error_code (Errc::InUse);
112 return -1;
113 }
114
115 if (!this->_socket.opened () && (this->_socket.open (endpoint.protocol ()) == -1))
116 {
117 return -1; // LCOV_EXCL_LINE
118 }
119
120 AsyncWrite* connect = this->allocateWrite ();
121 if (JOIN_UNLIKELY (connect == nullptr))
122 {
123 // LCOV_EXCL_START
125 return -1;
126 // LCOV_EXCL_STOP
127 }
128
129 this->_socket._state = Socket::Connecting;
130 this->_socket._remote = endpoint;
131 connect->connectHandler = std::move (handler);
132 connect->op = IoOperation::makeConnect (this->_socket.handle (), this->_socket._remote.addr (),
133 this->_socket._remote.length (), this);
134 connect->op.state.store (IoOperation::State::Submitted, std::memory_order_release);
135
136 if (this->_proactor->submit (connect->op, true, false) == -1)
137 {
138 // LCOV_EXCL_START
139 this->releaseOp (connect);
140 this->_socket.close ();
141 return -1;
142 // LCOV_EXCL_STOP
143 }
144
145 return 0;
146 }
147
152 const Endpoint& remoteEndpoint () const noexcept
153 {
154 return this->_socket.remoteEndpoint ();
155 }
156
161 bool connected () noexcept
162 {
163 return this->_socket.connected ();
164 }
165
170 bool connecting () const noexcept
171 {
172 return this->_socket.connecting ();
173 }
174
179 int mtu () const noexcept
180 {
181 return this->_socket.mtu ();
182 }
183 };
184}
185
186#endif
bool hasBackend() const noexcept
check if the arena still owns a memory region.
Definition allocator.hpp:487
asynchronous raw socket class.
Definition protocol.hpp:85
AsyncWrite * allocateWrite() noexcept
Definition async_raw_socket.hpp:579
Socket _socket
Definition async_socket.hpp:629
void releaseOp(Op *operation) noexcept
Definition async_socket.hpp:525
OpArena _arena
Definition async_socket.hpp:632
BasicProactor * _proactor
Definition async_socket.hpp:626
asynchronous stream socket class.
Definition protocol.hpp:91
typename AsyncWrite::Connect ConnectHandler
Definition async_stream_socket.hpp:48
BasicAsyncStreamSocket(Socket &&sock, Proactor &proactor=ProactorThread::proactor())
create the socket instance adopting an already connected socket.
Definition async_stream_socket.hpp:64
BasicAsyncStreamSocket & operator=(const BasicAsyncStreamSocket &other)=delete
copy assignment operator.
const Endpoint & remoteEndpoint() const noexcept
determine the remote endpoint associated with this socket.
Definition async_stream_socket.hpp:152
int mtu() const noexcept
get the path maximum transmission unit.
Definition async_stream_socket.hpp:179
BasicAsyncStreamSocket(BasicAsyncStreamSocket &&other) noexcept=default
move constructor.
typename Protocol::Socket Socket
Definition async_stream_socket.hpp:45
bool connecting() const noexcept
check if the socket is connecting.
Definition async_stream_socket.hpp:170
typename Protocol::Endpoint Endpoint
Definition async_stream_socket.hpp:46
int asyncConnect(const Endpoint &endpoint, ConnectHandler handler) noexcept
start an asynchronous connection to the given endpoint.
Definition async_stream_socket.hpp:101
BasicAsyncStreamSocket(const BasicAsyncStreamSocket &other)=delete
copy constructor.
BasicAsyncStreamSocket(Proactor &proactor=ProactorThread::proactor())
create the socket instance.
Definition async_stream_socket.hpp:54
bool connected() noexcept
check if the socket is connected.
Definition async_stream_socket.hpp:161
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
Definition error.hpp:144
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
Connect connectHandler
handler invoked on connection completion.
Definition async_operation.hpp:130
IoOperation op
operation submitted to the proactor.
Definition async_operation.hpp:127
static IoOperation makeConnect(int fd, const sockaddr *addr, socklen_t addrlen, CompletionHandler *handler) noexcept
build a connect operation.
Definition io_operation.cpp:136
std::atomic< State > state
operation state.
Definition io_operation.hpp:392
#define JOIN_UNLIKELY(x)
Definition utils.hpp:46