optimizations and more deadlock fixes

This commit is contained in:
Toshit Chawda 2024-02-03 22:46:19 -08:00
parent be340c0f82
commit ac39d82a53
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
8 changed files with 253 additions and 49 deletions

View file

@ -0,0 +1,15 @@
[package]
name = "simple-wisp-client"
version = "0.1.0"
edition = "2021"
[dependencies]
bytes = "1.5.0"
fastwebsockets = { version = "0.6.0", features = ["unstable-split", "upgrade"] }
futures = "0.3.30"
http-body-util = "0.1.0"
hyper = { version = "1.1.0", features = ["http1", "client"] }
tokio = { version = "1.36.0", features = ["full"] }
tokio-native-tls = "0.3.1"
wisp-mux = { path = "../wisp", features = ["fastwebsockets"]}

View file

@ -0,0 +1,105 @@
use bytes::Bytes;
use fastwebsockets::{handshake, FragmentCollectorRead};
use futures::io::AsyncWriteExt;
use http_body_util::Empty;
use hyper::{
header::{CONNECTION, UPGRADE},
Request,
};
use std::{error::Error, future::Future};
use tokio::net::TcpStream;
use tokio_native_tls::{native_tls, TlsConnector};
use wisp_mux::{ClientMux, StreamType};
#[derive(Debug)]
struct StrError(String);
impl StrError {
pub fn new(str: &str) -> Self {
Self(str.to_string())
}
}
impl std::fmt::Display for StrError {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(fmt, "{}", self.0)
}
}
impl Error for StrError {}
struct SpawnExecutor;
impl<Fut> hyper::rt::Executor<Fut> for SpawnExecutor
where
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
fn execute(&self, fut: Fut) {
tokio::task::spawn(fut);
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let addr = std::env::args()
.nth(1)
.ok_or(StrError::new("no src addr"))?;
let addr_port: u16 = std::env::args()
.nth(2)
.ok_or(StrError::new("no src port"))?
.parse()?;
let addr_dest = std::env::args()
.nth(3)
.ok_or(StrError::new("no dest addr"))?;
let addr_dest_port: u16 = std::env::args()
.nth(4)
.ok_or(StrError::new("no dest port"))?
.parse()?;
let socket = TcpStream::connect(format!("{}:{}", &addr, addr_port)).await?;
let cx = TlsConnector::from(native_tls::TlsConnector::builder().build()?);
let socket = cx.connect(&addr, socket).await?;
let req = Request::builder()
.method("GET")
.uri(format!("wss://{}:{}/", &addr, addr_port))
.header("Host", &addr)
.header(UPGRADE, "websocket")
.header(CONNECTION, "upgrade")
.header(
"Sec-WebSocket-Key",
fastwebsockets::handshake::generate_key(),
)
.header("Sec-WebSocket-Version", "13")
.header("Sec-WebSocket-Protocol", "wisp-v1")
.body(Empty::<Bytes>::new())?;
let (ws, _) = handshake::client(&SpawnExecutor, req, socket).await?;
let (rx, tx) = ws.split(tokio::io::split);
let rx = FragmentCollectorRead::new(rx);
let (mux, fut) = ClientMux::new(rx, tx);
tokio::task::spawn(fut);
let mut hi: u64 = 0;
loop {
let mut channel = mux
.client_new_stream(StreamType::Tcp, addr_dest.clone(), addr_dest_port)
.await?
.into_io()
.into_asyncrw();
for _ in 0..10 {
channel.write_all(b"hiiiiiiii").await?;
hi += 1;
println!("said hi {}", hi);
}
}
#[allow(unreachable_code)]
Ok(())
}