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
use crate::RpcMessage;
use futures::{Future, FutureExt, Sink, SinkExt, Stream, StreamExt};
use pin_project::pin_project;
use serde::{de::DeserializeOwned, Serialize};
use std::{fmt, io, marker::PhantomData, pin::Pin, result};
use tokio_serde::{formats::SymmetricalBincode, SymmetricallyFramed};
use tokio_util::codec::{FramedRead, FramedWrite, LengthDelimitedCodec};
type Socket<In, Out> = (SendSink<Out>, RecvStream<In>);
pub struct Channel<In: RpcMessage, Out: RpcMessage>(quinn::Connection, PhantomData<(In, Out)>);
impl<In: RpcMessage, Out: RpcMessage> Channel<In, Out> {
pub fn new(conn: quinn::Connection) -> Self {
Self(conn, PhantomData)
}
}
impl<In: RpcMessage, Out: RpcMessage> fmt::Debug for Channel<In, Out> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Channel")
.field(&self.0)
.field(&self.1)
.finish()
}
}
impl<In: RpcMessage, Out: RpcMessage> Clone for Channel<In, Out> {
fn clone(&self) -> Self {
Self(self.0.clone(), PhantomData)
}
}
#[pin_project]
pub struct SendSink<Out>(
#[pin]
tokio_serde::SymmetricallyFramed<
FramedWrite<::quinn::SendStream, LengthDelimitedCodec>,
Out,
SymmetricalBincode<Out>,
>,
);
impl<Out: Serialize> Sink<Out> for SendSink<Out> {
type Error = io::Error;
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.project().0.poll_ready_unpin(cx)
}
fn start_send(self: Pin<&mut Self>, item: Out) -> Result<(), Self::Error> {
self.project().0.start_send_unpin(item)
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.project().0.poll_flush_unpin(cx)
}
fn poll_close(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.project().0.poll_close_unpin(cx)
}
}
#[pin_project]
pub struct RecvStream<In>(
#[pin]
tokio_serde::SymmetricallyFramed<
FramedRead<::quinn::RecvStream, LengthDelimitedCodec>,
In,
SymmetricalBincode<In>,
>,
);
impl<In: DeserializeOwned> Stream for RecvStream<In> {
type Item = result::Result<In, io::Error>;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
self.project().0.poll_next_unpin(cx)
}
}
pub type OpenBiError = quinn::ConnectionError;
pub type AcceptBiError = quinn::ConnectionError;
#[derive(Debug, Clone, Copy)]
pub struct QuinnChannelTypes;
#[pin_project]
pub struct OpenBiFuture<'a, In, Out>(#[pin] quinn::OpenBi<'a>, PhantomData<(In, Out)>);
impl<'a, In, Out> Future for OpenBiFuture<'a, In, Out> {
type Output = result::Result<self::Socket<In, Out>, self::OpenBiError>;
fn poll(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
self.project().0.poll_unpin(cx).map(|conn| {
let (send, recv) = conn?;
let send = FramedWrite::new(send, LengthDelimitedCodec::new());
let recv = FramedRead::new(recv, LengthDelimitedCodec::new());
let recv = SymmetricallyFramed::new(recv, SymmetricalBincode::<In>::default());
let send = SymmetricallyFramed::new(send, SymmetricalBincode::<Out>::default());
let send = SendSink(send);
let recv = RecvStream(recv);
Ok((send, recv))
})
}
}
#[pin_project]
pub struct AcceptBiFuture<'a, In, Out>(#[pin] quinn::AcceptBi<'a>, PhantomData<(In, Out)>);
impl<'a, In, Out> Future for AcceptBiFuture<'a, In, Out> {
type Output = result::Result<self::Socket<In, Out>, self::OpenBiError>;
fn poll(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
self.project().0.poll_unpin(cx).map(|conn| {
let (send, recv) = conn?;
let send = FramedWrite::new(send, LengthDelimitedCodec::new());
let recv = FramedRead::new(recv, LengthDelimitedCodec::new());
let recv = SymmetricallyFramed::new(recv, SymmetricalBincode::<In>::default());
let send = SymmetricallyFramed::new(send, SymmetricalBincode::<Out>::default());
let send = SendSink(send);
let recv = RecvStream(recv);
Ok((send, recv))
})
}
}
impl crate::ChannelTypes for QuinnChannelTypes {
type CreateChannelError = self::CreateChannelError;
type SendSink<M: RpcMessage> = self::SendSink<M>;
type RecvStream<M: RpcMessage> = self::RecvStream<M>;
type OpenBiError = self::OpenBiError;
type AcceptBiError = self::OpenBiError;
type SendError = io::Error;
type RecvError = io::Error;
type OpenBiFuture<'a, In: RpcMessage, Out: RpcMessage> = self::OpenBiFuture<'a, In, Out>;
type AcceptBiFuture<'a, In: RpcMessage, Out: RpcMessage> = self::AcceptBiFuture<'a, In, Out>;
type Channel<In: RpcMessage, Out: RpcMessage> = self::Channel<In, Out>;
}
impl<In: RpcMessage + Sync, Out: RpcMessage + Sync> crate::Channel<In, Out, QuinnChannelTypes>
for self::Channel<In, Out>
{
fn open_bi(&self) -> OpenBiFuture<'_, In, Out> {
OpenBiFuture(self.0.open_bi(), PhantomData)
}
fn accept_bi(&self) -> AcceptBiFuture<'_, In, Out> {
AcceptBiFuture(self.0.accept_bi(), PhantomData)
}
}
#[derive(Debug, Clone)]
pub enum CreateChannelError {
Io(io::ErrorKind, String),
Connect(quinn::ConnectError),
Connection(quinn::ConnectionError),
}
impl From<io::Error> for CreateChannelError {
fn from(e: io::Error) -> Self {
CreateChannelError::Io(e.kind(), e.to_string())
}
}
impl From<quinn::ConnectionError> for CreateChannelError {
fn from(e: quinn::ConnectionError) -> Self {
CreateChannelError::Connection(e)
}
}
impl From<quinn::ConnectError> for CreateChannelError {
fn from(e: quinn::ConnectError) -> Self {
CreateChannelError::Connect(e)
}
}
impl fmt::Display for CreateChannelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl std::error::Error for CreateChannelError {}