use async_trait::async_trait;
use delegate::delegate;
use num_enum::TryFromPrimitive;
-use std::{io, sync::Arc};
+use std::{borrow::Cow, io, sync::Arc};
use tokio::sync::mpsc;
pub const PROTO_ID: u32 = 0x4f457403;
}
#[derive(Debug)]
-pub struct Pkt<T> {
+pub struct Pkt<'a> {
pub unrel: bool,
pub chan: u8,
- pub data: T,
+ pub data: Cow<'a, [u8]>,
}
-pub type InPkt = Result<Pkt<Vec<u8>>, error::Error>;
+pub type InPkt = Result<Pkt<'static>, Error>;
#[derive(Debug)]
pub struct RudpReceiver<S: UdpSender> {
use async_recursion::async_recursion;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::{
+ borrow::Cow,
cell::OnceCell,
collections::HashMap,
io,
Pkt {
unrel: true,
chan: 0,
- data: &[CtlType::Disco as u8],
+ data: Cow::Borrowed(&[CtlType::Disco as u8]),
},
)
.await
self.pkt_tx.send(Ok(Pkt {
chan: chan.num,
unrel,
- data: cursor.remaining_slice().into(),
+ data: Cow::Owned(cursor.remaining_slice().into()),
}))?;
}
PktType::Split => {
Pkt {
unrel: true,
chan: chan.num,
- data: &ack_data,
+ data: Cow::Borrowed(&ack_data),
},
)
.await?;
pub type AckResult = io::Result<Option<watch::Receiver<bool>>>;
impl<S: UdpSender> RudpSender<S> {
- pub async fn send(&self, pkt: Pkt<&[u8]>) -> AckResult {
+ pub async fn send(&self, pkt: Pkt<'_>) -> AckResult {
self.share.send(PktType::Orig, pkt).await // TODO: splits
}
}
impl<S: UdpSender> RudpShare<S> {
- pub async fn send(&self, tp: PktType, pkt: Pkt<&[u8]>) -> AckResult {
+ pub async fn send(&self, tp: PktType, pkt: Pkt<'_>) -> AckResult {
let mut buf = Vec::with_capacity(4 + 2 + 1 + 1 + 2 + 1 + pkt.data.len());
buf.write_u32::<BigEndian>(PROTO_ID)?;
buf.write_u16::<BigEndian>(*self.remote_id.read().await)?;
}
buf.write_u8(tp as u8)?;
- buf.write_all(pkt.data)?;
+ buf.write_all(pkt.data.as_ref())?;
self.send_raw(&buf).await?;
use super::*;
-use std::{collections::HashMap, io, sync::Arc, time::Duration};
+use std::{borrow::Cow, collections::HashMap, io, sync::Arc, time::Duration};
use tokio::{
sync::{mpsc, watch, Mutex, RwLock},
task::JoinSet,
Pkt {
chan: 0,
unrel: false,
- data: &[CtlType::Ping as u8],
+ data: Cow::Borrowed(&[CtlType::Ping as u8]),
},
)
.await