Skip to content

Commit

Permalink
send client count to owners
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Nov 24, 2024
1 parent 608a4a1 commit 2cc5e05
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
28 changes: 25 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum SyncCommand<'a> {
Join { session: &'a str },
Tick { session: &'a str, tick: u64 },
Play { session: &'a str, play: bool },
Clients { session: &'a str, count: usize },
}

pub struct Server {
Expand Down Expand Up @@ -63,7 +64,7 @@ impl Server {
}
}

async fn handle_command<'a>(&self, command: SyncCommand<'_>, sender: SocketAddr) {
fn handle_command<'a>(&self, command: SyncCommand<'_>, sender: SocketAddr) {
match &command {
SyncCommand::Create { session, token } => {
self.sessions
Expand All @@ -84,6 +85,13 @@ impl Server {
self.send_command(&sender, &initial_command);
}
session.join(sender);
self.send_command(
&session.owner,
&SyncCommand::Clients {
session: session_name,
count: session.clients().count(),
},
)
}
None => error!(session = session_name, "session not found for command"),
},
Expand All @@ -99,6 +107,17 @@ impl Server {
error!(session, "session not found for command");
}
},
_ => {}
}
}

fn handle_disconnect(&self, peer: &SocketAddr) {
for mut session in self.sessions.iter_mut() {
session.remove_client(peer);
self.send_command(&session.owner, &SyncCommand::Clients {
session: &session.token,
count: session.clients().count(),
})
}
}

Expand Down Expand Up @@ -132,7 +151,7 @@ impl Server {
match serde_json::from_str(message) {
Ok(command) => {
debug!(sender = %addr, message = ?command, "Received a message");
self.handle_command(command, addr).await;
self.handle_command(command, addr);
}
Err(e) => {
warn!(sender = %addr, message, error = %e, "Error while decoding message");
Expand All @@ -152,6 +171,7 @@ impl Server {

info!(%addr, "disconnected");
self.peers.remove(&addr);
self.handle_disconnect(&addr);
}
}

Expand All @@ -169,7 +189,9 @@ async fn main() -> MainResult {
let state = Arc::new(Server::new());

// Create the event loop and TCP listener we'll accept connections on.
let listener = TcpListener::bind(&listen_address).await.expect("Failed to bind");
let listener = TcpListener::bind(&listen_address)
.await
.expect("Failed to bind");

info!("listening on: {:?}", listen_address);

Expand Down
6 changes: 5 additions & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Session {
tick: u64,
playing: bool,
owner_left: Option<Instant>,
token: String,
pub token: String,
}

impl PartialEq for Session {
Expand Down Expand Up @@ -66,6 +66,10 @@ impl Session {
self.clients.iter()
}

pub fn remove_client(&mut self, peer: &SocketAddr) {
self.clients.retain(|client| client != peer)
}

pub fn handle_command(&mut self, command: &SyncCommand) {
match command {
SyncCommand::Tick { tick, .. } => {
Expand Down

0 comments on commit 2cc5e05

Please sign in to comment.