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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (c) 2018-2019 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 or MIT license, at your option.
//
// A copy of the Apache License, Version 2.0 is included in the software as
// LICENSE-APACHE and a copy of the MIT license is included in the software
// as LICENSE-MIT. You may also obtain a copy of the Apache License, Version 2.0
// at https://www.apache.org/licenses/LICENSE-2.0 and a copy of the MIT license
// at https://opensource.org/licenses/MIT.

use crate::yamux::{error::ConnectionError, Connection, Result, Stream, MAX_ACK_BACKLOG};
use futures::{
    channel::{mpsc, oneshot},
    prelude::*,
};
use std::{
    pin::Pin,
    task::{Context, Poll},
};

/// A Yamux [`Connection`] controller.
///
/// This presents an alternative API for using a yamux [`Connection`].
///
/// A [`Control`] communicates with a [`ControlledConnection`] via a channel. This allows
/// a [`Control`] to be cloned and shared between tasks and threads.
#[derive(Clone, Debug)]
pub struct Control {
    /// Command channel to [`ControlledConnection`].
    sender: mpsc::Sender<ControlCommand>,
}

impl Control {
    pub fn new<T>(connection: Connection<T>) -> (Self, ControlledConnection<T>) {
        let (sender, receiver) = mpsc::channel(MAX_ACK_BACKLOG);

        let control = Control { sender };
        let connection = ControlledConnection {
            state: State::Idle(connection),
            commands: receiver,
        };

        (control, connection)
    }

    /// Open a new stream to the remote.
    pub async fn open_stream(&mut self) -> Result<Stream> {
        let (tx, rx) = oneshot::channel();
        self.sender.send(ControlCommand::OpenStream(tx)).await?;
        rx.await?
    }

    /// Close the connection.
    pub async fn close(&mut self) -> Result<()> {
        let (tx, rx) = oneshot::channel();
        if self.sender.send(ControlCommand::CloseConnection(tx)).await.is_err() {
            // The receiver is closed which means the connection is already closed.
            return Ok(());
        }
        // A dropped `oneshot::Sender` means the `Connection` is gone,
        // so we do not treat receive errors differently here.
        let _ = rx.await;
        Ok(())
    }
}

/// Wraps a [`Connection`] which can be controlled with a [`Control`].
pub struct ControlledConnection<T> {
    state: State<T>,
    commands: mpsc::Receiver<ControlCommand>,
}

impl<T> ControlledConnection<T>
where
    T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
    fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Stream>>> {
        loop {
            match std::mem::replace(&mut self.state, State::Poisoned) {
                State::Idle(mut connection) => {
                    match connection.poll_next_inbound(cx) {
                        Poll::Ready(maybe_stream) => {
                            self.state = State::Idle(connection);
                            return Poll::Ready(maybe_stream);
                        }
                        Poll::Pending => {}
                    }

                    match self.commands.poll_next_unpin(cx) {
                        Poll::Ready(Some(ControlCommand::OpenStream(reply))) => {
                            self.state = State::OpeningNewStream { reply, connection };
                            continue;
                        }
                        Poll::Ready(Some(ControlCommand::CloseConnection(reply))) => {
                            self.commands.close();

                            self.state = State::Closing {
                                reply: Some(reply),
                                inner: Closing::DrainingControlCommands { connection },
                            };
                            continue;
                        }
                        Poll::Ready(None) => {
                            // Last `Control` sender was dropped, close te connection.
                            self.state = State::Closing {
                                reply: None,
                                inner: Closing::ClosingConnection { connection },
                            };
                            continue;
                        }
                        Poll::Pending => {}
                    }

                    self.state = State::Idle(connection);
                    return Poll::Pending;
                }
                State::OpeningNewStream {
                    reply,
                    mut connection,
                } => match connection.poll_new_outbound(cx) {
                    Poll::Ready(stream) => {
                        let _ = reply.send(stream);

                        self.state = State::Idle(connection);
                        continue;
                    }
                    Poll::Pending => {
                        self.state = State::OpeningNewStream { reply, connection };
                        return Poll::Pending;
                    }
                },
                State::Closing {
                    reply,
                    inner: Closing::DrainingControlCommands { connection },
                } => match self.commands.poll_next_unpin(cx) {
                    Poll::Ready(Some(ControlCommand::OpenStream(new_reply))) => {
                        let _ = new_reply.send(Err(ConnectionError::Closed));

                        self.state = State::Closing {
                            reply,
                            inner: Closing::DrainingControlCommands { connection },
                        };
                        continue;
                    }
                    Poll::Ready(Some(ControlCommand::CloseConnection(new_reply))) => {
                        let _ = new_reply.send(());

                        self.state = State::Closing {
                            reply,
                            inner: Closing::DrainingControlCommands { connection },
                        };
                        continue;
                    }
                    Poll::Ready(None) => {
                        self.state = State::Closing {
                            reply,
                            inner: Closing::ClosingConnection { connection },
                        };
                        continue;
                    }
                    Poll::Pending => {
                        self.state = State::Closing {
                            reply,
                            inner: Closing::DrainingControlCommands { connection },
                        };
                        return Poll::Pending;
                    }
                },
                State::Closing {
                    reply,
                    inner: Closing::ClosingConnection { mut connection },
                } => match connection.poll_close(cx) {
                    Poll::Ready(Ok(())) | Poll::Ready(Err(ConnectionError::Closed)) => {
                        if let Some(reply) = reply {
                            let _ = reply.send(());
                        }
                        return Poll::Ready(None);
                    }
                    Poll::Ready(Err(other)) => {
                        if let Some(reply) = reply {
                            let _ = reply.send(());
                        }
                        return Poll::Ready(Some(Err(other)));
                    }
                    Poll::Pending => {
                        self.state = State::Closing {
                            reply,
                            inner: Closing::ClosingConnection { connection },
                        };
                        return Poll::Pending;
                    }
                },
                State::Poisoned => unreachable!(),
            }
        }
    }
}

#[derive(Debug)]
enum ControlCommand {
    /// Open a new stream to the remote end.
    OpenStream(oneshot::Sender<Result<Stream>>),
    /// Close the whole connection.
    CloseConnection(oneshot::Sender<()>),
}

/// The state of a [`ControlledConnection`].
enum State<T> {
    Idle(Connection<T>),
    OpeningNewStream {
        reply: oneshot::Sender<Result<Stream>>,
        connection: Connection<T>,
    },
    Closing {
        /// A channel to the [`Control`] in case the close was requested. `None` if we are closing
        /// because the last [`Control`] was dropped.
        reply: Option<oneshot::Sender<()>>,
        inner: Closing<T>,
    },
    Poisoned,
}

/// A sub-state of our larger state machine for a [`ControlledConnection`].
///
/// Closing connection involves two steps:
///
/// 1. Draining and answered all remaining [`Closing::DrainingControlCommands`].
/// 1. Closing the underlying [`Connection`].
enum Closing<T> {
    DrainingControlCommands { connection: Connection<T> },
    ClosingConnection { connection: Connection<T> },
}

impl<T> futures::Stream for ControlledConnection<T>
where
    T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
    type Item = Result<Stream>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.get_mut().poll_next(cx)
    }
}