that's multiplayer. that counts

main
_ 2021-05-08 23:38:33 +00:00
parent 2135bb0f24
commit 06c173e6da
1 changed files with 42 additions and 34 deletions

View File

@ -24,50 +24,58 @@ async fn main () -> anyhow::Result <()> {
let connected_players = Arc::new (Mutex::new (vec! [])); let connected_players = Arc::new (Mutex::new (vec! []));
use opengl_rust::timestep::TimeStep;
let mut player_seq = 0;
let incoming_conn = incoming.next ().await.unwrap ();
let new_conn = incoming_conn.await.unwrap ();
println! (
"[server] connection accepted: addr={}",
new_conn.connection.remote_address ()
);
let quinn::NewConnection {
connection,
mut datagrams,
..
} = new_conn;
{ {
let mut guard = connected_players.lock ().await;
guard.push (ConnectedPlayer {
connection,
cmd: NetworkCommand::default (),
position: (32.0, 32.0),
});
}
// Read player commands
{
let player_seq = player_seq;
let connected_players = Arc::clone (&connected_players); let connected_players = Arc::clone (&connected_players);
tokio::spawn (async move { tokio::spawn (async move {
while let Some (Ok (datagram)) = datagrams.next ().await { let mut player_seq = 0;
let cmd: NetworkCommand = rmp_serde::from_slice (&datagram)?;
let mut guard = connected_players.lock ().await; while let Some (incoming_conn) = incoming.next ().await {
guard [player_seq].cmd = cmd; let new_conn = incoming_conn.await?;
println! (
"[server] connection accepted: addr={}",
new_conn.connection.remote_address ()
);
let quinn::NewConnection {
connection,
mut datagrams,
..
} = new_conn;
{
let mut guard = connected_players.lock ().await;
guard.push (ConnectedPlayer {
connection,
cmd: NetworkCommand::default (),
position: (32.0, 32.0),
});
}
// Read player commands
{
let player_seq = player_seq;
let connected_players = Arc::clone (&connected_players);
tokio::spawn (async move {
while let Some (Ok (datagram)) = datagrams.next ().await {
let cmd: NetworkCommand = rmp_serde::from_slice (&datagram)?;
let mut guard = connected_players.lock ().await;
guard [player_seq].cmd = cmd;
}
Ok::<_, anyhow::Error> (())
});
}
player_seq += 1;
} }
Ok::<_, anyhow::Error> (()) Ok::<_, anyhow::Error> (())
}); });
} }
player_seq += 1; let mut time_step = opengl_rust::timestep::TimeStep::new (120, 1000);
let mut time_step = TimeStep::new (120, 1000);
let mut state = NetworkedState { let mut state = NetworkedState {
positions: vec! [ positions: vec! [
(32.0, 32.0), (32.0, 32.0),