From 12339aad906eff1a9c81f87f6136e3011a8558a0 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Thu, 30 Sep 2021 01:00:19 +0100 Subject: [PATCH] This isn't working..... I think I've bitten off more than I can chew again with Rust :-( --- src/agent/mod.rs | 10 +++------- src/agent/peer_connection.rs | 27 +++++++++++++++++++++++++-- src/protocol/messages.rs | 6 ++++++ src/protocol/mod.rs | 1 + src/utils/dual_channel.rs | 25 +++++++++++++++++++++++++ src/utils/mod.rs | 1 + 6 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/protocol/messages.rs create mode 100644 src/protocol/mod.rs create mode 100644 src/utils/dual_channel.rs create mode 100644 src/utils/mod.rs diff --git a/src/agent/mod.rs b/src/agent/mod.rs index 2c01974..b1a568c 100644 --- a/src/agent/mod.rs +++ b/src/agent/mod.rs @@ -26,14 +26,10 @@ impl Agent { loop { let (client, _) = agent.listener.accept().await.unwrap(); - let peer = PeerConnection { - stream: client - }; + let peer = PeerConnection::new(client); + agent.peers.push(peer); - tokio::spawn(async { - let peer_borrow = &peer; - peer_borrow.handle().await - }); + } } diff --git a/src/agent/peer_connection.rs b/src/agent/peer_connection.rs index b51f775..cfa2ffc 100644 --- a/src/agent/peer_connection.rs +++ b/src/agent/peer_connection.rs @@ -1,11 +1,34 @@ use tokio::net::TcpStream; +use tokio::io::Interest; +use tokio::sync::oneshot; pub struct PeerConnection { - stream: TcpStream + send: oneshot::Sender, + receive: oneshot::Receiver } impl PeerConnection { - pub async fn handle(&self) { + pub fn new(stream: TcpStream) -> PeerConnection { + let (in_tx1, in_rx1) = oneshot::channel(); + let (out_tx2, out_rx2) = oneshot::channel(); + let peer = PeerConnection { send: in_tx1, receive: out_rx2 }; + + tokio::spawn(async { + peer.handle(stream, in_rx1, out_tx2); + }); + + return peer; + } + + pub async fn handle(&self, + stream: TcpStream, + stream_in: oneshot::Receiver, + stream_out: oneshot::Sender) { + let ready = stream.ready(Interest::READABLE | Interest::WRITABLE).await?; + + if ready.is_readable() { + let mut data = + } } } diff --git a/src/protocol/messages.rs b/src/protocol/messages.rs new file mode 100644 index 0000000..71f261c --- /dev/null +++ b/src/protocol/messages.rs @@ -0,0 +1,6 @@ + + +pub struct JoinRequest { + hostname: String, + datetime: Date +} diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs new file mode 100644 index 0000000..ba63992 --- /dev/null +++ b/src/protocol/mod.rs @@ -0,0 +1 @@ +pub mod messages; diff --git a/src/utils/dual_channel.rs b/src/utils/dual_channel.rs new file mode 100644 index 0000000..d4606b8 --- /dev/null +++ b/src/utils/dual_channel.rs @@ -0,0 +1,25 @@ +use tokio::sync::oneshot::{ Sender, Receiver }; + +struct SingleChannel { + tx: Sender, + rx: Receiver +} + +impl Default for SingleChannel { + fn default() -> SingleChannel { + let (tx, rx) = oneshot::channel(); + + SingleChannel { tx, rx } + } +} + +struct DualChannel { + left: SingleChannel, + right: SingleChannel +} + +impl Default for DualChannel { + fn default() -> DualChannel { + DualChannel { left: SingleChannel::default(), right: SingleChannel::default() }; + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +