sock: Arc<net::UdpSocket>,
}
+#[async_trait]
impl UdpSender for Sender {
async fn send(&self, data: Vec<u8>) -> io::Result<()> {
self.sock.send(&data).await?;
sock: Arc<net::UdpSocket>,
}
+#[async_trait]
impl UdpReceiver for Receiver {
async fn recv(&self) -> io::Result<Vec<u8>> {
let mut buffer = Vec::new();
#![feature(yeet_expr)]
#![feature(cursor_remaining)]
#![feature(hash_drain_filter)]
-#![feature(async_fn_in_trait)]
mod client;
pub mod error;
mod recv_worker;
+use async_trait::async_trait;
use byteorder::{BigEndian, WriteBytesExt};
pub use client::{connect, Sender as Client};
use num_enum::TryFromPrimitive;
pub const INIT_SEQNUM: u16 = 65500;
pub const TIMEOUT: u64 = 30;
+#[async_trait]
pub trait UdpSender: Send + Sync + 'static {
async fn send(&self, data: Vec<u8>) -> io::Result<()>;
}
+#[async_trait]
pub trait UdpReceiver: Send + Sync + 'static {
async fn recv(&self) -> io::Result<Vec<u8>>;
}
}
}
+impl<S: UdpSender> ops::DerefMut for RudpReceiver<S> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.pkt_rx
+ }
+}
+
pub fn new<S: UdpSender, R: UdpReceiver>(
id: u16,
remote_id: u16,
});
let recv_share = Arc::clone(&share);
- tokio::spawn(async { recv_worker::RecvWorker::new(udp_rx, recv_share, pkt_tx).await });
+ tokio::spawn(async {
+ let worker = recv_worker::RecvWorker::new(udp_rx, recv_share, pkt_tx);
+ worker.run().await;
+ });
(
RudpSender {
#[tokio::main]
async fn main() -> io::Result<()> {
//println!("{}", x.deep_size_of());
- let (tx, rx) = connect("127.0.0.1:30000").await?;
+ let (tx, mut rx) = connect("127.0.0.1:30000").await?;
let mut mtpkt = vec![];
mtpkt.write_u16::<BigEndian>(2)?; // high level type
}
impl<R: UdpReceiver, S: UdpSender> RecvWorker<R, S> {
- pub async fn new(udp_rx: R, share: Arc<RudpShare<S>>, pkt_tx: mpsc::UnboundedSender<InPkt>) {
+ pub fn new(udp_rx: R, share: Arc<RudpShare<S>>, pkt_tx: mpsc::UnboundedSender<InPkt>) -> Self {
Self {
udp_rx,
share,
.collect(),
),
}
- .run()
- .await
}
pub async fn run(&self) {