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,116 +10,117 @@ 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
|
use opengl_rust::timestep::TimeStep;
|
||||||
let server_task = tokio::spawn (async move {
|
|
||||||
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 ();
|
||||||
"[server] connection accepted: addr={}",
|
println! (
|
||||||
new_conn.connection.remote_address()
|
"[server] connection accepted: addr={}",
|
||||||
);
|
new_conn.connection.remote_address ()
|
||||||
|
);
|
||||||
|
|
||||||
let quinn::NewConnection {
|
let quinn::NewConnection {
|
||||||
|
connection,
|
||||||
|
mut datagrams,
|
||||||
|
..
|
||||||
|
} = new_conn;
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut guard = connected_players.lock ().await;
|
||||||
|
guard.push (ConnectedPlayer {
|
||||||
connection,
|
connection,
|
||||||
mut datagrams,
|
cmd: NetworkCommand::default (),
|
||||||
..
|
position: (32.0, 32.0),
|
||||||
} = new_conn;
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let mut i = 0_u64;
|
// Read player commands
|
||||||
|
{
|
||||||
|
let player_seq = player_seq;
|
||||||
|
let connected_players = Arc::clone (&connected_players);
|
||||||
|
|
||||||
let cmd = Arc::new (Mutex::new (NetworkCommand::default ()));
|
|
||||||
let cmd_2 = Arc::clone (&cmd);
|
|
||||||
|
|
||||||
// Read player commands
|
|
||||||
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> (())
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let mut time_step = TimeStep::new (120, 1000);
|
player_seq += 1;
|
||||||
let mut state = NetworkedState {
|
|
||||||
positions: vec! [
|
|
||||||
(32.0, 32.0),
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
loop {
|
let mut time_step = TimeStep::new (120, 1000);
|
||||||
let cmd = {
|
let mut state = NetworkedState {
|
||||||
let guard = cmd.lock ().await;
|
positions: vec! [
|
||||||
guard.clone ()
|
(32.0, 32.0),
|
||||||
};
|
],
|
||||||
let frames_to_do = time_step.step ();
|
};
|
||||||
|
|
||||||
|
let mut i = 0_u64;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
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;
|
||||||
if cmd.left {
|
|
||||||
state.positions [0].0 -= speed;
|
for player in &mut guard [..] {
|
||||||
}
|
let cmd = &player.cmd;
|
||||||
if cmd.right {
|
let mut pos = &mut player.position;
|
||||||
state.positions [0].0 += speed;
|
|
||||||
}
|
if cmd.left {
|
||||||
if cmd.up {
|
pos.0 -= speed;
|
||||||
state.positions [0].1 += speed;
|
}
|
||||||
}
|
if cmd.right {
|
||||||
if cmd.down {
|
pos.0 += speed;
|
||||||
state.positions [0].1 -= speed;
|
}
|
||||||
|
if cmd.up {
|
||||||
|
pos.1 += speed;
|
||||||
|
}
|
||||||
|
if cmd.down {
|
||||||
|
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;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Dropping all handles associated with a connection implicitly closes it
|
tokio::time::sleep (std::time::Duration::from_millis(16)).await;
|
||||||
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