requiem_http/ws/
dispatcher.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use requiem_codec::{AsyncRead, AsyncWrite, Framed};
6use requiem_service::{IntoService, Service};
7use requiem_utils::framed;
8
9use super::{Codec, Frame, Message};
10
11pub struct Dispatcher<S, T>
12where
13    S: Service<Request = Frame, Response = Message> + 'static,
14    T: AsyncRead + AsyncWrite,
15{
16    inner: framed::Dispatcher<S, T, Codec>,
17}
18
19impl<S, T> Dispatcher<S, T>
20where
21    T: AsyncRead + AsyncWrite,
22    S: Service<Request = Frame, Response = Message>,
23    S::Future: 'static,
24    S::Error: 'static,
25{
26    pub fn new<F: IntoService<S>>(io: T, service: F) -> Self {
27        Dispatcher {
28            inner: framed::Dispatcher::new(Framed::new(io, Codec::new()), service),
29        }
30    }
31
32    pub fn with<F: IntoService<S>>(framed: Framed<T, Codec>, service: F) -> Self {
33        Dispatcher {
34            inner: framed::Dispatcher::new(framed, service),
35        }
36    }
37}
38
39impl<S, T> Future for Dispatcher<S, T>
40where
41    T: AsyncRead + AsyncWrite,
42    S: Service<Request = Frame, Response = Message>,
43    S::Future: 'static,
44    S::Error: 'static,
45{
46    type Output = Result<(), framed::DispatcherError<S::Error, Codec>>;
47
48    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
49        Pin::new(&mut self.inner).poll(cx)
50    }
51}