#packet-stream #encode-decode #derive #default #enums #channel #packetize

macro no-std packetize_derive

encode and decode your packet with stream channel

97 breaking releases

new 0.101.0 Dec 15, 2024
0.99.0 Nov 30, 2024
0.98.0 Nov 30, 2024
0.51.0 Jul 30, 2024

#5 in #packet-stream

Download history 155/week @ 2024-08-25 9/week @ 2024-09-01 104/week @ 2024-09-15 227/week @ 2024-09-22 55/week @ 2024-09-29 6/week @ 2024-10-06 197/week @ 2024-10-20 14/week @ 2024-10-27 4/week @ 2024-11-03 156/week @ 2024-11-24 107/week @ 2024-12-01 226/week @ 2024-12-08

489 downloads per month
Used in packetize

MIT license

21KB
401 lines

packetize

  • Supports fast packet encoding and decoding for network packets.
  • Features packet framing with macros.
  • Designed for multiple packet formats (e.g., WebSocket, Minecraft, TCP)

#[packet_stream]
#[derive(Default)]
pub enum ConnectionState {
    #[default]
    HandShake(#[change_state_to(Idle)] HandShakeC2s),
    Idle(
        RoomListRequestC2s,
        RoomListResponseS2c<'_>,
        RoomJoinRequestC2s,
        RoomCreateRequestC2s,
        RoomJoinResponseS2c<'_>,
        RoomCreateResponseS2c<'_>,
        #[change_state_to(Conf)] RoomJoinedAckC2s,
    ),
    Conf(
        #[change_state_to(Disconnected)] DisconnectedConfS2c,
        ChatConfS2c,
        ChatConfC2s,
        GameStartS2c,
        #[change_state_to(Play)] GameStartAckC2s,
        LeaveRoomConfC2s,
        LeaveRoomConfS2c,
        #[change_state_to(Idle)] LeaveRoomConfAckS2c,
        #[change_state_to(Idle)] LeaveRoomConfAckC2s,
    ),
    Play(
        #[change_state_to(Disconnected)] DisconnectedPlayS2c,
        ChatPlayC2s,
        ChatPlayS2c,
        LeaveRoomPlayC2s,
        LeaveRoomPlayS2c,
        #[change_state_to(Idle)] LeaveRoomPlayAckS2c,
        #[change_state_to(Idle)] LeaveRoomPlayAckC2s,
    ),
    Disconnected,
}

use fastbuf::Buffer;
use packetize::ClientBoundPacketStream;
use packetize::{streaming_packets, Decode, Encode, SimplePacketStreamFormat};

#[streaming_packets]
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum PacketStreamState {
    #[default]
    HandShake(#[change_state_to(Login)] HandShakeS2c),
    Login(LoginRequestS2c, #[id(1)] LoginSuccessC2s),
    //...
}

#[derive(Encode, Decode)]
pub struct HandShakeS2c {
    protocol_version: i32,
}

#[derive(Encode, Decode)]
pub struct LoginRequestS2c {}

#[derive(Encode, Decode)]
pub struct LoginSuccessC2s {}

let buf = &mut Buffer::<100>::new();
let mut state = PacketStreamState::HandShake;
state
    .encode_client_bound_packet(
        &HandShakeS2c {
            protocol_version: 123,
        }
        .into(),
        buf,
        &mut SimplePacketStreamFormat,
    )
    .unwrap();
assert_eq!(state, PacketStreamState::Login);

Dependencies

~225–670KB
~16K SLoC