//! Socket handlers use super::*; use std::collections::HashSet; use std::net::{ SocketAddr, }; use tokio::io::{ AsyncWrite, AsyncRead }; use tokio::task::JoinHandle; use tokio::sync::{ mpsc, oneshot, }; use futures::Future; use bytes::Bytes; use cancel::*; pub mod enc; /// Details of a newly accepted raw socket peer. /// /// This connected will have been "accepted", but not yet trusted #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct RawSockPeerAccepted { /// Address of socket. pub addr: SocketAddr, /// Trust this peer from the start? This should almost always be false. pub auto_trust: bool, } /// Details of a connected, set-up raw socket connection. #[derive(Debug, Clone, PartialEq, Eq)] pub struct RawSockPeerTrusted { /// The socket's details pub sock_details: RawSockPeerAccepted, /// Capabilities for this peer pub cap_allow: HashSet, } /// A raw, received message from a `RawPeer`. #[derive(Debug)] pub struct RawMessage{ message_bytes: Bytes, } /// A connected raw peer, created and handled by `handle_new_socket_with_shutdown()`. #[derive(Debug)] pub struct RawPeer{ info: RawSockPeerTrusted, rx: mpsc::Receiver, } /// Handles a **newly connected** raw socket. /// /// This will handle setting up socket peer encryption and validation. pub fn handle_new_socket_with_shutdown( sock_details: RawSockPeerAccepted, set_peer: oneshot::Sender, tx: W, rx: R, shutdown: C ) -> JoinHandle> where R: AsyncRead + Unpin + Send + 'static, W: AsyncWrite + Unpin + Send + 'static { tokio::spawn(async move { match { with_cancel!(async move { // Create empty cap let mut sock_details = RawSockPeerTrusted { sock_details, cap_allow: HashSet::new(), }; // Set up encryption //TODO: Find caps for this peer. Ok(()) }, shutdown) } { Ok(v) => v, Err(x) => Err(eyre::Report::from(x)), } }) }