217 lines
6.7 KiB
Rust
217 lines
6.7 KiB
Rust
//! The ENet 1.3 datagram codec (`docs/PROTOCOL.md` 3.1). Multi-byte fields are big-endian.
|
|
|
|
/// Maximum datagram size accepted or produced (ENet's maximum MTU).
|
|
pub const MAX_DATAGRAM_BYTES: usize = 4096;
|
|
/// Minimum MTU ENet allows.
|
|
pub const MIN_MTU: u16 = 576;
|
|
/// Maximum number of commands in one datagram.
|
|
pub const MAX_COMMANDS_PER_DATAGRAM: usize = 32;
|
|
/// Maximum channel count ENet allows.
|
|
pub const MAX_CHANNEL_COUNT: u8 = 255;
|
|
/// Maximum peer id ENet allows.
|
|
pub const MAX_PEER_ID: u16 = 0x0FFF;
|
|
/// Peer id meaning "no peer yet" (used in the connect datagram).
|
|
pub const NO_PEER_ID: u16 = MAX_PEER_ID;
|
|
/// Minimum ENet window size.
|
|
pub const MIN_WINDOW_SIZE: u32 = 4096;
|
|
/// Maximum ENet window size.
|
|
pub const MAX_WINDOW_SIZE: u32 = 65536;
|
|
|
|
/// Header flag: the datagram carries `sent_time`.
|
|
pub const HEADER_FLAG_SENT_TIME: u16 = 1 << 15;
|
|
/// Header flag: the datagram is compressed (refused by this client).
|
|
pub const HEADER_FLAG_COMPRESSED: u16 = 1 << 14;
|
|
/// Command flag: the receiver must acknowledge.
|
|
pub const COMMAND_FLAG_ACKNOWLEDGE: u8 = 1 << 7;
|
|
/// Command flag: unsequenced delivery.
|
|
pub const COMMAND_FLAG_UNSEQUENCED: u8 = 1 << 6;
|
|
|
|
/// ENet command numbers (low 4 bits of the command byte).
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
#[repr(u8)]
|
|
pub enum CommandKind {
|
|
/// Acknowledge a reliable command.
|
|
Acknowledge = 1,
|
|
/// Open a connection.
|
|
Connect = 2,
|
|
/// Answer to connect.
|
|
VerifyConnect = 3,
|
|
/// Close.
|
|
Disconnect = 4,
|
|
/// Keep-alive.
|
|
Ping = 5,
|
|
/// Reliable data.
|
|
SendReliable = 6,
|
|
/// Unreliable sequenced data.
|
|
SendUnreliable = 7,
|
|
/// Reliable fragment.
|
|
SendFragment = 8,
|
|
/// Unsequenced data.
|
|
SendUnsequenced = 9,
|
|
/// Bandwidth limits.
|
|
BandwidthLimit = 10,
|
|
/// Throttle parameters.
|
|
ThrottleConfigure = 11,
|
|
/// Unreliable fragment.
|
|
SendUnreliableFragment = 12,
|
|
}
|
|
|
|
/// Parameters of connect and verify connect.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
pub struct ConnectParams {
|
|
/// Sender's peer id for the receiver to use.
|
|
pub outgoing_peer_id: u16,
|
|
/// Session id for incoming traffic.
|
|
pub incoming_session_id: u8,
|
|
/// Session id for outgoing traffic.
|
|
pub outgoing_session_id: u8,
|
|
/// MTU.
|
|
pub mtu: u32,
|
|
/// Reliable window size.
|
|
pub window_size: u32,
|
|
/// Channel count.
|
|
pub channel_count: u32,
|
|
/// Incoming bandwidth, bytes/s, 0 = unlimited.
|
|
pub incoming_bandwidth: u32,
|
|
/// Outgoing bandwidth, bytes/s, 0 = unlimited.
|
|
pub outgoing_bandwidth: u32,
|
|
/// Throttle interval, ms.
|
|
pub packet_throttle_interval: u32,
|
|
/// Throttle acceleration.
|
|
pub packet_throttle_acceleration: u32,
|
|
/// Throttle deceleration.
|
|
pub packet_throttle_deceleration: u32,
|
|
/// Random id that ties verify connect to connect.
|
|
pub connect_id: u32,
|
|
}
|
|
|
|
/// Fragment fields shared by reliable and unreliable fragments.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct Fragment {
|
|
/// Sequence number of the first fragment.
|
|
pub start_sequence: u16,
|
|
/// Number of fragments.
|
|
pub fragment_count: u32,
|
|
/// This fragment's number.
|
|
pub fragment_number: u32,
|
|
/// Length of the whole packet.
|
|
pub total_length: u32,
|
|
/// This fragment's offset in the whole packet.
|
|
pub fragment_offset: u32,
|
|
/// This fragment's bytes.
|
|
pub data: Vec<u8>,
|
|
}
|
|
|
|
/// One command with its 4-byte header fields.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct Command {
|
|
/// The receiver must acknowledge (flag bit 7).
|
|
pub acknowledge: bool,
|
|
/// Channel id (0xFF for connection-level commands).
|
|
pub channel_id: u8,
|
|
/// Reliable sequence number.
|
|
pub reliable_sequence: u16,
|
|
/// The command body.
|
|
pub body: CommandBody,
|
|
}
|
|
|
|
/// Command bodies.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub enum CommandBody {
|
|
/// Acknowledges `received_reliable_sequence` sent at `received_sent_time`.
|
|
Acknowledge {
|
|
/// Acknowledged sequence number.
|
|
received_reliable_sequence: u16,
|
|
/// The acknowledged datagram's `sent_time`.
|
|
received_sent_time: u16,
|
|
},
|
|
/// Connect, with its user data word.
|
|
Connect {
|
|
/// Connection parameters.
|
|
params: ConnectParams,
|
|
/// User data (UNKNOWN U9: what a GameStream host expects here).
|
|
data: u32,
|
|
},
|
|
/// Verify connect.
|
|
VerifyConnect(ConnectParams),
|
|
/// Disconnect with a user data word.
|
|
Disconnect {
|
|
/// User data.
|
|
data: u32,
|
|
},
|
|
/// Ping.
|
|
Ping,
|
|
/// Reliable data.
|
|
SendReliable(Vec<u8>),
|
|
/// Unreliable sequenced data.
|
|
SendUnreliable {
|
|
/// Unreliable sequence number.
|
|
unreliable_sequence: u16,
|
|
/// The data.
|
|
data: Vec<u8>,
|
|
},
|
|
/// Reliable fragment.
|
|
SendFragment(Fragment),
|
|
/// Unsequenced data.
|
|
SendUnsequenced {
|
|
/// Unsequenced group.
|
|
unsequenced_group: u16,
|
|
/// The data.
|
|
data: Vec<u8>,
|
|
},
|
|
/// Bandwidth limits, bytes/s.
|
|
BandwidthLimit {
|
|
/// Incoming.
|
|
incoming: u32,
|
|
/// Outgoing.
|
|
outgoing: u32,
|
|
},
|
|
/// Throttle parameters.
|
|
ThrottleConfigure {
|
|
/// Interval, ms.
|
|
interval: u32,
|
|
/// Acceleration.
|
|
acceleration: u32,
|
|
/// Deceleration.
|
|
deceleration: u32,
|
|
},
|
|
/// Unreliable fragment.
|
|
SendUnreliableFragment(Fragment),
|
|
}
|
|
|
|
/// One datagram: protocol header plus commands.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct Datagram {
|
|
/// Receiver's peer id (12 bits).
|
|
pub peer_id: u16,
|
|
/// Session id (2 bits).
|
|
pub session_id: u8,
|
|
/// `sent_time` (low 16 bits of the sender's ms clock), if present.
|
|
pub sent_time: Option<u16>,
|
|
/// The commands, at most [`MAX_COMMANDS_PER_DATAGRAM`].
|
|
pub commands: Vec<Command>,
|
|
}
|
|
|
|
/// Parses one datagram strictly: every length checked against the bytes present, unknown command numbers and
|
|
/// compressed datagrams refused.
|
|
///
|
|
/// # Errors
|
|
/// [`crate::Error::TooLarge`] over [`MAX_DATAGRAM_BYTES`] or [`MAX_COMMANDS_PER_DATAGRAM`],
|
|
/// [`crate::Error::Truncated`] for a cut command, [`crate::Error::UnexpectedValue`] for an unknown command or the
|
|
/// compressed flag, [`crate::Error::Malformed`] for a data length that does not match. Phase A: always
|
|
/// [`crate::Error::Unimplemented`].
|
|
pub fn parse_datagram(datagram: &[u8]) -> crate::Result<Datagram> {
|
|
let _ = datagram;
|
|
Err(crate::Error::Unimplemented)
|
|
}
|
|
|
|
/// Serializes one datagram.
|
|
///
|
|
/// # Errors
|
|
/// [`crate::Error::TooLarge`] over [`MAX_DATAGRAM_BYTES`] or [`MAX_COMMANDS_PER_DATAGRAM`]. Phase A: always
|
|
/// [`crate::Error::Unimplemented`].
|
|
pub fn encode_datagram(datagram: &Datagram) -> crate::Result<Vec<u8>> {
|
|
let _ = datagram;
|
|
Err(crate::Error::Unimplemented)
|
|
}
|