Chops Net IP
Loading...
Searching...
No Matches
io_output_delivery.hpp
Go to the documentation of this file.
1
41#ifndef IO_OUTPUT_DELIVERY_HPP_INCLUDED
42#define IO_OUTPUT_DELIVERY_HPP_INCLUDED
43
44#include <cstddef> // std::size_t
45#include <utility> // std::move, std::pair, std::forward
46#include <system_error>
47#include <memory>
48#include <exception>
49#include <future>
50
51#include "net_ip/net_entity.hpp"
55
56#include "queue/wait_queue.hpp"
57
58namespace chops {
59namespace net {
60
64template <typename IOT>
67 std::size_t num_handlers;
68 bool starting;
69
70 io_state_chg_data(basic_io_output<IOT> io, std::size_t num, bool s) :
71 io_out(std::move(io)), num_handlers(num), starting(s) { }
72};
73
77template <typename IOT>
78using io_wait_q = chops::wait_queue<io_state_chg_data<IOT> >;
79
88
105template <typename IOT, typename IOS, typename EF>
107 IOS&& io_start,
108 io_wait_q<IOT>& wq,
109 EF&& err_func) {
110 return ne.start( [io_start = std::move(io_start), &wq]
111 (basic_io_interface<IOT> io, std::size_t num, bool starting) {
112 if (starting) {
113 io_start(io, num, starting);
114 }
115 wq.emplace_push(*(io.make_io_output()), num, starting);
116 },
117 std::forward<EF>(err_func)
118 );
119}
120
124template <typename IOT>
125using io_output_future = std::future<basic_io_output<IOT> >;
126
135
136
144template <typename IOT>
146 io_output_future<IOT> start_fut;
147 io_output_future<IOT> stop_fut;
148};
149
158
159
182template <typename IOT, typename IOS, typename EF>
184 IOS&& io_start,
185 EF&& err_func) {
186 // io state change function object must be copyable since it will
187 // be stored in a std::function, therefore wrap promise in shared ptr
188 auto start_prom_ptr = std::make_shared<std::promise<basic_io_output<IOT> > >();
189 auto start_fut = start_prom_ptr->get_future();
190
191 auto lam = [io_start = std::move(io_start), start_prom_ptr]
192 (basic_io_interface<IOT> io, std::size_t num, bool starting) {
193 if (starting) {
194 io_start(io, num, starting);
195 start_prom_ptr->set_value(*(io.make_io_output()));
196 }
197 };
198 auto e = ent.start(lam, std::forward<EF>(err_func));
199 if (!e) { // error return from net_entity start method
200 start_prom_ptr->set_exception(std::make_exception_ptr(std::system_error(e.error())));
201 }
202 return start_fut;
203}
204
232template <typename IOT, typename IOS, typename EF>
234 IOS&& io_start,
235 EF&& err_func) {
236
237 auto start_prom_ptr = std::make_shared<std::promise<basic_io_output<IOT> > >();
238 auto start_fut = start_prom_ptr->get_future();
239 auto stop_prom_ptr = std::make_shared<std::promise<basic_io_output<IOT> > >();
240 auto stop_fut = stop_prom_ptr->get_future();
241
242 auto lam = [io_start = std::move(io_start), start_prom_ptr, stop_prom_ptr]
243 (basic_io_interface<IOT> io, std::size_t num, bool starting) {
244 if (starting) {
245 io_start(io, num, starting);
246 start_prom_ptr->set_value(*(io.make_io_output()));
247 }
248 else {
249 stop_prom_ptr->set_value(*(io.make_io_output()));
250 }
251 };
252
253 auto e = ent.start(lam, std::forward<EF>(err_func));
254 if (!e) { // error return from net_entity start method
255 auto ep = std::make_exception_ptr(std::system_error(e.error()));
256 start_prom_ptr->set_exception(ep);
257 stop_prom_ptr->set_exception(ep);
258 }
259 return io_output_future_pair<IOT> { std::move(start_fut), std::move(stop_fut) };
260}
261
262} // end net namespace
263} // end chops namespace
264
265#endif
266
basic_io_interface class template, providing start_io, stop_io, visit_socket, make_io_output and rela...
basic_io_output class template, providing send and get_output_queue_stats methods.
The basic_io_interface class template provides access to an underlying network IO handler (TCP or UDP...
Definition basic_io_interface.hpp:100
auto make_io_output() const -> nonstd::expected< basic_io_output< IOT >, std::error_code >
Make a basic_io_output object from the associated IO handler.
Definition basic_io_interface.hpp:153
The basic_io_output class template provides methods for sending data to an associated network IO hand...
Definition basic_io_output.hpp:57
The net_entity class provides the primary application interface into the TCP acceptor,...
Definition net_entity.hpp:82
auto start(F1 &&io_state_chg_func, F2 &&err_func) -> nonstd::expected< void, std::error_code >
Start network processing on the associated net entity with the application providing IO state change ...
Definition net_entity.hpp:344
io_output_future< tcp_io > tcp_io_output_future
io_output_future for TCP IO handlers.
Definition io_output_delivery.hpp:130
io_output_future< IOT > make_io_output_future(net_entity &ent, IOS &&io_start, EF &&err_func)
Return a std::future object containing a basic_io_output, which will become available after start is ...
Definition io_output_delivery.hpp:183
io_output_future< udp_io > udp_io_output_future
io_output_future for UDP IO handlers.
Definition io_output_delivery.hpp:134
std::future< basic_io_output< IOT > > io_output_future
An alias for a std::future containing a basic_io_output.
Definition io_output_delivery.hpp:125
io_wait_q< chops::net::tcp_io > tcp_io_wait_q
io_wait_q for tcp_io_interface objects.
Definition io_output_delivery.hpp:83
io_wait_q< chops::net::udp_io > udp_io_wait_q
io_wait_q for udp_io_interface objects.
Definition io_output_delivery.hpp:87
chops::wait_queue< io_state_chg_data< IOT > > io_wait_q
wait_queue declaration that provides IO state change data.
Definition io_output_delivery.hpp:78
io_output_future_pair< IOT > make_io_output_future_pair(net_entity &ent, IOS &&io_start, EF &&err_func)
Return a pair of std::future objects each containing a basic_io_output, which will become available a...
Definition io_output_delivery.hpp:233
auto start_with_io_wait_queue(net_entity ne, IOS &&io_start, io_wait_q< IOT > &wq, EF &&err_func)
Start the entity with an IO state change function object that calls start_io and also passes basic_io...
Definition io_output_delivery.hpp:106
IO type declarations, relating to the basic_io_interface and basic_io_output class templates.
net_entity class and related functionality.
A struct containing two std::future objects that deliver basic_io_output objects corresponding to the...
Definition io_output_delivery.hpp:145
Data provided through an IO state change.
Definition io_output_delivery.hpp:65