Skip to content

Commit

Permalink
feat: closes #28, last additions to be made
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bv committed Jan 27, 2022
1 parent aca4286 commit 6b3e2e7
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 105 deletions.
17 changes: 12 additions & 5 deletions src/connection/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::VecDeque, sync::Arc, time::SystemTime};

use crate::{
internal::{
frame::reliability::Reliability,
queue::{Queue, SendPriority},
RakConnHandler, RakConnHandlerMeta,
},
Expand Down Expand Up @@ -90,6 +91,13 @@ impl Connection {
}
}

/// Get the maximum allowed size of a entire frame packet.
/// This is the MTU - the size of all possible raknet headers,
/// so: `40 (Datagram Protocol) + 20 (Raknet)`
pub fn max_frame_size(&self) -> usize {
self.mtu as usize - 60
}

/// Adds the given stream to the connection's queue by priority.
/// If instant is set to "true" the packet will be sent immediately.
pub fn send(&mut self, stream: Vec<u8>, instant: bool) {
Expand All @@ -106,7 +114,7 @@ impl Connection {
/// Packets here will be batched together and sent in frames.
pub fn send_stream(&mut self, stream: Vec<u8>, priority: SendPriority) {
if priority == SendPriority::Immediate {
RakConnHandler::send_as_framed(self, stream);
RakConnHandler::send_framed(self, stream, Reliability::ReliableOrd);
} else {
self.queue.push(stream, priority);
}
Expand All @@ -132,8 +140,7 @@ impl Connection {
pub fn send_frame(&mut self, stream: Vec<u8>, priority: SendPriority) {
if priority == SendPriority::Immediate {
// we need to batch this frame immediately.
let frame = RakConnHandler::create_frame(self, stream).unwrap();
self.send_immediate(frame.fparse());
RakConnHandler::send_framed(self, stream, Reliability::ReliableOrd);
} else {
// we need to batch this frame.
self.queue.push(stream, priority);
Expand Down Expand Up @@ -257,14 +264,14 @@ impl Connection {
if self.state.is_reliable() {
// we need to update the state of the connection.
// check whether or not we're becoming un-reliable.
if self.recv_time.elapsed().unwrap().as_secs() > 5 {
if self.recv_time.elapsed().unwrap().as_secs() > 8 {
// we're becoming un-reliable.
self.state = ConnectionState::TimingOut;
}
// tick the rakhandler
RakConnHandler::tick(self);
} else {
if self.recv_time.elapsed().unwrap().as_secs() >= 10 {
if self.recv_time.elapsed().unwrap().as_secs() >= 15 {
// we're not reliable anymore.
self.state = ConnectionState::Disconnected;
self.disconnect("Timed Out", true);
Expand Down
10 changes: 8 additions & 2 deletions src/internal/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub struct FramePacket {
/// The frames for this frame packet, not to exceed the mtu size.
pub frames: Vec<Frame>,

/// This is internal use only.
pub(crate) reliability: Reliability,

/// This is internal use only.
pub(crate) byte_length: usize,
}
Expand All @@ -34,6 +37,7 @@ impl FramePacket {
Self {
sequence: 0,
frames: Vec::new(),
reliability: Reliability::ReliableOrd,
byte_length: 0,
}
}
Expand Down Expand Up @@ -98,6 +102,7 @@ impl Streamable for FramePacket {
loop {
if stream.position() > source.len() as u64 {
return Ok(FramePacket {
reliability: Reliability::ReliableOrd,
sequence,
frames,
byte_length: 0,
Expand All @@ -106,6 +111,7 @@ impl Streamable for FramePacket {

if stream.position() == source.len() as u64 {
break Ok(FramePacket {
reliability: Reliability::ReliableOrd,
sequence,
frames,
byte_length: 0,
Expand Down Expand Up @@ -232,7 +238,7 @@ impl Streamable for Frame {

// check whether or not this frame is ordered, if it is, read the order index
// and order channel
if frame.reliability.is_ordered() {
if frame.reliability.is_sequenced_or_ordered() {
frame.order_index = Some(stream.read_u24::<LittleEndian>()?);
frame.order_channel = Some(stream.read_u8()?);
}
Expand Down Expand Up @@ -285,7 +291,7 @@ impl Streamable for Frame {

// check whether or not this frame is ordered, if it is, write the order index
// and order channel
if self.reliability.is_ordered() {
if self.reliability.is_sequenced_or_ordered() {
stream.write_u24::<LittleEndian>(self.order_index.unwrap())?;
stream.write_u8(self.order_channel.unwrap())?;
}
Expand Down
53 changes: 53 additions & 0 deletions src/internal/frame/reliability/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::{collections::HashMap, time::SystemTime};

// this is a cache store for packets,
// any packets in here will be ticked, and over time, will be resent, or discarded
#[derive(Debug, Clone)]
pub struct CacheStore<K, V> {
pub(crate) store: HashMap<K, (SystemTime, Vec<V>)>,
}

impl<K, V> CacheStore<K, V>
where
K: std::hash::Hash + std::cmp::Eq,
V: ?Sized + Clone,
{
pub fn new() -> Self {
Self {
store: HashMap::new(),
}
}

pub fn add(&mut self, sequence: K, buffer: V) {
let ent = self
.store
.entry(sequence)
.or_insert((SystemTime::now(), Vec::new()));
ent.1.push(buffer);
}

pub fn add_bulk(&mut self, sequence: K, buffers: Vec<V>) {
let ent = self
.store
.entry(sequence)
.or_insert((SystemTime::now(), Vec::new()));
ent.1.extend(buffers);
}

// clear old packets from the cache and return them
pub fn flush(&mut self) -> Vec<(K, SystemTime, Vec<V>)> {
let mut flushed = Vec::new();
for (sequence, (time, frames)) in self.store.drain() {
flushed.push((sequence, time, frames));
}
return flushed;
}

pub fn flush_key(&mut self, key: K) -> Option<(SystemTime, Vec<V>)> {
self.store.remove(&key)
}

pub fn has(&self, key: &K) -> bool {
self.store.contains_key(key)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Clone, Debug)]
pub mod cache;

#[derive(Clone, Debug, Copy)]
#[repr(u8)]
pub enum Reliability {
/// Unreliable (with no ack)
Expand Down Expand Up @@ -47,9 +49,7 @@ impl Reliability {
/// Whether or not the packet is ordered.
pub fn is_ordered(&self) -> bool {
match self {
Self::UnreliableSeq | Self::ReliableOrd | Self::ReliableSeq | Self::ReliableOrdAck => {
true
}
Self::UnreliableSeq | Self::ReliableOrd | Self::ReliableOrdAck => true,
_ => false,
}
}
Expand Down Expand Up @@ -78,6 +78,15 @@ impl Reliability {
}
}

pub fn is_sequenced_or_ordered(&self) -> bool {
match self {
Self::UnreliableSeq | Self::ReliableSeq | Self::ReliableOrd | Self::ReliableOrdAck => {
true
}
_ => false,
}
}

/// Whether or not the packet has an acknowledgement.
pub fn is_ack(&self) -> bool {
match self {
Expand Down
Loading

0 comments on commit 6b3e2e7

Please sign in to comment.