use std::io::{Write, Read};
use polariton::packet::{Ping, Packet};
pub fn receive_packet<R: Read, C: core::fmt::Debug, CSI: polariton::serdes::CustomSerdes<C>>(socket: &mut R, ctx: &polariton::packet::SerdesContext<C, CSI>) -> std::io::Result<Packet<C>> {
let packet = Packet::parse(socket, ctx)?;
log::trace!("Received packet {:?}", packet);
#[cfg(feature = "debug-logs")]
log::debug!("Got {}", packet);
Ok(packet)
}
#[cfg(feature = "tokio-async")]
pub async fn receive_packet_async<R: tokio::io::AsyncRead + Unpin, C: Send + Sync + Clone + core::fmt::Debug + 'static, CSI: polariton::serdes::CustomSerdes<C>>(socket: &mut R, ctx: &polariton::packet::SerdesContext<C, CSI>) -> std::io::Result<Packet<C>> {
let packet = Packet::parse_async(socket, ctx).await?;
log::trace!("Received packet {:?}", packet);
#[cfg(feature = "debug-logs")]
log::debug!("Got {}", packet);
Ok(packet)
}
pub fn send_packet<W: Write, C: core::fmt::Debug, CSI: polariton::serdes::CustomSerdes<C>>(packet: &Packet<C>, socket: &mut W, ctx: &polariton::packet::SerdesContext<C, CSI>) -> std::io::Result<usize> {
log::trace!("Transmitting packet {:?}", packet);
#[cfg(feature = "debug-logs")]
log::debug!("Sending {}", packet);
let len = packet.dump(socket, ctx)?;
log::trace!("Wrote {} bytes to socket", len);
Ok(len)
}
#[cfg(feature = "tokio-async")]
pub async fn send_packet_async<W: tokio::io::AsyncWrite + Unpin, C: core::fmt::Debug, CSI: polariton::serdes::CustomSerdes<C>>(packet: &Packet<C>, socket: &mut W, ctx: &polariton::packet::SerdesContext<C, CSI>) -> std::io::Result<usize> {
log::trace!("Transmitting packet {:?}", packet);
#[cfg(feature = "debug-logs")]
log::debug!("Sending {}", packet);
let len = packet.dump_async(socket, ctx).await?;
log::trace!("Wrote {} bytes to socket", len);
Ok(len)
}
pub fn handle_ping<W: Write, C: core::fmt::Debug, CSI: polariton::serdes::CustomSerdes<C>>(mut ping: Ping, socket: &mut W, ctx: &polariton::packet::SerdesContext<C, CSI>) -> std::io::Result<usize> {
ping.tick2 = Some(ping.tick1);
send_packet(&Packet::Ping(ping), socket, ctx)
}
pub fn process_ping<C>(mut ping: Ping) -> Packet<C> {
ping.tick2 = Some(ping.tick1);
Packet::Ping(ping)
}
#[cfg(feature = "tokio-async")]
pub async fn handle_ping_async<W: tokio::io::AsyncWrite + Unpin, C: core::fmt::Debug, CSI: polariton::serdes::CustomSerdes<C>>(mut ping: Ping, socket: &mut W, ctx: &polariton::packet::SerdesContext<C, CSI>) -> std::io::Result<usize> {
ping.tick2 = Some(ping.tick1);
send_packet_async(&Packet::Ping(ping), socket, ctx).await
}