From 3f14151feb991a1c8a3434b0cb2a776b0b7ed02b Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 22 Jan 2023 20:51:08 +0100 Subject: [PATCH] Send different kinds of events --- examples/testbot_instance.rs | 10 ++++++---- src/bot/instance.rs | 27 +++++++++++++++------------ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/examples/testbot_instance.rs b/examples/testbot_instance.rs index 617a53a..99e3323 100644 --- a/examples/testbot_instance.rs +++ b/examples/testbot_instance.rs @@ -1,9 +1,9 @@ //! Similar to the `testbot_manual` example, but using [`Instance`] to connect -//! to the room (and toreconnect). +//! to the room (and to reconnect). use euphoxide::api::packet::ParsedPacket; use euphoxide::api::{Data, Nick, Send}; -use euphoxide::bot::instance::{ServerConfig, Snapshot}; +use euphoxide::bot::instance::{Event, ServerConfig, Snapshot}; use time::OffsetDateTime; use tokio::sync::mpsc; @@ -161,8 +161,10 @@ async fn main() { }); while let Some(event) = rx.recv().await { - if on_packet(event.packet, event.snapshot).await.is_err() { - break; + if let Event::Packet(_config, packet, snapshot) = event { + if on_packet(packet, snapshot).await.is_err() { + break; + } } } } diff --git a/src/bot/instance.rs b/src/bot/instance.rs index 047260b..9a0e78d 100644 --- a/src/bot/instance.rs +++ b/src/bot/instance.rs @@ -157,11 +157,16 @@ pub struct Snapshot { pub state: State, } +// Most of the time, the largest variant (`Packet`) is sent. The size of this +// enum is not critical anyways since it's not constructed that often. +#[allow(clippy::large_enum_variant)] #[derive(Debug)] -pub struct Event { - pub config: InstanceConfig, - pub packet: ParsedPacket, - pub snapshot: Snapshot, +pub enum Event { + Connecting(InstanceConfig), + Connected(InstanceConfig, ConnTx), + Packet(InstanceConfig, ParsedPacket, Snapshot), + Disconnected(InstanceConfig), + Stopped(InstanceConfig), } /// A single instance of a bot in a single room. @@ -176,7 +181,6 @@ pub struct Event { /// one instance per room. #[derive(Debug)] pub struct Instance { - // TODO Share Arc instead of cloning InstanceConfig everywhere config: InstanceConfig, request_tx: mpsc::UnboundedSender>, } @@ -226,7 +230,10 @@ impl Instance { { // TODO Only delay reconnecting if previous reconnect attempt failed loop { + on_event(Event::Connecting(config.clone())); Self::run_once::(&config, &on_event, &mut request_rx).await; + on_event(Event::Disconnected(config.clone())); + debug!( "{}: Waiting {} seconds before reconnecting", config.name, @@ -279,6 +286,7 @@ impl Instance { .await .ok()?; Self::set_cookies(config, cookies); + on_event(Event::Connected(config.clone(), conn.tx().clone())); let conn_tx = conn.tx().clone(); let result = select! { @@ -306,13 +314,8 @@ impl Instance { conn_tx: conn.tx().clone(), state: conn.state().clone(), }; - let event = Event { - config: config.clone(), - packet, - snapshot, - }; - match &event.packet.content { + match &packet.content { Ok(Data::SnapshotEvent(_)) => { if let Some(username) = &config.username { debug!("{}: Setting nick to username {}", config.name, username); @@ -336,7 +339,7 @@ impl Instance { _ => {} } - on_event(event); + on_event(Event::Packet(config.clone(), packet, snapshot)); } Ok(())