fastnet 0.3.1

Ultra-low latency encrypted networking for real-time games. TLS 1.3 + ChaCha20-Poly1305 with ~15µs RTT.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Delta compression for bandwidth-efficient state synchronization.
//!
//! This module provides delta encoding to send only the bytes that changed
//! between two states, dramatically reducing bandwidth for game state updates.
//!
//! # Algorithm
//!
//! Uses run-length encoding of differences:
//! - Scans for changed byte ranges
//! - Encodes as: [offset:u16][length:u16][data...]
//! - Multiple ranges packed sequentially
//!
//! # Example
//!
//! ```rust,ignore
//! use fastnet::net::fast::delta::{DeltaEncoder, DeltaDecoder};
//!
//! let mut encoder = DeltaEncoder::new();
//! let old_state = [0u8; 1000];
//! let new_state = /* slightly modified */;
//!
//! // Encode delta (typically 10-50 bytes instead of 1000)
//! let delta = encoder.encode(&old_state, &new_state);
//!
//! // Decode on receiver
//! let mut decoder = DeltaDecoder::new();
//! let mut reconstructed = old_state.clone();
//! decoder.apply(&delta, &mut reconstructed);
//! assert_eq!(reconstructed, new_state);
//! ```
//!
//! # Performance
//!
//! - Typical compression: 80-95% for game state
//! - Zero allocations in hot path (uses fixed buffers)
//! - O(n) encoding/decoding time

/// Maximum size for delta-encoded data.
pub const DELTA_MAX_SIZE: usize = 4096;

/// Minimum run of identical bytes to skip (tuned for typical game state).
const MIN_SKIP_RUN: usize = 4;

/// Delta encoding header.
/// 
/// Layout:
/// - [0-1]: Base sequence number (u16 LE) - which state this delta is relative to
/// - [2-3]: Total ranges count (u16 LE)
#[derive(Debug, Clone, Copy)]
pub struct DeltaHeader {
    pub base_seq: u16,
    pub range_count: u16,
}

impl DeltaHeader {
    pub const SIZE: usize = 4;

    #[inline]
    pub fn write_to(&self, buf: &mut [u8]) {
        buf[0..2].copy_from_slice(&self.base_seq.to_le_bytes());
        buf[2..4].copy_from_slice(&self.range_count.to_le_bytes());
    }

    #[inline]
    pub fn read_from(buf: &[u8]) -> Option<Self> {
        if buf.len() < Self::SIZE {
            return None;
        }
        Some(Self {
            base_seq: u16::from_le_bytes([buf[0], buf[1]]),
            range_count: u16::from_le_bytes([buf[2], buf[3]]),
        })
    }
}

/// A range of changed bytes.
#[derive(Debug, Clone, Copy)]
struct DeltaRange {
    offset: u16,
    length: u16,
}

impl DeltaRange {
    const SIZE: usize = 4;

    #[inline]
    fn write_to(&self, buf: &mut [u8]) {
        buf[0..2].copy_from_slice(&self.offset.to_le_bytes());
        buf[2..4].copy_from_slice(&self.length.to_le_bytes());
    }

    #[inline]
    fn read_from(buf: &[u8]) -> Option<Self> {
        if buf.len() < Self::SIZE {
            return None;
        }
        Some(Self {
            offset: u16::from_le_bytes([buf[0], buf[1]]),
            length: u16::from_le_bytes([buf[2], buf[3]]),
        })
    }
}

/// Delta encoder with zero-allocation hot path.
pub struct DeltaEncoder {
    buffer: Box<[u8; DELTA_MAX_SIZE]>,
    sequence: u16,
}

impl DeltaEncoder {
    /// Create a new delta encoder.
    pub fn new() -> Self {
        Self {
            buffer: Box::new([0u8; DELTA_MAX_SIZE]),
            sequence: 0,
        }
    }

    /// Encode the difference between old and new state.
    ///
    /// Returns the encoded delta and its length, or None if delta would be
    /// larger than the full state (in which case, send full state).
    ///
    /// # Arguments
    ///
    /// * `old` - Previous state (known to receiver)
    /// * `new` - New state to encode
    ///
    /// # Returns
    ///
    /// `Some((data, len))` if delta is smaller than full state, `None` otherwise.
    pub fn encode<'a>(&'a mut self, old: &[u8], new: &[u8]) -> Option<(&'a [u8], usize)> {
        if old.len() != new.len() || old.len() > u16::MAX as usize {
            return None;
        }

        let state_len = old.len();
        let mut write_pos = DeltaHeader::SIZE;
        let mut range_count: u16 = 0;

        let mut i = 0;
        while i < state_len {
            // Skip identical bytes
            while i < state_len && old[i] == new[i] {
                i += 1;
            }

            if i >= state_len {
                break;
            }

            // Found difference, scan for end of changed region
            let start = i;
            let mut end = i + 1;
            
            // Extend while different OR short identical runs (to coalesce nearby changes)
            while end < state_len {
                if old[end] != new[end] {
                    end += 1;
                } else {
                    // Check if there's another difference soon
                    let mut skip_end = end;
                    while skip_end < state_len && skip_end - end < MIN_SKIP_RUN && old[skip_end] == new[skip_end] {
                        skip_end += 1;
                    }
                    if skip_end < state_len && old[skip_end] != new[skip_end] {
                        // Coalesce
                        end = skip_end + 1;
                    } else {
                        break;
                    }
                }
            }

            let range_len = end - start;
            
            // Check if we have space
            let needed = DeltaRange::SIZE + range_len;
            if write_pos + needed > DELTA_MAX_SIZE {
                return None; // Delta too large
            }

            // Write range header
            let range = DeltaRange {
                offset: start as u16,
                length: range_len as u16,
            };
            range.write_to(&mut self.buffer[write_pos..]);
            write_pos += DeltaRange::SIZE;

            // Write changed data
            self.buffer[write_pos..write_pos + range_len].copy_from_slice(&new[start..end]);
            write_pos += range_len;

            range_count += 1;
            i = end;
        }

        // No changes?
        if range_count == 0 {
            return Some((&self.buffer[..DeltaHeader::SIZE], DeltaHeader::SIZE));
        }

        // Delta larger than full state? Send full state instead
        if write_pos >= state_len {
            return None;
        }

        // Write header
        let header = DeltaHeader {
            base_seq: self.sequence,
            range_count,
        };
        header.write_to(&mut self.buffer[..]);

        self.sequence = self.sequence.wrapping_add(1);

        Some((&self.buffer[..write_pos], write_pos))
    }

    /// Encode full state (when delta would be too large).
    ///
    /// Returns encoded data with a special marker indicating full state.
    pub fn encode_full<'a>(&'a mut self, state: &[u8]) -> Option<(&'a [u8], usize)> {
        if state.len() + DeltaHeader::SIZE > DELTA_MAX_SIZE {
            return None;
        }

        // Use range_count = 0xFFFF as marker for full state
        let header = DeltaHeader {
            base_seq: self.sequence,
            range_count: 0xFFFF,
        };
        header.write_to(&mut self.buffer[..]);

        self.buffer[DeltaHeader::SIZE..DeltaHeader::SIZE + state.len()].copy_from_slice(state);
        
        self.sequence = self.sequence.wrapping_add(1);

        let total = DeltaHeader::SIZE + state.len();
        Some((&self.buffer[..total], total))
    }

    /// Get current sequence number.
    #[inline]
    pub fn sequence(&self) -> u16 {
        self.sequence
    }

    /// Reset sequence counter.
    pub fn reset(&mut self) {
        self.sequence = 0;
    }
}

impl Default for DeltaEncoder {
    fn default() -> Self {
        Self::new()
    }
}

/// Delta decoder that applies deltas to reconstruct state.
pub struct DeltaDecoder {
    last_applied_seq: u16,
}

impl DeltaDecoder {
    /// Create a new decoder.
    pub fn new() -> Self {
        Self {
            last_applied_seq: 0,
        }
    }

    /// Apply a delta to the current state.
    ///
    /// # Arguments
    ///
    /// * `delta` - Encoded delta data
    /// * `state` - State buffer to modify in-place
    ///
    /// # Returns
    ///
    /// `Ok(())` on success, `Err` with description on failure.
    pub fn apply(&mut self, delta: &[u8], state: &mut [u8]) -> Result<(), &'static str> {
        let header = DeltaHeader::read_from(delta).ok_or("Invalid delta header")?;
        
        // Full state marker
        if header.range_count == 0xFFFF {
            let data = &delta[DeltaHeader::SIZE..];
            if data.len() != state.len() {
                return Err("Full state size mismatch");
            }
            state.copy_from_slice(data);
            self.last_applied_seq = header.base_seq;
            return Ok(());
        }

        let mut read_pos = DeltaHeader::SIZE;

        for _ in 0..header.range_count {
            if read_pos + DeltaRange::SIZE > delta.len() {
                return Err("Truncated delta range header");
            }

            let range = DeltaRange::read_from(&delta[read_pos..]).ok_or("Invalid range")?;
            read_pos += DeltaRange::SIZE;

            let offset = range.offset as usize;
            let length = range.length as usize;

            if offset + length > state.len() {
                return Err("Delta range exceeds state bounds");
            }
            if read_pos + length > delta.len() {
                return Err("Truncated delta data");
            }

            state[offset..offset + length].copy_from_slice(&delta[read_pos..read_pos + length]);
            read_pos += length;
        }

        self.last_applied_seq = header.base_seq;
        Ok(())
    }

    /// Get last applied sequence number.
    #[inline]
    pub fn last_seq(&self) -> u16 {
        self.last_applied_seq
    }
}

impl Default for DeltaDecoder {
    fn default() -> Self {
        Self::new()
    }
}

/// Compute compression ratio for a delta.
#[inline]
pub fn compression_ratio(original_size: usize, delta_size: usize) -> f32 {
    if original_size == 0 {
        return 0.0;
    }
    1.0 - (delta_size as f32 / original_size as f32)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_delta_no_changes() {
        let mut encoder = DeltaEncoder::new();
        let mut decoder = DeltaDecoder::new();

        let state = [1u8, 2, 3, 4, 5, 6, 7, 8];
        let (delta, len) = encoder.encode(&state, &state).unwrap();

        assert_eq!(len, DeltaHeader::SIZE); // Just header, no ranges

        let mut reconstructed = state;
        decoder.apply(&delta[..len], &mut reconstructed).unwrap();
        assert_eq!(reconstructed, state);
    }

    #[test]
    fn test_delta_single_byte_change() {
        let mut encoder = DeltaEncoder::new();
        let mut decoder = DeltaDecoder::new();

        let old = [0u8; 100];
        let mut new = old;
        new[50] = 0xFF;

        let (delta, len) = encoder.encode(&old, &new).unwrap();
        
        // Should be much smaller than 100 bytes
        assert!(len < 20);

        let mut reconstructed = old;
        decoder.apply(&delta[..len], &mut reconstructed).unwrap();
        assert_eq!(reconstructed, new);
    }

    #[test]
    fn test_delta_multiple_changes() {
        let mut encoder = DeltaEncoder::new();
        let mut decoder = DeltaDecoder::new();

        let old = [0u8; 200];
        let mut new = old;
        new[10..15].copy_from_slice(&[1, 2, 3, 4, 5]);
        new[100..105].copy_from_slice(&[6, 7, 8, 9, 10]);
        new[180..185].copy_from_slice(&[11, 12, 13, 14, 15]);

        let (delta, len) = encoder.encode(&old, &new).unwrap();
        
        // Much smaller than 200 bytes
        assert!(len < 50);
        
        let ratio = compression_ratio(200, len);
        assert!(ratio > 0.7); // At least 70% compression

        let mut reconstructed = old;
        decoder.apply(&delta[..len], &mut reconstructed).unwrap();
        assert_eq!(reconstructed, new);
    }

    #[test]
    fn test_delta_full_state_fallback() {
        let mut encoder = DeltaEncoder::new();
        let mut decoder = DeltaDecoder::new();

        // When everything changes, should return None
        let old = [0u8; 100];
        let new = [1u8; 100];

        let result = encoder.encode(&old, &new);
        assert!(result.is_none()); // Delta would be larger than state

        // Use full state encoding
        let (full, len) = encoder.encode_full(&new).unwrap();
        
        let mut reconstructed = old;
        decoder.apply(&full[..len], &mut reconstructed).unwrap();
        assert_eq!(reconstructed, new);
    }

    #[test]
    fn test_delta_header_roundtrip() {
        let header = DeltaHeader {
            base_seq: 12345,
            range_count: 42,
        };

        let mut buf = [0u8; DeltaHeader::SIZE];
        header.write_to(&mut buf);

        let parsed = DeltaHeader::read_from(&buf).unwrap();
        assert_eq!(parsed.base_seq, header.base_seq);
        assert_eq!(parsed.range_count, header.range_count);
    }

    #[test]
    fn test_delta_game_state_simulation() {
        let mut encoder = DeltaEncoder::new();
        let mut decoder = DeltaDecoder::new();

        // Simulate a game state: positions, health, etc.
        let old_state = vec![0u8; 1000];
        let mut new_state = old_state.clone();

        // Simulate player movement (position changes)
        new_state[0..12].copy_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); // pos
        new_state[100] = 95; // health changed

        let (delta, len) = encoder.encode(&old_state, &new_state).unwrap();
        
        let ratio = compression_ratio(1000, len);
        println!("Compression ratio: {:.1}%", ratio * 100.0);
        assert!(ratio > 0.9); // 90%+ compression

        let mut reconstructed = old_state.clone();
        decoder.apply(&delta[..len], &mut reconstructed).unwrap();
        assert_eq!(reconstructed, new_state);
    }
}