use core::future::Future;
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use crate::transport::error::TransportError;
#[cfg(not(feature = "std"))]
use alloc::sync::Arc;
#[cfg(feature = "std")]
use std::sync::Arc;
#[cfg(feature = "x509")]
mod x509 {
pub use crate::crypto::profiles::CryptoProvider;
pub use crate::crypto::x509::store::CertificateTrust;
pub use crate::transport::handshake::HandshakeKeyManager;
pub use crate::transport::TransportEncryptionConfig;
pub use crate::x509::Certificate;
}
#[cfg(feature = "x509")]
use x509::*;
pub trait TightBeamAddress: Into<Vec<u8>> + Clone + Send {}
pub trait ProtocolStream: Send {
type Error: Into<TransportError>;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>;
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>;
#[cfg(feature = "std")]
fn set_timeout(&mut self, timeout: Option<std::time::Duration>) -> Result<(), Self::Error> {
let _ = timeout;
Ok(())
}
#[cfg(not(feature = "std"))]
fn set_timeout(&mut self, timeout: Option<core::time::Duration>) -> Result<(), Self::Error> {
let _ = timeout;
Ok(())
}
}
pub trait Protocol {
type Listener: Send;
type Stream: Send;
type Transport: Send;
type Error: Into<TransportError>;
type Address: TightBeamAddress;
fn default_bind_address() -> Result<Self::Address, Self::Error>;
fn bind(addr: Self::Address) -> impl Future<Output = Result<(Self::Listener, Self::Address), Self::Error>> + Send;
fn connect(addr: Self::Address) -> impl Future<Output = Result<Self::Stream, Self::Error>> + Send;
fn create_transport(stream: Self::Stream) -> Self::Transport;
fn to_tightbeam_addr(&self) -> Result<Self::Address, Self::Error>;
}
#[cfg(feature = "x509")]
pub trait EncryptedProtocol: Protocol {
type Encryptor: Send;
type Decryptor: Send;
type CryptoProvider: CryptoProvider;
fn bind_with(
addr: Self::Address,
config: TransportEncryptionConfig<Self::CryptoProvider>,
) -> impl Future<Output = Result<(Self::Listener, Self::Address), Self::Error>> + Send;
}
#[cfg(feature = "x509")]
pub trait X509ClientConfig: Sized {
type CryptoProvider: CryptoProvider;
fn with_trust_store(self, store: Arc<dyn CertificateTrust>) -> Self;
fn with_client_identity(self, cert: Certificate, key: HandshakeKeyManager<Self::CryptoProvider>) -> Self;
}
pub trait Mycelial: Protocol {
fn try_available_connect(
&self,
) -> impl Future<Output = Result<(Self::Listener, Self::Address), Self::Error>> + Send;
}
pub trait AsyncListenerTrait: Protocol + Send {
#[allow(async_fn_in_trait)]
async fn accept(&self) -> Result<(Self::Transport, Self::Address), Self::Error>;
}
pub trait PersistentConnection: Protocol {
fn is_connected(transport: &Self::Transport) -> bool;
fn try_close(transport: &mut Self::Transport);
}