Chops Net IP
Loading...
Searching...
No Matches
send_to_all.hpp
Go to the documentation of this file.
1
20#ifndef SEND_TO_ALL_HPP_INCLUDED
21#define SEND_TO_ALL_HPP_INCLUDED
22
23#include <cstddef> // std::size_t
24#include <utility> // std::move
25
26#include <mutex>
27#include <vector>
28
31
33
34#include "buffer/shared_buffer.hpp"
35
36namespace chops {
37namespace net {
38
63template <typename IOT>
65private:
66 using lock_guard = std::scoped_lock<std::mutex>;
68 using io_outs = std::vector<io_out>;
70
71private:
72 mutable std::mutex m_mutex;
73 io_outs m_io_outs;
74
75public:
82 lock_guard gd { m_mutex };
83 m_io_outs.push_back(io);
84 }
85
92 lock_guard gd { m_mutex };
93 std::erase_if (m_io_outs, [io] (auto out) { return io == out; } );
94 }
95
103 void operator() (io_interface io, std::size_t, bool starting) {
104 auto ret = io.make_io_output();
105 if (!ret) {
106 return;
107 }
108
109 if (starting) {
110 add_io_output(*ret);
111 }
112 else {
113 remove_io_output(*ret);
114 }
115 }
116
123 void send(chops::const_shared_buffer buf) const {
124 lock_guard gd { m_mutex };
125 for (const auto& io : m_io_outs) {
126 io.send(buf);
127 }
128 }
129
138 void send(chops::const_shared_buffer buf, io_out cur_io) const { // TG
139 lock_guard gd { m_mutex };
140 for (const auto& io : m_io_outs) {
141 if ( !(cur_io == io) ) {
142 io.send(buf);
143 }
144 }
145 }
146
156 void send(const void* buf, std::size_t sz) const {
157 send(chops::const_shared_buffer(buf, sz));
158 }
159
170 void send(const void* buf, std::size_t sz, io_out cur_io) const { // TG
171 send(chops::const_shared_buffer(buf, sz), cur_io);
172 }
173
180 void send(chops::mutable_shared_buffer&& buf) const {
181 send(chops::const_shared_buffer(std::move(buf)));
182 }
183
192 void send(chops::mutable_shared_buffer&& buf, io_out cur_io) const {
193 send(chops::const_shared_buffer(std::move(buf)), cur_io);
194 }
195
199 std::size_t size() const noexcept {
200 lock_guard gd { m_mutex };
201 return m_io_outs.size();
202 }
203
209 auto get_total_output_queue_stats() const noexcept {
210 lock_guard gd { m_mutex };
211 return accumulate_output_queue_stats(m_io_outs.cbegin(), m_io_outs.cend());
212 }
213};
214
215} // end net namespace
216} // end chops namespace
217
218#endif
219
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
Manage a collection of basic_io_output objects and provide a way to send data to all....
Definition send_to_all.hpp:64
void operator()(io_interface io, std::size_t, bool starting)
Interface for io_state_change parameter of start method.
Definition send_to_all.hpp:103
void send(chops::mutable_shared_buffer &&buf) const
Move the buffer from a writable reference counted buffer to an immutable reference counted buffer,...
Definition send_to_all.hpp:180
void send(const void *buf, std::size_t sz, io_out cur_io) const
Copy the bytes, create a reference counted buffer, then send it to all basic_io_output objects except...
Definition send_to_all.hpp:170
void send(chops::const_shared_buffer buf, io_out cur_io) const
Send a reference counted buffer to all basic_io_output objects except cur_io.
Definition send_to_all.hpp:138
void remove_io_output(io_out io)
Remove a basic_io_output object from the collection.
Definition send_to_all.hpp:91
void send(chops::const_shared_buffer buf) const
Send a reference counted buffer to all basic_io_output objects.
Definition send_to_all.hpp:123
void send(const void *buf, std::size_t sz) const
Copy the bytes, create a reference counted buffer, then send it to all basic_io_output objects.
Definition send_to_all.hpp:156
std::size_t size() const noexcept
Return the number of basic_io_output objects in the collection.
Definition send_to_all.hpp:199
void send(chops::mutable_shared_buffer &&buf, io_out cur_io) const
Move the buffer from a writable reference counted buffer to a immutable reference counted buffer,...
Definition send_to_all.hpp:192
void add_io_output(io_out io)
Add a basic_io_output object to the collection.
Definition send_to_all.hpp:81
auto get_total_output_queue_stats() const noexcept
Return the sum total of output queue statistics.
Definition send_to_all.hpp:209
Functions that collect and deliver output_queue_stats from a sequence.
output_queue_stats accumulate_output_queue_stats(Iter beg, Iter end)
Accumulate output_queue_stats given a sequence of basic_io_output objects.
Definition output_queue_stats.hpp:48