disable the timer so it can be used practically as chat

main
_ 2025-02-24 05:20:02 +00:00
parent a0b6726af0
commit 951defe859
3 changed files with 9 additions and 39 deletions

View File

@ -2,7 +2,6 @@ use crate::prelude::*;
pub(crate) struct Args { pub(crate) struct Args {
pub(crate) name: String, pub(crate) name: String,
pub(crate) period_ms: u32,
pub(crate) port: u16, pub(crate) port: u16,
} }
@ -10,7 +9,6 @@ impl Default for Args {
fn default() -> Self { fn default() -> Self {
Self { Self {
name: "anon".to_string(), name: "anon".to_string(),
period_ms: 1_000,
port: 9000, port: 9000,
} }
} }
@ -19,19 +17,15 @@ impl Default for Args {
pub(crate) struct App { pub(crate) struct App {
client: Client, client: Client,
stream: Framed<TcpStream, LengthDelimitedCodec>, stream: Framed<TcpStream, LengthDelimitedCodec>,
timer: Interval,
} }
impl App { impl App {
pub(crate) async fn new(args: Args) -> Result<Self> { pub(crate) async fn new(args: Args) -> Result<Self> {
let stream = TcpStream::connect(("127.0.0.1", args.port)).await?; let stream = TcpStream::connect(("127.0.0.1", args.port)).await?;
let stream = Framed::new(stream, LengthDelimitedCodec::new()); let stream = Framed::new(stream, LengthDelimitedCodec::new());
let mut timer = tokio::time::interval(Duration::from_millis(args.period_ms.into()));
timer.set_missed_tick_behavior(MissedTickBehavior::Skip);
Ok(Self { Ok(Self {
client: Client::new(args.name)?, client: Client::new(args.name)?,
stream, stream,
timer,
}) })
} }
@ -71,16 +65,10 @@ impl App {
let frame = frame_opt.context("Server closed cxn")?; let frame = frame_opt.context("Server closed cxn")?;
cx.waker().wake_by_ref(); cx.waker().wake_by_ref();
let frame = frame.context("network framing decode")?; let frame = frame.context("network framing decode")?;
self.client let output = self.client
.handle_frame(frame.into()) .handle_frame(frame.into())
.context("client.handle_frame")?; .context("client.handle_frame")?;
} println!("{output}");
if self.timer.poll_tick(cx).is_ready() {
cx.waker().wake_by_ref();
self.client
.handle_timeout()
.context("client.handle_timeout")?;
} }
Ok(()) Ok(())
@ -113,17 +101,14 @@ impl Client {
Ok(()) Ok(())
} }
fn handle_frame(&self, frame: Bytes) -> Result<()> { fn handle_frame(&self, frame: Bytes) -> Result<String> {
let ToClient { id, name, event } = rmp_serde::from_slice(&frame)?; let ToClient { id, name, event } = rmp_serde::from_slice(&frame)?;
match event { Ok(match event {
ToClientEvent::ChatLine { line } => tracing::info!(?name, ?line), ToClientEvent::ChatLine { line } => format!("{name}: {line}"),
ToClientEvent::Connected => tracing::info!(?id, "Connected"), ToClientEvent::Connected => format!("New client ({id}) connected"),
ToClientEvent::Disconnected => tracing::info!(?id, ?name, "Disconnected"), ToClientEvent::Disconnected => format!("{name} ({id}) disconnected"),
ToClientEvent::NameChanged { old_name } => { ToClientEvent::NameChanged { old_name } => format!("\"{old_name}\" ({id}) changed their name to \"{name}\""),
tracing::info!(?id, ?old_name, new_name = name, "Name changed") })
}
}
Ok(())
} }
fn handle_set_name(&mut self, name: String) -> Result<()> { fn handle_set_name(&mut self, name: String) -> Result<()> {
@ -139,15 +124,6 @@ impl Client {
self.enqueue(&msg) self.enqueue(&msg)
} }
fn handle_timeout(&mut self) -> Result<()> {
let msg = ToServer::ChatLine {
line: "There was a time, in the era of great chaos, when the Earth and the Moon were at war with each other. A daredevil from the Moon piloted a bizarre aircraft. It was feared, and because of its shape, called EINHANDER.".to_string(),
sequence: self.sequence,
};
self.sequence += 1;
self.enqueue(&msg)
}
fn poll_send(&mut self) -> Option<Bytes> { fn poll_send(&mut self) -> Option<Bytes> {
self.outbox.pop_front() self.outbox.pop_front()
} }

View File

@ -27,10 +27,6 @@ impl Args {
Some("--name") => { Some("--name") => {
client_args.name = args.next().context("Missing arg")?.to_string() client_args.name = args.next().context("Missing arg")?.to_string()
} }
Some("--period-ms") => {
client_args.period_ms =
u32::from_str(&args.next().context("Missing arg")?)?
}
Some("--port") => { Some("--port") => {
client_args.port = u16::from_str(&args.next().context("Missing arg")?)? client_args.port = u16::from_str(&args.next().context("Missing arg")?)?
} }

View File

@ -10,12 +10,10 @@ pub use std::{
pin::pin, pin::pin,
str::FromStr, str::FromStr,
task::{Context, Poll}, task::{Context, Poll},
time::Duration,
}; };
pub use tokio::{ pub use tokio::{
net::{TcpListener, TcpStream}, net::{TcpListener, TcpStream},
signal::unix::{SignalKind, signal}, signal::unix::{SignalKind, signal},
time::{Interval, MissedTickBehavior},
}; };
// Don't use BytesCodec, it is _nonsense_ // Don't use BytesCodec, it is _nonsense_
pub use tokio_util::codec::{Framed, LengthDelimitedCodec}; pub use tokio_util::codec::{Framed, LengthDelimitedCodec};