@@ -35,6 +35,7 @@ use std::net::TcpStream;
3535use std:: io;
3636use std:: io:: { Write , Read } ;
3737use std:: convert;
38+ use std:: sync:: atomic:: { AtomicBool , AtomicI32 , Ordering } ;
3839use byteorder:: { BigEndian , WriteBytesExt , ReadBytesExt } ;
3940use flate2:: read:: { ZlibDecoder , ZlibEncoder } ;
4041use flate2:: Compression ;
@@ -44,9 +45,20 @@ use log::debug;
4445
4546pub const SUPPORTED_PROTOCOLS : [ i32 ; 18 ] = [ 575 , 498 , 490 , 485 , 480 , 477 , 452 , 451 , 404 , 340 , 316 , 315 , 210 , 109 , 107 , 74 , 47 , 5 ] ;
4647
47- // TODO: switch to using thread_local storage?, see https://doc.rust-lang.org/std/macro.thread_local.html
48- pub static mut CURRENT_PROTOCOL_VERSION : i32 = SUPPORTED_PROTOCOLS [ 0 ] ;
49- pub static mut NETWORK_DEBUG : bool = false ;
48+ static CURRENT_PROTOCOL_VERSION : AtomicI32 = AtomicI32 :: new ( SUPPORTED_PROTOCOLS [ 0 ] ) ;
49+ static NETWORK_DEBUG : AtomicBool = AtomicBool :: new ( false ) ;
50+
51+ pub fn current_protocol_version ( ) -> i32 {
52+ CURRENT_PROTOCOL_VERSION . load ( Ordering :: Relaxed )
53+ }
54+
55+ pub fn enable_network_debug ( ) {
56+ NETWORK_DEBUG . store ( true , Ordering :: Relaxed ) ;
57+ }
58+
59+ pub fn is_network_debug ( ) -> bool {
60+ NETWORK_DEBUG . load ( Ordering :: Relaxed )
61+ }
5062
5163/// Helper macro for defining packets
5264#[ macro_export]
@@ -1005,9 +1017,7 @@ pub struct Conn {
10051017
10061018impl Conn {
10071019 pub fn new ( target : & str , protocol_version : i32 ) -> Result < Conn , Error > {
1008- unsafe {
1009- CURRENT_PROTOCOL_VERSION = protocol_version;
1010- }
1020+ CURRENT_PROTOCOL_VERSION . store ( protocol_version, Ordering :: Relaxed ) ;
10111021
10121022 // TODO SRV record support
10131023 let mut parts = target. split ( ':' ) . collect :: < Vec < & str > > ( ) ;
@@ -1047,8 +1057,7 @@ impl Conn {
10471057 VarInt ( uncompressed_size as i32 ) . write_to ( & mut new) ?;
10481058 let mut write = ZlibEncoder :: new ( io:: Cursor :: new ( buf) , Compression :: default ( ) ) ;
10491059 write. read_to_end ( & mut new) ?;
1050- let network_debug = unsafe { NETWORK_DEBUG } ;
1051- if network_debug {
1060+ if is_network_debug ( ) {
10521061 debug ! ( "Compressed for sending {} bytes to {} since > threshold {}, new={:?}" ,
10531062 uncompressed_size, new. len( ) , self . compression_threshold,
10541063 new) ;
@@ -1066,7 +1075,6 @@ impl Conn {
10661075 }
10671076
10681077 pub fn read_packet ( & mut self ) -> Result < packet:: Packet , Error > {
1069- let network_debug = unsafe { NETWORK_DEBUG } ;
10701078 let len = VarInt :: read_from ( self ) ?. 0 as usize ;
10711079 let mut ibuf = vec ! [ 0 ; len] ;
10721080 self . read_exact ( & mut ibuf) ?;
@@ -1081,7 +1089,7 @@ impl Conn {
10811089 let mut reader = ZlibDecoder :: new ( buf) ;
10821090 reader. read_to_end ( & mut new) ?;
10831091 }
1084- if network_debug {
1092+ if is_network_debug ( ) {
10851093 debug ! ( "Decompressed threshold={} len={} uncompressed_size={} to {} bytes" ,
10861094 self . compression_threshold, len, uncompressed_size, new. len( ) ) ;
10871095 }
@@ -1095,14 +1103,14 @@ impl Conn {
10951103 Direction :: Serverbound => Direction :: Clientbound ,
10961104 } ;
10971105
1098- if network_debug {
1106+ if is_network_debug ( ) {
10991107 debug ! ( "about to parse id={:x}, dir={:?} state={:?}" , id, dir, self . state) ;
11001108 fs:: File :: create ( "last-packet" ) ?. write_all ( buf. get_ref ( ) ) ?;
11011109 }
11021110
11031111 let packet = packet:: packet_by_id ( self . protocol_version , self . state , dir, id, & mut buf) ?;
11041112
1105- if network_debug {
1113+ if is_network_debug ( ) {
11061114 debug ! ( "packet = {:?}" , packet) ;
11071115 }
11081116
0 commit comments