some tinkering
This commit is contained in:
parent
bbd79d2b00
commit
dc87e678b8
|
@ -1,6 +1,4 @@
|
|||
use crate::chat::types::PackedMessage;
|
||||
use crate::db;
|
||||
use crate::encrypting;
|
||||
use async_std::{
|
||||
io,
|
||||
net::{TcpListener, TcpStream},
|
||||
|
@ -72,8 +70,7 @@ async fn accept_client(stream: TcpStream, server_sender: SP) -> io::Result<()> {
|
|||
"userID": 123456789,
|
||||
"receiverID": 123456789,
|
||||
"message": "hey dude",
|
||||
"time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)",
|
||||
|
||||
"time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)"
|
||||
}
|
||||
*/
|
||||
sender
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
pub mod chat_api_base;
|
||||
pub mod front_conn;
|
||||
pub mod serv_conn;
|
||||
pub mod types;
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
use crate::chat::types::PackedMessage;
|
||||
use crate::fcpv2;
|
||||
use async_std::{
|
||||
io,
|
||||
net::{TcpListener, TcpStream},
|
||||
task,
|
||||
};
|
||||
use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use serde_derive::Deserialize;
|
||||
use std::env;
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
type SP = Sender<PackedMessage>;
|
||||
|
||||
pub fn listen_server(server_sender: SP) -> io::Result<()> {
|
||||
task::block_on(connect_to_server(server_sender))
|
||||
}
|
||||
|
||||
async fn connect_to_server(server_sender: SP) -> io::Result<()> {
|
||||
let addr = env::args()
|
||||
.nth(1)
|
||||
.unwrap_or_else(|| "127.0.0.1:9481".to_string());
|
||||
|
||||
println!("Thats All?!");
|
||||
let listener = TcpStream::connect(&addr).await?;
|
||||
println!("Listened ");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct FrontMsg {
|
||||
userID: u32,
|
||||
receiverID: u32,
|
||||
message: String,
|
||||
time: String,
|
||||
}
|
||||
|
||||
async fn accept_server(stream: TcpStream, server_sender: SP) -> io::Result<()> {
|
||||
let addr = stream
|
||||
.peer_addr()
|
||||
.expect("connected streams should have a peer address");
|
||||
println!("Peer address: {}", addr);
|
||||
Ok(())
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
pub mod basic;
|
||||
pub mod types;
|
3
src/fcpv2/mod.rs
Normal file
3
src/fcpv2/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
mod types;
|
||||
|
||||
pub use types::test;
|
|
@ -4,6 +4,7 @@ use std::net::{IpAddr, SocketAddr};
|
|||
use tokio::io::{self, AsyncBufRead, AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::net::TcpStream;
|
||||
#[derive(Debug)]
|
||||
struct Fcp {
|
||||
connected: bool,
|
||||
stream: TcpStream,
|
||||
|
@ -21,18 +22,16 @@ trait FcpConnection {
|
|||
|
||||
#[async_trait]
|
||||
impl FcpConnection for Fcp {
|
||||
async fn new(addr: &'static str, port: u16, name: String) -> Result<Box<Self>, Box<dyn Error>> {
|
||||
async fn new(addr: &'static str, port: u16, name: String) -> Result<Box<Fcp>, Box<dyn Error>> {
|
||||
let socket = SocketAddr::new(IpAddr::V4(addr.parse().unwrap()), port);
|
||||
|
||||
match TcpStream::connect(&socket).await {
|
||||
Ok(mut stream) => Ok(Box::new(Fcp {
|
||||
let stream = TcpStream::connect(&socket).await?;
|
||||
Ok(Box::new(Fcp {
|
||||
connected: true,
|
||||
stream: stream,
|
||||
addr: socket,
|
||||
name: name,
|
||||
})),
|
||||
Err(e) => Err(Box::new(e)),
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,6 +55,19 @@ impl FCP for Fcp {
|
|||
}
|
||||
}
|
||||
}
|
||||
pub async fn test() -> io::Result<()> {
|
||||
println!("WTFAAA");
|
||||
/*
|
||||
let fcp = Fcp::new("localhost", 9481, "lol".to_string())
|
||||
.await
|
||||
.unwrap();
|
||||
println!("{:?}", fcp);
|
||||
*/
|
||||
let stream = TcpStream::connect("127.0.0.1:9481").await?;
|
||||
println!("WTF");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/*
|
||||
// TODO add error if freenet not connected
|
|
@ -1,5 +1,5 @@
|
|||
pub mod encrypting;
|
||||
pub mod db;
|
||||
|
||||
mod chat;
|
||||
pub mod db;
|
||||
pub mod encrypting;
|
||||
pub mod fcpv2;
|
||||
pub use chat::front_conn;
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
mod chat;
|
||||
|
||||
mod db;
|
||||
mod encrypting;
|
||||
mod fcp;
|
||||
mod fcpv2;
|
||||
use async_std::io;
|
||||
use chat::front_conn::listen_client;
|
||||
//use chat::serv_conn::listen_server;
|
||||
use chat::types::PackedMessage;
|
||||
|
||||
use async_std::task;
|
||||
use std::{
|
||||
sync::{
|
||||
mpsc,
|
||||
|
@ -49,9 +50,10 @@ fn main() -> io::Result<()> {
|
|||
mpsc::channel();
|
||||
|
||||
let server_thread = thread::spawn(move || {
|
||||
let cr = client_sender;
|
||||
let cs = client_sender;
|
||||
let sr = server_receiver;
|
||||
|
||||
//thread::spawn(|| listen_server(cs));
|
||||
println!("Multithreadding YAY!!! {}", sr.recv().unwrap().message);
|
||||
});
|
||||
let client_thread = thread::spawn(move || {
|
||||
|
@ -59,6 +61,7 @@ fn main() -> io::Result<()> {
|
|||
let cr = client_receiver;
|
||||
|
||||
thread::spawn(|| listen_client(ss));
|
||||
// println!("From Server Yaaay {}", cr.recv().unwrap().message);
|
||||
});
|
||||
server_thread.join();
|
||||
client_thread.join();
|
||||
|
|
Loading…
Reference in a new issue