use bytes::{Buf, BufMut};
use crate::types::{self, ProtocolError};
use super::Packet;
#[derive(Debug, Clone)]
pub struct StatusRequestPacket;
impl Packet for StatusRequestPacket {
const PACKET_ID: i32 = 0x00;
fn decode(_buf: &mut impl Buf) -> Result<Self, ProtocolError> {
Ok(Self)
}
fn encode(&self, _buf: &mut impl BufMut) {}
}
#[derive(Debug, Clone)]
pub struct StatusResponsePacket {
pub json: String,
}
impl Packet for StatusResponsePacket {
const PACKET_ID: i32 = 0x00;
fn decode(buf: &mut impl Buf) -> Result<Self, ProtocolError> {
let json = types::read_string(buf)?;
Ok(Self { json })
}
fn encode(&self, buf: &mut impl BufMut) {
types::write_string(buf, &self.json);
}
}
#[derive(Debug, Clone)]
pub struct PingRequestPacket {
pub payload: i64,
}
impl Packet for PingRequestPacket {
const PACKET_ID: i32 = 0x01;
fn decode(buf: &mut impl Buf) -> Result<Self, ProtocolError> {
if buf.remaining() < 8 {
return Err(ProtocolError::UnexpectedEof);
}
Ok(Self {
payload: buf.get_i64(),
})
}
fn encode(&self, buf: &mut impl BufMut) {
buf.put_i64(self.payload);
}
}
#[derive(Debug, Clone)]
pub struct PongResponsePacket {
pub payload: i64,
}
impl Packet for PongResponsePacket {
const PACKET_ID: i32 = 0x01;
fn decode(buf: &mut impl Buf) -> Result<Self, ProtocolError> {
if buf.remaining() < 8 {
return Err(ProtocolError::UnexpectedEof);
}
Ok(Self {
payload: buf.get_i64(),
})
}
fn encode(&self, buf: &mut impl BufMut) {
buf.put_i64(self.payload);
}
}