pars 0.2.1.99
Loading...
Searching...
No Matches
context.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "nngxx/aio.h"
33#include "nngxx/ctx.h"
34
35#include "pars/ev/enqueuer.h"
37#include "pars/net/op.h"
38#include "pars/net/socket.h"
39
40#include <format>
41
42namespace pars::net
43{
44
46{
47public:
49 : router_m{r}
50 , ctx_m{std::move(c)}
51 , sock_m{s}
52 {
53 }
54
55 ~context() { stop(); }
56
57 operator tool_view() { return tool_view{ctx_m}; }
58
60 {
61 if (opts.recv_timeout)
62 ctx_m.set_recv_timeout(*opts.recv_timeout).or_abort();
63
64 if (opts.send_timeout)
65 ctx_m.set_send_timeout(*opts.send_timeout).or_abort();
66 }
67
69 {
70 return {.recv_timeout = ctx_m.get_recv_timeout().value_or_abort(),
71 .send_timeout = ctx_m.get_send_timeout().value_or_abort()};
72 }
73
74 void send_aio(nngxx::aio_view& a) { ctx_m.send(a); }
75
76 void recv_aio(nngxx::aio_view& a) { ctx_m.recv(a); }
77
78 template<ev::event_c event_t>
79 void send(event_t ev, pipe p)
80 {
81 op_m.send(router_m, *this, p, ev);
82 }
83
84 void recv() { op_m.recv(router_m, *this); }
85
86 void stop() { op_m.stop(); }
87
88 int id() const { return ctx_m.id(); }
89
90 int socket_id() const { return sock_m.id(); }
91
92 const net::socket& sock() const { return sock_m; }
93
94 auto format_to(std::format_context& ctx) const -> decltype(ctx.out())
95 {
96 return std::format_to(ctx.out(), "context #{}", id());
97 }
98
99private:
100 ev::enqueuer& router_m;
101 op op_m;
102 nngxx::ctx ctx_m;
103 const net::socket& sock_m;
104};
105
106} // namespace pars::net
context_opt options() const
Definition context.h:68
int id() const
Definition context.h:88
context(ev::enqueuer &r, nngxx::ctx &&c, const net::socket &s)
Definition context.h:48
void send_aio(nngxx::aio_view &a)
Definition context.h:74
void recv_aio(nngxx::aio_view &a)
Definition context.h:76
int socket_id() const
Definition context.h:90
void send(event_t ev, pipe p)
Definition context.h:79
auto format_to(std::format_context &ctx) const -> decltype(ctx.out())
Definition context.h:94
const net::socket & sock() const
Definition context.h:92
void set_options(context_opt opts)
Definition context.h:59
Represents an nng_socket.
Definition socket.h:71
Represents an nng_socket or nng_ctx view.
Definition tool_view.h:48
clev::own< nng_ctx > ctx
Definition ctx.h:39
clev::iface< nng_aio * > aio_view
Definition aio.h:37
std::optional< nng_duration > send_timeout
Definition context_opt.h:44
std::optional< nng_duration > recv_timeout
Definition context_opt.h:43