ombrac_client/
client.rs

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
use std::io;

use ombrac::io::Streamable;
use ombrac::request::{Address, Request};
use ombrac::Provider;
use tokio::io::{AsyncRead, AsyncWrite};

pub struct Client<T> {
    transport: T,
}

impl<Transport, Stream> Client<Transport>
where
    Transport: Provider<Item = Stream>,
    Stream: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
    pub fn new(transport: Transport) -> Self {
        Self { transport }
    }

    pub async fn outbound(&mut self) -> io::Result<Stream> {
        match self.transport.fetch().await {
            Some(value) => Ok(value),
            None => Err(io::Error::new(
                io::ErrorKind::NotConnected,
                "Not outbound connected",
            )),
        }
    }

    pub async fn tcp_connect(outbound: &mut Stream, address: Address) -> io::Result<()> {
        <Request as Streamable>::write(Request::TcpConnect(address), outbound).await?;

        Ok(())
    }
}