Compare commits

4 Commits

Author SHA1 Message Date
15672536f6 fix c string formatting 2023-06-10 20:18:52 +02:00
b5d2a63909 fix start without exisiting cache 2023-06-09 20:23:33 +02:00
904091c455 remove unnecessary .ok() 2023-06-09 15:43:53 +02:00
02f44cfab6 continue if accept fails 2023-06-09 15:43:01 +02:00
3 changed files with 34 additions and 29 deletions

View File

@@ -350,7 +350,11 @@ async fn tokio_main(config: Arc<Config>) -> eyre::Result<()> {
"centralex server listening" "centralex server listening"
); );
while let Ok((stream, addr)) = listener.accept().await { loop {
let connection = listener.accept().await;
match connection {
Ok((stream, addr)) => {
info!(%addr, "new connection"); info!(%addr, "new connection");
spawn( spawn(
@@ -358,8 +362,11 @@ async fn tokio_main(config: Arc<Config>) -> eyre::Result<()> {
connection_handler(stream, addr, config.clone(), port_handler.clone()), connection_handler(stream, addr, config.clone(), port_handler.clone()),
); );
} }
Err(err) => {
Ok(()) error!(%err, "failed to accept connection");
}
}
}
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]

View File

@@ -1,4 +1,4 @@
use std::fmt::Debug; use std::{ffi::CStr, fmt::Debug};
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use eyre::eyre; use eyre::eyre;
@@ -85,18 +85,18 @@ impl Debug for Packet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let data = &self.data; let data = &self.data;
let str_data = std::str::from_utf8(&data[..data.len().saturating_sub(1)]).ok(); let mut debugger = f.debug_struct("Packet");
let data = if let Some(str_data) = str_data.as_ref() { debugger.field("kind", &PacketKind::from_u8(self.header.kind));
str_data as &dyn Debug
let c_str = CStr::from_bytes_until_nul(data).ok();
if let Some(str_data) = c_str.as_ref().and_then(|x| x.to_str().ok()) {
debugger.field("data", &str_data);
} else { } else {
&data as &dyn Debug debugger.field("data", &data);
}; }
f.debug_struct("Packet") debugger.finish()
.field("kind", &PacketKind::from_u8(self.header.kind))
.field("data", &data)
.finish()
} }
} }

View File

@@ -283,7 +283,7 @@ impl PortHandler {
self.last_update = Some(now); self.last_update = Some(now);
self.change_sender self.change_sender
.as_ref() .as_ref()
.expect("PortHandler is missing it's change_sender") .expect("PortHandler is missing its change_sender")
.send(now) .send(now)
.expect("failed to notify cache writer"); .expect("failed to notify cache writer");
} }
@@ -301,15 +301,9 @@ impl PortHandler {
} }
#[allow(clippy::missing_errors_doc)] #[allow(clippy::missing_errors_doc)]
#[instrument(skip(change_sender))] pub fn load(cache: &Path) -> std::io::Result<Self> {
pub fn load(
cache: &Path,
change_sender: tokio::sync::watch::Sender<Instant>,
) -> std::io::Result<Self> {
info!("loading cache"); info!("loading cache");
let mut cache: Self = serde_json::from_reader(BufReader::new(File::open(cache)?))?; Ok(serde_json::from_reader(BufReader::new(File::open(cache)?))?)
cache.change_sender = Some(change_sender);
Ok(cache)
} }
#[must_use] #[must_use]
@@ -318,10 +312,14 @@ impl PortHandler {
path: &Path, path: &Path,
change_sender: tokio::sync::watch::Sender<Instant>, change_sender: tokio::sync::watch::Sender<Instant>,
) -> Self { ) -> Self {
Self::load(path, change_sender).unwrap_or_else(|error| { let mut this = Self::load(path).unwrap_or_else(|error| {
error!(?path, %error, "failed to parse cache file"); error!(?path, %error, "failed to parse cache file");
Self::default() Self::default()
}) });
this.change_sender = Some(change_sender);
this
} }
pub fn update_allowed_ports(&mut self, allowed_ports: &AllowedList) { pub fn update_allowed_ports(&mut self, allowed_ports: &AllowedList) {