1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::{LocalAddr, RpcMessage};
use core::fmt;
use futures::{Future, FutureExt, Sink, SinkExt, StreamExt};
use pin_project::pin_project;
use std::{error, fmt::Display, pin::Pin, result, task::Poll};
#[derive(Debug)]
pub enum RecvError {}
impl fmt::Display for RecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl error::Error for RecvError {}
pub struct RecvStream<Res: RpcMessage>(flume::r#async::RecvStream<'static, Res>);
impl<In: RpcMessage> Clone for RecvStream<In> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<Res: RpcMessage> futures::Stream for RecvStream<Res> {
type Item = Result<Res, RecvError>;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
match self.0.poll_next_unpin(cx) {
Poll::Ready(Some(item)) => Poll::Ready(Some(Ok(item))),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}
}
type Socket<In, Out> = (self::SendSink<Out>, self::RecvStream<In>);
pub struct ServerChannel<In: RpcMessage, Out: RpcMessage> {
stream: flume::Receiver<Socket<In, Out>>,
}
impl<In: RpcMessage, Out: RpcMessage> Clone for ServerChannel<In, Out> {
fn clone(&self) -> Self {
Self {
stream: self.stream.clone(),
}
}
}
impl<In: RpcMessage, Out: RpcMessage> fmt::Debug for ServerChannel<In, Out> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ServerChannel")
.field("stream", &self.stream)
.finish()
}
}
pub struct ClientChannel<In: RpcMessage, Out: RpcMessage> {
sink: flume::Sender<Socket<Out, In>>,
}
impl<In: RpcMessage, Out: RpcMessage> Clone for ClientChannel<In, Out> {
fn clone(&self) -> Self {
Self {
sink: self.sink.clone(),
}
}
}
impl<In: RpcMessage, Out: RpcMessage> fmt::Debug for ClientChannel<In, Out> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClientChannel")
.field("sink", &self.sink)
.finish()
}
}
#[derive(Debug)]
pub enum AcceptBiError {
RemoteDropped,
}
impl fmt::Display for AcceptBiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl error::Error for AcceptBiError {}
#[pin_project]
pub struct OpenBiFuture<'a, In: RpcMessage, Out: RpcMessage> {
#[pin]
inner: flume::r#async::SendFut<'a, Socket<Out, In>>,
res: Option<Socket<In, Out>>,
}
impl<'a, In: RpcMessage, Out: RpcMessage> OpenBiFuture<'a, In, Out> {
fn new(inner: flume::r#async::SendFut<'a, Socket<Out, In>>, res: Socket<In, Out>) -> Self {
Self {
inner,
res: Some(res),
}
}
}
impl<'a, In: RpcMessage, Out: RpcMessage> Future for OpenBiFuture<'a, In, Out> {
type Output = result::Result<Socket<In, Out>, self::OpenBiError>;
fn poll(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
let mut this = self.project();
match this.inner.poll_unpin(cx) {
Poll::Ready(Ok(())) => this
.res
.take()
.map(|x| Poll::Ready(Ok(x)))
.unwrap_or(Poll::Pending),
Poll::Ready(Err(_)) => Poll::Ready(Err(self::OpenBiError::RemoteDropped)),
Poll::Pending => Poll::Pending,
}
}
}
pub struct AcceptBiFuture<'a, In: RpcMessage, Out: RpcMessage>(
flume::r#async::RecvFut<'a, Socket<In, Out>>,
);
impl<'a, In: RpcMessage, Out: RpcMessage> Future for AcceptBiFuture<'a, In, Out> {
type Output = result::Result<Socket<In, Out>, AcceptBiError>;
fn poll(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
match self.0.poll_unpin(cx) {
Poll::Ready(Ok(socket)) => Poll::Ready(Ok(socket)),
Poll::Ready(Err(_)) => Poll::Ready(Err(AcceptBiError::RemoteDropped)),
Poll::Pending => Poll::Pending,
}
}
}
pub struct SendSink<Out: RpcMessage>(flume::r#async::SendSink<'static, Out>);
impl<Out: RpcMessage> Sink<Out> for SendSink<Out> {
type Error = SendError;
fn poll_ready(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
self.0
.poll_ready_unpin(cx)
.map_err(|_| SendError::ReceiverDropped)
}
fn start_send(mut self: Pin<&mut Self>, item: Out) -> Result<(), Self::Error> {
self.0
.start_send_unpin(item)
.map_err(|_| SendError::ReceiverDropped)
}
fn poll_flush(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
self.0
.poll_flush_unpin(cx)
.map_err(|_| SendError::ReceiverDropped)
}
fn poll_close(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
self.0
.poll_close_unpin(cx)
.map_err(|_| SendError::ReceiverDropped)
}
}
#[derive(Debug)]
pub enum SendError {
ReceiverDropped,
}
impl Display for SendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl std::error::Error for SendError {}
#[derive(Debug)]
pub enum OpenBiError {
RemoteDropped,
}
impl Display for OpenBiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl std::error::Error for OpenBiError {}
#[derive(Debug, Clone, Copy)]
pub enum CreateChannelError {}
impl Display for CreateChannelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl std::error::Error for CreateChannelError {}
#[derive(Debug, Clone, Copy)]
pub struct ChannelTypes;
impl crate::ChannelTypes for ChannelTypes {
type SendSink<M: RpcMessage> = self::SendSink<M>;
type RecvStream<M: RpcMessage> = self::RecvStream<M>;
type SendError = self::SendError;
type RecvError = self::RecvError;
type OpenBiError = self::OpenBiError;
type OpenBiFuture<'a, In: RpcMessage, Out: RpcMessage> = self::OpenBiFuture<'a, In, Out>;
type AcceptBiError = self::AcceptBiError;
type AcceptBiFuture<'a, In: RpcMessage, Out: RpcMessage> = self::AcceptBiFuture<'a, In, Out>;
type ClientChannel<In: RpcMessage, Out: RpcMessage> = self::ClientChannel<In, Out>;
type ServerChannel<In: RpcMessage, Out: RpcMessage> = self::ServerChannel<In, Out>;
}
impl<In: RpcMessage, Out: RpcMessage> crate::ClientChannel<In, Out, ChannelTypes>
for ClientChannel<In, Out>
{
fn open_bi(&self) -> OpenBiFuture<'_, In, Out> {
let (local_send, remote_recv) = flume::bounded::<Out>(128);
let (remote_send, local_recv) = flume::bounded::<In>(128);
let remote_recv = RecvStream(remote_recv.into_stream());
let local_recv = RecvStream(local_recv.into_stream());
let remote_send = SendSink(remote_send.into_sink());
let local_send = SendSink(local_send.into_sink());
let inner = self.sink.send_async((remote_send, remote_recv));
OpenBiFuture::new(inner, (local_send, local_recv))
}
}
impl<In: RpcMessage, Out: RpcMessage> crate::ServerChannel<In, Out, ChannelTypes>
for ServerChannel<In, Out>
{
fn accept_bi(&self) -> AcceptBiFuture<'_, In, Out> {
AcceptBiFuture(self.stream.recv_async())
}
fn local_addr(&self) -> &[crate::LocalAddr] {
&[LocalAddr::Mem]
}
}
pub fn connection<Req: RpcMessage, Res: RpcMessage>(
buffer: usize,
) -> (ServerChannel<Req, Res>, ClientChannel<Res, Req>) {
let (sink, stream) = flume::bounded::<Socket<Req, Res>>(buffer);
(ServerChannel { stream }, ClientChannel { sink })
}