Initial MeshPort SDK and cross-platform client
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
use meshport::Network;
|
||||
use std::{net::SocketAddr, time::Duration};
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args: Vec<_> = std::env::args().collect();
|
||||
anyhow::ensure!(
|
||||
args.len() == 3,
|
||||
"usage: tcp_client <config.toml> <virtual-ip:port>"
|
||||
);
|
||||
let config = std::fs::read_to_string(&args[1])?;
|
||||
let target: SocketAddr = args[2].parse()?;
|
||||
let network = Network::start(&config).await?;
|
||||
let result = async {
|
||||
let mut stream = network.connect_tcp(target, Duration::from_secs(10)).await?;
|
||||
stream.write_all(b"MeshPort TCP example").await?;
|
||||
let mut buffer = [0; 256];
|
||||
let n = tokio::time::timeout(Duration::from_secs(5), stream.read(&mut buffer)).await??;
|
||||
println!("Received {n} bytes: {:?}", &buffer[..n]);
|
||||
Ok::<_, anyhow::Error>(())
|
||||
}
|
||||
.await;
|
||||
network.stop().await;
|
||||
result
|
||||
}
|
||||
Reference in New Issue
Block a user