]> git.lizzy.rs Git - mt_rudp.git/commitdiff
fix
authorLizzy Fleckenstein <eliasfleckenstein@web.de>
Thu, 22 Dec 2022 22:26:09 +0000 (23:26 +0100)
committerLizzy Fleckenstein <eliasfleckenstein@web.de>
Thu, 22 Dec 2022 22:26:09 +0000 (23:26 +0100)
src/client.rs
src/main.rs
src/recv_worker.rs

index e506a3e17cdeed32b9b3f71a0b1a82dfb3741835..e4864882e8d500a79271cdcf657da6c479711b89 100644 (file)
@@ -1,14 +1,5 @@
-use crate::{PeerID, UdpReceiver, UdpSender};
-use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
-use num_enum::{TryFromPrimitive, TryFromPrimitiveError};
-use std::{
-    cell::Cell,
-    fmt,
-    io::{self, Write},
-    net, ops,
-    sync::{mpsc, Arc},
-    thread,
-};
+use crate::*;
+use std::{io, net, sync::Arc};
 
 pub struct Sender {
     sock: Arc<net::UdpSocket>,
index da535730877fb86cd37ddfac63a5e21f7950282d..2a5ff2f1f5f710c0b0c6f0b2425a4640a3b8bf84 100644 (file)
@@ -4,9 +4,9 @@ mod client;
 pub mod error;
 mod recv_worker;
 
-use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
+use byteorder::{BigEndian, WriteBytesExt};
 pub use client::{connect, Sender as Client};
-use num_enum::{TryFromPrimitive, TryFromPrimitiveError};
+use num_enum::TryFromPrimitive;
 use std::{
     io::{self, Write},
     ops,
@@ -20,8 +20,6 @@ pub const NUM_CHANS: usize = 3;
 pub const REL_BUFFER: usize = 0x8000;
 pub const INIT_SEQNUM: u16 = 65500;
 
-pub type Error = error::Error;
-
 pub trait UdpSender: Send + Sync + 'static {
     fn send(&self, data: Vec<u8>) -> io::Result<()>;
 }
@@ -62,6 +60,7 @@ pub struct Pkt<T> {
     data: T,
 }
 
+pub type Error = error::Error;
 pub type InPkt = Result<Pkt<Vec<u8>>, Error>;
 
 #[derive(Debug)]
@@ -87,15 +86,6 @@ pub struct RudpSender<S: UdpSender> {
 }
 
 impl<S: UdpSender> RudpShare<S> {
-    pub fn new(id: u16, remote_id: u16, udp_tx: S) -> Self {
-        Self {
-            id,
-            remote_id,
-            udp_tx,
-            chans: (0..NUM_CHANS).map(|_| AckChan).collect(),
-        }
-    }
-
     pub fn send(&self, tp: PktType, pkt: Pkt<&[u8]>) -> io::Result<()> {
         let mut buf = Vec::with_capacity(4 + 2 + 1 + 1 + pkt.data.len());
         buf.write_u32::<BigEndian>(PROTO_ID)?;
@@ -132,10 +122,15 @@ pub fn new<S: UdpSender, R: UdpReceiver>(
 ) -> (RudpSender<S>, RudpReceiver<S>) {
     let (pkt_tx, pkt_rx) = mpsc::channel();
 
-    let share = Arc::new(RudpShare::new(id, remote_id, udp_tx));
+    let share = Arc::new(RudpShare {
+        id,
+        remote_id,
+        udp_tx,
+        chans: (0..NUM_CHANS).map(|_| AckChan).collect(),
+    });
     let recv_share = Arc::clone(&share);
 
-    thread::spawn(move || {
+    thread::spawn(|| {
         recv_worker::RecvWorker::new(udp_rx, recv_share, pkt_tx).run();
     });
 
index d1ae5b1b0435b3378b8c81b141c91c4046b8f83e..578cf2ecbe80328e2ad0c0f4ab7e0d1f00303949 100644 (file)
@@ -1,6 +1,5 @@
-use crate::{error::Error, CtlType, InPkt, Pkt, PktType, RudpShare, UdpReceiver, UdpSender};
-use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
-use num_enum::{TryFromPrimitive, TryFromPrimitiveError};
+use crate::{error::Error, *};
+use byteorder::{BigEndian, ReadBytesExt};
 use std::{
     cell::Cell,
     io, result,
@@ -8,7 +7,7 @@ use std::{
 };
 
 fn to_seqnum(seqnum: u16) -> usize {
-    (seqnum as usize) & (crate::REL_BUFFER - 1)
+    (seqnum as usize) & (REL_BUFFER - 1)
 }
 
 struct RelChan {
@@ -36,11 +35,11 @@ impl<R: UdpReceiver, S: UdpSender> RecvWorker<R, S> {
     }
 
     pub fn run(&self) {
-        let mut recv_chans = (0..crate::NUM_CHANS as u8)
+        let mut recv_chans = (0..NUM_CHANS as u8)
             .map(|num| RelChan {
                 num,
-                packets: (0..crate::REL_BUFFER).map(|_| Cell::new(None)).collect(),
-                seqnum: crate::INIT_SEQNUM,
+                packets: (0..REL_BUFFER).map(|_| Cell::new(None)).collect(),
+                seqnum: INIT_SEQNUM,
             })
             .collect();
 
@@ -70,7 +69,7 @@ impl<R: UdpReceiver, S: UdpSender> RecvWorker<R, S> {
         let mut cursor = io::Cursor::new(self.udp_rx.recv()?);
 
         let proto_id = cursor.read_u32::<BigEndian>()?;
-        if proto_id != crate::PROTO_ID {
+        if proto_id != PROTO_ID {
             do yeet InvalidProtoId(proto_id);
         }