refactoring
cargo devel CI / cargo CI (push) Failing after 3s Details

This commit is contained in:
Christoph J. Scherr 2024-02-09 17:53:42 +01:00
parent 0242100fb5
commit 642dc1f558
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
1 changed files with 7 additions and 9 deletions

View File

@ -11,8 +11,7 @@ use tokio::{
net::TcpStream, net::TcpStream,
}; };
use tokio_rustls::{ use tokio_rustls::{
rustls::{self, pki_types}, rustls::{self, pki_types}, TlsConnector, TlsStream
TlsConnector,
}; };
use webpki_roots; use webpki_roots;
@ -20,7 +19,7 @@ const BUF_SIZE: usize = 512;
pub struct Client { pub struct Client {
cfg: Config, cfg: Config,
stream: TcpStream, stream: TlsStream<TcpStream>,
connector: TlsConnector, connector: TlsConnector,
domain: pki_types::ServerName<'static>, domain: pki_types::ServerName<'static>,
} }
@ -41,7 +40,6 @@ impl Client {
.with_root_certificates(root_cert_store) .with_root_certificates(root_cert_store)
.with_no_client_auth(); .with_no_client_auth();
let connector = TlsConnector::from(Arc::new(tls_config)); let connector = TlsConnector::from(Arc::new(tls_config));
let stream = TcpStream::connect(&cfg.addr).await?;
let domain = match pki_types::ServerName::try_from(cfg.hostname.clone()) { let domain = match pki_types::ServerName::try_from(cfg.hostname.clone()) {
Ok(domain) => domain, Ok(domain) => domain,
Err(err) => { Err(err) => {
@ -49,6 +47,7 @@ impl Client {
return Err(err.into()); return Err(err.into());
} }
}; };
let mut stream = connector.connect(domain, TcpStream::connect(&cfg.addr).await?).await?;
Ok(Client { Ok(Client {
cfg: cfg.clone(), cfg: cfg.clone(),
@ -58,18 +57,17 @@ impl Client {
}) })
} }
pub async fn run(self) -> anyhow::Result<()> { pub async fn run(mut self) -> anyhow::Result<()> {
let mut stream = self.connector.connect(self.domain, self.stream).await?;
let mut buf = [0; BUF_SIZE]; let mut buf = [0; BUF_SIZE];
stream.write_all(b"ping").await?; self.stream.write_all(b"ping").await?;
info!("> ({}) ping", self.cfg.hostname); info!("> ({}) ping", self.cfg.hostname);
while stream.read(&mut buf).await? > 0 { while self.stream.read(&mut buf).await? > 0 {
let response = decode(&buf)?; let response = decode(&buf)?;
info!("< ({}) {}", self.cfg.hostname, response); info!("< ({}) {}", self.cfg.hostname, response);
if response == "You win!" { if response == "You win!" {
break; break;
} }
stream.write_all(b"ping").await?; self.stream.write_all(b"ping").await?;
info!("> ({}) ping", self.cfg.hostname); info!("> ({}) ping", self.cfg.hostname);
// we should wait, so that we don't spam the client // we should wait, so that we don't spam the client
std::thread::sleep(self.cfg.delay); std::thread::sleep(self.cfg.delay);