Chops Net IP
Loading...
Searching...
No Matches
basic_io_output.hpp
Go to the documentation of this file.
1
17#ifndef BASIC_IO_OUTPUT_HPP_INCLUDED
18#define BASIC_IO_OUTPUT_HPP_INCLUDED
19
20#include <memory> // std::weak_ptr, std::shared_ptr
21#include <system_error>
22#include <cstddef> // std::size_t, std::byte
23#include <utility> // std::move
24
25#include "nonstd/expected.hpp"
26
27#include "buffer/shared_buffer.hpp"
29
31
32namespace chops {
33namespace net {
34
35
56template <typename IOT>
58
59private:
60 std::weak_ptr<IOT> m_ioh_wptr;
61
62public:
63 using endpoint_type = typename IOT::endpoint_type;
64
65public:
66
74 basic_io_output() = default;
75
80 explicit basic_io_output(std::weak_ptr<IOT> p) noexcept : m_ioh_wptr(p) { }
81
91 bool is_valid() const noexcept { return !m_ioh_wptr.expired(); }
92
102 nonstd::expected<output_queue_stats, std::error_code> {
103 return detail::wp_access<output_queue_stats>( m_ioh_wptr,
104 [] (std::shared_ptr<IOT> sp) { return sp->get_output_queue_stats(); } );
105 }
106
121 bool send(const void* buf, std::size_t sz) const { return send(chops::const_shared_buffer(buf, sz)); }
122
134 bool send(const chops::const_shared_buffer& buf) const {
135 auto sp = m_ioh_wptr.lock();
136 return sp ? sp->send(buf) : false;
137 }
138
161 bool send(chops::mutable_shared_buffer&& buf) const {
162 return send(chops::const_shared_buffer(std::move(buf)));
163 }
164
184 bool send(const void* buf, std::size_t sz, const endpoint_type& endp) const {
185 return send(chops::const_shared_buffer(buf, sz), endp);
186 }
187
202 bool send(const chops::const_shared_buffer& buf, const endpoint_type& endp) const {
203 auto sp = m_ioh_wptr.lock();
204 return sp ? sp->send(buf, endp) : false;
205 }
206
222 bool send(chops::mutable_shared_buffer&& buf, const endpoint_type& endp) const {
223 return send(chops::const_shared_buffer(std::move(buf)), endp);
224 }
225
237 bool operator==(const basic_io_output<IOT>& rhs) const noexcept {
238 return (m_ioh_wptr.lock() == rhs.m_ioh_wptr.lock());
239 }
240
250 bool operator<(const basic_io_output<IOT>& rhs) const noexcept {
251 return (m_ioh_wptr.lock() < rhs.m_ioh_wptr.lock());
252 }
253
254};
255
256} // end net namespace
257} // end chops namespace
258
259#endif
260
The basic_io_output class template provides methods for sending data to an associated network IO hand...
Definition basic_io_output.hpp:57
bool send(const void *buf, std::size_t sz, const endpoint_type &endp) const
Send a buffer to a specific destination endpoint (address and port), implemented only for UDP IO hand...
Definition basic_io_output.hpp:184
bool send(chops::mutable_shared_buffer &&buf) const
Move a reference counted buffer and send it through the associated network IO handler.
Definition basic_io_output.hpp:161
bool operator<(const basic_io_output< IOT > &rhs) const noexcept
Compare two basic_io_output objects for ordering purposes.
Definition basic_io_output.hpp:250
bool send(const void *buf, std::size_t sz) const
Send a buffer of data through the associated network IO handler.
Definition basic_io_output.hpp:121
bool operator==(const basic_io_output< IOT > &rhs) const noexcept
Compare two basic_io_output objects for equality.
Definition basic_io_output.hpp:237
bool is_valid() const noexcept
Query whether an IO handler is associated with this object.
Definition basic_io_output.hpp:91
auto get_output_queue_stats() const -> nonstd::expected< output_queue_stats, std::error_code >
Return output queue statistics, allowing application monitoring of output queue sizes.
Definition basic_io_output.hpp:101
basic_io_output()=default
Default construct a basic_io_output.
bool send(const chops::const_shared_buffer &buf) const
Send a reference counted buffer through the associated network IO handler.
Definition basic_io_output.hpp:134
bool send(const chops::const_shared_buffer &buf, const endpoint_type &endp) const
Send a reference counted buffer to a specific destination endpoint (address and port),...
Definition basic_io_output.hpp:202
basic_io_output(std::weak_ptr< IOT > p) noexcept
Construct a std::weak_ptr to an internal IO handler. This constructor is for internal use only and no...
Definition basic_io_output.hpp:80
bool send(chops::mutable_shared_buffer &&buf, const endpoint_type &endp) const
Move a reference counted buffer and send it through the associated network IO handler,...
Definition basic_io_output.hpp:222
Structures containing statistics gathered on internal queues.
output_queue_stats provides information on the internal output queue.
Definition queue_stats.hpp:29
Common code for accessing std::weak_ptr referenced objects, used in basic_io_interface and net_entity...