12 #ifndef __NET_SOCKETLISTENER_H__
13 #define __NET_SOCKETLISTENER_H__
30 #include <type_traits>
41 virtual bool isOpen()
const = 0;
48 : myExecutor(executor), mySocketGroup(nullptr)
53 void fail(
const hboost::system::error_code& ec,
const char* what);
60 template <
typename Endpo
intT,
typename Enabled =
void>
65 template <
typename Endpo
intT>
71 template <
typename SocketT>
82 = hboost::asio::basic_stream_socket<typename AcceptorT::protocol_type>>
92 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
95 int max_port_range = -1,
96 bool use_system_port =
false,
107 typename S = SocketT,
108 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
120 myAcceptor.set_option(
typename AcceptorT::reuse_address(
true));
124 catch (std::exception& e)
141 hboost::asio::dispatch(
143 [
this,
self = shared_from_this(),
force]()
149 hboost::system::error_code ec;
152 fail(ec,
"acceptor close");
163 typename S = SocketT,
164 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
170 typename S = SocketT,
171 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
177 typename S = SocketT,
178 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
184 typename S = SocketT,
185 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
191 typename S = SocketT,
192 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
198 typename S = SocketT,
199 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
205 typename S = SocketT,
206 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
212 typename S = SocketT,
213 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
229 ,
myAcceptor(hboost::asio::make_strand(executor))
241 const hboost::system::error_code& ec,
261 std::conditional_t<socket_info<socket_type>::uses_port,
PortInfo,
Empty>
265 template <
typename AcceptorT,
typename SocketT>
266 template <
typename S,
typename Detected>
272 UT_ASSERT(!
"Trying to start the same connection twice.");
275 endpoint_type ep(myPortInfo.myBindAddress, myPortInfo.myPort);
278 if (!isOpen() && myPortInfo.myPort != 0)
281 if (myPortInfo.myMaxPort > 0 && myPortInfo.myMaxPort > myPortInfo.myPort)
283 for (
int port = myPortInfo.myPort + 1;
284 port <= myPortInfo.myMaxPort && !isOpen(); port++)
292 if (!isOpen() && myPortInfo.myUseSystemPort)
300 template <
typename AcceptorT,
typename SocketT>
306 myAcceptor.async_accept(
307 hboost::asio::make_strand(myExecutor),
308 [
this,
self = shared_from_this()](
309 hboost::system::error_code ec,
socket_type socket) {
311 if (!myAcceptor.is_open())
316 if (socket.is_open())
318 onAccept_(ec, std::move(socket));
326 if (ec == hboost::asio::error::operation_aborted)
335 #endif // __NET_SOCKETLISTENER_H__
virtual void onAccept_(const hboost::system::error_code &ec, socket_type socket)=0
void start(const ASIO_TCPEndpoint &ep)
void setUseSystemPort(bool use_sys)
void start(int initial_port, int max_port_range=-1, bool use_system_port=false, const ASIO_IPAddress &address=ASIO_IPAddressV4())
NET_SocketListener(ASIO_IOContext &ctx)
void setBindAddress(const ASIO_IPAddress &bind_address)
static constexpr bool uses_port
hboost::asio::ip::address_v4 ASIO_IPAddressV4
void close(bool force) override
NET_ConnectionManager myConnectionManager
ASIO_TCPAcceptor acceptor_type
typename ASIO_TCPAcceptor::endpoint_type endpoint_type
ASIO_IOContext::executor_type myExecutor
std::enable_shared_from_this< T > UTenable_shared_from_this
typename SocketT::endpoint_type endpoint_type
hboost::asio::basic_stream_socket< typename ASIO_TCPAcceptor::protocol_type > socket_type
endpoint_type endpoint() const
#define ASIO_HANDLER_LOCATION
NET_ISocketListener(const ASIO_IOContext::executor_type &executor)
ASIO_IPAddress myBindAddress
NET_SocketListener(const ASIO_IOContext::executor_type &executor)
NET_ISocketListener(ASIO_IOContext &ctx)
void startAccept_()
Start an async accept. If the accept was a success onAccept_ is called.
hboost::asio::io_context ASIO_IOContext
hboost::asio::ip::tcp::endpoint ASIO_TCPEndpoint
bool isOpen() const override
void NETaddFatal(NET_LogManager &mgr, int code, const char *fmt, Args &&...args)
SIM_API const UT_StringHolder force
void setMaxPort(int port)
void fail(const hboost::system::error_code &ec, const char *what)
Called when something failed with the socket listener.
void setUseIPv6(bool use_v6)
std::conditional_t< socket_info< socket_type >::uses_port, PortInfo, Empty > myPortInfo
NET_ISocketGroup * mySocketGroup
hboost::asio::ip::address ASIO_IPAddress
const ASIO_IPAddress & bindAddress() const