Skip to content

Commit 643de31

Browse files
committed
protocol: atomics replace unsafe for version/debug. Closes #261
1 parent 90f7d9a commit 643de31

File tree

8 files changed

+30
-22
lines changed

8 files changed

+30
-22
lines changed

src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl Default for Stack {
3939

4040
impl Serializable for Option<Stack> {
4141
fn read_from<R: io::Read>(buf: &mut R) -> Result<Option<Stack>, protocol::Error> {
42-
let protocol_version = unsafe { protocol::CURRENT_PROTOCOL_VERSION };
42+
let protocol_version = protocol::current_protocol_version();
4343

4444
if protocol_version >= 404 {
4545
let present = buf.read_u8()? != 0;

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ fn main2() {
297297
game.renderer.camera.pos = cgmath::Point3::new(0.5, 13.2, 0.5);
298298

299299
if opt.network_debug {
300-
unsafe { protocol::NETWORK_DEBUG = true; }
300+
protocol::enable_network_debug();
301301
}
302302

303303
if opt.server.is_some() {

src/protocol/forge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl Serializable for FmlHs {
147147
})
148148
},
149149
3 => {
150-
let protocol_version = unsafe { super::CURRENT_PROTOCOL_VERSION };
150+
let protocol_version = super::current_protocol_version();
151151

152152
if protocol_version >= 47 {
153153
Ok(FmlHs::RegistryData {

src/protocol/mod.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use std::net::TcpStream;
3535
use std::io;
3636
use std::io::{Write, Read};
3737
use std::convert;
38+
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
3839
use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};
3940
use flate2::read::{ZlibDecoder, ZlibEncoder};
4041
use flate2::Compression;
@@ -44,9 +45,20 @@ use log::debug;
4445

4546
pub 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

10061018
impl 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

src/protocol/packet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,7 +2600,7 @@ impl Serializable for Recipe {
26002600
let a = String::read_from(buf)?;
26012601
let b = String::read_from(buf)?;
26022602

2603-
let protocol_version = unsafe { super::CURRENT_PROTOCOL_VERSION };
2603+
let protocol_version = super::current_protocol_version();
26042604

26052605
// 1.14+ swaps recipe identifier and type, and adds namespace to type
26062606
if protocol_version >= 477 {
@@ -2735,7 +2735,7 @@ pub struct Trade {
27352735

27362736
impl Serializable for Trade {
27372737
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
2738-
let protocol_version = unsafe { super::CURRENT_PROTOCOL_VERSION };
2738+
let protocol_version = super::current_protocol_version();
27392739

27402740
Ok(Trade {
27412741
input_item_1: Serializable::read_from(buf)?,

src/server/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl Server {
688688
}
689689

690690
fn on_plugin_message_clientbound(&mut self, channel: &str, data: &[u8]) {
691-
if unsafe { protocol::NETWORK_DEBUG } {
691+
if protocol::is_network_debug() {
692692
debug!("Received plugin message: channel={}, data={:?}", channel, data);
693693
}
694694

@@ -765,7 +765,7 @@ impl Server {
765765
}
766766

767767
fn write_plugin_message(&mut self, channel: &str, data: &[u8]) {
768-
if unsafe { protocol::NETWORK_DEBUG } {
768+
if protocol::is_network_debug() {
769769
debug!("Sending plugin message: channel={}, data={:?}", channel, data);
770770
}
771771
if self.protocol_version >= 47 {

src/server/plugin_messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct Brand {
99

1010
impl Brand {
1111
pub fn as_message(self) -> PluginMessageServerbound {
12-
let protocol_version = unsafe { crate::protocol::CURRENT_PROTOCOL_VERSION };
12+
let protocol_version = crate::protocol::current_protocol_version();
1313

1414
let channel_name = if protocol_version >= 404 {
1515
"minecraft:brand"

src/types/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl Metadata {
433433

434434
impl Serializable for Metadata {
435435
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, protocol::Error> {
436-
let protocol_version = unsafe { protocol::CURRENT_PROTOCOL_VERSION };
436+
let protocol_version = protocol::current_protocol_version();
437437

438438
if protocol_version >= 404 {
439439
Metadata::read_from113(buf)
@@ -445,7 +445,7 @@ impl Serializable for Metadata {
445445
}
446446

447447
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), protocol::Error> {
448-
let protocol_version = unsafe { protocol::CURRENT_PROTOCOL_VERSION };
448+
let protocol_version = protocol::current_protocol_version();
449449

450450
if protocol_version >= 404 {
451451
self.write_to113(buf)

0 commit comments

Comments
 (0)