1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-02-12 08:05:34 +01:00
actix-web/actix-http/src/ws/dispatcher.rs

52 lines
1.3 KiB
Rust
Raw Normal View History

2019-11-18 18:42:27 +06:00
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
2018-12-10 18:08:33 -08:00
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_service::{IntoService, Service};
2019-12-11 19:20:20 +06:00
use actix_utils::framed;
2018-10-05 14:30:40 -07:00
2018-10-10 13:20:00 -07:00
use super::{Codec, Frame, Message};
2018-10-05 14:30:40 -07:00
2019-12-11 19:20:20 +06:00
pub struct Dispatcher<S, T>
2018-10-05 14:30:40 -07:00
where
S: Service<Request = Frame, Response = Message> + 'static,
2019-11-19 18:54:19 +06:00
T: AsyncRead + AsyncWrite,
2018-10-05 14:30:40 -07:00
{
2019-12-11 19:20:20 +06:00
inner: framed::Dispatcher<S, T, Codec>,
2018-10-05 14:30:40 -07:00
}
2019-12-11 19:20:20 +06:00
impl<S, T> Dispatcher<S, T>
2018-10-05 14:30:40 -07:00
where
2019-11-19 18:54:19 +06:00
T: AsyncRead + AsyncWrite,
S: Service<Request = Frame, Response = Message>,
2018-10-05 14:30:40 -07:00
S::Future: 'static,
2019-11-19 18:54:19 +06:00
S::Error: 'static,
2018-10-05 14:30:40 -07:00
{
pub fn new<F: IntoService<S>>(io: T, service: F) -> Self {
2019-12-11 19:20:20 +06:00
Dispatcher {
inner: framed::Dispatcher::new(Framed::new(io, Codec::new()), service),
2018-10-05 14:30:40 -07:00
}
}
pub fn with<F: IntoService<S>>(framed: Framed<T, Codec>, service: F) -> Self {
2019-12-11 19:20:20 +06:00
Dispatcher {
inner: framed::Dispatcher::new(framed, service),
2018-10-05 14:30:40 -07:00
}
}
}
2019-12-11 19:20:20 +06:00
impl<S, T> Future for Dispatcher<S, T>
2018-10-05 14:30:40 -07:00
where
2019-11-19 18:54:19 +06:00
T: AsyncRead + AsyncWrite,
S: Service<Request = Frame, Response = Message>,
2018-10-05 14:30:40 -07:00
S::Future: 'static,
2019-11-19 18:54:19 +06:00
S::Error: 'static,
2018-10-05 14:30:40 -07:00
{
2019-12-11 19:20:20 +06:00
type Output = Result<(), framed::DispatcherError<S::Error, Codec>>;
2018-10-05 14:30:40 -07:00
2019-12-08 00:46:51 +06:00
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2019-11-18 18:42:27 +06:00
Pin::new(&mut self.inner).poll(cx)
2018-10-05 14:30:40 -07:00
}
}