Skip to content

Commit

Permalink
Merge pull request #98 from arkedge/add-tcp-exec
Browse files Browse the repository at this point in the history
  • Loading branch information
KOBA789 authored May 29, 2024
2 parents 8b49380 + 838a571 commit 6e86aec
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
"kble-socket",
"kble-c2a",
"kble-eb90",
"kble-tcp",
]

[workspace.dependencies]
Expand Down
25 changes: 25 additions & 0 deletions kble-tcp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "kble-tcp"
version.workspace = true
repository.workspace = true
license.workspace = true
edition.workspace = true
readme.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
notalawyer-build.workspace = true

[dependencies]
anyhow.workspace = true
futures.workspace = true
tokio = { workspace = true, features = ["full"] }
kble-socket = { workspace = true, features = ["stdio", "tungstenite"] }
tokio-util.workspace = true
bytes.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
clap.workspace = true
notalawyer.workspace = true
notalawyer-clap.workspace = true
55 changes: 55 additions & 0 deletions kble-tcp/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::net::SocketAddr;

use anyhow::Result;
use clap::Parser;
use futures::{SinkExt, StreamExt};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tracing_subscriber::{prelude::*, EnvFilter};

#[derive(Debug, Parser)]
#[clap(author, version, about, long_about = None)]
struct Args {
addr: SocketAddr,
}

#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(std::io::stderr),
)
.with(EnvFilter::from_default_env())
.init();
let args = Args::parse();

let tcp_stream = TcpStream::connect(args.addr).await?;
let (mut tcp_upstream, mut tcp_downstream) = tokio::io::split(tcp_stream);
let (mut tx, mut rx) = kble_socket::from_stdio().await;
let to_tcp = async {
while let Some(body) = rx.next().await {
let body = body?;
tcp_downstream.write_all(&body).await?;
}
anyhow::Ok(())
};
let from_tcp = async {
let mut buffer = [0; 8192];
loop {
match tcp_upstream.read(&mut buffer).await? {
0 => break,
n => {
tx.send(buffer[..n].to_vec().into()).await?;
}
}
}
anyhow::Ok(())
};

tokio::select! {
_ = to_tcp => Ok(()),
_ = from_tcp => Ok(())
}
}

0 comments on commit 6e86aec

Please sign in to comment.