ReactivePlusPlus
ReactiveX implementation for C++20
Toggle main menu visibility
Loading...
Searching...
No Matches
client_reactor.hpp
1
// ReactivePlusPlus library
2
//
3
// Copyright Aleksey Loginov 2023 - present.
4
// Distributed under the Boost Software License, Version 1.0.
5
// (See accompanying file LICENSE_1_0.txt or copy at
6
// https://www.boost.org/LICENSE_1_0.txt)
7
//
8
// Project home: https://github.com/AlexInLog/ReactivePlusPlus
9
//
10
11
#pragma once
12
13
#include <rpp/observables/fwd.hpp>
14
15
#include <grpcpp/support/client_callback.h>
16
#include <rppgrpc/details/base.hpp>
17
#include <rppgrpc/fwd.hpp>
18
#include <rppgrpc/utils/exceptions.hpp>
19
20
namespace
rppgrpc
21
{
37
template
<rpp::constra
int
::decayed_type Request, rpp::constra
int
::decayed_type Response>
38
class
client_bidi_reactor final :
public
grpc::ClientBidiReactor<Request, Response>
39
,
private
details::base_writer
<Request>
40
,
private
details::base_reader
<Response>
41
{
42
using
Base = grpc::ClientBidiReactor<Request, Response>;
43
44
public
:
45
client_bidi_reactor() =
default
;
46
47
void
init()
48
{
49
// The first read is posted before StartCall(): gRPC keeps it until the call starts and
50
// counts it as an outstanding operation. StartCall() is the last use of the reactor in
51
// init(), because a call that ends at once may run OnDone (and so `delete this`) on a
52
// gRPC thread before StartCall() has even returned.
53
details::base_reader<Response>::handle_read_done(
true
);
54
Base::StartCall();
55
}
56
57
using
details::base_writer
<Request>::get_observer;
58
using
details::base_reader
<Response>::get_observable;
59
60
private
:
61
void
start_read(Response& data)
override
62
{
63
Base::StartRead(&data);
64
}
65
66
void
start_write(
const
Request& v)
override
67
{
68
Base::StartWrite(&v);
69
}
70
71
void
finish_writes(
const
grpc::Status&)
override
72
{
73
Base::StartWritesDone();
74
}
75
76
using
Base::StartCall;
77
using
Base::StartRead;
78
79
void
OnReadDone(
bool
ok)
override
80
{
81
if
(!ok)
82
return
;
83
84
details::base_reader<Response>::handle_read_done();
85
}
86
87
void
OnWriteDone(
bool
ok)
override
88
{
89
if
(!ok)
90
return
;
91
92
details::base_writer<Request>::handle_write_done();
93
}
94
95
void
OnDone(
const
grpc::Status& s)
override
96
{
97
details::base_writer<Request>::handle_on_done();
98
details::base_reader<Response>::handle_on_done(s.ok() ? std::exception_ptr{} : std::make_exception_ptr(
rppgrpc::utils::reactor_failed
{s.error_message()}));
99
delete
this
;
100
}
101
};
102
118
template
<rpp::constra
int
::decayed_type Request>
119
class
client_write_reactor final :
public
grpc::ClientWriteReactor<Request>
120
,
private
details::base_writer
<Request>
121
,
private
details::base_reader
<rpp::utils::none>
122
{
123
using
Base = grpc::ClientWriteReactor<Request>;
124
125
public
:
126
client_write_reactor() =
default
;
127
128
void
init()
129
{
130
Base::StartCall();
131
}
132
133
using
details::base_writer
<Request>::get_observer;
134
using
details::base_reader
<
rpp::utils::none
>::get_observable;
135
136
private
:
137
void
start_read(
rpp::utils::none
& data)
override
138
{
139
}
140
141
void
start_write(
const
Request& v)
override
142
{
143
Base::StartWrite(&v);
144
}
145
146
void
finish_writes(
const
grpc::Status&)
override
147
{
148
Base::StartWritesDone();
149
}
150
151
using
Base::StartCall;
152
153
void
OnWriteDone(
bool
ok)
override
154
{
155
if
(!ok)
156
return
;
157
158
details::base_writer<Request>::handle_write_done();
159
}
160
161
void
OnDone(
const
grpc::Status& s)
override
162
{
163
details::base_writer<Request>::handle_on_done();
164
details::base_reader<rpp::utils::none>::handle_on_done(s.ok() ? std::exception_ptr{} : std::make_exception_ptr(
rppgrpc::utils::reactor_failed
{s.error_message()}));
165
166
delete
this
;
167
}
168
};
169
182
template
<rpp::constra
int
::decayed_type Response>
183
class
client_read_reactor final :
public
grpc::ClientReadReactor<Response>
184
,
private
details::base_reader
<Response>
185
{
186
using
Base = grpc::ClientReadReactor<Response>;
187
188
public
:
189
client_read_reactor() =
default
;
190
191
void
init()
192
{
193
// The first read is posted before StartCall(): gRPC keeps it until the call starts and
194
// counts it as an outstanding operation. StartCall() is the last use of the reactor in
195
// init(), because a call that ends at once may run OnDone (and so `delete this`) on a
196
// gRPC thread before StartCall() has even returned.
197
details::base_reader<Response>::handle_read_done(
true
);
198
Base::StartCall();
199
}
200
201
using
details::base_reader
<Response>::get_observable;
202
203
private
:
204
using
Base::StartCall;
205
using
Base::StartRead;
206
207
void
start_read(Response& data)
override
208
{
209
Base::StartRead(&data);
210
}
211
212
void
OnReadDone(
bool
ok)
override
213
{
214
if
(!ok)
215
return
;
216
217
details::base_reader<Response>::handle_read_done();
218
}
219
220
void
OnDone(
const
grpc::Status& s)
override
221
{
222
details::base_reader<Response>::handle_on_done(s.ok() ? std::exception_ptr{} : std::make_exception_ptr(
rppgrpc::utils::reactor_failed
{s.error_message()}));
223
delete
this
;
224
}
225
};
226
}
// namespace rppgrpc
rppgrpc::details::base_reader
Definition
base.hpp:112
rppgrpc::details::base_writer
Definition
base.hpp:28
rpp::utils::none
Definition
utils.hpp:25
rppgrpc::utils::reactor_failed
Definition
exceptions.hpp:18
src
extensions
rppgrpc
rppgrpc
client_reactor.hpp
Generated by
1.18.0