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
use std::sync::Arc;
use holochain_serialized_bytes::SerializedBytes;
use must_future::MustBoxFuture;
use stream_cancel::Trigger;
use stream_cancel::Valved;
use url2::Url2;
use crate::websocket::PairShutdown;
use crate::websocket::RxFromWebsocket;
use crate::websocket::TxToWebsocket;
use crate::OutgoingMessage;
use crate::WebsocketResult;
pub struct WebsocketReceiver {
rx_from_websocket: Valved<Valved<RxFromWebsocket>>,
remote_addr: Url2,
handle: Option<ReceiverHandle>,
__pair_shutdown: Arc<PairShutdown>,
}
pub type Response = Box<
dyn FnOnce(SerializedBytes) -> MustBoxFuture<'static, WebsocketResult<()>>
+ 'static
+ Send
+ Sync,
>;
pub enum Respond {
Signal,
Request(Response),
}
pub(crate) struct CancelResponse(bool, TxToWebsocket, u64);
pub struct ReceiverHandle {
shutdown: Trigger,
}
pub(crate) enum IncomingMessage {
Close {
acknowledge: tokio::sync::oneshot::Sender<()>,
},
Msg(SerializedBytes, Respond),
}
pub type WebsocketMessage = (SerializedBytes, Respond);
impl WebsocketReceiver {
pub(crate) fn new(
rx_from_websocket: Valved<RxFromWebsocket>,
remote_addr: Url2,
pair_shutdown: Arc<PairShutdown>,
) -> Self {
let (shutdown, rx_from_websocket_valved) = Valved::new(rx_from_websocket);
let handle = Some(ReceiverHandle { shutdown });
Self {
rx_from_websocket: rx_from_websocket_valved,
remote_addr,
handle,
__pair_shutdown: pair_shutdown,
}
}
pub fn take_handle(&mut self) -> Option<ReceiverHandle> {
self.handle.take()
}
pub fn remote_addr(&self) -> &Url2 {
&self.remote_addr
}
}
impl futures::stream::Stream for WebsocketReceiver {
type Item = WebsocketMessage;
fn poll_next(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
use std::task::Poll::*;
let p = std::pin::Pin::new(&mut self.rx_from_websocket);
match futures::stream::Stream::poll_next(p, cx) {
Ready(Some(IncomingMessage::Msg(msg, resp))) => Ready(Some((msg, resp))),
Ready(Some(IncomingMessage::Close { acknowledge })) => {
acknowledge.send(()).ok();
Ready(None)
}
Ready(None) => Ready(None),
Pending => Pending,
}
}
}
impl ReceiverHandle {
pub fn close(self) {
tracing::trace!("Closing Receiver");
self.shutdown.cancel()
}
pub async fn close_on<F>(self, f: F)
where
F: std::future::Future<Output = bool>,
{
if f.await {
self.close()
}
}
}
impl Respond {
pub fn is_request(&self) -> bool {
match self {
Respond::Signal => false,
Respond::Request(_) => true,
}
}
pub async fn respond(self, msg: SerializedBytes) -> WebsocketResult<()> {
match self {
Respond::Signal => Ok(()),
Respond::Request(r) => r(msg).await,
}
}
}
impl CancelResponse {
pub fn new(send_response: TxToWebsocket, id: u64) -> Self {
Self(true, send_response, id)
}
pub fn response_sent(mut self) {
self.0 = false;
}
}
impl Drop for CancelResponse {
fn drop(&mut self) {
if self.0 {
let tx = self.1.clone();
let id = self.2;
tokio::spawn(async move {
if let Err(e) = tx.send(OutgoingMessage::Response(None, id)).await {
tracing::warn!("Failed to cancel response on drop {:?}", e);
}
});
}
}
}