set up communication for server thread
This commit is contained in:
parent
c48739d836
commit
4594b0f750
|
@ -1,38 +1,45 @@
|
||||||
use crate::chat::types::PackedMessage;
|
use crate::chat::types::PackedMessage;
|
||||||
use crate::fcpv2;
|
use crate::fcpv2;
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
use futures::{SinkExt, StreamExt};
|
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::sync::mpsc::{Receiver, Sender};
|
use std::sync::mpsc::{Receiver, Sender};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{self, AsyncReadExt, AsyncWriteExt},
|
io::{self, AsyncReadExt, AsyncWriteExt},
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpStream, tcp::{OwnedReadHalf, OwnedWriteHalf}}
|
||||||
};
|
};
|
||||||
|
|
||||||
type SP = Sender<PackedMessage>;
|
type SP = Sender<PackedMessage>;
|
||||||
type RP = Receiver<PackedMessage>;
|
type RP = Receiver<PackedMessage>;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn listen_server(client_sender: SP) -> io::Result<()> {
|
pub async fn listen_server(client_sender: SP, server_receiver: RP) -> io::Result<()> {
|
||||||
task::block_on(connect_to_server(client_sender))
|
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()
|
let addr = env::args()
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.unwrap_or_else(|| "127.0.0.1:9481".to_string());
|
.unwrap_or_else(|| "127.0.0.1:9481".to_string());
|
||||||
|
|
||||||
let stream = TcpStream::connect(&addr).await.expect("weeror here");
|
let stream = TcpStream::connect(&addr).await.expect("weeror here");
|
||||||
let (mut receiver, mut sender) = stream.into_split();
|
let (mut receiver, mut sender) = stream.into_split();
|
||||||
let _ = sender
|
let t = task::spawn(server_responce_getter(receiver, client_sender));
|
||||||
.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes())
|
to_server_sender(sender, server_receiver).await?;
|
||||||
.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 {
|
loop {
|
||||||
let mut buffer = [0; 512];
|
let mut buffer = [0; 512];
|
||||||
match receiver.read(&mut buffer).await {
|
match receiver.read(&mut buffer).await {
|
||||||
Ok(s) => {
|
Ok(_) => {
|
||||||
let received = String::from_utf8_lossy(&buffer[..]);
|
let received = String::from_utf8_lossy(&buffer[..]);
|
||||||
|
println!("received {}", received);
|
||||||
client_sender
|
client_sender
|
||||||
.send(PackedMessage {
|
.send(PackedMessage {
|
||||||
message: received.to_string(),
|
message: received.to_string(),
|
||||||
|
@ -42,29 +49,29 @@ async fn connect_to_server(client_sender: SP) -> io::Result<()> {
|
||||||
Err(e) => println!("Error: {} ", e),
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct FrontMsg {
|
struct FrontMsg {
|
||||||
userID: u32,
|
user_id: u32,
|
||||||
receiverID: u32,
|
receiver_id: u32,
|
||||||
message: String,
|
message: String,
|
||||||
time: 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(())
|
|
||||||
}
|
|
||||||
|
|
21
src/main.rs
21
src/main.rs
|
@ -4,10 +4,9 @@ mod encrypting;
|
||||||
mod fcpv2;
|
mod fcpv2;
|
||||||
use async_std::io;
|
use async_std::io;
|
||||||
use chat::front_conn::listen_client;
|
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 chat::types::PackedMessage;
|
||||||
|
|
||||||
use async_std::task;
|
|
||||||
use std::{
|
use std::{
|
||||||
sync::{
|
sync::{
|
||||||
mpsc,
|
mpsc,
|
||||||
|
@ -52,14 +51,14 @@ fn main() -> io::Result<()> {
|
||||||
let server_thread = thread::spawn(move || {
|
let server_thread = thread::spawn(move || {
|
||||||
let cs = client_sender;
|
let cs = client_sender;
|
||||||
let sr = server_receiver;
|
let sr = server_receiver;
|
||||||
let cs1 = cs.clone();
|
// let cs1 = cs.clone();
|
||||||
let cs2 = cs.clone();
|
// let cs2 = cs.clone();
|
||||||
|
|
||||||
let t1 = thread::spawn(move || listen_server(cs1));
|
let t1 = thread::spawn(move || listen_server(cs, sr));
|
||||||
let t2 = thread::spawn(move || responding_to_client(cs2, sr));
|
// let t2 = thread::spawn(move || responding_to_client(cs2, sr));
|
||||||
|
|
||||||
t1.join();
|
t1.join().expect("failed server thread").unwrap();
|
||||||
t2.join();
|
// t2.join();
|
||||||
// while let Ok(res) = sr.recv() {
|
// while let Ok(res) = sr.recv() {
|
||||||
// println!("From Server:\n {}", res.message);
|
// println!("From Server:\n {}", res.message);
|
||||||
// }
|
// }
|
||||||
|
@ -70,10 +69,10 @@ fn main() -> io::Result<()> {
|
||||||
|
|
||||||
let t1 = thread::spawn(move || listen_client(ss, cr));
|
let t1 = thread::spawn(move || listen_client(ss, cr));
|
||||||
|
|
||||||
t1.join();
|
t1.join().expect("failed client thread").unwrap();
|
||||||
});
|
});
|
||||||
server_thread.join();
|
server_thread.join().unwrap();
|
||||||
client_thread.join();
|
client_thread.join().unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue