aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client.rs2
-rw-r--r--src/main.rs17
-rw-r--r--src/recv_worker.rs4
3 files changed, 17 insertions, 6 deletions
diff --git a/src/client.rs b/src/client.rs
index 97a18d7..d416e53 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -6,6 +6,7 @@ pub struct Sender {
sock: Arc<net::UdpSocket>,
}
+#[async_trait]
impl UdpSender for Sender {
async fn send(&self, data: Vec<u8>) -> io::Result<()> {
self.sock.send(&data).await?;
@@ -17,6 +18,7 @@ pub struct Receiver {
sock: Arc<net::UdpSocket>,
}
+#[async_trait]
impl UdpReceiver for Receiver {
async fn recv(&self) -> io::Result<Vec<u8>> {
let mut buffer = Vec::new();
diff --git a/src/main.rs b/src/main.rs
index 241d324..a190bcd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,11 +1,11 @@
#![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;
@@ -24,10 +24,12 @@ pub const REL_BUFFER: usize = 0x8000;
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>>;
}
@@ -118,6 +120,12 @@ impl<S: UdpSender> ops::Deref for RudpReceiver<S> {
}
}
+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,
@@ -134,7 +142,10 @@ pub fn new<S: UdpSender, R: UdpReceiver>(
});
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 {
@@ -149,7 +160,7 @@ pub fn new<S: UdpSender, R: UdpReceiver>(
#[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
diff --git a/src/recv_worker.rs b/src/recv_worker.rs
index 60cadeb..5a156eb 100644
--- a/src/recv_worker.rs
+++ b/src/recv_worker.rs
@@ -34,7 +34,7 @@ pub struct RecvWorker<R: UdpReceiver, S: UdpSender> {
}
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,
@@ -52,8 +52,6 @@ impl<R: UdpReceiver, S: UdpSender> RecvWorker<R, S> {
.collect(),
),
}
- .run()
- .await
}
pub async fn run(&self) {