use tokio::net::{TcpStream, ToSocketAddrs};
use crate::transport::build_stream_session;
use crate::{Error, Protocol, Session, Version};
#[derive(Debug, Clone)]
pub struct Config {
inner: crate::Config,
}
impl Config {
pub fn new(version: Version) -> Self {
Self {
inner: crate::Config::new(version),
}
}
pub fn protocols<I, S>(mut self, protocols: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.inner.protocol = Protocol::Negotiate(protocols.into_iter().map(Into::into).collect());
self
}
pub async fn connect(self, addr: impl ToSocketAddrs) -> Result<Session, Error> {
let stream = TcpStream::connect(addr).await?;
finish(stream, self.inner, false).await
}
pub async fn accept(self, stream: TcpStream) -> Result<Session, Error> {
finish(stream, self.inner, true).await
}
}
async fn finish(
stream: TcpStream,
config: crate::Config,
is_server: bool,
) -> Result<Session, Error> {
let session = build_stream_session(stream, config, is_server)?;
session.negotiated().await;
Ok(session)
}