This isn't working..... I think I've bitten off more than I can chew again with Rust :-(

This commit is contained in:
Starbeamrainbowlabs 2021-09-30 01:00:19 +01:00
parent 2e062fd78f
commit 12339aad90
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
6 changed files with 61 additions and 9 deletions

View file

@ -26,14 +26,10 @@ impl Agent {
loop { loop {
let (client, _) = agent.listener.accept().await.unwrap(); let (client, _) = agent.listener.accept().await.unwrap();
let peer = PeerConnection { let peer = PeerConnection::new(client);
stream: client
};
agent.peers.push(peer); agent.peers.push(peer);
tokio::spawn(async {
let peer_borrow = &peer;
peer_borrow.handle().await
});
} }
} }

View file

@ -1,11 +1,34 @@
use tokio::net::TcpStream; use tokio::net::TcpStream;
use tokio::io::Interest;
use tokio::sync::oneshot;
pub struct PeerConnection { pub struct PeerConnection {
stream: TcpStream send: oneshot::Sender<String>,
receive: oneshot::Receiver<String>
} }
impl PeerConnection { 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<String>,
stream_out: oneshot::Sender<String>) {
let ready = stream.ready(Interest::READABLE | Interest::WRITABLE).await?;
if ready.is_readable() {
let mut data =
}
} }
} }

6
src/protocol/messages.rs Normal file
View file

@ -0,0 +1,6 @@
pub struct JoinRequest {
hostname: String,
datetime: Date
}

1
src/protocol/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod messages;

25
src/utils/dual_channel.rs Normal file
View file

@ -0,0 +1,25 @@
use tokio::sync::oneshot::{ Sender, Receiver };
struct SingleChannel<T> {
tx: Sender<T>,
rx: Receiver<T>
}
impl Default for SingleChannel {
fn default() -> SingleChannel {
let (tx, rx) = oneshot::channel();
SingleChannel { tx, rx }
}
}
struct DualChannel<T> {
left: SingleChannel<T>,
right: SingleChannel<T>
}
impl Default for DualChannel {
fn default() -> DualChannel {
DualChannel { left: SingleChannel::default(), right: SingleChannel::default() };
}
}

1
src/utils/mod.rs Normal file
View file

@ -0,0 +1 @@