feat: Use fixed size buffer for receiving packet
This commit is contained in:
+6
-4
@@ -310,8 +310,8 @@ impl Packet {
|
||||
}
|
||||
|
||||
pub async fn recv_from(socket: &UdpSocket) -> Result<(Self, SocketAddr), Error> {
|
||||
let mut buf = Vec::with_capacity(PACKET_SIZE);
|
||||
let (_, src) = socket.recv_buf_from(&mut buf).await?;
|
||||
let mut buf = [0u8; PACKET_SIZE];
|
||||
let (_, src) = socket.recv_from(&mut buf).await?;
|
||||
|
||||
let packet = buf.try_into()?;
|
||||
|
||||
@@ -342,10 +342,12 @@ impl TryFrom<&Packet> for Vec<u8> {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for Packet {
|
||||
impl TryFrom<[u8; PACKET_SIZE]> for Packet {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(buf: Vec<u8>) -> Result<Self, <Packet as TryFrom<Vec<u8>>>::Error> {
|
||||
fn try_from(
|
||||
buf: [u8; PACKET_SIZE],
|
||||
) -> Result<Self, <Packet as TryFrom<[u8; PACKET_SIZE]>>::Error> {
|
||||
let mut reader = BufReader::new(Cursor::new(buf));
|
||||
|
||||
Packet::decode(&mut reader)
|
||||
|
||||
Reference in New Issue
Block a user