nimble_protocol/
host_to_client.rs

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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/nimble
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */

use crate::serialize::{
    CombinedSteps, InternalAllParticipantVectors, InternalAuthoritativeStepRange,
};
use crate::{ClientRequestId, SessionConnectionSecret};
use flood_rs::{Deserialize, ReadOctetStream, Serialize, WriteOctetStream};
use log::trace;
use nimble_blob_stream::prelude::SenderToReceiverFrontCommands;
use nimble_participant::ParticipantId;
use std::fmt::{Debug, Display, Formatter};
use std::io;
use tick_id::TickId;

#[repr(u8)]
#[allow(clippy::module_name_repetitions)] // TODO: Rename module or enum
pub enum HostToClientCommand {
    GameStep = 0x08,
    JoinGame = 0x09,
    DownloadGameState = 0x0B,
    BlobStreamChannel = 0x0C,
    Connect = 0x0D,
    Pong = 0x0E,
}

impl TryFrom<u8> for HostToClientCommand {
    type Error = io::Error;

    fn try_from(value: u8) -> io::Result<Self> {
        Ok(match value {
            0x09 => Self::JoinGame,
            0x08 => Self::GameStep,
            0x0B => Self::DownloadGameState,
            0x0C => Self::BlobStreamChannel,
            0x0D => Self::Connect,
            0x0E => Self::Pong,
            _ => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Unknown host to client command 0x{value:0X}"),
            ))?,
        })
    }
}

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct TickIdUtil;

impl TickIdUtil {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(tick_id: TickId, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u32(tick_id.0)
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<TickId> {
        Ok(TickId(stream.read_u32()?))
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct DownloadGameStateResponse {
    pub client_request: u8,
    pub tick_id: TickId,
    pub blob_stream_channel: u16,
}

impl Display for DownloadGameStateResponse {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "download game state response {} {} {}",
            self.client_request, self.tick_id, self.blob_stream_channel
        )
    }
}

impl DownloadGameStateResponse {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.client_request)?;
        TickIdUtil::to_stream(self.tick_id, stream)?;
        stream.write_u16(self.blob_stream_channel)
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            client_request: stream.read_u8()?,
            tick_id: TickIdUtil::from_stream(stream)?,
            blob_stream_channel: stream.read_u16()?,
        })
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct GameStatePart {
    pub blob_stream_command: SenderToReceiverFrontCommands,
}

#[derive(Debug)]
pub struct ConnectResponse {
    pub flags: u8,
    pub client_request_id: ClientRequestId,
}

impl ConnectResponse {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.flags)?;
        stream.write_u8(self.client_request_id.0)?;
        Ok(())
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            flags: stream.read_u8()?,
            client_request_id: ClientRequestId(stream.read_u8()?),
        })
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct ConnectionAccepted {
    pub flags: u8,
    pub response_to_request: ClientRequestId,
}

impl Display for ConnectionAccepted {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "connection accepted {} {}",
            self.flags, self.response_to_request
        )
    }
}

impl ConnectionAccepted {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.flags)?;
        self.response_to_request.serialize(stream)?;
        Ok(())
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            flags: stream.read_u8()?,
            response_to_request: ClientRequestId::deserialize(stream)?,
        })
    }
}

#[derive(Debug)]
pub struct PongInfo {
    pub lower_millis: u16,
}

impl Serialize for PongInfo {
    fn serialize(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u16(self.lower_millis)
    }
}

impl Deserialize for PongInfo {
    fn deserialize(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            lower_millis: stream.read_u16()?,
        })
    }
}

#[derive(Debug)]
#[allow(clippy::module_name_repetitions)] // TODO: Rename module or enum
pub enum HostToClientCommands<StepT: Deserialize + Serialize + Debug + Clone + Display> {
    JoinGame(JoinGameAccepted),
    GameStep(GameStepResponse<StepT>),
    DownloadGameState(DownloadGameStateResponse),
    BlobStreamChannel(SenderToReceiverFrontCommands),
    ConnectType(ConnectionAccepted),
    Pong(PongInfo),
}

impl<StepT: Clone + Debug + Serialize + Deserialize + Display> Serialize
    for HostToClientCommands<StepT>
{
    fn serialize(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.into())?;
        match self {
            Self::JoinGame(join_game_response) => join_game_response.to_stream(stream),
            Self::GameStep(game_step_response) => game_step_response.to_stream(stream),
            Self::DownloadGameState(download_game_state_response) => {
                download_game_state_response.to_stream(stream)
            }
            Self::BlobStreamChannel(blob_stream_command) => blob_stream_command.to_stream(stream),
            Self::ConnectType(connect_response) => connect_response.to_stream(stream),
            Self::Pong(pong_info) => pong_info.serialize(stream),
        }
    }
}

impl<StepT: Clone + Debug + Serialize + Deserialize + Display> Display
    for HostToClientCommands<StepT>
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::JoinGame(join_game_response) => {
                write!(f, "JoinGameResponse({join_game_response})")
            }
            Self::GameStep(game_step_response) => {
                write!(f, "GameStepResponse({game_step_response})")
            }
            Self::DownloadGameState(download_game_state_response) => {
                write!(f, "DownloadGameState({download_game_state_response})")
            }
            Self::BlobStreamChannel(blob_stream_command) => {
                write!(f, "BlobStreamChannel({blob_stream_command})")
            }
            Self::ConnectType(connect_response) => {
                write!(f, "ConnectResponse({connect_response})")
            }
            Self::Pong(pong_info) => write!(f, "Pong({pong_info:?})"),
        }
    }
}

impl<StepT: Clone + Debug + Serialize + Deserialize + Display> Deserialize
    for HostToClientCommands<StepT>
{
    fn deserialize(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        let command_value = stream.read_u8()?;
        let command = HostToClientCommand::try_from(command_value)?;
        Ok(match command {
            HostToClientCommand::JoinGame => Self::JoinGame(JoinGameAccepted::from_stream(stream)?),
            HostToClientCommand::GameStep => Self::GameStep(GameStepResponse::from_stream(stream)?),
            HostToClientCommand::DownloadGameState => {
                Self::DownloadGameState(DownloadGameStateResponse::from_stream(stream)?)
            }
            HostToClientCommand::BlobStreamChannel => {
                Self::BlobStreamChannel(SenderToReceiverFrontCommands::from_stream(stream)?)
            }
            HostToClientCommand::Connect => {
                Self::ConnectType(ConnectionAccepted::from_stream(stream)?)
            }
            HostToClientCommand::Pong => Self::Pong(PongInfo::deserialize(stream)?),
        })
    }
}

impl<StepT: Deserialize + Serialize + Debug + Display + Clone> From<&HostToClientCommands<StepT>>
    for u8
{
    fn from(command: &HostToClientCommands<StepT>) -> Self {
        match command {
            HostToClientCommands::JoinGame(_) => HostToClientCommand::JoinGame as Self,
            HostToClientCommands::GameStep(_) => HostToClientCommand::GameStep as Self,
            HostToClientCommands::DownloadGameState(_) => {
                HostToClientCommand::DownloadGameState as Self
            }
            HostToClientCommands::BlobStreamChannel(_) => {
                HostToClientCommand::BlobStreamChannel as Self
            }
            HostToClientCommands::ConnectType(_) => HostToClientCommand::Connect as Self,
            HostToClientCommands::Pong(_) => HostToClientCommand::Pong as Self,
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct PartyAndSessionSecret {
    pub session_secret: SessionConnectionSecret,
    pub party_id: u8,
}

impl PartyAndSessionSecret {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        self.session_secret.to_stream(stream)?;
        stream.write_u8(self.party_id)
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            session_secret: SessionConnectionSecret::from_stream(stream)?,
            party_id: stream.read_u8()?,
        })
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct JoinGameParticipant {
    pub local_index: u8,
    pub participant_id: ParticipantId,
}

impl JoinGameParticipant {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.local_index)?;
        self.participant_id.serialize(stream)?;
        Ok(())
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            local_index: stream.read_u8()?,
            participant_id: ParticipantId::deserialize(stream)?,
        })
    }
}

#[derive(Debug)]
pub struct JoinGameParticipants(pub Vec<JoinGameParticipant>);

impl JoinGameParticipants {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.0.len() as u8)?;
        for join_game_participant in &self.0 {
            join_game_participant.to_stream(stream)?;
        }
        Ok(())
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        let count = stream.read_u8()?;
        let mut vec = Vec::<JoinGameParticipant>::with_capacity(count as usize);
        for _ in 0..count {
            vec.push(JoinGameParticipant::from_stream(stream)?);
        }

        Ok(Self(vec))
    }
}

#[derive(Debug)]
pub struct JoinGameAccepted {
    pub client_request_id: ClientRequestId,
    pub party_and_session_secret: PartyAndSessionSecret,
    pub participants: JoinGameParticipants,
}

impl Display for JoinGameAccepted {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "JoinGameAccepted {} {:?} {:?}",
            self.client_request_id, self.party_and_session_secret, self.participants
        )
    }
}

impl JoinGameAccepted {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        self.client_request_id.serialize(stream)?;
        self.party_and_session_secret.to_stream(stream)?;
        self.participants.to_stream(stream)
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            client_request_id: ClientRequestId::deserialize(stream)?,
            party_and_session_secret: PartyAndSessionSecret::from_stream(stream)?,
            participants: JoinGameParticipants::from_stream(stream)?,
        })
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct GameStepResponseHeader {
    pub connection_buffer_count: u8,
    pub delta_buffer: i8,
    pub next_expected_tick_id: TickId,
}

impl Display for GameStepResponseHeader {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "game_step_response: count:{} expected:{} delta-buf:{}",
            self.connection_buffer_count, self.next_expected_tick_id, self.delta_buffer
        )
    }
}

impl GameStepResponseHeader {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.connection_buffer_count)?;
        stream.write_i8(self.delta_buffer)?;
        TickIdUtil::to_stream(self.next_expected_tick_id, stream)
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            connection_buffer_count: stream.read_u8()?,
            delta_buffer: stream.read_i8()?,
            next_expected_tick_id: TickIdUtil::from_stream(stream)?,
        })
    }
}

impl<StepT: Deserialize + Serialize + Debug + Clone + Display>
    InternalAuthoritativeStepRange<StepT>
{
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        stream.write_u8(self.delta_tick_id_from_previous)?;

        self.authoritative_steps.serialize(stream)?;

        Ok(())
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        let delta_steps = stream.read_u8()?;

        let authoritative_combined_step = InternalAllParticipantVectors::deserialize(stream)?;

        Ok(Self {
            delta_tick_id_from_previous: delta_steps,
            authoritative_steps: authoritative_combined_step,
        })
    }
}

#[derive(Debug)]
pub struct AuthoritativeStepRanges<StepT: Deserialize + Serialize + Debug + Clone + Display> {
    pub ranges: Vec<CombinedSteps<StepT>>,
}

impl<StepT: Deserialize + Serialize + Debug + Clone + Display> Display
    for AuthoritativeStepRanges<StepT>
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "auth_steps range-count:{} ranges:", self.ranges.len())?;

        for range in &self.ranges {
            write!(f, "\n{range}")?;
        }

        Ok(())
    }
}

impl<StepT: Deserialize + Serialize + Debug + Clone + Display> Serialize
    for AuthoritativeStepRanges<StepT>
{
    fn serialize(&self, stream: &mut impl WriteOctetStream) -> io::Result<()>
    where
        Self: Sized,
    {
        let mut converted_ranges = Vec::new();

        let root_tick_id = if self.ranges.is_empty() {
            TickId(0)
        } else {
            self.ranges[0].tick_id
        };
        let mut tick_id = root_tick_id;
        for auth_range in &self.ranges {
            let delta_ticks_from_previous = u8::try_from(auth_range.tick_id - tick_id)
                .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "ranges are incorrect"))?;
            tick_id = auth_range.tick_id + auth_range.steps.len() as u32;

            let internal = auth_range.to_internal();

            let range = InternalAuthoritativeStepRange {
                delta_tick_id_from_previous: delta_ticks_from_previous,
                authoritative_steps: internal,
            };
            converted_ranges.push(range);
        }

        let all_ranges = InternalAuthoritativeStepRanges {
            root_tick_id,
            ranges: converted_ranges,
        };

        all_ranges.to_stream(stream)
    }
}

impl<StepT: Deserialize + Serialize + Debug + Clone + Display> Deserialize
    for AuthoritativeStepRanges<StepT>
{
    fn deserialize(stream: &mut impl ReadOctetStream) -> io::Result<Self>
    where
        Self: Sized,
    {
        let internal_auth_step_ranges =
            InternalAuthoritativeStepRanges::<StepT>::from_stream(stream)?;
        let mut tick_id = internal_auth_step_ranges.root_tick_id;

        let mut converted_ranges = Vec::new();
        for internal_step_range in &internal_auth_step_ranges.ranges {
            tick_id += internal_step_range.delta_tick_id_from_previous as u32;

            let combined_steps =
                CombinedSteps::from_internal(&internal_step_range.authoritative_steps, tick_id);

            converted_ranges.push(combined_steps);
        }

        Ok(Self {
            ranges: converted_ranges,
        })
    }
}

#[derive(Debug)]
pub struct InternalAuthoritativeStepRanges<StepT: Deserialize + Serialize + Debug + Clone + Display>
{
    pub root_tick_id: TickId,
    pub ranges: Vec<InternalAuthoritativeStepRange<StepT>>,
}

impl<StepT: Deserialize + Serialize + Debug + Clone + Display>
    InternalAuthoritativeStepRanges<StepT>
{
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        TickIdUtil::to_stream(self.root_tick_id, stream)?;
        stream.write_u8(self.ranges.len() as u8)?;
        trace!(
            "tick_id: {} range_count: {}",
            self.root_tick_id,
            self.ranges.len()
        );
        for range in &self.ranges {
            range.to_stream(stream)?;
        }
        Ok(())
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        let root_tick_id = TickIdUtil::from_stream(stream)?;
        let range_count = stream.read_u8()?;

        let mut authoritative_step_ranges =
            Vec::<InternalAuthoritativeStepRange<StepT>>::with_capacity(range_count as usize);

        for _ in 0..range_count {
            authoritative_step_ranges.push(InternalAuthoritativeStepRange::from_stream(stream)?);
        }

        Ok(Self {
            root_tick_id,
            ranges: authoritative_step_ranges,
        })
    }
}

#[derive(Debug)]
pub struct GameStepResponse<StepT: Serialize + Deserialize + Debug + Clone + Display> {
    pub response_header: GameStepResponseHeader,
    pub authoritative_steps: AuthoritativeStepRanges<StepT>,
}

impl<StepT: Serialize + Deserialize + Debug + Clone + Display> Display for GameStepResponse<StepT> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "response: {} auth-steps: {}",
            self.response_header, self.authoritative_steps
        )
    }
}

impl<StepT: Deserialize + Serialize + Debug + Clone + Display> GameStepResponse<StepT> {
    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
        self.response_header.to_stream(stream)?;
        self.authoritative_steps.serialize(stream)
    }

    /// # Errors
    ///
    /// `io::Error` // TODO:
    pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
        Ok(Self {
            response_header: GameStepResponseHeader::from_stream(stream)?,
            authoritative_steps: AuthoritativeStepRanges::deserialize(stream)?,
        })
    }
}