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 ProactorType, size_t OpCount>
42 class BasicAsyncStreamSocket : public BasicAsyncRawSocket<Protocol, ProactorType, OpCount>
43 {
44 public:
45 using Socket = typename Protocol::Socket;
46 using Endpoint = typename Protocol::Endpoint;
49
54 explicit BasicAsyncStreamSocket (ProactorType& proactor = ProactorThread::proactor ())
55 : BasicAsyncRawSocket<Protocol, ProactorType, OpCount> (proactor)
56 {
57 }
58
64 explicit BasicAsyncStreamSocket (Socket&& sock, ProactorType& proactor = ProactorThread::proactor ())
65 : BasicAsyncRawSocket<Protocol, ProactorType, OpCount> (std::move (sock), proactor)
66 {
67 }
68
74
81
86 BasicAsyncStreamSocket (BasicAsyncStreamSocket&& other) noexcept = default;
87
94
101 int asyncConnect (ConnectHandler handler, const Endpoint& endpoint) noexcept
102 {
103 if (JOIN_UNLIKELY (!this->_allocator.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->releaseOperation (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
asynchronous raw socket class.
Definition protocol.hpp:88
AsyncWrite * allocateWrite() noexcept
Definition async_raw_socket.hpp:588
Socket _socket
Definition async_socket.hpp:657
void releaseOperation(Op *operation) noexcept
Definition async_socket.hpp:539
BasicProactor * _proactor
Definition async_socket.hpp:654
OpAllocator _allocator
Definition async_socket.hpp:660
asynchronous stream socket class.
Definition protocol.hpp:94
BasicAsyncStreamSocket(Socket &&sock, ProactorType &proactor=ProactorThread::proactor())
create the socket instance adopting an already connected socket.
Definition async_stream_socket.hpp:64
typename Protocol::Socket Socket
Definition async_stream_socket.hpp:45
BasicAsyncStreamSocket(ProactorType &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
BasicAsyncStreamSocket(const BasicAsyncStreamSocket &other)=delete
copy constructor.
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
BasicAsyncStreamSocket(BasicAsyncStreamSocket &&other) noexcept=default
move constructor.
int mtu() const noexcept
get the path maximum transmission unit.
Definition async_stream_socket.hpp:179
int asyncConnect(ConnectHandler handler, const Endpoint &endpoint) noexcept
start an asynchronous connection to the given endpoint.
Definition async_stream_socket.hpp:101
const Endpoint & remoteEndpoint() const noexcept
determine the remote endpoint associated with this socket.
Definition async_stream_socket.hpp:152
typename AsyncWrite::Connect ConnectHandler
Definition async_stream_socket.hpp:48
BasicAsyncStreamSocket & operator=(const BasicAsyncStreamSocket &other)=delete
copy assignment operator.
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
bool hasBackend() const noexcept
check if the slab allocator still owns a memory region.
Definition slab_allocator.hpp:444
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:121
Connect connectHandler
handler invoked on connection completion.
Definition async_operation.hpp:132
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
static IoOperation makeConnect(int fd, const sockaddr *addr, socklen_t addrlen, CompletionHandler *handler) noexcept
build a connect operation.
Definition io_operation.cpp:144
std::atomic< State > state
operation state.
Definition io_operation.hpp:404
#define JOIN_UNLIKELY(x)
Definition utils.hpp:51