2019-12-17 07:34:25 +00:00
|
|
|
use std::cell::Cell;
|
2020-05-18 02:45:26 +00:00
|
|
|
use std::future::Future;
|
2019-12-17 07:34:25 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
2019-04-09 00:49:27 +00:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
|
|
|
use actix_http::{body, h1, ws, Error, HttpService, Request, Response};
|
2019-12-12 17:08:38 +00:00
|
|
|
use actix_http_test::test_server;
|
2019-12-17 07:34:25 +00:00
|
|
|
use actix_service::{fn_factory, Service};
|
2020-08-24 09:13:35 +00:00
|
|
|
use actix_utils::dispatcher::Dispatcher;
|
2019-12-12 08:06:54 +00:00
|
|
|
use bytes::Bytes;
|
2020-05-18 02:45:26 +00:00
|
|
|
use futures_util::future;
|
|
|
|
use futures_util::task::{Context, Poll};
|
2021-03-11 03:48:38 +00:00
|
|
|
use futures_util::{SinkExt as _, StreamExt as _};
|
2019-04-09 00:49:27 +00:00
|
|
|
|
2019-12-17 07:34:25 +00:00
|
|
|
struct WsService<T>(Arc<Mutex<(PhantomData<T>, Cell<bool>)>>);
|
2019-04-09 00:49:27 +00:00
|
|
|
|
2019-12-17 07:34:25 +00:00
|
|
|
impl<T> WsService<T> {
|
|
|
|
fn new() -> Self {
|
|
|
|
WsService(Arc::new(Mutex::new((PhantomData, Cell::new(false)))))
|
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2021-02-07 01:00:40 +00:00
|
|
|
fn set_polled(&self) {
|
2019-12-17 07:34:25 +00:00
|
|
|
*self.0.lock().unwrap().1.get_mut() = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn was_polled(&self) -> bool {
|
|
|
|
self.0.lock().unwrap().1.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for WsService<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
WsService(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 23:47:04 +00:00
|
|
|
impl<T> Service<(Request, Framed<T, h1::Codec>)> for WsService<T>
|
2019-12-17 07:34:25 +00:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
|
|
|
{
|
|
|
|
type Response = ();
|
|
|
|
type Error = Error;
|
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
|
|
|
|
|
2021-02-07 01:00:40 +00:00
|
|
|
fn poll_ready(&self, _ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-12-17 07:34:25 +00:00
|
|
|
self.set_polled();
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
2021-02-07 01:00:40 +00:00
|
|
|
fn call(&self, (req, mut framed): (Request, Framed<T, h1::Codec>)) -> Self::Future {
|
2019-12-17 07:34:25 +00:00
|
|
|
let fut = async move {
|
|
|
|
let res = ws::handshake(req.head()).unwrap().message_body(());
|
|
|
|
|
|
|
|
framed
|
|
|
|
.send((res, body::BodySize::None).into())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-08-24 09:13:35 +00:00
|
|
|
Dispatcher::new(framed.replace_codec(ws::Codec::new()), service)
|
2019-12-17 07:34:25 +00:00
|
|
|
.await
|
|
|
|
.map_err(|_| panic!())
|
|
|
|
};
|
|
|
|
|
|
|
|
Box::pin(fut)
|
|
|
|
}
|
2019-04-09 00:49:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 12:54:19 +00:00
|
|
|
async fn service(msg: ws::Frame) -> Result<ws::Message, Error> {
|
2019-04-09 00:49:27 +00:00
|
|
|
let msg = match msg {
|
|
|
|
ws::Frame::Ping(msg) => ws::Message::Pong(msg),
|
|
|
|
ws::Frame::Text(text) => {
|
2021-01-04 11:27:32 +00:00
|
|
|
ws::Message::Text(String::from_utf8_lossy(&text).into_owned().into())
|
2019-04-09 00:49:27 +00:00
|
|
|
}
|
2019-12-12 08:06:54 +00:00
|
|
|
ws::Frame::Binary(bin) => ws::Message::Binary(bin),
|
|
|
|
ws::Frame::Continuation(item) => ws::Message::Continuation(item),
|
2019-04-09 00:49:27 +00:00
|
|
|
ws::Frame::Close(reason) => ws::Message::Close(reason),
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
2019-11-19 12:54:19 +00:00
|
|
|
Ok(msg)
|
2019-04-09 00:49:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_simple() {
|
2019-12-17 07:34:25 +00:00
|
|
|
let ws_service = WsService::new();
|
|
|
|
let mut srv = test_server({
|
|
|
|
let ws_service = ws_service.clone();
|
|
|
|
move || {
|
|
|
|
let ws_service = ws_service.clone();
|
|
|
|
HttpService::build()
|
|
|
|
.upgrade(fn_factory(move || future::ok::<_, ()>(ws_service.clone())))
|
|
|
|
.finish(|_| future::ok::<_, ()>(Response::NotFound()))
|
|
|
|
.tcp()
|
|
|
|
}
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-04-09 00:49:27 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// client service
|
|
|
|
let mut framed = srv.ws().await.unwrap();
|
2021-01-04 11:27:32 +00:00
|
|
|
framed.send(ws::Message::Text("text".into())).await.unwrap();
|
2019-11-26 05:25:50 +00:00
|
|
|
let (item, mut framed) = framed.into_future().await;
|
|
|
|
assert_eq!(
|
|
|
|
item.unwrap().unwrap(),
|
2019-12-12 08:06:54 +00:00
|
|
|
ws::Frame::Text(Bytes::from_static(b"text"))
|
2019-11-26 05:25:50 +00:00
|
|
|
);
|
2019-04-09 00:49:27 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
framed
|
|
|
|
.send(ws::Message::Binary("text".into()))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let (item, mut framed) = framed.into_future().await;
|
|
|
|
assert_eq!(
|
|
|
|
item.unwrap().unwrap(),
|
2019-12-12 08:06:54 +00:00
|
|
|
ws::Frame::Binary(Bytes::from_static(&b"text"[..]))
|
2019-11-26 05:25:50 +00:00
|
|
|
);
|
2019-04-09 00:49:27 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
framed.send(ws::Message::Ping("text".into())).await.unwrap();
|
|
|
|
let (item, mut framed) = framed.into_future().await;
|
|
|
|
assert_eq!(
|
|
|
|
item.unwrap().unwrap(),
|
|
|
|
ws::Frame::Pong("text".to_string().into())
|
|
|
|
);
|
2019-04-09 00:49:27 +00:00
|
|
|
|
2019-12-12 08:06:54 +00:00
|
|
|
framed
|
|
|
|
.send(ws::Message::Continuation(ws::Item::FirstText(
|
|
|
|
"text".into(),
|
|
|
|
)))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let (item, mut framed) = framed.into_future().await;
|
|
|
|
assert_eq!(
|
|
|
|
item.unwrap().unwrap(),
|
|
|
|
ws::Frame::Continuation(ws::Item::FirstText(Bytes::from_static(b"text")))
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(framed
|
|
|
|
.send(ws::Message::Continuation(ws::Item::FirstText(
|
|
|
|
"text".into()
|
|
|
|
)))
|
|
|
|
.await
|
|
|
|
.is_err());
|
|
|
|
assert!(framed
|
|
|
|
.send(ws::Message::Continuation(ws::Item::FirstBinary(
|
|
|
|
"text".into()
|
|
|
|
)))
|
|
|
|
.await
|
|
|
|
.is_err());
|
|
|
|
|
|
|
|
framed
|
|
|
|
.send(ws::Message::Continuation(ws::Item::Continue("text".into())))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let (item, mut framed) = framed.into_future().await;
|
|
|
|
assert_eq!(
|
|
|
|
item.unwrap().unwrap(),
|
|
|
|
ws::Frame::Continuation(ws::Item::Continue(Bytes::from_static(b"text")))
|
|
|
|
);
|
|
|
|
|
|
|
|
framed
|
|
|
|
.send(ws::Message::Continuation(ws::Item::Last("text".into())))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let (item, mut framed) = framed.into_future().await;
|
|
|
|
assert_eq!(
|
|
|
|
item.unwrap().unwrap(),
|
|
|
|
ws::Frame::Continuation(ws::Item::Last(Bytes::from_static(b"text")))
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(framed
|
|
|
|
.send(ws::Message::Continuation(ws::Item::Continue("text".into())))
|
|
|
|
.await
|
|
|
|
.is_err());
|
|
|
|
|
|
|
|
assert!(framed
|
|
|
|
.send(ws::Message::Continuation(ws::Item::Last("text".into())))
|
|
|
|
.await
|
|
|
|
.is_err());
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
framed
|
|
|
|
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2019-04-09 00:49:27 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let (item, _framed) = framed.into_future().await;
|
|
|
|
assert_eq!(
|
|
|
|
item.unwrap().unwrap(),
|
|
|
|
ws::Frame::Close(Some(ws::CloseCode::Normal.into()))
|
|
|
|
);
|
2019-12-17 07:34:25 +00:00
|
|
|
|
|
|
|
assert!(ws_service.was_polled());
|
2019-04-09 00:49:27 +00:00
|
|
|
}
|