pars 0.2.1
Loading...
Searching...
No Matches
pars::net::op Class Reference

#include <op.h>

Public Member Functions

 ~op ()
 
 operator bool ()
 
template<ev::event_c event_t, tool_c tool_t>
void send (ev::enqueuer &r, tool_t &t, pipe p, event_t ev)
 
template<tool_c tool_t>
void recv (ev::enqueuer &r, tool_t &t)
 
void sleep (nng_duration ms, std::function< void()> f)
 
void reset_sleep (nng_duration ms)
 
std::error_code result () const
 return result of asynchronous operation
 
void abort (nngxx::err err)
 abort asynchronous I/O operation
 
void cancel ()
 cancel asynchronous I/O operation
 
void wait () const
 wait for asynchronous I/O operation
 
void stop ()
 stop asynchronous I/O operation
 

Detailed Description

Definition at line 53 of file op.h.

Constructor & Destructor Documentation

◆ ~op()

pars::net::op::~op ( )
inline

Definition at line 56 of file op.h.

57 {
58 if (aio_m)
59 aio_m.wait();
60 }

Member Function Documentation

◆ abort()

void pars::net::op::abort ( nngxx::err err)
inline

abort asynchronous I/O operation

Parameters
err

The abort() function aborts an operation previously started with the handle aio.

If the operation is aborted, then the callback for the handle will be called, and the function result() will return the error err.

Definition at line 194 of file op.h.

194{ aio_m.abort(err); }
void err(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:211

References pars::err().

Here is the call graph for this function:

◆ cancel()

void pars::net::op::cancel ( )
inline

cancel asynchronous I/O operation

The nng_aio_cancel() function aborts an operation previously started with the handle aio. If the operation is aborted, then the callback for the handle will be called, and the function nng_aio_result() will return the error NNG_ECANCELED.

This function does not wait for the operation to be fully aborted, but returns immediately.

If no operation is currently in progress (either because it has already finished, or no operation has been started yet), then this function has no effect.

Same as abort(nngxx::error::canceled)

Definition at line 213 of file op.h.

213{ aio_m.cancel(); }

◆ operator bool()

pars::net::op::operator bool ( )
inlineexplicit

Definition at line 62 of file op.h.

62{ return static_cast<bool>(aio_m); }

◆ recv()

template<tool_c tool_t>
void pars::net::op::recv ( ev::enqueuer & r,
tool_t & t )
inline

Definition at line 107 of file op.h.

108 {
109 pars::debug(SL, lf::net, "{}: Receive Message!", f::pntl{{}, t});
110
111 // replace the operation with the new one
112 cb_m = [&](clev::expected<void> res, nngxx::msg m) {
113 if (res)
114 {
115 // NOTE: m is not empty on success
116
117 auto pv = m.get_pipe();
118
119 pars::debug(SL, lf::net, "{}: Received Message! [{}]", f::pntl{pv, t},
120 m);
121
122 r.queue_received(std::move(m), t.socket_id(), t, pv);
123 }
124 else
125 {
126 // NOTE: m is empty on failure
127
128 auto pv = nngxx::pipe_view();
129
130 pars::err(SL, lf::net, "{}: Error Receiving! [{}]", f::pntl{pv, t},
131 res.error());
132
133 r.queue_fire(ev::network_error{res.error(), dir::in}, t.socket_id(), t,
134 pv);
135 }
136 };
137
138 // make aio - NOTE: pass this, cant move op
139 aio_m = nngxx::make_aio(op::recv_cb, this).value_or_abort();
140
141 // start recv
142 t.recv_aio(aio_m);
143 }
#define SL
Definition log.h:58
clev::own< nng_msg * > msg
Definition msg.h:39
clev::iface< nng_pipe > pipe_view
Definition pipe.h:37
@ net
Definition flags.h:44
void debug(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:129

References pars::debug(), pars::err(), pars::net::in, pars::net, pars::ev::enqueuer::queue_fire(), pars::ev::enqueuer::queue_received(), and SL.

Here is the call graph for this function:

◆ reset_sleep()

void pars::net::op::reset_sleep ( nng_duration ms)
inline

Definition at line 161 of file op.h.

162 {
163 stop();
164
165 // make aio - NOTE: pass this, cant move op
166 aio_m = nngxx::make_aio(op::sleep_cb, this).value_or_abort();
167
168 nngxx::sleep(ms, aio_m);
169 }
void stop()
stop asynchronous I/O operation
Definition op.h:234

References stop().

Here is the call graph for this function:

◆ result()

std::error_code pars::net::op::result ( ) const
inline

return result of asynchronous operation

Returns

The nng_aio_result() returns the result of the operation associated with the handle aio. If the operation was successful, then 0 is returned. Otherwise a non-zero error code is returned.

Definition at line 179 of file op.h.

180 {
181 return aio_m.result().error_or(nngxx::err::success);
182 }
@ success
Definition err.h:75

References nngxx::success.

◆ send()

template<ev::event_c event_t, tool_c tool_t>
void pars::net::op::send ( ev::enqueuer & r,
tool_t & t,
pipe p,
event_t ev )
inline

Definition at line 65 of file op.h.

66 {
67 auto m = ev::serialize::to_network(ev);
68
69 if (p)
70 m.set_pipe(p);
71
72 pars::debug(SL, lf::net, "{}: Send Message [{}]!", f::pntl{p, t}, m);
73
74 // replace the callback with the new one
75 cb_m = [&, p](clev::expected<void> res, nngxx::msg m) mutable {
76 if (res)
77 {
78 // NOTE: m is empty on success
79
80 pars::debug(SL, lf::net, "{}: Sent Event [{}]!", f::pntl{p, t}, ev);
81
82 r.queue_sent(std::move(ev), t.socket_id(), t, p);
83 }
84 else
85 {
86 // NOTE: m is not empty on failure
87
88 auto pv = m.get_pipe();
89
90 pars::err(SL, lf::net, "{}: Error Sending {}! [msg:{},err:{}]",
91 f::pntl{pv, t}, nametype(ev), m, res.error());
92
93 r.queue_fire(ev::network_error{res.error(), dir::out}, t.socket_id(), t,
94 pv);
95 }
96 };
97
98 // make aio - NOTE: pass this, cant move op
99 aio_m = nngxx::make_aio(op::send_cb, this).value_or_abort();
100
101 // start send
102 aio_m.set_msg(std::move(m));
103 t.send_aio(aio_m);
104 }
std::string nametype()
Definition nametype.h:37
static nngxx::msg to_network(event_t &ev)
Definition serializer.h:53

References pars::debug(), pars::err(), nametype(), pars::net, pars::net::out, pars::ev::enqueuer::queue_fire(), pars::ev::enqueuer::queue_sent(), SL, and pars::ev::serialize::to_network().

Here is the call graph for this function:

◆ sleep()

void pars::net::op::sleep ( nng_duration ms,
std::function< void()> f )
inline

Definition at line 145 of file op.h.

146 {
147 cb_m = [&, f](clev::expected<void> res, nngxx::msg m) {
148 if (res)
149 {
150 // the sleep completed successfully, execute f
151 f();
152 }
153 };
154
155 // make aio - NOTE: pass this, cant move op
156 aio_m = nngxx::make_aio(op::sleep_cb, this).value_or_abort();
157
158 nngxx::sleep(ms, aio_m);
159 }

◆ stop()

void pars::net::op::stop ( )
inline

stop asynchronous I/O operation

The stop() function stops the asynchronous I/O operation associated with aio by aborting with NNG_ECANCELED, and then waits for it to complete or to be completely aborted, and for the callback associated with the aio to have completed executing.

Same as cancel() + wait().

Definition at line 234 of file op.h.

234{ aio_m.stop(); }

Referenced by reset_sleep().

Here is the caller graph for this function:

◆ wait()

void pars::net::op::wait ( ) const
inline

wait for asynchronous I/O operation

The nng_aio_wait() function waits for an asynchronous I/O operation to complete. If the operation has not been started, or has already completed, then it returns immediately.

Definition at line 222 of file op.h.

222{ aio_m.wait(); }

The documentation for this class was generated from the following file: