Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Aug 22, 2023
1 parent c58ec69 commit a87cb55
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 367 deletions.
10 changes: 10 additions & 0 deletions crates/connector/tests/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ fn test() {

comms_a.send(ToServer::OpenGame { max_players: 3 }).await;
let mut response = comms_a.recv::<FromServer>().await;

// TODO
println!("A");

assert_eq!(response.len(), 1);
let response = response.pop().unwrap();
let game_port = match response {
Expand All @@ -55,10 +59,16 @@ fn test() {
comms_d.port = game_port;

check_response!(comms_a, FromGame::Joined(1));
// TODO
println!("b");

comms_b.send(ToGame::Join).await;
check_response!(comms_b, FromGame::Joined(2));
// TODO
println!("c");
check_response!(comms_a, FromGame::PeerJoined(2));
// TODO
println!("d");

comms_a.send(ToGame::Readiness(Readiness::Ready)).await;
// The other player is not yet ready -> no message should be received.
Expand Down
108 changes: 78 additions & 30 deletions crates/connector/tests/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use async_std::{prelude::FutureExt, task};
use de_net::Socket;
use de_net::{Reliability, Socket};
use futures::join;
use ntest::timeout;

Expand All @@ -14,8 +14,6 @@ mod common;

const SERVER_ADDR: SocketAddr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 8082));

// TODO fix headers

#[derive(Debug)]
struct ReceivedBuffer(Vec<Incomming>);

Expand All @@ -36,10 +34,14 @@ impl ReceivedBuffer {
);
}

fn find_id(&self, filter_reliable: bool, filter_data: &[u8]) -> Option<u32> {
fn find_id(&self, filter_reliability: Reliability, filter_data: &[u8]) -> Option<u32> {
self.0.iter().find_map(|incomming| match incomming {
Incomming::Data { reliable, id, data } => {
if *reliable == filter_reliable && data == filter_data {
Incomming::Data {
reliability,
id,
data,
} => {
if *reliability == filter_reliability && data == filter_data {
Some(*id)
} else {
None
Expand Down Expand Up @@ -70,7 +72,16 @@ impl ReceivedBuffer {
self.0.push(Incomming::Confirm(id));
}
} else {
let reliable = buf[0] & 64 > 0;
let reliability = (buf[0] & 96) >> 5;
let reliability = if reliability == 0 {
Reliability::Unreliable
} else if reliability == 1 {
Reliability::Unordered
} else if reliability == 2 {
Reliability::SemiOrdered
} else {
panic!("Invalid reliability bits");
};

id_bytes[0] = 0;
id_bytes[1] = buf[1];
Expand All @@ -79,7 +90,7 @@ impl ReceivedBuffer {
let id = u32::from_be_bytes(id_bytes);

self.0.push(Incomming::Data {
reliable,
reliability,
id,
data: buf[4..n].to_vec(),
});
Expand All @@ -91,7 +102,7 @@ impl ReceivedBuffer {
enum Incomming {
Confirm(u32),
Data {
reliable: bool,
reliability: Reliability,
id: u32,
data: Vec<u8>,
},
Expand All @@ -112,17 +123,22 @@ fn test() {
received.load(&mut client, &mut buffer).await;

// [5, 2] -> FromGame::PeerJoined(1)
let id = received.find_id(true, &[5, 2]).unwrap().to_be_bytes();
let id = received
.find_id(Reliability::SemiOrdered, &[5, 2])
.unwrap()
.to_be_bytes();
// And send a confirmation
client
.send(server, &[128, 0, 0, 0, id[1], id[2], id[3]])
.await
.unwrap();

let first_id = received.find_id(true, &[5, 6, 7, 8]).unwrap();
let first_id = received
.find_id(Reliability::Unordered, &[5, 6, 7, 8])
.unwrap();

let mut data = [22; 412];
data[0] = 64; // Reliable
data[0] = 32; // Unordered
data[1] = 0;
data[2] = 0;
data[3] = 22;
Expand All @@ -132,7 +148,9 @@ fn test() {
received.load(&mut client, &mut buffer).await;
received.load(&mut client, &mut buffer).await;
received.assert_confirmed(22);
received.find_id(false, &[82, 83, 84]).unwrap();
received
.find_id(Reliability::Unreliable, &[82, 83, 84])
.unwrap();

// Try to send invalid data -- wrong header
client
Expand All @@ -148,10 +166,20 @@ fn test() {
// Two retries before we confirm.
let mut received = ReceivedBuffer::new();
received.load(&mut client, &mut buffer).await;
assert_eq!(received.find_id(true, &[5, 6, 7, 8]).unwrap(), first_id);
assert_eq!(
received
.find_id(Reliability::Unordered, &[5, 6, 7, 8])
.unwrap(),
first_id
);
let mut received = ReceivedBuffer::new();
received.load(&mut client, &mut buffer).await;
assert_eq!(received.find_id(true, &[5, 6, 7, 8]).unwrap(), first_id);
assert_eq!(
received
.find_id(Reliability::Unordered, &[5, 6, 7, 8])
.unwrap(),
first_id
);

let id = first_id.to_be_bytes();
// And send a confirmation
Expand All @@ -160,8 +188,8 @@ fn test() {
.await
.unwrap();

client.send(server, &[64, 0, 0, 92, 16]).await.unwrap();
client.send(server, &[64, 0, 0, 86, 23]).await.unwrap();
client.send(server, &[32, 0, 0, 92, 16]).await.unwrap();
client.send(server, &[32, 0, 0, 86, 23]).await.unwrap();
let mut received = ReceivedBuffer::new();
received.load(&mut client, &mut buffer).await;
received.assert_confirmed(92);
Expand All @@ -181,16 +209,19 @@ fn test() {
let mut buffer = [0u8; 1024];

client
// Reliable
.send(server, &[64, 0, 0, 14, 5, 6, 7, 8])
// unordered
.send(server, &[32, 0, 0, 14, 5, 6, 7, 8])
.await
.unwrap();

let mut received = ReceivedBuffer::new();
received.load(&mut client, &mut buffer).await;
received.load(&mut client, &mut buffer).await;
received.assert_confirmed(14);
let id = received.find_id(true, &[22; 408]).unwrap().to_be_bytes();
let id = received
.find_id(Reliability::Unordered, &[22; 408])
.unwrap()
.to_be_bytes();
// Sending confirmation

client
Expand All @@ -209,15 +240,21 @@ fn test() {

let mut received = ReceivedBuffer::new();
received.load(&mut client, &mut buffer).await;
let id = received.find_id(true, &[16]).unwrap().to_be_bytes();
let id = received
.find_id(Reliability::Unordered, &[16])
.unwrap()
.to_be_bytes();
client
.send(server, &[128, 0, 0, 0, id[1], id[2], id[3]])
.await
.unwrap();

let mut received = ReceivedBuffer::new();
received.load(&mut client, &mut buffer).await;
let id = received.find_id(true, &[23]).unwrap().to_be_bytes();
let id = received
.find_id(Reliability::Unordered, &[23])
.unwrap()
.to_be_bytes();
client
.send(server, &[128, 0, 0, 0, id[1], id[2], id[3]])
.await
Expand Down Expand Up @@ -247,11 +284,11 @@ async fn create_game() -> (Socket, u16) {

let mut client = Socket::bind(None).await.unwrap();

// [64 + 32] -> reliable + Peers::Server
// [64 + 16] -> semi-ordered + Peers::Server
// [0, 0, 7] -> datagram ID = 7
// [1 3] -> ToGame::OpenGame { max_players: 3 }
client
.send(SERVER_ADDR, &[64 + 32, 0, 0, 7, 1, 3])
.send(SERVER_ADDR, &[64 + 16, 0, 0, 7, 1, 3])
.await
.unwrap();

Expand All @@ -261,11 +298,16 @@ async fn create_game() -> (Socket, u16) {
assert_eq!(received.0.len(), 1);

let port = {
let Incomming::Data { reliable, id, data } = &(received.0)[0] else {
let Incomming::Data {
reliability,
id,
data,
} = &(received.0)[0]
else {
panic!("Unexpected data received: {:?}", received);
};

assert!(reliable);
assert!(reliability.is_reliable());

// Confirm
let id = id.to_be_bytes();
Expand Down Expand Up @@ -297,7 +339,10 @@ async fn create_game() -> (Socket, u16) {
received.load(&mut client, &mut buffer).await;

// [2, 1] -> FromGame::Joined(1)
let id = received.find_id(true, &[2, 1]).unwrap().to_be_bytes();
let id = received
.find_id(Reliability::SemiOrdered, &[2, 1])
.unwrap()
.to_be_bytes();
client
.send(server, &[128, 0, 0, 0, id[1], id[2], id[3]])
.await
Expand All @@ -312,18 +357,21 @@ async fn join_game(game_port: u16) -> Socket {
let server = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, game_port));
let mut client = Socket::bind(None).await.unwrap();

// [64 + 32] -> reliable + Peers::Server
// [64 + 16] -> semi-ordered + Peers::Server
// [0, 0, 3] -> datagram ID = 3
// [1] -> ToGame::Join
client.send(server, &[64 + 32, 0, 0, 3, 1]).await.unwrap();
client.send(server, &[64 + 16, 0, 0, 3, 1]).await.unwrap();

let mut received = ReceivedBuffer::new();
received.load(&mut client, &mut buffer).await;
received.load(&mut client, &mut buffer).await;
received.assert_confirmed(3);

// [2, 2] -> FromGame::Joined(2)
let id = received.find_id(true, &[2, 2]).unwrap().to_be_bytes();
let id = received
.find_id(Reliability::SemiOrdered, &[2, 2])
.unwrap()
.to_be_bytes();
client
.send(server, &[128, 0, 0, 0, id[1], id[2], id[3]])
.await
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ use async_std::{
channel::{SendError, Sender},
sync::{Arc, Mutex, MutexGuard},
};
pub(crate) use received::ReceivedIdError;

use super::{
book::{Connection, ConnectionBook},
use self::{
confirms::{ConfirmsBuffer, MAX_BUFF_AGE},
pending::Pending,
received::{IdContinuity, Received, ReceivedIdError},
received::{IdContinuity, Received},
};
use super::book::{Connection, ConnectionBook};
use crate::{header::PackageId, record::DeliveryRecord, tasks::OutDatagram, Reliability};

// TODO rename
mod confirms;
mod pending;
mod received;

#[derive(Clone)]
pub(crate) struct Confirmations {
book: Arc<Mutex<ConnectionBook<ReceiveHandler>>>,
pub(crate) struct DeliveryHandler {
book: Arc<Mutex<ConnectionBook<ConnDeliveryHandler>>>,
}

impl Confirmations {
impl DeliveryHandler {
pub(crate) fn new() -> Self {
Self {
book: Arc::new(Mutex::new(ConnectionBook::new())),
Expand Down Expand Up @@ -76,7 +80,7 @@ impl Confirmations {
}

pub(crate) struct ReceiveHandlerGuard<'a> {
guard: MutexGuard<'a, ConnectionBook<ReceiveHandler>>,
guard: MutexGuard<'a, ConnectionBook<ConnDeliveryHandler>>,
}

impl<'a> ReceiveHandlerGuard<'a> {
Expand All @@ -94,18 +98,18 @@ impl<'a> ReceiveHandlerGuard<'a> {
buf: &'buf mut [u8],
) -> Result<Deliveries<'_, 'buf>, ReceivedIdError> {
self.guard
.update(record.time(), addr, ReceiveHandler::new)
.update(record.time(), addr, ConnDeliveryHandler::new)
.push(record, data, buf)
}
}

struct ReceiveHandler {
struct ConnDeliveryHandler {
received: Received,
pending: Pending,
confirms: ConfirmsBuffer,
}

impl ReceiveHandler {
impl ConnDeliveryHandler {
fn new() -> Self {
Self {
received: Received::new(),
Expand Down Expand Up @@ -165,7 +169,7 @@ impl ReceiveHandler {
}
}

impl Connection for ReceiveHandler {
impl Connection for ConnDeliveryHandler {
fn pending(&self) -> bool {
!self.confirms.is_empty()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::BTreeMap;

use super::databuf::DataBuf;
use crate::{header::PackageId, record::DeliveryRecord};
use crate::{connection::databuf::DataBuf, header::PackageId, record::DeliveryRecord};

/// Buffer for packages delivered out-of-order and thus awaiting the right
/// moment to be delivered.
Expand Down
File renamed without changes.
Loading

0 comments on commit a87cb55

Please sign in to comment.