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
use core::time::Duration;
use std::sync::Arc;

use ssh2::{BlockDirections, Listener, Session};

use crate::{channel::AsyncChannel, error::Error, session_stream::AsyncSessionStream};

//
pub struct AsyncListener<S> {
    inner: Listener,
    sess: Session,
    stream: Arc<S>,
}

impl<S> AsyncListener<S> {
    pub(crate) fn from_parts(inner: Listener, sess: Session, stream: Arc<S>) -> Self {
        Self {
            inner,
            sess,
            stream,
        }
    }
}

impl<S> AsyncListener<S>
where
    S: AsyncSessionStream + Send + Sync + 'static,
{
    pub async fn accept(&mut self) -> Result<AsyncChannel<S>, Error> {
        let channel = self
            .stream
            .x_with(
                || self.inner.accept(),
                &self.sess,
                BlockDirections::Both,
                Some(Duration::from_millis(10)),
            )
            .await?;

        Ok(AsyncChannel::from_parts(
            channel,
            self.sess.clone(),
            self.stream.clone(),
        ))
    }
}