mirror of
https://codeberg.org/demostf/sync.git
synced 2026-06-03 16:44:07 +02:00
send client count to owners
This commit is contained in:
parent
608a4a1bde
commit
2cc5e05e18
2 changed files with 30 additions and 4 deletions
28
src/main.rs
28
src/main.rs
|
|
@ -29,6 +29,7 @@ pub enum SyncCommand<'a> {
|
||||||
Join { session: &'a str },
|
Join { session: &'a str },
|
||||||
Tick { session: &'a str, tick: u64 },
|
Tick { session: &'a str, tick: u64 },
|
||||||
Play { session: &'a str, play: bool },
|
Play { session: &'a str, play: bool },
|
||||||
|
Clients { session: &'a str, count: usize },
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
|
|
@ -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 {
|
match &command {
|
||||||
SyncCommand::Create { session, token } => {
|
SyncCommand::Create { session, token } => {
|
||||||
self.sessions
|
self.sessions
|
||||||
|
|
@ -84,6 +85,13 @@ impl Server {
|
||||||
self.send_command(&sender, &initial_command);
|
self.send_command(&sender, &initial_command);
|
||||||
}
|
}
|
||||||
session.join(sender);
|
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"),
|
None => error!(session = session_name, "session not found for command"),
|
||||||
},
|
},
|
||||||
|
|
@ -99,6 +107,17 @@ impl Server {
|
||||||
error!(session, "session not found for command");
|
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(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,7 +151,7 @@ impl Server {
|
||||||
match serde_json::from_str(message) {
|
match serde_json::from_str(message) {
|
||||||
Ok(command) => {
|
Ok(command) => {
|
||||||
debug!(sender = %addr, message = ?command, "Received a message");
|
debug!(sender = %addr, message = ?command, "Received a message");
|
||||||
self.handle_command(command, addr).await;
|
self.handle_command(command, addr);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!(sender = %addr, message, error = %e, "Error while decoding message");
|
warn!(sender = %addr, message, error = %e, "Error while decoding message");
|
||||||
|
|
@ -152,6 +171,7 @@ impl Server {
|
||||||
|
|
||||||
info!(%addr, "disconnected");
|
info!(%addr, "disconnected");
|
||||||
self.peers.remove(&addr);
|
self.peers.remove(&addr);
|
||||||
|
self.handle_disconnect(&addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -169,7 +189,9 @@ async fn main() -> MainResult {
|
||||||
let state = Arc::new(Server::new());
|
let state = Arc::new(Server::new());
|
||||||
|
|
||||||
// Create the event loop and TCP listener we'll accept connections on.
|
// 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);
|
info!("listening on: {:?}", listen_address);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ pub struct Session {
|
||||||
tick: u64,
|
tick: u64,
|
||||||
playing: bool,
|
playing: bool,
|
||||||
owner_left: Option<Instant>,
|
owner_left: Option<Instant>,
|
||||||
token: String,
|
pub token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Session {
|
impl PartialEq for Session {
|
||||||
|
|
@ -66,6 +66,10 @@ impl Session {
|
||||||
self.clients.iter()
|
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) {
|
pub fn handle_command(&mut self, command: &SyncCommand) {
|
||||||
match command {
|
match command {
|
||||||
SyncCommand::Tick { tick, .. } => {
|
SyncCommand::Tick { tick, .. } => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue