disable the timer so it can be used practically as chat
parent
a0b6726af0
commit
951defe859
|
@ -2,7 +2,6 @@ use crate::prelude::*;
|
|||
|
||||
pub(crate) struct Args {
|
||||
pub(crate) name: String,
|
||||
pub(crate) period_ms: u32,
|
||||
pub(crate) port: u16,
|
||||
}
|
||||
|
||||
|
@ -10,7 +9,6 @@ impl Default for Args {
|
|||
fn default() -> Self {
|
||||
Self {
|
||||
name: "anon".to_string(),
|
||||
period_ms: 1_000,
|
||||
port: 9000,
|
||||
}
|
||||
}
|
||||
|
@ -19,19 +17,15 @@ impl Default for Args {
|
|||
pub(crate) struct App {
|
||||
client: Client,
|
||||
stream: Framed<TcpStream, LengthDelimitedCodec>,
|
||||
timer: Interval,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub(crate) async fn new(args: Args) -> Result<Self> {
|
||||
let stream = TcpStream::connect(("127.0.0.1", args.port)).await?;
|
||||
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 {
|
||||
client: Client::new(args.name)?,
|
||||
stream,
|
||||
timer,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -71,16 +65,10 @@ impl App {
|
|||
let frame = frame_opt.context("Server closed cxn")?;
|
||||
cx.waker().wake_by_ref();
|
||||
let frame = frame.context("network framing decode")?;
|
||||
self.client
|
||||
let output = self.client
|
||||
.handle_frame(frame.into())
|
||||
.context("client.handle_frame")?;
|
||||
}
|
||||
|
||||
if self.timer.poll_tick(cx).is_ready() {
|
||||
cx.waker().wake_by_ref();
|
||||
self.client
|
||||
.handle_timeout()
|
||||
.context("client.handle_timeout")?;
|
||||
println!("{output}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -113,17 +101,14 @@ impl Client {
|
|||
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)?;
|
||||
match event {
|
||||
ToClientEvent::ChatLine { line } => tracing::info!(?name, ?line),
|
||||
ToClientEvent::Connected => tracing::info!(?id, "Connected"),
|
||||
ToClientEvent::Disconnected => tracing::info!(?id, ?name, "Disconnected"),
|
||||
ToClientEvent::NameChanged { old_name } => {
|
||||
tracing::info!(?id, ?old_name, new_name = name, "Name changed")
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
Ok(match event {
|
||||
ToClientEvent::ChatLine { line } => format!("{name}: {line}"),
|
||||
ToClientEvent::Connected => format!("New client ({id}) connected"),
|
||||
ToClientEvent::Disconnected => format!("{name} ({id}) disconnected"),
|
||||
ToClientEvent::NameChanged { old_name } => format!("\"{old_name}\" ({id}) changed their name to \"{name}\""),
|
||||
})
|
||||
}
|
||||
|
||||
fn handle_set_name(&mut self, name: String) -> Result<()> {
|
||||
|
@ -139,15 +124,6 @@ impl Client {
|
|||
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> {
|
||||
self.outbox.pop_front()
|
||||
}
|
||||
|
|
|
@ -27,10 +27,6 @@ impl Args {
|
|||
Some("--name") => {
|
||||
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") => {
|
||||
client_args.port = u16::from_str(&args.next().context("Missing arg")?)?
|
||||
}
|
||||
|
|
|
@ -10,12 +10,10 @@ pub use std::{
|
|||
pin::pin,
|
||||
str::FromStr,
|
||||
task::{Context, Poll},
|
||||
time::Duration,
|
||||
};
|
||||
pub use tokio::{
|
||||
net::{TcpListener, TcpStream},
|
||||
signal::unix::{SignalKind, signal},
|
||||
time::{Interval, MissedTickBehavior},
|
||||
};
|
||||
// Don't use BytesCodec, it is _nonsense_
|
||||
pub use tokio_util::codec::{Framed, LengthDelimitedCodec};
|
||||
|
|
Loading…
Reference in New Issue