use crate::{channel::Channel, Error};
use smol::{Async, Timer};
use ssh2::{self};
use std::{net::TcpStream, sync::Arc, time::Duration};
pub struct Listener {
inner: ssh2::Listener,
stream: Arc<Async<TcpStream>>,
}
impl Listener {
pub(crate) fn new(listener: ssh2::Listener, stream: Arc<Async<TcpStream>>) -> Self {
Self {
inner: listener,
stream,
}
}
pub async fn accept(&mut self) -> Result<Channel, Error> {
let channel = loop {
match self.inner.accept() {
Ok(channel) => break channel,
Err(e)
if std::io::Error::from(ssh2::Error::from_errno(e.code())).kind()
== std::io::ErrorKind::WouldBlock => {}
Err(e) => return Err(Error::SSH2(e)),
};
Timer::after(Duration::from_millis(10)).await;
};
Ok(Channel::new(channel, self.stream.clone()))
}
}