create 4 threads for communicating

This commit is contained in:
Horhik 2021-01-08 12:35:42 +02:00
parent f55ed0700a
commit 67b2c20959
6 changed files with 205 additions and 70 deletions

44
.idea/workspace.xml Normal file
View file

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="e434c6bf-d215-476e-8569-84080f63e0f9" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/Cargo.lock" beforeDir="false" afterPath="$PROJECT_DIR$/Cargo.lock" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Cargo.toml" beforeDir="false" afterPath="$PROJECT_DIR$/Cargo.toml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/chat/front_conn.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/chat/front_conn.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/chat/mod.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/chat/mod.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/chat/serv_conn.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/chat/serv_conn.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/fcp/basic.rs" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/fcp/mod.rs" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/fcp/types.rs" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/lib.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/lib.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/main.rs" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="1mg1VEtaMSKRg5zbRaD9vETlRRi" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="e434c6bf-d215-476e-8569-84080f63e0f9" name="Default Changelist" comment="" />
<created>1609900445316</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1609900445316</updated>
</task>
<servers />
</component>
</project>

175
Cargo.lock generated
View file

@ -94,7 +94,7 @@ dependencies = [
"memchr",
"num_cpus",
"once_cell",
"pin-project-lite",
"pin-project-lite 0.1.10",
"pin-utils",
"slab",
"wasm-bindgen-futures",
@ -155,7 +155,7 @@ dependencies = [
"log",
"native-tls",
"pin-project",
"tokio",
"tokio 0.2.22",
"tokio-native-tls",
"tungstenite",
]
@ -225,6 +225,12 @@ version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
[[package]]
name = "bytes"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0dcbc35f504eb6fc275a6d20e4ebcda18cf50d40ba6fabff8c711fa16cb3b16"
[[package]]
name = "cache-padded"
version = "1.1.1"
@ -243,6 +249,12 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "concurrent-queue"
version = "1.2.2"
@ -281,7 +293,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
dependencies = [
"autocfg",
"cfg-if",
"cfg-if 0.1.10",
"lazy_static",
]
@ -417,7 +429,7 @@ dependencies = [
"futures-io",
"memchr",
"parking",
"pin-project-lite",
"pin-project-lite 0.1.10",
"waker-fn",
]
@ -490,7 +502,7 @@ version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
"libc",
"wasi",
]
@ -533,7 +545,7 @@ dependencies = [
"serde",
"serde_derive",
"serde_json",
"tokio",
"tokio 0.3.6",
"toml",
"tungstenite",
]
@ -544,7 +556,7 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9"
dependencies = [
"bytes",
"bytes 0.5.6",
"fnv",
"itoa",
]
@ -572,7 +584,7 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19a8a95243d5a0398cae618ec29477c6e3cb631152be5c19481f80bc71559754"
dependencies = [
"bytes",
"bytes 0.5.6",
]
[[package]]
@ -581,7 +593,7 @@ version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63312a18f7ea8760cdd0a7c5aac1a619752a246b833545e3e36d1f81f7cd9e66"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
]
[[package]]
@ -655,13 +667,22 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
[[package]]
name = "lock_api"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
]
[[package]]
@ -691,7 +712,7 @@ version = "0.6.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
"fuchsia-zircon",
"fuchsia-zircon-sys",
"iovec",
@ -705,26 +726,16 @@ dependencies = [
]
[[package]]
name = "mio-named-pipes"
version = "0.1.7"
name = "mio"
version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656"
checksum = "e50ae3f04d169fcc9bde0b547d1c205219b7157e07ded9c5aff03e0637cb3ed7"
dependencies = [
"log",
"mio",
"miow 0.3.5",
"winapi 0.3.9",
]
[[package]]
name = "mio-uds"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0"
dependencies = [
"iovec",
"libc",
"mio",
"log",
"miow 0.3.6",
"ntapi",
"winapi 0.3.9",
]
[[package]]
@ -741,9 +752,9 @@ dependencies = [
[[package]]
name = "miow"
version = "0.3.5"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e"
checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897"
dependencies = [
"socket2",
"winapi 0.3.9",
@ -783,11 +794,20 @@ version = "0.2.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ebc3ec692ed7c9a255596c67808dee269f64655d8baf7b4f0638e51ba1d6853"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
"libc",
"winapi 0.3.9",
]
[[package]]
name = "ntapi"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "ntru"
version = "0.5.6"
@ -810,9 +830,9 @@ dependencies = [
[[package]]
name = "once_cell"
version = "1.4.1"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad"
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"
[[package]]
name = "opaque-debug"
@ -827,7 +847,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d575eff3665419f9b83678ff2815858ad9d11567e082f5ac1814baba4e2bcb4"
dependencies = [
"bitflags",
"cfg-if",
"cfg-if 0.1.10",
"foreign-types",
"lazy_static",
"libc",
@ -859,6 +879,31 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
[[package]]
name = "parking_lot"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb"
dependencies = [
"instant",
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ccb628cad4f84851442432c60ad8e1f607e29752d0bf072cbd0baf28aa34272"
dependencies = [
"cfg-if 1.0.0",
"instant",
"libc",
"redox_syscall",
"smallvec",
"winapi 0.3.9",
]
[[package]]
name = "percent-encoding"
version = "2.1.0"
@ -891,6 +936,12 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e555d9e657502182ac97b539fb3dae8b79cda19e3e4f8ffb5e8de4f18df93c95"
[[package]]
name = "pin-project-lite"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e36743d754ccdf9954c2e352ce2d4b106e024c814f6499c2dadff80da9a442d8"
[[package]]
name = "pin-utils"
version = "0.1.0"
@ -909,7 +960,7 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7215a098a80ab8ebd6349db593dc5faf741781bad0c4b7c5701fea6af548d52c"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
"libc",
"log",
"wepoll-sys",
@ -1067,6 +1118,12 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "sct"
version = "0.6.0"
@ -1135,7 +1192,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "170a36ea86c864a3f16dd2687712dd6646f7019f301e57537c7f4dc9f5916770"
dependencies = [
"block-buffer",
"cfg-if",
"cfg-if 0.1.10",
"cpuid-bool",
"digest",
"opaque-debug",
@ -1165,13 +1222,12 @@ checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252"
[[package]]
name = "socket2"
version = "0.3.15"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1fa70dc5c8104ec096f4fe7ede7a221d35ae13dcd19ba1ad9a81d2cab9a1c44"
checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e"
dependencies = [
"cfg-if",
"cfg-if 1.0.0",
"libc",
"redox_syscall",
"winapi 0.3.9",
]
@ -1198,7 +1254,7 @@ version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
"libc",
"rand",
"redox_syscall",
@ -1218,18 +1274,29 @@ version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd"
dependencies = [
"bytes",
"fnv",
"futures-core",
"bytes 0.5.6",
"iovec",
"lazy_static",
"mio 0.6.22",
"pin-project-lite 0.1.10",
]
[[package]]
name = "tokio"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "720ba21c25078711bf456d607987d95bce90f7c3bea5abe1db587862e7a1e87c"
dependencies = [
"autocfg",
"bytes 0.6.0",
"futures-core",
"libc",
"memchr",
"mio",
"mio-named-pipes",
"mio-uds",
"mio 0.7.7",
"num_cpus",
"pin-project-lite",
"once_cell",
"parking_lot",
"pin-project-lite 0.2.1",
"signal-hook-registry",
"slab",
"tokio-macros",
@ -1238,9 +1305,9 @@ dependencies = [
[[package]]
name = "tokio-macros"
version = "0.2.5"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389"
checksum = "46dfffa59fc3c8aad216ed61bdc2c263d2b9d87a9c8ac9de0c11a813e51b6db7"
dependencies = [
"proc-macro2",
"quote",
@ -1254,7 +1321,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd608593a919a8e05a7d1fc6df885e40f6a88d3a70a3a7eff23ff27964eda069"
dependencies = [
"native-tls",
"tokio",
"tokio 0.2.22",
]
[[package]]
@ -1274,7 +1341,7 @@ checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23"
dependencies = [
"base64",
"byteorder",
"bytes",
"bytes 0.5.6",
"http",
"httparse",
"input_buffer",
@ -1375,7 +1442,7 @@ version = "0.2.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
"wasm-bindgen-macro",
]
@ -1400,7 +1467,7 @@ version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
"js-sys",
"wasm-bindgen",
"web-sys",

View file

@ -11,7 +11,7 @@ ntru = "0.5.6"
rusqlite = "0.24.0"
toml = "0.5.6"
tungstenite = "0.11.1"
tokio = { version = "0.2", features = ["full"] }
tokio = { version = "0.3.3", features = ["full"] }
log = "0.4.11"
futures = "0.3.6"
async-std = "1.6.5"

View file

@ -8,9 +8,10 @@ use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
use futures::{SinkExt, StreamExt};
use serde_derive::Deserialize;
use std::env;
use std::sync::mpsc::Sender;
use std::sync::mpsc::{Receiver, Sender};
type SP = Sender<PackedMessage>;
type RP = Receiver<PackedMessage>;
pub fn listen_client(server_sender: SP) -> io::Result<()> {
task::block_on(connect_to_client(server_sender))
@ -23,12 +24,10 @@ async fn connect_to_client(server_sender: SP) -> io::Result<()> {
let listener = TcpListener::bind(&addr).await?;
println!("Debugging!");
while let Ok((stream, _)) = listener.accept().await {
let ss = server_sender.clone();
task::spawn(accept_client(stream, ss));
}
println!("Debugging 2!");
Ok(())
}
@ -92,3 +91,10 @@ async fn accept_client(stream: TcpStream, server_sender: SP) -> io::Result<()> {
Ok(())
}
pub fn responding_to_server(server_sender: SP, client_receiver: RP) -> io::Result<()> {
while let Ok(res) = client_receiver.recv() {
println!("From SERVER!:\n {}", res.message);
}
Ok(())
}

View file

@ -4,13 +4,14 @@ use async_std::task;
use futures::{SinkExt, StreamExt};
use serde_derive::Deserialize;
use std::env;
use std::sync::mpsc::Sender;
use std::sync::mpsc::{Receiver, Sender};
use tokio::{
io::{self, AsyncReadExt, AsyncWriteExt},
net::{TcpListener, TcpStream},
};
type SP = Sender<PackedMessage>;
type RP = Receiver<PackedMessage>;
#[tokio::main]
pub async fn listen_server(client_sender: SP) -> io::Result<()> {
@ -22,11 +23,8 @@ async fn connect_to_server(client_sender: SP) -> io::Result<()> {
.nth(1)
.unwrap_or_else(|| "127.0.0.1:9481".to_string());
println!("Thats All?!");
let stream = TcpStream::connect(&addr).await.expect("weeror here");
println!("stream created");
let (mut receiver, mut sender) = stream.into_split();
println!("FSFDSFD");
let _ = sender
.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes())
.await?;
@ -63,3 +61,10 @@ async fn accept_server(stream: TcpStream, client_sender: SP) -> io::Result<()> {
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 SERVER!:\n {}", res.message);
}
Ok(())
}

View file

@ -3,8 +3,8 @@ mod db;
mod encrypting;
mod fcpv2;
use async_std::io;
use chat::front_conn::listen_client;
use chat::serv_conn::listen_server;
use chat::front_conn::{listen_client, responding_to_server};
use chat::serv_conn::{listen_server, responding_to_client};
use chat::types::PackedMessage;
use async_std::task;
@ -44,7 +44,7 @@ use std::{
*/
fn main() -> io::Result<()> {
let (server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel();
let (client_sender, client_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel();
@ -52,16 +52,29 @@ 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();
thread::spawn(|| listen_server(cs));
println!("Multithreadding YAY!!! {}", sr.recv().unwrap().message);
let t1 = thread::spawn(move || listen_server(cs1));
let t2 = thread::spawn(move || responding_to_client(cs2, sr));
t1.join();
t2.join();
// while let Ok(res) = sr.recv() {
// println!("From Server:\n {}", res.message);
// }
});
let client_thread = thread::spawn(move || {
let ss = server_sender;
let ss = to_server_sender;
let cr = client_receiver;
let ss1 = ss.clone();
let ss2 = ss.clone();
thread::spawn(|| listen_client(ss));
println!("From Server Yaaay {}", cr.recv().unwrap().message);
let t1 = thread::spawn(move || listen_client(ss1.clone()));
let t2 = thread::spawn(move || responding_to_server(ss2.clone(), cr));
t1.join();
t2.join();
});
server_thread.join();
client_thread.join();