|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+use async_tungstenite::tungstenite::{Error as WsError, Message};
|
|
|
2
|
+use async_tungstenite::WebSocketStream;
|
|
|
3
|
+use futures::prelude::*;
|
|
|
4
|
+use futures::sink::Sink;
|
|
|
5
|
+use futures::stream::Stream;
|
|
|
6
|
+use futures::task::Context;
|
|
|
7
|
+use futures::task::Poll;
|
|
|
8
|
+use std::pin::Pin;
|
|
|
9
|
+
|
|
|
10
|
+pub struct WebSocketConnection<S> {
|
|
|
11
|
+ stream: Pin<Box<WebSocketStream<S>>>,
|
|
|
12
|
+}
|
|
|
13
|
+
|
|
|
14
|
+impl<S> Sink<Vec<u8>> for WebSocketConnection<S>
|
|
|
15
|
+where
|
|
|
16
|
+ S: AsyncRead + AsyncWrite + Unpin,
|
|
|
17
|
+{
|
|
|
18
|
+ type Error = WsError;
|
|
|
19
|
+
|
|
|
20
|
+ fn poll_ready(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
21
|
+ // Safe, as we will not move self_.
|
|
|
22
|
+ let self_ = unsafe { self.get_unchecked_mut() };
|
|
|
23
|
+
|
|
|
24
|
+ Pin::as_mut(&mut self_.stream).poll_ready(cx)
|
|
|
25
|
+ }
|
|
|
26
|
+
|
|
|
27
|
+ fn start_send(self: Pin<&mut Self>, item: Vec<u8>) -> Result<(), Self::Error> {
|
|
|
28
|
+ // Safe, as we will not move self_.
|
|
|
29
|
+ let self_ = unsafe { self.get_unchecked_mut() };
|
|
|
30
|
+
|
|
|
31
|
+ Pin::as_mut(&mut self_.stream).start_send(Message::Binary(item))
|
|
|
32
|
+ }
|
|
|
33
|
+
|
|
|
34
|
+ fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
35
|
+ // Safe, as we will not move self_.
|
|
|
36
|
+ let self_ = unsafe { self.get_unchecked_mut() };
|
|
|
37
|
+
|
|
|
38
|
+ Pin::as_mut(&mut self_.stream).poll_flush(cx)
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
42
|
+ // Safe, as we will not move self_.
|
|
|
43
|
+ let self_ = unsafe { self.get_unchecked_mut() };
|
|
|
44
|
+
|
|
|
45
|
+ Pin::as_mut(&mut self_.stream).poll_close(cx)
|
|
|
46
|
+ }
|
|
|
47
|
+}
|
|
|
48
|
+
|
|
|
49
|
+impl<S> Stream for WebSocketConnection<S>
|
|
|
50
|
+where
|
|
|
51
|
+ S: AsyncRead + AsyncWrite + Unpin,
|
|
|
52
|
+{
|
|
|
53
|
+ type Item = Result<Vec<u8>, WsError>;
|
|
|
54
|
+
|
|
|
55
|
+ fn poll_next(
|
|
|
56
|
+ self: Pin<&mut Self>,
|
|
|
57
|
+ cx: &mut Context,
|
|
|
58
|
+ ) -> Poll<Option<<WebSocketConnection<S> as Stream>::Item>> {
|
|
|
59
|
+ // Safe, as we will not move self_.
|
|
|
60
|
+ let self_ = unsafe { self.get_unchecked_mut() };
|
|
|
61
|
+
|
|
|
62
|
+ match Pin::as_mut(&mut self_.stream).poll_next(cx) {
|
|
|
63
|
+ Poll::Ready(Some(Ok(Message::Binary(data)))) => Poll::Ready(Some(Ok(data))),
|
|
|
64
|
+ Poll::Ready(Some(Ok(_))) => unsafe {
|
|
|
65
|
+ // Ignore other message types, try again.
|
|
|
66
|
+ Pin::new_unchecked(self_).poll_next(cx)
|
|
|
67
|
+ },
|
|
|
68
|
+ Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
|
|
|
69
|
+ Poll::Ready(None) => Poll::Ready(None),
|
|
|
70
|
+ Poll::Pending => Poll::Pending,
|
|
|
71
|
+ }
|
|
|
72
|
+ }
|
|
|
73
|
+}
|