18#ifndef NET_IP_HPP_INCLUDED
19#define NET_IP_HPP_INCLUDED
31#include "asio/io_context.hpp"
32#include "asio/ip/tcp.hpp"
33#include "asio/ip/udp.hpp"
125 asio::io_context& m_ioc;
126 mutable std::mutex m_mutex;
128 std::vector<detail::tcp_acceptor_shared_ptr> m_acceptors;
129 std::vector<detail::tcp_connector_shared_ptr> m_connectors;
130 std::vector<detail::udp_entity_io_shared_ptr> m_udp_entities;
133 using lg = std::scoped_lock<std::mutex>;
143 m_ioc(ioc), m_acceptors(), m_connectors(), m_udp_entities() { }
176 std::string_view listen_intf =
"",
177 bool reuse_addr =
true) {
178 auto p = std::make_shared<detail::tcp_acceptor>(m_ioc, local_port_or_service,
179 listen_intf, reuse_addr);
181 m_acceptors.push_back(p);
202 bool reuse_addr =
true) {
203 auto p = std::make_shared<detail::tcp_acceptor>(m_ioc, endp, reuse_addr);
205 m_acceptors.push_back(p);
236 template <
typename F = simple_timeout>
238 std::string_view remote_host,
240 bool reconn_on_err =
false) {
242 auto p = std::make_shared<detail::tcp_connector>(m_ioc, remote_port_or_service, remote_host,
243 tcp_connector_timeout_func(timeout_func),
246 m_connectors.push_back(p);
277 template <
typename Iter,
typename F = simple_timeout>
280 bool reconn_on_err =
false) ->
281 std::enable_if_t<std::is_same_v<std::decay<
decltype(*beg)>, asio::ip::tcp::endpoint>,
net_entity> {
282 auto p = std::make_shared<detail::tcp_connector>(m_ioc, beg, end,
283 tcp_connector_timeout_func(timeout_func),
286 m_connectors.push_back(p);
306 template <
typename F = simple_timeout>
309 bool reconn_on_err =
false) {
310 std::vector<asio::ip::tcp::endpoint> vec { endp };
342 std::string_view local_intf =
"") {
343 auto p = std::make_shared<detail::udp_entity_io>(m_ioc, local_port_or_service, local_intf);
345 m_udp_entities.push_back(p);
363 auto p = std::make_shared<detail::udp_entity_io>(m_ioc, endp);
365 m_udp_entities.push_back(p);
396 [
this] (detail::tcp_acceptor_weak_ptr p) {
397 std::erase_if(m_acceptors, [p] (detail::tcp_acceptor_shared_ptr sp) {
return sp == p.lock(); } );
399 [
this] (detail::tcp_connector_weak_ptr p) {
400 std::erase_if(m_connectors, [p] (detail::tcp_connector_shared_ptr sp) {
return sp == p.lock(); } );
402 [
this] (detail::udp_entity_io_weak_ptr p) {
403 std::erase_if(m_udp_entities, [p] (detail::udp_entity_io_shared_ptr sp) {
return sp == p.lock(); } );
417 m_udp_entities.clear();
418 m_connectors.clear();
430 for (
auto i : m_udp_entities) { i->stop(); }
431 for (
auto i : m_connectors) { i->stop(); }
432 for (
auto i : m_acceptors) { i->stop(); }
The net_entity class provides the primary application interface into the TCP acceptor,...
Definition net_entity.hpp:82
Primary class for the Chops Net IP library and the initial API point for providing TCP acceptor,...
Definition net_ip.hpp:122
auto make_tcp_connector(Iter beg, Iter end, const F &timeout_func=simple_timeout { }, bool reconn_on_err=false) -> std::enable_if_t< std::is_same_v< std::decay< decltype(*beg)>, asio::ip::tcp::endpoint >, net_entity >
Create a TCP connector net_entity with the endpoints already created, passing in iterators to the end...
Definition net_ip.hpp:278
void stop_all()
Call stop on all acceptors, connectors, and UDP entities.
Definition net_ip.hpp:428
net_entity make_udp_sender()
Create a UDP unicast net_entity for sending only (no local bind is performed).
Definition net_ip.hpp:377
void remove_all()
Remove all acceptors, connectors, and UDP entities.
Definition net_ip.hpp:415
void remove(net_entity ent)
Remove a net_entity from the internal list of net_entity objects.
Definition net_ip.hpp:392
net_entity make_tcp_acceptor(const asio::ip::tcp::endpoint &endp, bool reuse_addr=true)
Create a TCP acceptor net_entity, using an already created endpoint.
Definition net_ip.hpp:201
net_entity make_tcp_connector(const asio::ip::tcp::endpoint &endp, const F &timeout_func=simple_timeout { }, bool reconn_on_err=false)
Create a TCP connector net_entity using a single remote endpoint.
Definition net_ip.hpp:307
net_ip(asio::io_context &ioc)
Construct a net_ip object without starting any network processing.
Definition net_ip.hpp:142
net_entity make_udp_unicast(std::string_view local_port_or_service, std::string_view local_intf="")
Create a UDP unicast net_entity that allows receiving as well as sending.
Definition net_ip.hpp:341
net_entity make_tcp_acceptor(std::string_view local_port_or_service, std::string_view listen_intf="", bool reuse_addr=true)
Create a TCP acceptor net_entity, which will listen on a port for incoming connections (once started)...
Definition net_ip.hpp:175
net_entity make_udp_unicast(const asio::ip::udp::endpoint &endp)
Create a UDP unicast net_entity for receiving and sending, using an already created endpoint.
Definition net_ip.hpp:362
net_entity make_tcp_connector(std::string_view remote_port_or_service, std::string_view remote_host, const F &timeout_func=simple_timeout { }, bool reconn_on_err=false)
Create a TCP connector net_entity, which will perform an active TCP connect to the specified host and...
Definition net_ip.hpp:237
net_entity class and related functionality.
Error codes, exception class, and error category within Chops net_ip library.
Definition net_entity.hpp:46
Definition tcp_connector_timeout.hpp:69
TCP acceptor, for internal use.
TCP connector class, for internal use.
Classes that implement a connect timeout function object interface for the tcp_connector detail class...
Internal class that combines a UDP entity and UDP io handler.