2021-11-27 11:19:41 +00:00
|
|
|
#![allow(clippy::all)]
|
2021-10-05 09:54:11 +00:00
|
|
|
use crate::{
|
2021-12-16 14:24:05 +00:00
|
|
|
errors::{internal_error, WSError},
|
2021-10-05 09:54:11 +00:00
|
|
|
MsgReceiver,
|
|
|
|
MsgSender,
|
|
|
|
};
|
2021-09-19 04:54:28 +00:00
|
|
|
use futures_core::{future::BoxFuture, ready};
|
2021-09-19 15:21:10 +00:00
|
|
|
use futures_util::{FutureExt, StreamExt};
|
2021-09-18 14:32:00 +00:00
|
|
|
use pin_project::pin_project;
|
|
|
|
use std::{
|
2021-09-19 04:54:28 +00:00
|
|
|
fmt,
|
2021-09-18 14:32:00 +00:00
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2021-09-19 04:54:28 +00:00
|
|
|
use tokio::net::TcpStream;
|
2021-09-18 14:32:00 +00:00
|
|
|
use tokio_tungstenite::{
|
|
|
|
connect_async,
|
2021-09-19 15:21:10 +00:00
|
|
|
tungstenite::{handshake::client::Response, Error, Message},
|
2021-09-18 14:32:00 +00:00
|
|
|
MaybeTlsStream,
|
|
|
|
WebSocketStream,
|
|
|
|
};
|
|
|
|
|
2021-11-27 11:19:41 +00:00
|
|
|
type WsConnectResult = Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response), Error>;
|
|
|
|
|
2021-09-18 14:32:00 +00:00
|
|
|
#[pin_project]
|
2021-12-16 14:24:05 +00:00
|
|
|
pub struct WSConnectionFuture {
|
2021-09-18 14:32:00 +00:00
|
|
|
msg_tx: Option<MsgSender>,
|
|
|
|
ws_rx: Option<MsgReceiver>,
|
|
|
|
#[pin]
|
2021-11-27 11:19:41 +00:00
|
|
|
fut: Pin<Box<dyn Future<Output = WsConnectResult> + Send + Sync>>,
|
2021-09-18 14:32:00 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 14:24:05 +00:00
|
|
|
impl WSConnectionFuture {
|
2021-09-18 14:32:00 +00:00
|
|
|
pub fn new(msg_tx: MsgSender, ws_rx: MsgReceiver, addr: String) -> Self {
|
2021-12-16 14:24:05 +00:00
|
|
|
WSConnectionFuture {
|
2021-09-18 14:32:00 +00:00
|
|
|
msg_tx: Some(msg_tx),
|
|
|
|
ws_rx: Some(ws_rx),
|
|
|
|
fut: Box::pin(async move { connect_async(&addr).await }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 14:24:05 +00:00
|
|
|
impl Future for WSConnectionFuture {
|
|
|
|
type Output = Result<WSStream, WSError>;
|
2021-09-18 14:32:00 +00:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
// [[pin]]
|
|
|
|
// poll async function. The following methods not work.
|
|
|
|
// 1.
|
|
|
|
// let f = connect_async("");
|
|
|
|
// pin_mut!(f);
|
|
|
|
// ready!(Pin::new(&mut a).poll(cx))
|
|
|
|
//
|
|
|
|
// 2.ready!(Pin::new(&mut Box::pin(connect_async(""))).poll(cx))
|
|
|
|
//
|
|
|
|
// An async method calls poll multiple times and might return to the executor. A
|
|
|
|
// single poll call can only return to the executor once and will get
|
|
|
|
// resumed through another poll invocation. the connect_async call multiple time
|
|
|
|
// from the beginning. So I use fut to hold the future and continue to
|
|
|
|
// poll it. (Fix me if i was wrong)
|
|
|
|
loop {
|
|
|
|
return match ready!(self.as_mut().project().fut.poll(cx)) {
|
|
|
|
Ok((stream, _)) => {
|
2021-11-03 07:37:38 +00:00
|
|
|
tracing::debug!("🐴 ws connect success");
|
2021-09-19 08:11:02 +00:00
|
|
|
let (msg_tx, ws_rx) = (
|
|
|
|
self.msg_tx.take().expect("WsConnection should be call once "),
|
|
|
|
self.ws_rx.take().expect("WsConnection should be call once "),
|
|
|
|
);
|
2021-12-16 14:24:05 +00:00
|
|
|
Poll::Ready(Ok(WSStream::new(msg_tx, ws_rx, stream)))
|
2021-09-18 14:32:00 +00:00
|
|
|
},
|
2021-09-19 04:54:28 +00:00
|
|
|
Err(error) => {
|
2021-11-03 07:37:38 +00:00
|
|
|
tracing::debug!("🐴 ws connect failed: {:?}", error);
|
2021-09-19 10:39:56 +00:00
|
|
|
Poll::Ready(Err(error.into()))
|
2021-09-19 04:54:28 +00:00
|
|
|
},
|
2021-09-18 14:32:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 14:24:05 +00:00
|
|
|
type Fut = BoxFuture<'static, Result<(), WSError>>;
|
2021-09-18 14:32:00 +00:00
|
|
|
#[pin_project]
|
2021-12-16 14:24:05 +00:00
|
|
|
pub struct WSStream {
|
2021-09-22 06:42:14 +00:00
|
|
|
#[allow(dead_code)]
|
2021-09-19 08:11:02 +00:00
|
|
|
msg_tx: MsgSender,
|
2021-09-18 14:32:00 +00:00
|
|
|
#[pin]
|
2021-09-19 04:54:28 +00:00
|
|
|
inner: Option<(Fut, Fut)>,
|
2021-09-18 14:32:00 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 14:24:05 +00:00
|
|
|
impl WSStream {
|
2021-09-18 14:32:00 +00:00
|
|
|
pub fn new(msg_tx: MsgSender, ws_rx: MsgReceiver, stream: WebSocketStream<MaybeTlsStream<TcpStream>>) -> Self {
|
|
|
|
let (ws_write, ws_read) = stream.split();
|
|
|
|
Self {
|
2021-09-19 08:11:02 +00:00
|
|
|
msg_tx: msg_tx.clone(),
|
2021-09-19 04:54:28 +00:00
|
|
|
inner: Some((
|
2021-09-18 14:32:00 +00:00
|
|
|
Box::pin(async move {
|
2021-12-05 06:04:25 +00:00
|
|
|
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
2021-10-08 07:08:56 +00:00
|
|
|
let read = async {
|
|
|
|
ws_read
|
|
|
|
.for_each(|message| async {
|
2021-12-05 06:04:25 +00:00
|
|
|
match tx.send(send_message(msg_tx.clone(), message)) {
|
2021-10-08 07:08:56 +00:00
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => log::error!("WsStream tx closed unexpectedly: {} ", e),
|
2021-10-05 09:54:11 +00:00
|
|
|
}
|
2021-10-08 07:08:56 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
2021-12-05 06:04:25 +00:00
|
|
|
let read_ret = async {
|
2021-10-08 07:08:56 +00:00
|
|
|
loop {
|
|
|
|
match rx.recv().await {
|
|
|
|
None => {
|
2021-12-16 14:24:05 +00:00
|
|
|
return Err(WSError::internal().context("WsStream rx closed unexpectedly"));
|
2021-10-08 07:08:56 +00:00
|
|
|
},
|
|
|
|
Some(result) => {
|
|
|
|
if result.is_err() {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2021-10-05 09:54:11 +00:00
|
|
|
}
|
2021-10-08 07:08:56 +00:00
|
|
|
};
|
|
|
|
futures::pin_mut!(read);
|
2021-12-05 06:04:25 +00:00
|
|
|
futures::pin_mut!(read_ret);
|
2021-11-27 11:19:41 +00:00
|
|
|
return tokio::select! {
|
|
|
|
result = read => result,
|
2021-12-05 06:04:25 +00:00
|
|
|
result = read_ret => result,
|
2021-10-08 07:08:56 +00:00
|
|
|
};
|
2021-09-18 14:32:00 +00:00
|
|
|
}),
|
|
|
|
Box::pin(async move {
|
2021-10-05 09:54:11 +00:00
|
|
|
let result = ws_rx.map(Ok).forward(ws_write).await.map_err(internal_error);
|
|
|
|
result
|
2021-09-18 14:32:00 +00:00
|
|
|
}),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 14:24:05 +00:00
|
|
|
impl fmt::Debug for WSStream {
|
2021-09-19 04:54:28 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("WsStream").finish() }
|
|
|
|
}
|
|
|
|
|
2021-12-16 14:24:05 +00:00
|
|
|
impl Future for WSStream {
|
|
|
|
type Output = Result<(), WSError>;
|
2021-09-19 04:54:28 +00:00
|
|
|
|
2021-09-18 14:32:00 +00:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2021-09-19 08:11:02 +00:00
|
|
|
let (mut ws_read, mut ws_write) = self.inner.take().unwrap();
|
|
|
|
match ws_read.poll_unpin(cx) {
|
2021-09-19 04:54:28 +00:00
|
|
|
Poll::Ready(l) => Poll::Ready(l),
|
|
|
|
Poll::Pending => {
|
|
|
|
//
|
2021-09-19 08:11:02 +00:00
|
|
|
match ws_write.poll_unpin(cx) {
|
2021-09-19 04:54:28 +00:00
|
|
|
Poll::Ready(r) => Poll::Ready(r),
|
|
|
|
Poll::Pending => {
|
2021-09-19 08:11:02 +00:00
|
|
|
self.inner = Some((ws_read, ws_write));
|
2021-09-19 04:54:28 +00:00
|
|
|
Poll::Pending
|
|
|
|
},
|
|
|
|
}
|
2021-09-18 14:32:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 14:24:05 +00:00
|
|
|
fn send_message(msg_tx: MsgSender, message: Result<Message, Error>) -> Result<(), WSError> {
|
2021-09-18 14:32:00 +00:00
|
|
|
match message {
|
2021-12-05 06:04:25 +00:00
|
|
|
Ok(Message::Binary(bytes)) => msg_tx.unbounded_send(Message::Binary(bytes)).map_err(internal_error),
|
2021-10-05 09:54:11 +00:00
|
|
|
Ok(_) => Ok(()),
|
2021-12-16 14:24:05 +00:00
|
|
|
Err(e) => Err(WSError::internal().context(e)),
|
2021-09-18 14:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-30 09:24:02 +00:00
|
|
|
#[allow(dead_code)]
|
2021-09-19 08:11:02 +00:00
|
|
|
pub struct Retry<F> {
|
|
|
|
f: F,
|
2021-09-22 06:42:14 +00:00
|
|
|
#[allow(dead_code)]
|
2021-09-19 08:11:02 +00:00
|
|
|
retry_time: usize,
|
|
|
|
addr: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> Retry<F>
|
|
|
|
where
|
|
|
|
F: Fn(&str),
|
|
|
|
{
|
2021-09-30 09:24:02 +00:00
|
|
|
#[allow(dead_code)]
|
2021-09-19 08:11:02 +00:00
|
|
|
pub fn new(addr: &str, f: F) -> Self {
|
|
|
|
Self {
|
|
|
|
f,
|
|
|
|
retry_time: 3,
|
|
|
|
addr: addr.to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> Future for Retry<F>
|
|
|
|
where
|
|
|
|
F: Fn(&str),
|
|
|
|
{
|
|
|
|
type Output = ();
|
|
|
|
|
2021-09-19 15:21:10 +00:00
|
|
|
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2021-09-19 08:11:02 +00:00
|
|
|
(self.f)(&self.addr);
|
|
|
|
|
|
|
|
Poll::Ready(())
|
|
|
|
}
|
|
|
|
}
|