Chops Net IP
Loading...
Searching...
No Matches
basic_io_interface.hpp
Go to the documentation of this file.
1
18#ifndef BASIC_IO_INTERFACE_HPP_INCLUDED
19#define BASIC_IO_INTERFACE_HPP_INCLUDED
20
21#include <memory> // std::weak_ptr, std::shared_ptr
22#include <string_view>
23#include <system_error>
24#include <cstddef> // std::size_t, std::byte
25#include <utility> // std::forward, std::move
26
27#include "nonstd/expected.hpp"
28
31
33
35
36namespace chops {
37namespace net {
38
99template <typename IOT>
101private:
102 std::weak_ptr<IOT> m_ioh_wptr;
103
104public:
105 using endpoint_type = typename IOT::endpoint_type;
106
107public:
108
117
118 basic_io_interface(const basic_io_interface&) = default;
120
121 basic_io_interface<IOT>& operator=(const basic_io_interface&) = default;
122 basic_io_interface<IOT>& operator=(basic_io_interface&&) = default;
123
129 explicit basic_io_interface(std::weak_ptr<IOT> p) noexcept : m_ioh_wptr(p) { }
130
139 bool is_valid() const noexcept { return !m_ioh_wptr.expired(); }
140
153 auto make_io_output() const ->
154 nonstd::expected<basic_io_output<IOT>, std::error_code> {
155 return detail::wp_access<basic_io_output<IOT>>( m_ioh_wptr,
156 [] (std::shared_ptr<IOT> sp) { return basic_io_output<IOT>(sp); } );
157 }
158
168 auto is_io_started() const ->
169 nonstd::expected<bool, std::error_code> {
170 return detail::wp_access<bool>( m_ioh_wptr,
171 [] (std::shared_ptr<IOT> sp) { return sp->is_io_started(); } );
172 }
173
191 template <typename F>
192 auto visit_socket(F&& func) ->
193 nonstd::expected<void, std::error_code> {
194 return detail::wp_access_void( m_ioh_wptr, [&func] (std::shared_ptr<IOT> sp) {
195 sp->visit_socket(func); return std::error_code { }; } );
196 }
197
264 template <typename MH, typename MF>
265 auto start_io(std::size_t header_size, MH&& msg_handler, MF&& msg_frame) ->
266 nonstd::expected<void, std::error_code> {
267 return detail::wp_access_void( m_ioh_wptr,
268 [header_size, &msg_handler, &msg_frame] (std::shared_ptr<IOT> sp) {
269 return sp->start_io(header_size, msg_handler, msg_frame) ? std::error_code() :
270 std::make_error_code(net_ip_errc::io_already_started);
271 } );
272 }
273
316 template <typename MH>
317 auto start_io(std::size_t header_size, MH&& msg_handler, hdr_decoder_func func) ->
318 nonstd::expected<void, std::error_code> {
319 return detail::wp_access_void( m_ioh_wptr,
320 [header_size, &msg_handler, func] (std::shared_ptr<IOT> sp) {
321 return sp->start_io(header_size, msg_handler, func) ? std::error_code() :
322 std::make_error_code(net_ip_errc::io_already_started);
323 } );
324 }
325
363 template <typename MH>
364 auto start_io(std::string_view delimiter, MH&& msg_handler) ->
365 nonstd::expected<void, std::error_code> {
366 return detail::wp_access_void( m_ioh_wptr,
367 [delimiter, &msg_handler] (std::shared_ptr<IOT> sp) {
368 return sp->start_io(delimiter, msg_handler) ? std::error_code() :
369 std::make_error_code(net_ip_errc::io_already_started);
370 } );
371 }
372
413 template <typename MH>
414 auto start_io(std::size_t read_size, MH&& msg_handler) ->
415 nonstd::expected<void, std::error_code> {
416 return detail::wp_access_void( m_ioh_wptr,
417 [read_size, &msg_handler] (std::shared_ptr<IOT> sp) {
418 return sp->start_io(read_size, msg_handler) ? std::error_code() :
419 std::make_error_code(net_ip_errc::io_already_started);
420 } );
421 }
422
459 template <typename MH>
460 auto start_io(const endpoint_type& endp, std::size_t max_size, MH&& msg_handler) ->
461 nonstd::expected<void, std::error_code> {
462 return detail::wp_access_void( m_ioh_wptr,
463 [endp, max_size, &msg_handler] (std::shared_ptr<IOT> sp) {
464 return sp->start_io(endp, max_size, msg_handler) ? std::error_code() :
465 std::make_error_code(net_ip_errc::io_already_started);
466 } );
467 }
468
485 auto start_io() ->
486 nonstd::expected<void, std::error_code> {
487 return detail::wp_access_void( m_ioh_wptr,
488 [] (std::shared_ptr<IOT> sp) {
489 return sp->start_io() ? std::error_code() :
490 std::make_error_code(net_ip_errc::io_already_started);
491 } );
492 }
493
510 auto start_io(const endpoint_type& endp) ->
511 nonstd::expected<void, std::error_code> {
512 return detail::wp_access_void( m_ioh_wptr,
513 [endp] (std::shared_ptr<IOT> sp) {
514 return sp->start_io(endp) ? std::error_code() :
515 std::make_error_code(net_ip_errc::io_already_started);
516 } );
517 }
518
534 auto stop_io() ->
535 nonstd::expected<void, std::error_code> {
536 return detail::wp_access_void( m_ioh_wptr,
537 [] (std::shared_ptr<IOT> sp) {
538 return sp->stop_io() ? std::error_code() :
539 std::make_error_code(net_ip_errc::io_already_stopped);
540 } );
541 }
542
554 bool operator==(const basic_io_interface<IOT>& rhs) const noexcept {
555 return (m_ioh_wptr.lock() == rhs.m_ioh_wptr.lock());
556 }
557
567 bool operator<(const basic_io_interface<IOT>& rhs) const noexcept {
568 return (m_ioh_wptr.lock() < rhs.m_ioh_wptr.lock());
569 }
570
581 const void* get_ptr() const noexcept {
582 return static_cast<const void*>(m_ioh_wptr.lock().get());
583 }
584
585};
586
587} // end net namespace
588} // end chops namespace
589
590#endif
591
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
std::size_t(*)(const std::byte *ptr, std::size_t sz) hdr_decoder_func
Signature for a variable length message header decoder function, used in one of the basic_io_interfac...
Definition simple_variable_len_msg_frame.hpp:51
auto visit_socket(F &&func) -> nonstd::expected< void, std::error_code >
Provide an application supplied function object which will be called with a reference to the associat...
Definition basic_io_interface.hpp:192
auto start_io(const endpoint_type &endp, std::size_t max_size, MH &&msg_handler) -> nonstd::expected< void, std::error_code >
Enable IO processing for the associated network IO handler with a maximum buffer size and a default d...
Definition basic_io_interface.hpp:460
auto start_io(std::size_t header_size, MH &&msg_handler, MF &&msg_frame) -> nonstd::expected< void, std::error_code >
Enable IO processing for the associated network IO handler with message frame logic.
Definition basic_io_interface.hpp:265
auto start_io(std::size_t read_size, MH &&msg_handler) -> nonstd::expected< void, std::error_code >
Enable IO processing for the associated network IO handler with fixed or maximum buffer size logic.
Definition basic_io_interface.hpp:414
const void * get_ptr() const noexcept
Return a raw pointer to an associated IO handler.
Definition basic_io_interface.hpp:581
auto start_io(std::size_t header_size, MH &&msg_handler, hdr_decoder_func func) -> nonstd::expected< void, std::error_code >
Enable IO processing for the associated network IO handler with simple variable length message frame ...
Definition basic_io_interface.hpp:317
auto start_io(std::string_view delimiter, MH &&msg_handler) -> nonstd::expected< void, std::error_code >
Enable IO processing for the associated network IO handler with delimeter logic.
Definition basic_io_interface.hpp:364
basic_io_interface()=default
Default construct a basic_io_interface.
auto stop_io() -> nonstd::expected< void, std::error_code >
Stop IO processing and close the associated network IO handler.
Definition basic_io_interface.hpp:534
bool is_valid() const noexcept
Query whether an IO handler is associated with this object.
Definition basic_io_interface.hpp:139
basic_io_interface(std::weak_ptr< IOT > p) noexcept
Construct with a shared weak pointer to an internal IO handler, this is an internal constructor only ...
Definition basic_io_interface.hpp:129
bool operator==(const basic_io_interface< IOT > &rhs) const noexcept
Compare two basic_io_interface objects for equality.
Definition basic_io_interface.hpp:554
auto start_io() -> nonstd::expected< void, std::error_code >
Enable IO processing for the associated network IO handler with no incoming message handling.
Definition basic_io_interface.hpp:485
auto is_io_started() const -> nonstd::expected< bool, std::error_code >
Query whether an IO handler is in a started state or not.
Definition basic_io_interface.hpp:168
auto start_io(const endpoint_type &endp) -> nonstd::expected< void, std::error_code >
Enable IO processing for the associated network IO handler with no incoming message handling,...
Definition basic_io_interface.hpp:510
bool operator<(const basic_io_interface< IOT > &rhs) const noexcept
Compare two basic_io_interface objects for ordering purposes.
Definition basic_io_interface.hpp:567
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
Error codes, exception class, and error category within Chops net_ip library.
Function object class and declaration for simple variable length TCP message framing.
Common code for accessing std::weak_ptr referenced objects, used in basic_io_interface and net_entity...