12 #ifndef __NET_SOCKETLISTENER_H__
13 #define __NET_SOCKETLISTENER_H__
30 #include <type_traits>
41 virtual bool isOpen()
const = 0;
45 : myIOContext(context), mySocketGroup(nullptr)
50 void fail(
const hboost::system::error_code& ec,
const char* what);
57 template <
typename Endpo
intT,
typename Enabled =
void>
62 template <
typename Endpo
intT>
68 template <
typename SocketT>
79 = hboost::asio::basic_stream_socket<typename AcceptorT::protocol_type>>
89 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
92 int max_port_range = -1,
93 bool use_system_port =
false,
104 typename S = SocketT,
105 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
117 myAcceptor.set_option(
typename AcceptorT::reuse_address(
true));
121 catch (std::exception& e)
138 hboost::asio::dispatch(
140 [
this,
self = shared_from_this(),
force]()
146 hboost::system::error_code ec;
149 fail(ec,
"acceptor close");
160 typename S = SocketT,
161 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
167 typename S = SocketT,
168 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
174 typename S = SocketT,
175 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
181 typename S = SocketT,
182 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
188 typename S = SocketT,
189 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
195 typename S = SocketT,
196 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
202 typename S = SocketT,
203 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
209 typename S = SocketT,
210 typename =
typename std::enable_if_t<socket_info<S>::uses_port>>
223 ,
myAcceptor(hboost::asio::make_strand(context))
235 const hboost::system::error_code& ec,
255 std::conditional_t<socket_info<socket_type>::uses_port,
PortInfo,
Empty>
259 template <
typename AcceptorT,
typename SocketT>
260 template <
typename S,
typename Detected>
266 UT_ASSERT(!
"Trying to start the same connection twice.");
269 endpoint_type ep(myPortInfo.myBindAddress, myPortInfo.myPort);
272 if (!isOpen() && myPortInfo.myPort != 0)
275 if (myPortInfo.myMaxPort > 0 && myPortInfo.myMaxPort > myPortInfo.myPort)
277 for (
int port = myPortInfo.myPort + 1;
278 port <= myPortInfo.myMaxPort && !isOpen(); port++)
286 if (!isOpen() && myPortInfo.myUseSystemPort)
294 template <
typename AcceptorT,
typename SocketT>
300 myAcceptor.async_accept(
301 hboost::asio::make_strand(myIOContext),
302 [
this,
self = shared_from_this()](
303 hboost::system::error_code ec,
socket_type socket) {
305 if (!myAcceptor.is_open())
310 if (socket.is_open())
312 onAccept_(ec, std::move(socket));
320 if (ec == hboost::asio::error::operation_aborted)
329 #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())
void setBindAddress(const ASIO_IPAddress &bind_address)
NET_ISocketListener(ASIO_IOContext &context)
ASIO_IOContext & myIOContext
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
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_SocketListener(ASIO_IOContext &context)
ASIO_IPAddress myBindAddress
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