2 #![feature(cursor_remaining)]
3 #![feature(hash_drain_filter)]
8 use byteorder::{BigEndian, WriteBytesExt};
9 pub use client::{connect, Sender as Client};
10 use num_enum::TryFromPrimitive;
18 pub const PROTO_ID: u32 = 0x4f457403;
19 pub const UDP_PKT_SIZE: usize = 512;
20 pub const NUM_CHANS: usize = 3;
21 pub const REL_BUFFER: usize = 0x8000;
22 pub const INIT_SEQNUM: u16 = 65500;
23 pub const TIMEOUT: u64 = 30;
25 pub trait UdpSender: Send + Sync + 'static {
26 fn send(&self, data: Vec<u8>) -> io::Result<()>;
29 pub trait UdpReceiver: Send + Sync + 'static {
30 fn recv(&self) -> io::Result<Vec<u8>>;
33 #[derive(Debug, Copy, Clone)]
40 #[derive(Debug, Copy, Clone, PartialEq, TryFromPrimitive)]
49 #[derive(Debug, Copy, Clone, PartialEq, TryFromPrimitive)]
65 pub type Error = error::Error;
66 pub type InPkt = Result<Pkt<Vec<u8>>, Error>;
72 pub struct RudpShare<S: UdpSender> {
75 pub chans: Vec<AckChan>,
80 pub struct RudpReceiver<S: UdpSender> {
81 share: Arc<RudpShare<S>>,
82 pkt_rx: mpsc::Receiver<InPkt>,
86 pub struct RudpSender<S: UdpSender> {
87 share: Arc<RudpShare<S>>,
90 impl<S: UdpSender> RudpShare<S> {
91 pub fn send(&self, tp: PktType, pkt: Pkt<&[u8]>) -> io::Result<()> {
92 let mut buf = Vec::with_capacity(4 + 2 + 1 + 1 + pkt.data.len());
93 buf.write_u32::<BigEndian>(PROTO_ID)?;
94 buf.write_u16::<BigEndian>(self.remote_id)?;
95 buf.write_u8(pkt.chan as u8)?;
96 buf.write_u8(tp as u8)?;
99 self.udp_tx.send(buf)?;
105 impl<S: UdpSender> RudpSender<S> {
106 pub fn send(&self, pkt: Pkt<&[u8]>) -> io::Result<()> {
107 self.share.send(PktType::Orig, pkt) // TODO
111 impl<S: UdpSender> ops::Deref for RudpReceiver<S> {
112 type Target = mpsc::Receiver<InPkt>;
114 fn deref(&self) -> &Self::Target {
119 pub fn new<S: UdpSender, R: UdpReceiver>(
124 ) -> (RudpSender<S>, RudpReceiver<S>) {
125 let (pkt_tx, pkt_rx) = mpsc::channel();
127 let share = Arc::new(RudpShare {
131 chans: (0..NUM_CHANS).map(|_| AckChan).collect(),
133 let recv_share = Arc::clone(&share);
136 recv_worker::RecvWorker::new(udp_rx, recv_share, pkt_tx).run();
141 share: Arc::clone(&share),
143 RudpReceiver { share, pkt_rx },
149 fn main() -> io::Result<()> {
150 //println!("{}", x.deep_size_of());
151 let (tx, rx) = connect("127.0.0.1:30000")?;
153 let mut mtpkt = vec![];
154 mtpkt.write_u16::<BigEndian>(2)?; // high level type
155 mtpkt.write_u8(29)?; // serialize ver
156 mtpkt.write_u16::<BigEndian>(0)?; // compression modes
157 mtpkt.write_u16::<BigEndian>(40)?; // MinProtoVer
158 mtpkt.write_u16::<BigEndian>(40)?; // MaxProtoVer
159 mtpkt.write_u16::<BigEndian>(3)?; // player name length
160 mtpkt.write(b"foo")?; // player name
168 while let Ok(result) = rx.recv() {
171 io::stdout().write(pkt.data.as_slice())?;
173 Err(err) => eprintln!("Error: {}", err),