- move: null string to utils
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::auth_client::AuthClient;
|
||||
use crate::handlers::null_string::NullTerminatedString;
|
||||
use utils::null_string::NullTerminatedString;
|
||||
use crate::packet::{send_packet, Packet, PacketPayload};
|
||||
use crate::packet_type::PacketType;
|
||||
use crate::packets::cli_accept_req::CliAcceptReq;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::handlers::null_string::NullTerminatedString;
|
||||
use utils::null_string::NullTerminatedString;
|
||||
use crate::packet::{send_packet, Packet, PacketPayload};
|
||||
use crate::packet_type::PacketType;
|
||||
use crate::packets::cli_char_list_req::CliCharListReq;
|
||||
@@ -18,4 +18,4 @@ pub(crate) async fn handle_char_list_req(stream: &mut TcpStream, packet: Packet)
|
||||
debug!("{:?}", request);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
pub mod null_string;
|
||||
pub mod auth;
|
||||
pub mod character;
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
use bincode::{Decode, Encode, de::Decoder, enc::Encoder, error::{DecodeError, EncodeError}};
|
||||
use std::str;
|
||||
use bincode::de::read::Reader;
|
||||
use bincode::enc::write::Writer;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NullTerminatedString(pub String);
|
||||
|
||||
impl NullTerminatedString {
|
||||
pub fn new(string: &str) -> Self {
|
||||
NullTerminatedString(string.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for NullTerminatedString {
|
||||
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
|
||||
let bytes = self.0.as_bytes();
|
||||
encoder.writer().write(bytes)?; // Write the string bytes
|
||||
encoder.writer().write(&[0])?; // Add the null terminator
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for NullTerminatedString {
|
||||
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
|
||||
let mut buffer = Vec::new();
|
||||
let mut byte = [0u8; 1];
|
||||
|
||||
// Read until the null terminator
|
||||
while decoder.reader().read(&mut byte).is_ok() {
|
||||
if byte[0] == 0 {
|
||||
break; // Null terminator found
|
||||
}
|
||||
buffer.push(byte[0]);
|
||||
}
|
||||
|
||||
let string = str::from_utf8(&buffer)
|
||||
.map_err(|e| DecodeError::OtherString(format!("Invalid UTF-8: {}", e)))?
|
||||
.to_string();
|
||||
|
||||
Ok(NullTerminatedString(string))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user