1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::time::Duration;

use futures::{
    future::ready,
    stream::{once, BoxStream},
    Stream, StreamExt,
};

use crate::websocket::{
    error::WsError,
    protocol::frame::{Name, RequestFrame, ServerFrame, StreamFrame},
};

use tokio::sync::broadcast;

pub(crate) type ResponseToken = tokio::sync::oneshot::Receiver<()>;
type RequestToken = tokio::sync::oneshot::Sender<()>;

pub(crate) enum MultiplexRequestKind {
    MainStream(Name),
    SubStream {
        token: RequestToken,
        timeout: Option<Duration>,
        stream: BoxStream<'static, RequestFrame>,
    },
}

/// Multiplex request.
pub struct MultiplexRequest {
    pub(crate) id: usize,
    pub(crate) kind: MultiplexRequestKind,
}

impl MultiplexRequest {
    pub(crate) fn main_stream(name: Name) -> Self {
        Self {
            id: 0,
            kind: MultiplexRequestKind::MainStream(name),
        }
    }

    pub(crate) fn new<S, F>(stream: F) -> Self
    where
        F: FnOnce(ResponseToken) -> S,
        S: Stream<Item = RequestFrame> + Send + 'static,
    {
        let (tx, rx) = tokio::sync::oneshot::channel();
        let stream = stream(rx).boxed();
        Self {
            id: 0,
            kind: MultiplexRequestKind::SubStream {
                token: tx,
                timeout: None,
                stream,
            },
        }
    }

    pub(crate) fn timeout(mut self, duration: Duration) -> Self {
        match &mut self.kind {
            MultiplexRequestKind::MainStream(_) => {}
            MultiplexRequestKind::SubStream { timeout, .. } => {
                *timeout = Some(duration);
            }
        }
        self
    }
}

/// Multiplex response.
#[derive(Debug)]
pub enum MultiplexResponse {
    /// Main stream.
    MainStream(usize, Option<broadcast::Receiver<StreamFrame>>),
    /// Sub stream.
    SubStream {
        /// Id.
        id: usize,
        /// Token.
        token: RequestToken,
        /// Rx.
        rx: tokio::sync::mpsc::UnboundedReceiver<Result<ServerFrame, WsError>>,
    },
}

impl MultiplexResponse {
    pub(crate) fn into_stream(self) -> impl Stream<Item = Result<ServerFrame, WsError>> {
        match self {
            Self::MainStream(_, rx) => match rx {
                Some(rx) => {
                    let stream = tokio_stream::wrappers::BroadcastStream::new(rx).filter_map(
                        |res| match res {
                            Ok(frame) => ready(Some(Ok(ServerFrame::Stream(frame)))),
                            Err(_) => ready(None),
                        },
                    );
                    once(ready(Ok(ServerFrame::Empty)))
                        .chain(stream)
                        .left_stream()
                }
                None => once(ready(Err(WsError::MainStreamNotFound))).right_stream(),
            }
            .left_stream(),
            Self::SubStream { token, rx, .. } => {
                tokio_stream::wrappers::UnboundedReceiverStream::new(rx)
                    .scan(token, |_, item| futures::future::ready(Some(item)))
                    .right_stream()
            }
        }
    }
}