@@ -126,6 +126,11 @@ impl Socket {
126
126
}
127
127
}
128
128
129
+ /// Set the link conditioner for this socket. See [LinkConditioner] for further details.
130
+ pub fn set_link_conditioner ( & mut self , link_conditioner : Option < LinkConditioner > ) {
131
+ self . link_conditioner = link_conditioner;
132
+ }
133
+
129
134
/// Iterate through all of the idle connections based on `idle_connection_timeout` config and
130
135
/// remove them from the active connections. For each connection removed, we will send a
131
136
/// `SocketEvent::TimeOut` event to the `event_sender` channel.
@@ -238,8 +243,8 @@ impl Socket {
238
243
239
244
// In the presence of a link conditioner, we would like it to determine whether or not we should
240
245
// send a packet.
241
- fn should_send_packet ( & self ) -> bool {
242
- if let Some ( link_conditioner) = & self . link_conditioner {
246
+ fn should_send_packet ( & mut self ) -> bool {
247
+ if let Some ( link_conditioner) = & mut self . link_conditioner {
243
248
link_conditioner. should_send ( )
244
249
} else {
245
250
true
@@ -277,7 +282,7 @@ impl Socket {
277
282
mod tests {
278
283
use crate :: {
279
284
net:: constants:: { ACKED_PACKET_HEADER , FRAGMENT_HEADER_SIZE , STANDARD_HEADER_SIZE } ,
280
- Config , Packet , Socket , SocketEvent ,
285
+ Config , LinkConditioner , Packet , Socket , SocketEvent ,
281
286
} ;
282
287
use std:: collections:: HashSet ;
283
288
use std:: net:: { SocketAddr , UdpSocket } ;
@@ -779,4 +784,73 @@ mod tests {
779
784
let time = Instant :: now ( ) ;
780
785
server. manual_poll ( time) ;
781
786
}
787
+
788
+ #[ test]
789
+ fn really_bad_network_keeps_chugging_along ( ) {
790
+ let server_addr = "127.0.0.1:12320" . parse :: < SocketAddr > ( ) . unwrap ( ) ;
791
+ let client_addr = "127.0.0.1:12321" . parse :: < SocketAddr > ( ) . unwrap ( ) ;
792
+
793
+ let ( mut server, server_sender, server_receiver) = Socket :: bind ( server_addr) . unwrap ( ) ;
794
+ let ( mut client, client_sender, client_receiver) = Socket :: bind ( client_addr) . unwrap ( ) ;
795
+
796
+ let time = Instant :: now ( ) ;
797
+
798
+ // We give both the server and the client a really bad bidirectional link
799
+ let link_conditioner = {
800
+ let mut lc = LinkConditioner :: new ( ) ;
801
+ lc. set_packet_loss ( 0.9 ) ;
802
+ Some ( lc)
803
+ } ;
804
+
805
+ client. set_link_conditioner ( link_conditioner. clone ( ) ) ;
806
+ server. set_link_conditioner ( link_conditioner) ;
807
+
808
+ let mut set = HashSet :: new ( ) ;
809
+
810
+ // We chat 100 packets between the client and server, which will re-send any non-acked
811
+ // packets
812
+ let mut send_many_packets = |dummy : Option < u8 > | {
813
+ for id in 0 ..100 {
814
+ client_sender
815
+ . send ( Packet :: reliable_unordered (
816
+ server_addr,
817
+ vec ! [ dummy. unwrap_or( id) ] ,
818
+ ) )
819
+ . unwrap ( ) ;
820
+
821
+ server_sender
822
+ . send ( Packet :: reliable_unordered ( client_addr, vec ! [ 255 ] ) )
823
+ . unwrap ( ) ;
824
+
825
+ client. manual_poll ( time) ;
826
+ server. manual_poll ( time) ;
827
+
828
+ while let Ok ( _) = client_receiver. try_recv ( ) { }
829
+ while let Ok ( event) = server_receiver. try_recv ( ) {
830
+ match event {
831
+ SocketEvent :: Packet ( pkt) => {
832
+ set. insert ( pkt. payload ( ) [ 0 ] ) ;
833
+ }
834
+ SocketEvent :: Timeout ( _) => {
835
+ panic ! [ "Unable to time out, time has not advanced" ]
836
+ }
837
+ SocketEvent :: Connect ( _) => { }
838
+ }
839
+ }
840
+ }
841
+
842
+ return set. len ( ) ;
843
+ } ;
844
+
845
+ // The first chatting sequence sends packets 0..100 from the client to the server. After
846
+ // this we just chat with a value of 255 so we don't accidentally overlap those chatting
847
+ // packets with the packets we want to ack.
848
+ assert_eq ! [ 42 , send_many_packets( None ) ] ;
849
+ assert_eq ! [ 85 , send_many_packets( Some ( 255 ) ) ] ;
850
+ assert_eq ! [ 98 , send_many_packets( Some ( 255 ) ) ] ;
851
+ assert_eq ! [ 100 , send_many_packets( Some ( 255 ) ) ] ;
852
+
853
+ // 101 because we have 0..100 and 255 from the dummies
854
+ assert_eq ! [ 101 , send_many_packets( Some ( 255 ) ) ] ;
855
+ }
782
856
}
0 commit comments