esp-hal/extras/bench-server/src/main.rs
Dániel Buga 8e6411bd31
Random cleanups in non-checked packages (#2034)
* Deduplicate feature check macros

* Re-enable rust-analyzer for most of the workspace

* Cargo fix

* Turn off defmt

* Only build xtask

* Clippy pls

* Fix CI

* Fix paths

* Always create doc directory first

* Revert r-a

* Update esp-hal-procmacros/src/lp_core.rs

Co-authored-by: Dominic Fischer <14130965+Dominaezzz@users.noreply.github.com>

---------

Co-authored-by: Dominic Fischer <14130965+Dominaezzz@users.noreply.github.com>
2024-08-30 14:45:54 +00:00

102 lines
2.4 KiB
Rust

use std::{
io::{Read, Write},
net::{TcpListener, TcpStream},
thread::spawn,
time::Duration,
};
use log::info;
fn main() {
pretty_env_logger::init();
spawn(|| rx_listen());
spawn(|| rxtx_listen());
tx_listen();
}
fn tx_listen() {
let listener = TcpListener::bind("0.0.0.0:4321").unwrap();
loop {
let (socket, addr) = listener.accept().unwrap();
info!("tx: received connection from: {}", addr);
spawn(|| tx_conn(socket));
}
}
fn tx_conn(mut socket: TcpStream) {
socket
.set_read_timeout(Some(Duration::from_secs(30)))
.unwrap();
socket
.set_write_timeout(Some(Duration::from_secs(30)))
.unwrap();
let buf = [0; 1024];
loop {
if let Err(e) = socket.write_all(&buf) {
info!("tx: failed to write to socket; err = {:?}", e);
return;
}
}
}
fn rx_listen() {
let listener = TcpListener::bind("0.0.0.0:4322").unwrap();
loop {
let (socket, addr) = listener.accept().unwrap();
info!("rx: received connection from: {}", addr);
spawn(|| rx_conn(socket));
}
}
fn rx_conn(mut socket: TcpStream) {
socket
.set_read_timeout(Some(Duration::from_secs(30)))
.unwrap();
socket
.set_write_timeout(Some(Duration::from_secs(30)))
.unwrap();
let mut buf = [0; 1024];
loop {
if let Err(e) = socket.read_exact(&mut buf) {
info!("rx: failed to read from socket; err = {:?}", e);
return;
}
}
}
fn rxtx_listen() {
let listener = TcpListener::bind("0.0.0.0:4323").unwrap();
loop {
let (socket, addr) = listener.accept().unwrap();
info!("rxtx: received connection from: {}", addr);
spawn(|| rxtx_conn(socket));
}
}
fn rxtx_conn(mut socket: TcpStream) {
socket
.set_read_timeout(Some(Duration::from_secs(30)))
.unwrap();
socket
.set_write_timeout(Some(Duration::from_secs(30)))
.unwrap();
let mut buf = [0; 1024];
loop {
match socket.read(&mut buf) {
Ok(n) => {
if let Err(e) = socket.write_all(&buf[..n]) {
info!("rxtx: failed to write to socket; err = {:?}", e);
return;
}
}
Err(e) => {
info!("rxtx: failed to read from socket; err = {:?}", e);
return;
}
}
}
}