#![allow(unused)]
use crate::{
error::ChannelError,
tunnel::{TunnelPoolConfig, TunnelPoolHandle},
};
use futures_channel::oneshot;
use thingbuf::mpsc;
use core::future::Future;
#[derive(Default, Clone)]
pub(super) struct CommandRecycle(());
impl thingbuf::Recycle<TunnelManagerCommand> for CommandRecycle {
fn new_element(&self) -> TunnelManagerCommand {
TunnelManagerCommand::Dummy
}
fn recycle(&self, element: &mut TunnelManagerCommand) {
*element = TunnelManagerCommand::Dummy;
}
}
#[derive(Default)]
pub(super) enum TunnelManagerCommand {
CreateTunnelPool {
config: TunnelPoolConfig,
tx: oneshot::Sender<TunnelPoolHandle>,
},
#[default]
Dummy,
}
#[derive(Clone)]
pub struct TunnelManagerHandle {
tx: mpsc::Sender<TunnelManagerCommand, CommandRecycle>,
}
impl TunnelManagerHandle {
pub(super) fn new() -> (Self, mpsc::Receiver<TunnelManagerCommand, CommandRecycle>) {
let (tx, rx) = mpsc::with_recycle(64, CommandRecycle(()));
(Self { tx }, rx)
}
pub fn create_tunnel_pool(
&self,
config: TunnelPoolConfig,
) -> Result<impl Future<Output = TunnelPoolHandle>, ChannelError> {
let (tx, rx) = oneshot::channel();
self.tx
.try_send(TunnelManagerCommand::CreateTunnelPool { config, tx })
.map(|_| async move { rx.await.expect("to succeed") })
.map_err(From::from)
}
}