Good day folks,
I have a Tcp listener in an async function in lib.rs. I am calling this function from main.rs. However the function is never actually called. I have tried adding await to the call, but then main() needs to be async, but that is not allowed. What am I missing to make this call happen()? Thanks all.
main.rs
use std::error::Error;
use label_printer::Config;
use std::env;
use std::process;
fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().collect();
let config = Config::new(&args).unwrap_or_else(|err| {
println!("Problem parsing arguments: {}", err);
process::exit(1);
});
match label_printer::start_pos_listener(config) {
Ok(x) => x,
Err(_) => println!("Listener failure"),
};
Ok(())
}
lib.rs
use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use std::error::Error;
pub async fn start_pos_listener(config:Config) -> Result<(), Box<dyn Error>> {
let mut pos = String::new();
pos = pos + &config.pos_ip.trim() + ":" + &config.pos_port.trim();
println!("{}", pos);
let listener = TcpListener::bind(pos).await?;
loop {
let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move {
let mut buf = [0; 1024];
// In a loop, read data from the socket and write the data back.
loop {
let n = match socket.read(&mut buf).await {
// socket closed
Ok(n) if n == 0 => return,
Ok(n) => n,
Err(e) => {
eprintln!("failed to read from socket; err = {:?}", e);
return;
}
};
// Write the data back
if let Err(e) = socket.write_all(&buf[0..n]).await {
eprintln!("failed to write to socket; err = {:?}", e);
return;
}
}
});
}