some tinkering

This commit is contained in:
Horhik 2021-01-06 06:18:46 +02:00
parent bbd79d2b00
commit dc87e678b8
9 changed files with 82 additions and 22 deletions

View file

@ -1,6 +1,4 @@
use crate::chat::types::PackedMessage; use crate::chat::types::PackedMessage;
use crate::db;
use crate::encrypting;
use async_std::{ use async_std::{
io, io,
net::{TcpListener, TcpStream}, net::{TcpListener, TcpStream},
@ -72,8 +70,7 @@ async fn accept_client(stream: TcpStream, server_sender: SP) -> io::Result<()> {
"userID": 123456789, "userID": 123456789,
"receiverID": 123456789, "receiverID": 123456789,
"message": "hey dude", "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 sender

View file

@ -1,3 +1,4 @@
pub mod chat_api_base; pub mod chat_api_base;
pub mod front_conn; pub mod front_conn;
pub mod serv_conn;
pub mod types; pub mod types;

View file

@ -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(())
}

View file

@ -1,2 +0,0 @@
pub mod basic;
pub mod types;

3
src/fcpv2/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod types;
pub use types::test;

View file

@ -4,6 +4,7 @@ use std::net::{IpAddr, SocketAddr};
use tokio::io::{self, AsyncBufRead, AsyncReadExt, AsyncWriteExt}; use tokio::io::{self, AsyncBufRead, AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio::net::TcpStream; use tokio::net::TcpStream;
#[derive(Debug)]
struct Fcp { struct Fcp {
connected: bool, connected: bool,
stream: TcpStream, stream: TcpStream,
@ -21,18 +22,16 @@ trait FcpConnection {
#[async_trait] #[async_trait]
impl FcpConnection for Fcp { 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); let socket = SocketAddr::new(IpAddr::V4(addr.parse().unwrap()), port);
match TcpStream::connect(&socket).await { let stream = TcpStream::connect(&socket).await?;
Ok(mut stream) => Ok(Box::new(Fcp { Ok(Box::new(Fcp {
connected: true, connected: true,
stream: stream, stream: stream,
addr: socket, addr: socket,
name: name, 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 // TODO add error if freenet not connected

View file

@ -1,5 +1,5 @@
pub mod encrypting;
pub mod db;
mod chat; mod chat;
pub mod db;
pub mod encrypting;
pub mod fcpv2;
pub use chat::front_conn; pub use chat::front_conn;

View file

@ -1,12 +1,13 @@
mod chat; mod chat;
mod db; mod db;
mod encrypting; mod encrypting;
mod fcp; 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;
use chat::types::PackedMessage; use chat::types::PackedMessage;
use async_std::task;
use std::{ use std::{
sync::{ sync::{
mpsc, mpsc,
@ -49,9 +50,10 @@ fn main() -> io::Result<()> {
mpsc::channel(); mpsc::channel();
let server_thread = thread::spawn(move || { let server_thread = thread::spawn(move || {
let cr = client_sender; let cs = client_sender;
let sr = server_receiver; let sr = server_receiver;
//thread::spawn(|| listen_server(cs));
println!("Multithreadding YAY!!! {}", sr.recv().unwrap().message); println!("Multithreadding YAY!!! {}", sr.recv().unwrap().message);
}); });
let client_thread = thread::spawn(move || { let client_thread = thread::spawn(move || {
@ -59,6 +61,7 @@ fn main() -> io::Result<()> {
let cr = client_receiver; let cr = client_receiver;
thread::spawn(|| listen_client(ss)); thread::spawn(|| listen_client(ss));
// println!("From Server Yaaay {}", cr.recv().unwrap().message);
}); });
server_thread.join(); server_thread.join();
client_thread.join(); client_thread.join();