Skip to content

Commit a9ba7bf

Browse files
author
VictoremWinbringer
committed
cange protocol version and added ask and sequence for lost packets
1 parent 2f085c0 commit a9ba7bf

File tree

7 files changed

+64
-39
lines changed

7 files changed

+64
-39
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "victorem"
3-
version = "0.7.1"
3+
version = "0.8.0"
44
edition = "2018"
55
description = "UPD Game Server Framework"
66
license = "MIT"

src/business_logic_layer/id.rs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::entities::{CommandPacket, Exception, StatePacket};
22
use std::collections::HashMap;
3+
use std::cmp::max;
34

45
pub trait IWithId {
56
fn get(&self) -> u32;
@@ -61,20 +62,22 @@ impl Filter {
6162
}
6263

6364
pub struct Arranger<T: IWithId> {
64-
last_id: u32,
65+
last_valid_packet_id: u32,
6566
packets: HashMap<u32, T>,
67+
last_received_packet_id: u32,
6668
received: Vec<u32>,
6769
}
6870

69-
const MAX_ID_BREAK: u32 = 64;
70-
const MAX_SAVED: usize = (MAX_ID_BREAK * 2) as usize;
71+
const MAX_ID_BREAK: u32 = 32;
72+
const MAX_SAVED: usize = (MAX_ID_BREAK * 4) as usize;
7173
const MAX_RECEIVED: usize = MAX_SAVED;
7274

7375
impl<T: IWithId> Arranger<T> {
7476
pub fn new(last_id: u32) -> Arranger<T> {
7577
Arranger {
76-
last_id,
78+
last_valid_packet_id: last_id,
7779
packets: HashMap::new(),
80+
last_received_packet_id: 0,
7881
received: Vec::new(),
7982
}
8083
}
@@ -87,16 +90,18 @@ impl<T: IWithId> Arranger<T> {
8790

8891
pub fn add(&mut self, data: T) -> Result<(), Exception> {
8992
let id = data.get();
90-
if id + MAX_ID_BREAK < self.last_id || self.last_id + MAX_ID_BREAK < id {
91-
self.last_id = id;
92-
self.received = Vec::new()
93+
if id > self.last_received_packet_id + MAX_ID_BREAK {
94+
self.packets = HashMap::new();
95+
self.last_valid_packet_id = id - 1;
96+
self.received = Vec::new();
9397
}
9498
self.clear_if_overflows();
95-
if self.received.contains(&data.get()) {
99+
if self.received.contains(&id) || data.get() + MAX_ID_BREAK < self.last_received_packet_id {
96100
Err(Exception::NotOrderedPacketError)
97101
} else {
98-
self.received.push(data.get());
99-
self.packets.entry(data.get()).or_insert(data);
102+
self.last_received_packet_id = id;
103+
self.packets.entry(id).or_insert(data);
104+
self.received.push(id);
100105
Ok(())
101106
}
102107
}
@@ -111,7 +116,7 @@ impl<T: IWithId> Arranger<T> {
111116
.min_by(|x, y| x.0.cmp(y.0))
112117
.map(|x| *x.0);
113118
min_id.map(|x| {
114-
self.last_id = if x > 0 { x - 1 } else { 0 };
119+
self.last_valid_packet_id = if x > 0 { x - 1 } else { 0 };
115120
});
116121
self.packets = self
117122
.packets
@@ -120,13 +125,9 @@ impl<T: IWithId> Arranger<T> {
120125
.skip(MAX_SAVED / 2)
121126
.collect();
122127
}
128+
123129
if self.received.len() > MAX_RECEIVED {
124-
self.received = self
125-
.received
126-
.clone()
127-
.into_iter()
128-
.skip(MAX_RECEIVED / 2)
129-
.collect();
130+
self.received = self.received.clone().into_iter().skip(MAX_RECEIVED / 2).collect();
130131
}
131132
}
132133

@@ -135,25 +136,27 @@ impl<T: IWithId> Arranger<T> {
135136
.iter()
136137
.map(|p| p.get())
137138
.max()
138-
.map(|max| self.last_id = max);
139+
.map(|max| self.last_valid_packet_id = max);
139140
}
140141

141-
pub fn get_lost(&self) -> Vec<u32> {
142-
self.packets
143-
.keys()
144-
.max()
145-
.map(|max| (self.last_id + 1, *max))
146-
.map(|(min, max)| min..=max)
147-
.map(|range| range.filter(|i| !self.packets.contains_key(i)))
148-
.map(|filter| {
149-
let ids: Vec<u32> = filter.collect();
150-
ids
151-
})
152-
.unwrap_or_else(Vec::new)
142+
pub fn get_lost(&self) -> (u32, u32) {
143+
let max = self.last_received_packet_id;
144+
let mut id: u32 = 0;
145+
let mut j = 0;
146+
let mut i = max;
147+
while j < 32 && i > 0 {
148+
i -= 1;
149+
if !self.packets.contains_key(&i) {
150+
let mask = 1u32 << j;
151+
id |= mask;
152+
}
153+
j += 1;
154+
}
155+
(id, max)
153156
}
154157

155158
fn get_valid(&mut self) -> Vec<T> {
156-
let mut i = self.last_id + 1;
159+
let mut i = self.last_valid_packet_id + 1;
157160
let mut vec: Vec<T> = Vec::new();
158161
while self.packets.contains_key(&i) {
159162
vec.push(self.packets.remove(&i).unwrap());

src/business_logic_layer/mod.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,29 @@ impl Client {
6767
self.id_filter = Filter::new(0);
6868
}
6969
self.id_filter.filter(&state)?;
70-
let vec = self.cache.get_range(&state.lost_ids);
70+
let vec = self.get_lost(state.last_received, state.sequence);
7171
Ok((state.state, vec))
7272
}
73+
74+
fn get_lost(&mut self, max_id: u32, sequence: u32) -> Vec<CommandPacket> {
75+
if max_id == 0 || sequence == 0 {
76+
return Vec::new();
77+
}
78+
79+
let mut x = max_id;
80+
let mut y = 0;
81+
let mut ids = Vec::<u32>::new();
82+
while x > 0 && y < 32 {
83+
x -= 1;
84+
let mask = 1u32 << y;
85+
y += 1;
86+
let res = sequence & mask;
87+
if res > 0 {
88+
ids.push(x);
89+
}
90+
}
91+
self.cache.get_range(&ids)
92+
}
7393
}
7494

7595
pub struct Server {
@@ -95,12 +115,14 @@ impl Server {
95115
}
96116

97117
pub fn send(&mut self, state: Vec<u8>) -> StatePacket {
118+
let (sequence, last_id) = self.arranger.get_lost();
98119
StatePacket {
99120
protocol_id: self.protocol_id.get(),
100121
protocol_version: self.protocol_version.get(),
101122
id: self.id.generate(),
102-
lost_ids: self.arranger.get_lost(),
103-
state,
123+
state: state,
124+
last_received: last_id,
125+
sequence: sequence,
104126
session_key: self.key_generator.generate(),
105127
}
106128
}

src/business_logic_layer/version.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::entities::{CommandPacket, Exception, StatePacket};
22

3-
const PROTOCOL_VERSION: u8 = 1;
3+
const PROTOCOL_VERSION: u8 = 2;
44

55
pub trait IWithVersion {
66
fn get(&self) -> u8;

src/data_access_layer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub struct Cache {
145145
}
146146

147147
impl Cache {
148-
const MAX_SAVED: usize = 2000;
148+
const MAX_SAVED: usize = 200;
149149
pub fn new() -> Cache {
150150
Cache { data: Vec::new() }
151151
}

src/entities.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ pub struct StatePacket {
1919
pub protocol_id: u8,
2020
pub protocol_version: u8,
2121
pub id: u32,
22-
pub lost_ids: Vec<u32>,
2322
pub state: Vec<u8>,
2423
pub session_key: Duration,
24+
pub last_received: u32,
25+
pub sequence: u32
2526
}
2627

2728
#[derive(Debug)]

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub enum ServerEvent {
2222
}
2323

2424
pub type ContinueRunning = bool;
25-
pub type DisconnectThisClient = bool;
2625

2726
///Game to use with server must implement this trait.
2827
pub trait Game {

0 commit comments

Comments
 (0)