diff --git a/src/chat/serv_conn.rs b/src/chat/serv_conn.rs index 661f0b8..bd3aab3 100644 --- a/src/chat/serv_conn.rs +++ b/src/chat/serv_conn.rs @@ -1,38 +1,45 @@ use crate::chat::types::PackedMessage; use crate::fcpv2; use async_std::task; -use futures::{SinkExt, StreamExt}; use serde_derive::Deserialize; use std::env; use std::sync::mpsc::{Receiver, Sender}; use tokio::{ io::{self, AsyncReadExt, AsyncWriteExt}, - net::{TcpListener, TcpStream}, + net::{TcpStream, tcp::{OwnedReadHalf, OwnedWriteHalf}} }; type SP = Sender; type RP = Receiver; #[tokio::main] -pub async fn listen_server(client_sender: SP) -> io::Result<()> { - task::block_on(connect_to_server(client_sender)) +pub async fn listen_server(client_sender: SP, server_receiver: RP) -> io::Result<()> { + task::block_on(connect_to_server(client_sender, server_receiver)) } -async fn connect_to_server(client_sender: SP) -> io::Result<()> { +async fn connect_to_server(client_sender: SP, server_receiver: RP) -> io::Result<()> { let addr = env::args() .nth(1) .unwrap_or_else(|| "127.0.0.1:9481".to_string()); let stream = TcpStream::connect(&addr).await.expect("weeror here"); let (mut receiver, mut sender) = stream.into_split(); - let _ = sender - .write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes()) - .await?; - loop { + let t = task::spawn(server_responce_getter(receiver, client_sender)); + to_server_sender(sender, server_receiver).await?; + match t.await { + Ok(_) => Ok(()), + Err(e) => Err(e) + } + +} +async fn server_responce_getter(mut receiver: OwnedReadHalf, client_sender: SP) -> io::Result<()>{ + println!("Running"); +loop { let mut buffer = [0; 512]; match receiver.read(&mut buffer).await { - Ok(s) => { + Ok(_) => { let received = String::from_utf8_lossy(&buffer[..]); + println!("received {}", received); client_sender .send(PackedMessage { message: received.to_string(), @@ -42,29 +49,29 @@ async fn connect_to_server(client_sender: SP) -> io::Result<()> { Err(e) => println!("Error: {} ", e), } } + //TODO HANDLE ERROR + Ok(()) +} +async fn to_server_sender(mut sender:OwnedWriteHalf, server_receiver: RP) -> io::Result<()>{ + + while let Ok(res) = server_receiver.recv(){ + + println!("{}", res.message); + println!("ALIVE\nALIVE\nALIVE\nALIVE\n"); + let _ = sender + .write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes()) + .await?; + println!("SENDED\nSENDED\nSENDED\nSENDED\n"); + } Ok(()) } #[derive(Deserialize, Debug)] struct FrontMsg { - userID: u32, - receiverID: u32, + user_id: u32, + receiver_id: u32, message: String, time: String, } -async fn accept_server(stream: TcpStream, client_sender: SP) -> io::Result<()> { - let addr = stream - .peer_addr() - .expect("connected streams should have a peer address"); - println!("Peer address: {}", addr); - Ok(()) -} - -pub fn responding_to_client(client_sender: SP, server_receiver: RP) -> io::Result<()> { - while let Ok(res) = server_receiver.recv() { - println!("From Client!:\n {}", res.message); - } - Ok(()) -} diff --git a/src/main.rs b/src/main.rs index 14e6dc1..6e1f7d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,9 @@ mod encrypting; mod fcpv2; use async_std::io; use chat::front_conn::listen_client; -use chat::serv_conn::{listen_server, responding_to_client}; +use chat::serv_conn::listen_server; use chat::types::PackedMessage; -use async_std::task; use std::{ sync::{ mpsc, @@ -52,14 +51,14 @@ fn main() -> io::Result<()> { let server_thread = thread::spawn(move || { let cs = client_sender; let sr = server_receiver; - let cs1 = cs.clone(); - let cs2 = cs.clone(); + // let cs1 = cs.clone(); + // let cs2 = cs.clone(); - let t1 = thread::spawn(move || listen_server(cs1)); - let t2 = thread::spawn(move || responding_to_client(cs2, sr)); + let t1 = thread::spawn(move || listen_server(cs, sr)); + // let t2 = thread::spawn(move || responding_to_client(cs2, sr)); - t1.join(); - t2.join(); + t1.join().expect("failed server thread").unwrap(); + // t2.join(); // while let Ok(res) = sr.recv() { // println!("From Server:\n {}", res.message); // } @@ -70,10 +69,10 @@ fn main() -> io::Result<()> { let t1 = thread::spawn(move || listen_client(ss, cr)); - t1.join(); + t1.join().expect("failed client thread").unwrap(); }); - server_thread.join(); - client_thread.join(); + server_thread.join().unwrap(); + client_thread.join().unwrap(); Ok(()) }