use crate::protocol::address::Address;
use bytes::{BufMut, BytesMut};
use std::io::Result;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
#[derive(Clone, Debug)]
pub struct UdpHeader {
pub frag: u8,
pub address: Address,
}
impl UdpHeader {
pub fn new(frag: u8, address: Address) -> Self {
Self { frag, address }
}
pub async fn from_stream<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
let mut buf = [0; 3];
r.read_exact(&mut buf).await?;
let frag = buf[2];
let address = Address::from_stream(r).await?;
Ok(Self { frag, address })
}
pub async fn write_to<W: AsyncWrite + Unpin>(&self, w: &mut W) -> Result<()> {
let mut buf = BytesMut::with_capacity(self.serialized_len());
self.write_to_buf(&mut buf);
w.write_all(&buf).await
}
pub fn write_to_buf<B: BufMut>(&self, buf: &mut B) {
buf.put_bytes(0x00, 2);
buf.put_u8(self.frag);
self.address.write_to_buf(buf);
}
pub fn serialized_len(&self) -> usize {
3 + self.address.serialized_len()
}
pub const fn max_serialized_len() -> usize {
3 + Address::max_serialized_len()
}
}