Chops Net IP
Loading...
Searching...
No Matches
net_ip.hpp
Go to the documentation of this file.
1
18#ifndef NET_IP_HPP_INCLUDED
19#define NET_IP_HPP_INCLUDED
20
21#include <memory> // std::shared_ptr
22#include <cstddef> // std::size_t
23#include <string_view>
24#include <vector>
25#include <chrono>
26#include <variant> // std::visit
27#include <type_traits> // std::enable_if
28
29#include <mutex> // std::scoped_lock, std::mutex
30
31#include "asio/io_context.hpp"
32#include "asio/ip/tcp.hpp"
33#include "asio/ip/udp.hpp"
34
36#include "net_ip/net_entity.hpp"
37
41
43
44namespace chops {
45namespace net {
46
122class net_ip {
123private:
124
125 asio::io_context& m_ioc;
126 mutable std::mutex m_mutex;
127
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;
131
132private:
133 using lg = std::scoped_lock<std::mutex>;
134
135public:
136
142 explicit net_ip(asio::io_context& ioc) :
143 m_ioc(ioc), m_acceptors(), m_connectors(), m_udp_entities() { }
144
145private:
146
147 net_ip() = delete;
148 net_ip(const net_ip&) = delete;
149 net_ip(net_ip&&) = delete;
150 net_ip& operator=(const net_ip&) = delete;
151 net_ip& operator=(net_ip&&) = delete;
152
153public:
154
175 net_entity make_tcp_acceptor (std::string_view local_port_or_service,
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);
180 lg g(m_mutex);
181 m_acceptors.push_back(p);
182 return net_entity(p);
183 }
184
201 net_entity make_tcp_acceptor (const asio::ip::tcp::endpoint& endp,
202 bool reuse_addr = true) {
203 auto p = std::make_shared<detail::tcp_acceptor>(m_ioc, endp, reuse_addr);
204 lg g(m_mutex);
205 m_acceptors.push_back(p);
206 return net_entity(p);
207 }
208
236 template <typename F = simple_timeout>
237 net_entity make_tcp_connector (std::string_view remote_port_or_service,
238 std::string_view remote_host,
239 const F& timeout_func = simple_timeout { },
240 bool reconn_on_err = false) {
241
242 auto p = std::make_shared<detail::tcp_connector>(m_ioc, remote_port_or_service, remote_host,
243 tcp_connector_timeout_func(timeout_func),
244 reconn_on_err);
245 lg g(m_mutex);
246 m_connectors.push_back(p);
247 return net_entity(p);
248 }
249
277 template <typename Iter, typename F = simple_timeout>
278 auto make_tcp_connector (Iter beg, Iter end,
279 const F& timeout_func = 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),
284 reconn_on_err);
285 lg g(m_mutex);
286 m_connectors.push_back(p);
287 return net_entity(p);
288 }
289
306 template <typename F = simple_timeout>
307 net_entity make_tcp_connector (const asio::ip::tcp::endpoint& endp,
308 const F& timeout_func = simple_timeout { },
309 bool reconn_on_err = false) {
310 std::vector<asio::ip::tcp::endpoint> vec { endp };
311 return make_tcp_connector(vec.cbegin(), vec.cend(), timeout_func, reconn_on_err);
312 }
313
341 net_entity make_udp_unicast (std::string_view local_port_or_service,
342 std::string_view local_intf = "") {
343 auto p = std::make_shared<detail::udp_entity_io>(m_ioc, local_port_or_service, local_intf);
344 lg g(m_mutex);
345 m_udp_entities.push_back(p);
346 return net_entity(p);
347 }
348
362 net_entity make_udp_unicast (const asio::ip::udp::endpoint& endp) {
363 auto p = std::make_shared<detail::udp_entity_io>(m_ioc, endp);
364 lg g(m_mutex);
365 m_udp_entities.push_back(p);
366 return net_entity(p);
367 }
368
378 return make_udp_unicast(asio::ip::udp::endpoint());
379 }
380
381// TODO: multicast make methods
382
392 void remove(net_entity ent) {
393 lg g(m_mutex);
394// overloaded utility brought in from net_entity.hpp
395 std::visit (detail::overloaded {
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(); } );
398 },
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(); } );
401 },
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(); } );
404 }
405 }, ent.m_wptr);
406 }
407
415 void remove_all() {
416 lg g(m_mutex);
417 m_udp_entities.clear();
418 m_connectors.clear();
419 m_acceptors.clear();
420 }
421
428 void stop_all() {
429 lg g(m_mutex);
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(); }
433 }
434
435};
436
437} // end net namespace
438} // end chops namespace
439
440#endif
441
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.