Clean up client code

This commit is contained in:
Jan Trefil 2024-07-21 17:33:46 +02:00
parent 20fb8efb7e
commit 6d986741ff

View file

@ -21,6 +21,7 @@ use tokio::io::BufWriter;
use tokio::net;
use tokio::sync::mpsc::{self, Sender};
use tracing::Instrument;
use tracing::Span;
#[derive(Error, Debug)]
pub enum Error {
@ -136,6 +137,7 @@ pub async fn run(
}
};
// TODO: Since this is a datagram, do we really want to wait here?
let _ = sender.send(datagram.events.into_owned()).await;
continue;
}
@ -241,21 +243,25 @@ async fn stream<T: AsyncRead + Send + Unpin + 'static>(
.await
.map_err(NetworkError::from)?;
let id = device_info.id;
let span = tracing::info_span!("device", id = ?device_info.id);
let span = tracing::info_span!("device", id = %device_info.id);
async {
let mut writer = build(&device_info).await.map_err(Error::Input)?;
let mut writer = build(device_info).await.map_err(Error::Input)?;
tracing::info!(
name = ?device_info.name,
vendor = %device_info.vendor,
product = %device_info.product,
version = %device_info.version,
"Created new device"
);
let (datagram_sender, mut datagram_receiver) = mpsc::channel(1);
let event = DeviceEvent::Create {
id,
let _ = device_sender
.send(DeviceEvent::Create {
id: device_info.id,
sender: datagram_sender,
};
if device_sender.send(event).await.is_err() {
return Ok(());
}
})
.await;
let (event_sender, mut event_receiver) = mpsc::channel(1);
tokio::spawn(
@ -271,10 +277,10 @@ async fn stream<T: AsyncRead + Send + Unpin + 'static>(
}
}
}
.instrument(span.clone()),
.instrument(Span::current()),
);
let result = async move {
let result = async {
loop {
let event = async { event_receiver.recv().await.unwrap() };
@ -282,6 +288,8 @@ async fn stream<T: AsyncRead + Send + Unpin + 'static>(
event = event => {
let event = event.map_err(NetworkError::from)?;
writer.write(&event).await.map_err(Error::Input)?;
tracing::trace!("Wrote an event");
}
datagram = datagram_receiver.recv() => {
let datagram = match datagram {
@ -289,44 +297,49 @@ async fn stream<T: AsyncRead + Send + Unpin + 'static>(
None => break,
};
let length = datagram.len();
for event in datagram {
writer.write(&event).await.map_err(Error::Input)?;
}
tracing::trace!(
"Wrote {} unreliable event{}",
length,
if length == 1 { "" } else { "s" }
);
}
}
}
Ok(())
}
.instrument(span)
.await;
let _ = device_sender.send(DeviceEvent::Destroy { id }).await;
let _ = device_sender
.send(DeviceEvent::Destroy { id: device_info.id })
.await;
// Drop explicitly to make the log properly ordered.
drop(writer);
tracing::info!("Destroyed device");
result
}
.instrument(span)
.await
}
async fn build(device_info: DeviceInfo) -> Result<Writer, io::Error> {
let writer = Writer::builder()?
async fn build(device_info: &DeviceInfo) -> Result<Writer, io::Error> {
Writer::builder()?
.name(&device_info.name)
.vendor(device_info.vendor)
.product(device_info.product)
.version(device_info.version)
.rel(device_info.rel)?
.abs(device_info.abs)?
.key(device_info.keys)?
.rel(device_info.rel.iter().copied())?
.abs(device_info.abs.iter().map(|(axis, info)| (*axis, *info)))?
.key(device_info.keys.iter().copied())?
.delay(device_info.delay)?
.period(device_info.period)?
.build()
.await?;
tracing::info!(
name = ?device_info.name,
vendor = %device_info.vendor,
product = %device_info.product,
version = %device_info.version,
"Created new device"
);
Ok(writer)
.await
}