main
parent
2059e5a237
commit
2135bb0f24
|
@ -521,6 +521,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
|
"bytes",
|
||||||
"float-ord",
|
"float-ord",
|
||||||
"futures",
|
"futures",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
|
|
|
@ -10,6 +10,7 @@ edition = "2018"
|
||||||
|
|
||||||
anyhow = "1.0.36"
|
anyhow = "1.0.36"
|
||||||
byteorder = "1.3.2"
|
byteorder = "1.3.2"
|
||||||
|
bytes = "1.0.1"
|
||||||
float-ord = "0.2.0"
|
float-ord = "0.2.0"
|
||||||
futures = "0.3.8"
|
futures = "0.3.8"
|
||||||
futures-util = "0.3.9"
|
futures-util = "0.3.9"
|
||||||
|
|
|
@ -10,28 +10,28 @@ use opengl_rust::{
|
||||||
quinn_common::make_server_endpoint,
|
quinn_common::make_server_endpoint,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ConnectedPlayer {
|
||||||
|
connection: quinn::Connection,
|
||||||
|
cmd: NetworkCommand,
|
||||||
|
position: (f32, f32),
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main () -> anyhow::Result <()> {
|
async fn main () -> anyhow::Result <()> {
|
||||||
let server_addr = "127.0.0.1:5000".parse().unwrap();
|
let server_addr = "127.0.0.1:5000".parse().unwrap();
|
||||||
let (mut incoming, server_cert) = make_server_endpoint(server_addr)?;
|
let (mut incoming, server_cert) = make_server_endpoint (server_addr)?;
|
||||||
|
tokio::fs::write ("quic_server.crt", &server_cert).await?;
|
||||||
|
|
||||||
let state = NetworkedState {
|
let connected_players = Arc::new (Mutex::new (vec! []));
|
||||||
positions: vec! [
|
|
||||||
(32.0, 32.0),
|
|
||||||
],
|
|
||||||
};
|
|
||||||
let state = Arc::new (Mutex::new (state));
|
|
||||||
// let connected_players = Arc::new (Mutex::new (vec! []));
|
|
||||||
|
|
||||||
// accept a single connection
|
|
||||||
let server_task = tokio::spawn (async move {
|
|
||||||
use opengl_rust::timestep::TimeStep;
|
use opengl_rust::timestep::TimeStep;
|
||||||
|
|
||||||
let incoming_conn = incoming.next().await.unwrap();
|
let mut player_seq = 0;
|
||||||
let new_conn = incoming_conn.await.unwrap();
|
let incoming_conn = incoming.next ().await.unwrap ();
|
||||||
println!(
|
let new_conn = incoming_conn.await.unwrap ();
|
||||||
|
println! (
|
||||||
"[server] connection accepted: addr={}",
|
"[server] connection accepted: addr={}",
|
||||||
new_conn.connection.remote_address()
|
new_conn.connection.remote_address ()
|
||||||
);
|
);
|
||||||
|
|
||||||
let quinn::NewConnection {
|
let quinn::NewConnection {
|
||||||
|
@ -40,21 +40,32 @@ async fn main () -> anyhow::Result <()> {
|
||||||
..
|
..
|
||||||
} = new_conn;
|
} = new_conn;
|
||||||
|
|
||||||
let mut i = 0_u64;
|
{
|
||||||
|
let mut guard = connected_players.lock ().await;
|
||||||
let cmd = Arc::new (Mutex::new (NetworkCommand::default ()));
|
guard.push (ConnectedPlayer {
|
||||||
let cmd_2 = Arc::clone (&cmd);
|
connection,
|
||||||
|
cmd: NetworkCommand::default (),
|
||||||
|
position: (32.0, 32.0),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Read player commands
|
// Read player commands
|
||||||
|
{
|
||||||
|
let player_seq = player_seq;
|
||||||
|
let connected_players = Arc::clone (&connected_players);
|
||||||
|
|
||||||
tokio::spawn (async move {
|
tokio::spawn (async move {
|
||||||
while let Some (Ok (datagram)) = datagrams.next ().await {
|
while let Some (Ok (datagram)) = datagrams.next ().await {
|
||||||
let cmd: NetworkCommand = rmp_serde::from_slice (&datagram)?;
|
let cmd: NetworkCommand = rmp_serde::from_slice (&datagram)?;
|
||||||
let mut guard = cmd_2.lock ().await;
|
let mut guard = connected_players.lock ().await;
|
||||||
*guard = cmd;
|
guard [player_seq].cmd = cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok::<_, anyhow::Error> (())
|
Ok::<_, anyhow::Error> (())
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
player_seq += 1;
|
||||||
|
|
||||||
let mut time_step = TimeStep::new (120, 1000);
|
let mut time_step = TimeStep::new (120, 1000);
|
||||||
let mut state = NetworkedState {
|
let mut state = NetworkedState {
|
||||||
|
@ -63,63 +74,53 @@ async fn main () -> anyhow::Result <()> {
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut i = 0_u64;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let cmd = {
|
|
||||||
let guard = cmd.lock ().await;
|
|
||||||
guard.clone ()
|
|
||||||
};
|
|
||||||
let frames_to_do = time_step.step ();
|
let frames_to_do = time_step.step ();
|
||||||
|
|
||||||
|
let state;
|
||||||
|
{
|
||||||
|
let mut guard = connected_players.lock ().await;
|
||||||
for _ in 0..frames_to_do {
|
for _ in 0..frames_to_do {
|
||||||
let speed = 0.25;
|
let speed = 0.25;
|
||||||
|
|
||||||
|
for player in &mut guard [..] {
|
||||||
|
let cmd = &player.cmd;
|
||||||
|
let mut pos = &mut player.position;
|
||||||
|
|
||||||
if cmd.left {
|
if cmd.left {
|
||||||
state.positions [0].0 -= speed;
|
pos.0 -= speed;
|
||||||
}
|
}
|
||||||
if cmd.right {
|
if cmd.right {
|
||||||
state.positions [0].0 += speed;
|
pos.0 += speed;
|
||||||
}
|
}
|
||||||
if cmd.up {
|
if cmd.up {
|
||||||
state.positions [0].1 += speed;
|
pos.1 += speed;
|
||||||
}
|
}
|
||||||
if cmd.down {
|
if cmd.down {
|
||||||
state.positions [0].1 -= speed;
|
pos.1 -= speed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let bytes = rmp_serde::to_vec (&state)?;
|
state = NetworkedState {
|
||||||
connection.send_datagram (bytes.into ())?;
|
positions: guard.iter ()
|
||||||
|
.map (|player| player.position)
|
||||||
|
.collect (),
|
||||||
|
};
|
||||||
|
|
||||||
|
let bytes = bytes::Bytes::from (rmp_serde::to_vec (&state)?);
|
||||||
|
|
||||||
|
for player in &guard [..] {
|
||||||
|
player.connection.send_datagram (bytes.clone ())?;
|
||||||
|
}
|
||||||
|
|
||||||
i += 1;
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
tokio::time::sleep (std::time::Duration::from_millis(16)).await;
|
tokio::time::sleep (std::time::Duration::from_millis(16)).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dropping all handles associated with a connection implicitly closes it
|
|
||||||
Ok::<_, anyhow::Error> (())
|
|
||||||
});
|
|
||||||
|
|
||||||
tokio::fs::write ("quic_server.crt", &server_cert).await?;
|
|
||||||
|
|
||||||
/*
|
|
||||||
let endpoint = make_client_endpoint("0.0.0.0:0".parse().unwrap(), &[&server_cert])?;
|
|
||||||
// connect to server
|
|
||||||
let quinn::NewConnection {
|
|
||||||
connection,
|
|
||||||
mut uni_streams,
|
|
||||||
..
|
|
||||||
} = endpoint
|
|
||||||
.connect(&server_addr, "localhost")
|
|
||||||
.unwrap()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
println!("[client] connected: addr={}", connection.remote_address());
|
|
||||||
|
|
||||||
// Waiting for a stream will complete with an error when the server closes the connection
|
|
||||||
let _ = uni_streams.next().await;
|
|
||||||
|
|
||||||
// Give the server has a chance to clean up
|
|
||||||
endpoint.wait_idle().await;
|
|
||||||
*/
|
|
||||||
|
|
||||||
server_task.await??;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue