datagram_connections/
host_to_client.rs1use crate::{ConnectResponse, HostToClientPacketHeader, InChallengeCommand};
2use flood_rs::{ReadOctetStream, WriteOctetStream};
3use std::io;
4
5#[repr(u8)]
6pub enum HostToClientCommand {
7 Challenge = 0x11,
8 Connect = 0x12,
9 Packet = 0x13,
10}
11
12impl TryFrom<u8> for HostToClientCommand {
13 type Error = io::Error;
14
15 fn try_from(value: u8) -> io::Result<Self> {
16 match value {
17 0x11 => Ok(Self::Challenge),
18 0x12 => Ok(Self::Connect),
19 0x13 => Ok(Self::Packet),
20 _ => Err(io::Error::new(
21 std::io::ErrorKind::InvalidData,
22 format!("Unknown HostToClient DatagramConnections Command {}", value),
23 )),
24 }
25 }
26}
27
28#[derive(Debug)]
29pub enum HostToClientCommands {
30 ChallengeType(InChallengeCommand),
31 ConnectType(ConnectResponse),
32 PacketType(HostToClientPacketHeader),
33}
34
35impl HostToClientCommands {
36 #[allow(unused)]
37 pub fn to_octet(&self) -> HostToClientCommand {
38 match self {
39 Self::ChallengeType(_) => HostToClientCommand::Challenge,
40 Self::ConnectType(_) => HostToClientCommand::Connect,
41 Self::PacketType(_) => HostToClientCommand::Packet,
42 }
43 }
44
45 #[allow(unused)]
46 pub fn to_stream(&self, stream: &mut impl WriteOctetStream) -> io::Result<()> {
47 stream.write_u8(self.to_octet() as u8)?;
48 match self {
49 Self::ChallengeType(client_to_host_challenge) => {
50 client_to_host_challenge.to_stream(stream)
51 }
52 Self::ConnectType(connect_command) => connect_command.to_stream(stream),
53 Self::PacketType(client_to_host_packet) => client_to_host_packet.0.to_stream(stream),
54 }
55 }
56
57 pub fn from_stream(stream: &mut impl ReadOctetStream) -> io::Result<Self> {
58 let command_value = stream.read_u8()?;
59 let command = HostToClientCommand::try_from(command_value)?;
60 let x = match command {
61 HostToClientCommand::Challenge => {
62 Self::ChallengeType(InChallengeCommand::from_stream(stream)?)
63 }
64 HostToClientCommand::Connect => {
65 Self::ConnectType(ConnectResponse::from_stream(stream)?)
66 }
67 HostToClientCommand::Packet => {
68 Self::PacketType(HostToClientPacketHeader::from_stream(stream)?)
69 }
70 };
71 Ok(x)
72 }
73}