diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-26 15:43:53 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-26 15:44:51 +0200 |
commit | d8b81fc1ab3f335ebae97665bfd74a519391262b (patch) | |
tree | e4ff52e2caa3f74ebdf95720588970669ec58912 /pixel-client | |
parent | 5af9155955bc07e2333c81f4f5c56b97ff63aa79 (diff) | |
download | hurrycurry-d8b81fc1ab3f335ebae97665bfd74a519391262b.tar hurrycurry-d8b81fc1ab3f335ebae97665bfd74a519391262b.tar.bz2 hurrycurry-d8b81fc1ab3f335ebae97665bfd74a519391262b.tar.zst |
use binary protocol when possible
Diffstat (limited to 'pixel-client')
-rw-r--r-- | pixel-client/src/network.rs | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/pixel-client/src/network.rs b/pixel-client/src/network.rs index ed160773..e8aa08de 100644 --- a/pixel-client/src/network.rs +++ b/pixel-client/src/network.rs @@ -16,8 +16,8 @@ */ use anyhow::Result; -use hurrycurry_protocol::{PacketC, PacketS, BINCODE_CONFIG}; -use log::{debug, warn}; +use hurrycurry_protocol::{PacketC, PacketS, BINCODE_CONFIG, VERSION}; +use log::{debug, info, warn}; use std::{collections::VecDeque, net::TcpStream}; use tungstenite::{ client::{uri_mode, IntoClientRequest}, @@ -32,6 +32,7 @@ pub struct Network { sock: WebSocket<MaybeTlsStream<TcpStream>>, pub queue_in: VecDeque<PacketC>, pub queue_out: VecDeque<PacketS>, + use_bincode: bool, } impl Network { @@ -57,6 +58,8 @@ impl Network { Mode::Plain => 27032, Mode::Tls => 443, }); + + info!("Connecting: host={host:?} port={port}"); let stream = TcpStream::connect((host, port))?; stream.set_nodelay(true).unwrap(); @@ -68,18 +71,32 @@ impl Network { _ => todo!(), }; + info!("Handshake complete."); Ok(Self { sock, + use_bincode: false, queue_in: VecDeque::new(), queue_out: VecDeque::new(), }) } + pub fn poll(&mut self) { loop { self.queue_in.extend(match self.sock.read() { Ok(Message::Text(packet)) => match serde_json::from_str(&packet) { Ok(packet) => { debug!("<- {packet:?}"); + if let PacketC::Version { + minor, + major, + supports_bincode, + } = &packet + { + if *minor == VERSION.0 && *major == VERSION.1 && *supports_bincode { + info!("Binary protocol format enabled."); + self.use_bincode = true; + } + } Some(packet) } Err(e) => { @@ -113,9 +130,17 @@ impl Network { for packet in self.queue_out.drain(..) { debug!("-> {packet:?}"); - self.sock - .write(Message::Text(serde_json::to_string(&packet).unwrap())) - .unwrap(); + if self.use_bincode { + self.sock + .write(Message::Binary( + bincode::encode_to_vec(&packet, BINCODE_CONFIG).unwrap(), + )) + .unwrap(); + } else { + self.sock + .write(Message::Text(serde_json::to_string(&packet).unwrap())) + .unwrap(); + } } self.sock.flush().unwrap(); |