From 131b146ffbff8d51bd3be7d6dc3b729cbff863f2 Mon Sep 17 00:00:00 2001 From: Justin LeFebvre Date: Wed, 26 Jun 2019 22:44:10 -0400 Subject: [PATCH] remove a number of warnings. Also changed a number of tests to be more mac friendly --- examples/server_client.rs | 14 +- examples/simple_udp.rs | 5 +- examples/udp.rs | 7 +- src/net/connection.rs | 1 - src/net/socket.rs | 198 +++++++++++++++++----------- tests/fragmentation_packets_test.rs | 5 +- tests/unreliable_packets_test.rs | 4 + 7 files changed, 139 insertions(+), 95 deletions(-) diff --git a/examples/server_client.rs b/examples/server_client.rs index f6158acf..3d47793b 100644 --- a/examples/server_client.rs +++ b/examples/server_client.rs @@ -11,7 +11,7 @@ const SERVER: &str = "127.0.0.1:12351"; fn server() -> Result<(), ErrorKind> { let mut socket = Socket::bind(SERVER)?; - let (mut sender, mut receiver) = (socket.get_packet_sender(), socket.get_event_receiver()); + let (sender, receiver) = (socket.get_packet_sender(), socket.get_event_receiver()); let _thread = thread::spawn(move || socket.start_polling()); loop { @@ -29,10 +29,12 @@ fn server() -> Result<(), ErrorKind> { println!("Received {:?} from {:?}", msg, ip); - sender.send(Packet::reliable_unordered( - packet.addr(), - "Copy that!".as_bytes().to_vec(), - )); + sender + .send(Packet::reliable_unordered( + packet.addr(), + "Copy that!".as_bytes().to_vec(), + )) + .expect("This should send"); } SocketEvent::Timeout(address) => { println!("Client timed out: {}", address); @@ -65,7 +67,7 @@ fn client() -> Result<(), ErrorKind> { socket.send(Packet::reliable_unordered( server, line.clone().into_bytes(), - )); + ))?; socket.manual_poll(Instant::now()); diff --git a/examples/simple_udp.rs b/examples/simple_udp.rs index 179a80a5..678ac9ca 100644 --- a/examples/simple_udp.rs +++ b/examples/simple_udp.rs @@ -3,11 +3,10 @@ //! 2. setting up client to send data. //! 3. serialize data to send and deserialize when received. use bincode::{deserialize, serialize}; -use crossbeam_channel::{Receiver, Sender}; -use laminar::{ErrorKind, Packet, Socket, SocketEvent}; +use laminar::{Packet, Socket, SocketEvent}; use serde_derive::{Deserialize, Serialize}; use std::net::SocketAddr; -use std::{thread, time::Instant}; +use std::time::Instant; /// The socket address of where the server is located. const SERVER_ADDR: &'static str = "127.0.0.1:12345"; diff --git a/examples/udp.rs b/examples/udp.rs index a4e02130..72c7db3b 100644 --- a/examples/udp.rs +++ b/examples/udp.rs @@ -2,10 +2,9 @@ //! 1. sending data //! 2. receiving data //! 3. constructing the packet for sending. -use laminar::{Packet, Socket, SocketEvent}; +use laminar::{Packet, Result, Socket, SocketEvent}; use std::net::SocketAddr; -use std::thread; /// The socket address of where the server is located. const SERVER_ADDR: &'static str = "127.0.0.1:12345"; @@ -21,14 +20,14 @@ fn server_address() -> SocketAddr { } /// This is an example of how to send data to an specific address. -pub fn send_data() { +pub fn send_data() -> Result<()> { // Setup a udp socket and bind it to the client address. let mut socket = Socket::bind(client_address()).unwrap(); let packet = construct_packet(); // next send or packet to the endpoint we earlier putted into the packet. - socket.send(packet); + socket.send(packet) } /// This is an example of how to receive data over udp. diff --git a/src/net/connection.rs b/src/net/connection.rs index abe2bd91..d9a17789 100644 --- a/src/net/connection.rs +++ b/src/net/connection.rs @@ -84,7 +84,6 @@ mod tests { use super::{ActiveConnections, Config}; use std::{ sync::Arc, - thread, time::{Duration, Instant}, }; diff --git a/src/net/socket.rs b/src/net/socket.rs index 5ebd7aa6..33fffc78 100644 --- a/src/net/socket.rs +++ b/src/net/socket.rs @@ -166,7 +166,8 @@ impl Socket { pub fn set_link_conditioner(&mut self, link_conditioner: Option) { self.link_conditioner = link_conditioner; } - /// Get port of socket + + /// Get the local socket address pub fn local_addr(&self) -> Result { Ok(self.socket.local_addr()?) } @@ -305,7 +306,7 @@ impl Socket { if recv_len == 0 { panic!("Received data too short"); } - let received_payload = &self.recv_buffer[..recv_len]; + &self.recv_buffer[..recv_len]; } Err(e) => { if e.kind() != io::ErrorKind::WouldBlock { @@ -327,7 +328,6 @@ mod tests { }; use std::collections::HashSet; use std::net::{SocketAddr, UdpSocket}; - use std::thread; use std::time::{Duration, Instant}; #[test] @@ -346,13 +346,15 @@ mod tests { let time = Instant::now(); - let mut sender = client.get_packet_sender(); - let mut receiver = server.get_event_receiver(); + let sender = client.get_packet_sender(); + let receiver = server.get_event_receiver(); - sender.send(Packet::reliable_unordered( - server_addr, - b"Hello world!".iter().cloned().collect::>(), - )); + sender + .send(Packet::reliable_unordered( + server_addr, + b"Hello world!".iter().cloned().collect::>(), + )) + .unwrap(); client.manual_poll(time); server.manual_poll(time); @@ -373,10 +375,12 @@ mod tests { let time = Instant::now(); // Send a packet that the server ignores/drops - client.send(Packet::reliable_unordered( - "127.0.0.1:12335".parse::().unwrap(), - b"Do not arrive".iter().cloned().collect::>(), - )); + client + .send(Packet::reliable_unordered( + "127.0.0.1:12335".parse::().unwrap(), + b"Do not arrive".iter().cloned().collect::>(), + )) + .unwrap(); client.manual_poll(time); // Drop the inbound packet, this simulates a network error @@ -384,9 +388,13 @@ mod tests { // Send a packet that the server receives for id in 0..u8::max_value() { - client.send(create_test_packet(id, "127.0.0.1:12335")); + client + .send(create_test_packet(id, "127.0.0.1:12335")) + .unwrap(); - server.send(create_test_packet(id, "127.0.0.1:12336")); + server + .send(create_test_packet(id, "127.0.0.1:12336")) + .unwrap(); client.manual_poll(time); server.manual_poll(time); @@ -409,10 +417,12 @@ mod tests { // Send a bunch of packets to a server for _ in 0..3 { - client.send(Packet::unreliable( - "127.0.0.1:12337".parse::().unwrap(), - vec![1, 2, 3, 4, 5, 6, 7, 8, 9], - )); + client + .send(Packet::unreliable( + "127.0.0.1:12337".parse::().unwrap(), + vec![1, 2, 3, 4, 5, 6, 7, 8, 9], + )) + .unwrap(); } let time = Instant::now(); @@ -429,10 +439,12 @@ mod tests { // packets assert_eq![0, server.connection_count()]; - server.send(Packet::unreliable( - "127.0.0.1:12338".parse::().unwrap(), - vec![1], - )); + server + .send(Packet::unreliable( + "127.0.0.1:12338".parse::().unwrap(), + vec![1], + )) + .unwrap(); server.manual_poll(time); @@ -448,11 +460,13 @@ mod tests { let time = Instant::now(); // Send a packet that the server ignores/drops - client.send(Packet::reliable_sequenced( - "127.0.0.1:12329".parse::().unwrap(), - b"Do not arrive".iter().cloned().collect::>(), - None, - )); + client + .send(Packet::reliable_sequenced( + "127.0.0.1:12329".parse::().unwrap(), + b"Do not arrive".iter().cloned().collect::>(), + None, + )) + .unwrap(); client.manual_poll(time); // Drop the inbound packet, this simulates a network error @@ -460,9 +474,13 @@ mod tests { // Send a packet that the server receives for id in 0..36 { - client.send(create_sequenced_packet(id, "127.0.0.1:12329")); + client + .send(create_sequenced_packet(id, "127.0.0.1:12329")) + .unwrap(); - server.send(create_sequenced_packet(id, "127.0.0.1:12330")); + server + .send(create_sequenced_packet(id, "127.0.0.1:12330")) + .unwrap(); client.manual_poll(time); server.manual_poll(time); @@ -484,11 +502,13 @@ mod tests { let time = Instant::now(); // Send a packet that the server ignores/drops - client.send(Packet::reliable_ordered( - "127.0.0.1:12333".parse::().unwrap(), - b"Do not arrive".iter().cloned().collect::>(), - None, - )); + client + .send(Packet::reliable_ordered( + "127.0.0.1:12333".parse::().unwrap(), + b"Do not arrive".iter().cloned().collect::>(), + None, + )) + .unwrap(); client.manual_poll(time); // Drop the inbound packet, this simulates a network error @@ -496,9 +516,13 @@ mod tests { // Send a packet that the server receives for id in 0..36 { - client.send(create_ordered_packet(id, "127.0.0.1:12333")); + client + .send(create_ordered_packet(id, "127.0.0.1:12333")) + .unwrap(); - server.send(create_ordered_packet(id, "127.0.0.1:12334")); + server + .send(create_ordered_packet(id, "127.0.0.1:12334")) + .unwrap(); client.manual_poll(time); server.manual_poll(time); @@ -525,7 +549,9 @@ mod tests { let time = Instant::now(); for id in 0..100 { - client.send(Packet::reliable_sequenced(server_addr, vec![id], None)); + client + .send(Packet::reliable_sequenced(server_addr, vec![id], None)) + .unwrap(); client.manual_poll(time); } @@ -535,13 +561,13 @@ mod tests { while let Some(message) = server.recv() { match message { - SocketEvent::Connect(connect_event) => {} + SocketEvent::Connect(_) => {} SocketEvent::Packet(packet) => { let byte = packet.payload()[0]; assert![!seen.contains(&byte)]; seen.insert(byte); } - SocketEvent::Timeout(timeout_event) => { + SocketEvent::Timeout(_) => { panic!["This should not happen, as we've not advanced time"]; } } @@ -556,10 +582,12 @@ mod tests { let mut client = Socket::bind("127.0.0.1:12340".parse::().unwrap()).unwrap(); for _ in 0..3 { - client.send(Packet::unreliable( - "127.0.0.1:12339".parse::().unwrap(), - vec![1, 2, 3, 4, 5, 6, 7, 8, 9], - )); + client + .send(Packet::unreliable( + "127.0.0.1:12339".parse::().unwrap(), + vec![1, 2, 3, 4, 5, 6, 7, 8, 9], + )) + .unwrap(); } let time = Instant::now(); @@ -578,10 +606,12 @@ mod tests { let mut client = Socket::bind("127.0.0.1:12341".parse::().unwrap()).unwrap(); for _ in 0..3 { - client.send(Packet::unreliable( - "127.0.0.1:12342".parse::().unwrap(), - vec![1, 2, 3, 4, 5, 6, 7, 8, 9], - )); + client + .send(Packet::unreliable( + "127.0.0.1:12342".parse::().unwrap(), + vec![1, 2, 3, 4, 5, 6, 7, 8, 9], + )) + .unwrap(); } let now = Instant::now(); @@ -646,10 +676,12 @@ mod tests { let mut server = Socket::bind("127.0.0.1:12345".parse::().unwrap()).unwrap(); let mut client = Socket::bind("127.0.0.1:12344".parse::().unwrap()).unwrap(); - client.send(Packet::unreliable( - "127.0.0.1:12345".parse().unwrap(), - vec![0, 1, 2], - )); + client + .send(Packet::unreliable( + "127.0.0.1:12345".parse().unwrap(), + vec![0, 1, 2], + )) + .unwrap(); let now = Instant::now(); client.manual_poll(now); @@ -669,10 +701,12 @@ mod tests { let mut server = Socket::bind("127.0.0.1:12347".parse::().unwrap()).unwrap(); let mut client = Socket::bind("127.0.0.1:12346".parse::().unwrap()).unwrap(); - client.send(Packet::unreliable( - "127.0.0.1:12347".parse().unwrap(), - vec![0, 1, 2], - )); + client + .send(Packet::unreliable( + "127.0.0.1:12347".parse().unwrap(), + vec![0, 1, 2], + )) + .unwrap(); let now = Instant::now(); client.manual_poll(now); @@ -691,10 +725,12 @@ mod tests { ); // Acknowledge the client - server.send(Packet::unreliable( - "127.0.0.1:12346".parse().unwrap(), - vec![], - )); + server + .send(Packet::unreliable( + "127.0.0.1:12346".parse().unwrap(), + vec![], + )) + .unwrap(); server.manual_poll(now); client.manual_poll(now); @@ -728,20 +764,19 @@ mod tests { fn multiple_sends_should_start_sending_dropped() { // Start up a server and a client. let mut server = Socket::bind(REMOTE_ADDR.parse::().unwrap()).unwrap(); - let mut client = Socket::bind(LOCAL_ADDR.parse::().unwrap()).unwrap(); let now = Instant::now(); // Send enough packets to ensure that we must have dropped packets. for i in 0..35 { - client.send(create_test_packet(i, REMOTE_ADDR)); + client.send(create_test_packet(i, REMOTE_ADDR)).unwrap(); + client.manual_poll(now); } let mut events = Vec::new(); loop { - client.manual_poll(now); server.manual_poll(now); if let Some(event) = server.recv() { events.push(event); @@ -756,28 +791,27 @@ mod tests { // Finally the server decides to send us a message back. This necessarily will include // the ack information for 33 of the sent 35 packets. - server.send(create_test_packet(0, LOCAL_ADDR)); - + server.send(create_test_packet(0, LOCAL_ADDR)).unwrap(); server.manual_poll(now); - client.manual_poll(now); - // Block to ensure that the client gets the server message before moving on. - client.recv(); + // Loop to ensure that the client gets the server message before moving on. + loop { + client.manual_poll(now); + if client.recv().is_some() { + break; + } + } // This next sent message should end up sending the 2 unacked messages plus the new messages // with payload 35 events.clear(); - client.send(create_test_packet(35, REMOTE_ADDR)); - + client.send(create_test_packet(35, REMOTE_ADDR)).unwrap(); client.manual_poll(now); - server.manual_poll(now); loop { - client.manual_poll(now); server.manual_poll(now); if let Some(event) = server.recv() { events.push(event); - } else { break; } } @@ -801,7 +835,7 @@ mod tests { let client = UdpSocket::bind(sender).unwrap(); - client.send_to(&bytes, receiver); + client.send_to(&bytes, receiver).unwrap(); let time = Instant::now(); server.manual_poll(time); @@ -833,12 +867,16 @@ mod tests { // packets let mut send_many_packets = |dummy: Option| { for id in 0..100 { - client.send(Packet::reliable_unordered( - server_addr, - vec![dummy.unwrap_or(id)], - )); - - server.send(Packet::reliable_unordered(client_addr, vec![255])); + client + .send(Packet::reliable_unordered( + server_addr, + vec![dummy.unwrap_or(id)], + )) + .unwrap(); + + server + .send(Packet::reliable_unordered(client_addr, vec![255])) + .unwrap(); client.manual_poll(time); server.manual_poll(time); diff --git a/tests/fragmentation_packets_test.rs b/tests/fragmentation_packets_test.rs index 4142a559..f6109106 100644 --- a/tests/fragmentation_packets_test.rs +++ b/tests/fragmentation_packets_test.rs @@ -2,10 +2,13 @@ mod common; #[cfg(feature = "tester")] use common::{client_addr, Client, Server, ServerEvent}; - +#[cfg(feature = "tester")] use laminar::{DeliveryGuarantee, OrderingGuarantee, Packet}; +#[cfg(feature = "tester")] use log::debug; +#[cfg(feature = "tester")] use std::net::SocketAddr; +#[cfg(feature = "tester")] use std::{thread, time::Duration}; #[test] diff --git a/tests/unreliable_packets_test.rs b/tests/unreliable_packets_test.rs index 6230b97d..c6359cb4 100644 --- a/tests/unreliable_packets_test.rs +++ b/tests/unreliable_packets_test.rs @@ -4,9 +4,13 @@ mod common; #[cfg(feature = "tester")] use common::{client_addr, Client, Server, ServerEvent}; +#[cfg(feature = "tester")] use laminar::{DeliveryGuarantee, OrderingGuarantee, Packet}; +#[cfg(feature = "tester")] use log::{debug, error, info}; +#[cfg(feature = "tester")] use std::net::SocketAddr; +#[cfg(feature = "tester")] use std::{thread, time::Duration}; #[test]