try to split front conn by two threads

This commit is contained in:
Horhik 2021-01-08 21:30:52 +02:00
parent 871286221b
commit c9c79a4bc6
4 changed files with 280 additions and 195 deletions

377
Cargo.lock generated
View file

@ -61,7 +61,7 @@ dependencies = [
"polling",
"vec-arena",
"waker-fn",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -106,18 +106,6 @@ version = "4.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ab27c1aa62945039e44edaeee1dc23c74cc0c303dd5fe0fb462a184f1c3a518"
[[package]]
name = "async-tls"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7e7fbc0843fc5ad3d5ca889c5b2bea9130984d34cd0e62db57ab70c2529a8e3"
dependencies = [
"futures",
"rustls",
"webpki",
"webpki-roots",
]
[[package]]
name = "async-tls"
version = "0.10.0"
@ -126,9 +114,22 @@ checksum = "d85a97c4a0ecce878efd3f945f119c78a646d8975340bca0398f9bb05c30cc52"
dependencies = [
"futures-core",
"futures-io",
"rustls",
"rustls 0.18.1",
"webpki",
"webpki-roots",
"webpki-roots 0.20.0",
]
[[package]]
name = "async-tls"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f23d769dbf1838d5df5156e7b1ad404f4c463d1ac2c6aeb6cd943630f8a8400"
dependencies = [
"futures-core",
"futures-io",
"rustls 0.19.0",
"webpki",
"webpki-roots 0.21.0",
]
[[package]]
@ -144,20 +145,20 @@ dependencies = [
[[package]]
name = "async-tungstenite"
version = "0.8.0"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5c45a0dd44b7e6533ac4e7acc38ead1a3b39885f5bbb738140d30ea528abc7c"
checksum = "f7cc5408453d37e2b1c6f01d8078af1da58b6cfa6a80fa2ede3bd2b9a6ada9c4"
dependencies = [
"async-std",
"async-tls 0.9.0",
"async-tls 0.11.0",
"futures-io",
"futures-util",
"log",
"native-tls",
"pin-project",
"tokio 0.2.22",
"pin-project 1.0.3",
"tokio 1.0.1",
"tokio-native-tls",
"tungstenite",
"tungstenite 0.11.1",
]
[[package]]
@ -178,6 +179,12 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "bitflags"
version = "1.2.1"
@ -231,6 +238,12 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0dcbc35f504eb6fc275a6d20e4ebcda18cf50d40ba6fabff8c711fa16cb3b16"
[[package]]
name = "bytes"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad1f8e949d755f9d79112b5bb46938e0ef9d3804a0b16dfab13aafcaa5f0fa72"
[[package]]
name = "cache-padded"
version = "1.1.1"
@ -354,22 +367,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
dependencies = [
"bitflags",
"fuchsia-zircon-sys",
]
[[package]]
name = "fuchsia-zircon-sys"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
[[package]]
name = "futures"
version = "0.3.6"
@ -473,7 +470,7 @@ dependencies = [
"futures-sink",
"futures-task",
"memchr",
"pin-project",
"pin-project 0.4.26",
"pin-utils",
"proc-macro-hack",
"proc-macro-nested",
@ -504,7 +501,18 @@ checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
dependencies = [
"cfg-if 0.1.10",
"libc",
"wasi",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4060f4657be78b8e766215b02b18a2e862d83745545de804638e2b545e81aee6"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
]
[[package]]
@ -547,7 +555,7 @@ dependencies = [
"serde_json",
"tokio 0.3.6",
"toml",
"tungstenite",
"tungstenite 0.12.0",
]
[[package]]
@ -587,6 +595,15 @@ dependencies = [
"bytes 0.5.6",
]
[[package]]
name = "input_buffer"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f97967975f448f1a7ddb12b0bc41069d09ed6a1c161a92687e057325db35d413"
dependencies = [
"bytes 1.0.0",
]
[[package]]
name = "instant"
version = "0.1.7"
@ -596,15 +613,6 @@ dependencies = [
"cfg-if 0.1.10",
]
[[package]]
name = "iovec"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
dependencies = [
"libc",
]
[[package]]
name = "itoa"
version = "0.4.6"
@ -620,16 +628,6 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
dependencies = [
"winapi 0.2.8",
"winapi-build",
]
[[package]]
name = "kv-log-macro"
version = "1.0.7"
@ -706,25 +704,6 @@ version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "mio"
version = "0.6.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430"
dependencies = [
"cfg-if 0.1.10",
"fuchsia-zircon",
"fuchsia-zircon-sys",
"iovec",
"kernel32-sys",
"libc",
"log",
"miow 0.2.1",
"net2",
"slab",
"winapi 0.2.8",
]
[[package]]
name = "mio"
version = "0.7.7"
@ -733,21 +712,9 @@ checksum = "e50ae3f04d169fcc9bde0b547d1c205219b7157e07ded9c5aff03e0637cb3ed7"
dependencies = [
"libc",
"log",
"miow 0.3.6",
"miow",
"ntapi",
"winapi 0.3.9",
]
[[package]]
name = "miow"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919"
dependencies = [
"kernel32-sys",
"net2",
"winapi 0.2.8",
"ws2_32-sys",
"winapi",
]
[[package]]
@ -757,7 +724,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897"
dependencies = [
"socket2",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -785,18 +752,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8123a81538e457d44b933a02faf885d3fe8408806b23fa700e8f01c6c3a98998"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]]
name = "net2"
version = "0.2.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ebc3ec692ed7c9a255596c67808dee269f64655d8baf7b4f0638e51ba1d6853"
dependencies = [
"cfg-if 0.1.10",
"libc",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -805,7 +761,7 @@ version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44"
dependencies = [
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -901,7 +857,7 @@ dependencies = [
"libc",
"redox_syscall",
"smallvec",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -916,7 +872,16 @@ version = "0.4.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13fbdfd6bdee3dc9be46452f86af4a4072975899cf8592466668620bebfbcc17"
dependencies = [
"pin-project-internal",
"pin-project-internal 0.4.26",
]
[[package]]
name = "pin-project"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a83804639aad6ba65345661744708855f9fbcb71176ea8d28d05aeb11d975e7"
dependencies = [
"pin-project-internal 1.0.3",
]
[[package]]
@ -930,6 +895,17 @@ dependencies = [
"syn",
]
[[package]]
name = "pin-project-internal"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7bcc46b8f73443d15bc1c5fecbb315718491fa9187fa483f0e359323cde8b3a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pin-project-lite"
version = "0.1.10"
@ -964,7 +940,7 @@ dependencies = [
"libc",
"log",
"wepoll-sys",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1009,11 +985,23 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"getrandom 0.1.15",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc 0.2.0",
]
[[package]]
name = "rand"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c24fcd450d3fa2b592732565aa4f17a27a61c65ece4726353e000939b0edee34"
dependencies = [
"libc",
"rand_chacha 0.3.0",
"rand_core 0.6.1",
"rand_hc 0.3.0",
]
[[package]]
@ -1023,7 +1011,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core 0.6.1",
]
[[package]]
@ -1032,7 +1030,16 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
"getrandom 0.1.15",
]
[[package]]
name = "rand_core"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5"
dependencies = [
"getrandom 0.2.1",
]
[[package]]
@ -1041,7 +1048,16 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core 0.6.1",
]
[[package]]
@ -1056,7 +1072,7 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1071,7 +1087,7 @@ dependencies = [
"spin",
"untrusted",
"web-sys",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1095,7 +1111,20 @@ version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d1126dcf58e93cee7d098dbda643b5f92ed724f1f6a63007c1116eed6700c81"
dependencies = [
"base64",
"base64 0.12.3",
"log",
"ring",
"sct",
"webpki",
]
[[package]]
name = "rustls"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "064fd21ff87c6e87ed4506e68beb42459caa4a0e2eb144932e6776768556980b"
dependencies = [
"base64 0.13.0",
"log",
"ring",
"sct",
@ -1115,7 +1144,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75"
dependencies = [
"lazy_static",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1228,7 +1257,7 @@ checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e"
dependencies = [
"cfg-if 1.0.0",
"libc",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1239,9 +1268,9 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "syn"
version = "1.0.42"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c51d92969d209b54a98397e1b91c8ae82d8c87a7bb87df0b29aa2ad81454228"
checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5"
dependencies = [
"proc-macro2",
"quote",
@ -1256,10 +1285,10 @@ checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
dependencies = [
"cfg-if 0.1.10",
"libc",
"rand",
"rand 0.7.3",
"redox_syscall",
"remove_dir_all",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1268,19 +1297,6 @@ version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "238ce071d267c5710f9d31451efec16c5ee22de34df17cc05e56cbc92e967117"
[[package]]
name = "tokio"
version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd"
dependencies = [
"bytes 0.5.6",
"iovec",
"lazy_static",
"mio 0.6.22",
"pin-project-lite 0.1.10",
]
[[package]]
name = "tokio"
version = "0.3.6"
@ -1292,7 +1308,7 @@ dependencies = [
"futures-core",
"libc",
"memchr",
"mio 0.7.7",
"mio",
"num_cpus",
"once_cell",
"parking_lot",
@ -1300,7 +1316,19 @@ dependencies = [
"signal-hook-registry",
"slab",
"tokio-macros",
"winapi 0.3.9",
"winapi",
]
[[package]]
name = "tokio"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d258221f566b6c803c7b4714abadc080172b272090cdc5e244a6d4dd13c3a6bd"
dependencies = [
"autocfg",
"libc",
"mio",
"pin-project-lite 0.2.1",
]
[[package]]
@ -1316,12 +1344,12 @@ dependencies = [
[[package]]
name = "tokio-native-tls"
version = "0.1.0"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd608593a919a8e05a7d1fc6df885e40f6a88d3a70a3a7eff23ff27964eda069"
checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b"
dependencies = [
"native-tls",
"tokio 0.2.22",
"tokio 1.0.1",
]
[[package]]
@ -1339,15 +1367,35 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23"
dependencies = [
"base64",
"base64 0.12.3",
"byteorder",
"bytes 0.5.6",
"http",
"httparse",
"input_buffer",
"input_buffer 0.3.1",
"log",
"native-tls",
"rand",
"rand 0.7.3",
"sha-1",
"url",
"utf-8",
]
[[package]]
name = "tungstenite"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ada8297e8d70872fa9a551d93250a9f407beb9f37ef86494eb20012a2ff7c24"
dependencies = [
"base64 0.13.0",
"byteorder",
"bytes 1.0.0",
"http",
"httparse",
"input_buffer 0.4.0",
"log",
"native-tls",
"rand 0.8.1",
"sha-1",
"url",
"utf-8",
@ -1436,6 +1484,12 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "wasm-bindgen"
version = "0.2.68"
@ -1531,6 +1585,15 @@ dependencies = [
"webpki",
]
[[package]]
name = "webpki-roots"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82015b7e0b8bad8185994674a13a93306bea76cf5a16c5a181382fd3a5ec2376"
dependencies = [
"webpki",
]
[[package]]
name = "wepoll-sys"
version = "3.0.0"
@ -1540,12 +1603,6 @@ dependencies = [
"cc",
]
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
[[package]]
name = "winapi"
version = "0.3.9"
@ -1556,12 +1613,6 @@ dependencies = [
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
@ -1573,13 +1624,3 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "ws2_32-sys"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
dependencies = [
"winapi 0.2.8",
"winapi-build",
]

View file

@ -10,7 +10,7 @@ edition = "2018"
ntru = "0.5.6"
rusqlite = "0.24.0"
toml = "0.5.6"
tungstenite = "0.11.1"
tungstenite = "0.12.0"
tokio = { version = "0.3.3", features = ["full"] }
log = "0.4.11"
futures = "0.3.6"
@ -23,5 +23,5 @@ serde_derive = "1.0.116"
async-trait = "0.1.42"
[dependencies.async-tungstenite]
version = "0.8.0"
version = "0.11.0"
features = ["tokio-runtime", "tokio-native-tls", "async-tls", "async-std"]

View file

@ -4,7 +4,10 @@ use async_std::{
net::{TcpListener, TcpStream},
task,
};
use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
use async_tungstenite::{
accept_async_with_config, tungstenite::protocol::WebSocketConfig, tungstenite::Message,
WebSocketStream,
};
use futures::{SinkExt, StreamExt};
use serde_derive::Deserialize;
use std::env;
@ -13,25 +16,36 @@ 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))
pub fn listen_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()> {
task::block_on(connect_to_client(server_sender, to_client_receiver))
}
async fn connect_to_client(server_sender: SP) -> io::Result<()> {
async fn connect_to_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()> {
let addr = env::args()
.nth(1)
.unwrap_or_else(|| "127.0.0.1:5948".to_string());
let listener = TcpListener::bind(&addr).await?;
while let Ok((stream, _)) = listener.accept().await {
if let Ok((stream, _)) = listener.accept().await {
let ss = server_sender.clone();
task::spawn(accept_client(stream, ss));
let srm = stream.clone();
let t1 = task::spawn(accept_client(srm, ss, to_client_receiver));
// let t2 = task::spawn(respond_to_client(stream, to_client_receiver));
t1.await;
// t2.await;
}
Ok(())
}
async fn respond_to_client(stream: TcpStream, to_client_receiver: RP) -> io::Result<()> {
while let Ok(res) = to_client_receiver.recv() {
println!("From Server!:\n {}", res.message);
}
Ok(())
}
#[derive(Deserialize, Debug)]
struct FrontMsg {
userID: u32,
@ -40,19 +54,60 @@ struct FrontMsg {
time: String,
}
async fn accept_client(stream: TcpStream, server_sender: SP) -> io::Result<()> {
async fn accept_client(
stream: TcpStream,
server_sender: SP,
to_client_receiver: RP,
) -> io::Result<()> {
let addr = stream
.peer_addr()
.expect("connected streams should have a peer address");
println!("Peer address: {}", addr);
let ws = accept_async(stream)
let cfg = WebSocketConfig {
max_send_queue: None,
max_message_size: Some(67108864),
max_frame_size: Some(16777216),
};
// let ws = accept_async_with_config(stream, Some(cfg.clone()))
// .await
// .expect("err during the ws handshake");
// println!("connected to: {}", addr);
// let wsr = ws.get_ref().to_owned();
// let wsr2 = ws.get_ref().to_owned();
Ok(())
}
async fn connection_for_receiving(
wsr: TcpStream,
to_client_receiver: RP,
cfg: WebSocketConfig,
) -> io::Result<()> {
let (sc, _) = (accept_async_with_config(wsr, Some(cfg))
.await
.expect("err during the ws handshake");
.expect("error during the clone handshake"))
.split()
.unwrap();
println!("connected to: {}", addr);
Ok(())
}
let (mut sender, mut receiver) = ws.split();
async fn connection_for_sending(
ws: TcpStream,
server_sender: SP,
cfg: WebSocketConfig,
) -> io::Result<()> {
let (mut sender, mut receiver) = (accept_async_with_config(ws, Some(cfg))
.await
.expect("error during the clone handshake"))
.split();
/*
while let Ok(res) = to_client_receiver.recv() {
//sc.send(Message::Text(res.message.to_string()));
}
*/
let mut new_msg = receiver.next();
loop {
match new_msg.await {
@ -91,10 +146,3 @@ 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

@ -3,7 +3,7 @@ mod db;
mod encrypting;
mod fcpv2;
use async_std::io;
use chat::front_conn::{listen_client, responding_to_server};
use chat::front_conn::listen_client;
use chat::serv_conn::{listen_server, responding_to_client};
use chat::types::PackedMessage;
@ -33,7 +33,7 @@ use std::{
| | | | +----------------------+ | | +-----------------------------+ | | | |
| | | | | | | | | | | | | |
| | | | | Encode | | | | | | | | |
| <----------------------------- <----------------------------- from JSON <-------------------------------- |
| | | | | <----------------------------- from JSON <-------------------------------- |
| | | | | | | | | | | | | |
| | | | +----------------------+ | | +-----------------------------+ | | | |
+--------------+ | | | | | | | |
@ -67,14 +67,10 @@ fn main() -> io::Result<()> {
let client_thread = thread::spawn(move || {
let ss = to_server_sender;
let cr = client_receiver;
let ss1 = ss.clone();
let ss2 = ss.clone();
let t1 = thread::spawn(move || listen_client(ss1.clone()));
let t2 = thread::spawn(move || responding_to_server(ss2.clone(), cr));
let t1 = thread::spawn(move || listen_client(ss, cr));
t1.join();
t2.join();
});
server_thread.join();
client_thread.join();