diff options
| author | mat <github@matdoes.dev> | 2021-12-15 19:23:27 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-15 19:23:27 -0600 |
| commit | 4794b4f1a0a500fed258863d3d4e7216f67c8639 (patch) | |
| tree | 3112c9426cc4e637ff80910eef25afff3944d3fb /minecraft-protocol/src/mc_buf.rs | |
| parent | ff8e3f2d9e4752cf3ecf8ed80041ab352cceb870 (diff) | |
| download | azalea-drasl-4794b4f1a0a500fed258863d3d4e7216f67c8639.tar.xz | |
writing packets is now friendlier
Diffstat (limited to 'minecraft-protocol/src/mc_buf.rs')
| -rw-r--r-- | minecraft-protocol/src/mc_buf.rs | 90 |
1 files changed, 53 insertions, 37 deletions
diff --git a/minecraft-protocol/src/mc_buf.rs b/minecraft-protocol/src/mc_buf.rs index b42a33bb..54ba1f7d 100644 --- a/minecraft-protocol/src/mc_buf.rs +++ b/minecraft-protocol/src/mc_buf.rs @@ -10,51 +10,67 @@ use tokio::io::{AsyncRead, AsyncReadExt}; const MAX_STRING_LENGTH: u16 = 32767; // const MAX_COMPONENT_STRING_LENGTH: u32 = 262144; -pub fn write_byte(buf: &mut Vec<u8>, n: u8) { - WriteBytesExt::write_u8(buf, n).unwrap(); +#[async_trait] +pub trait Writable { + fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error>; + fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>; + fn write_varint(&mut self, value: i32) -> Result<(), std::io::Error>; + fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error>; + fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error>; + fn write_short(&mut self, n: u16) -> Result<(), std::io::Error>; + fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>; } -pub fn write_bytes(buf: &mut Vec<u8>, bytes: &[u8]) { - buf.extend_from_slice(bytes); -} +#[async_trait] +impl Writable for Vec<u8> { + fn write_byte(&mut self, n: u8) -> Result<(), std::io::Error> { + WriteBytesExt::write_u8(self, n) + } -pub fn write_varint(buf: &mut Vec<u8>, mut value: i32) { - let mut buffer = [0]; - if value == 0 { - buf.write_all(&buffer).unwrap(); + fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { + Ok(self.extend_from_slice(bytes)) } - while value != 0 { - buffer[0] = (value & 0b0111_1111) as u8; - value = (value >> 7) & (i32::max_value() >> 6); - if value != 0 { - buffer[0] |= 0b1000_0000; + + fn write_varint(&mut self, mut value: i32) -> Result<(), std::io::Error> { + let mut buffer = [0]; + if value == 0 { + self.write_all(&buffer).unwrap(); + } + while value != 0 { + buffer[0] = (value & 0b0111_1111) as u8; + value = (value >> 7) & (i32::max_value() >> 6); + if value != 0 { + buffer[0] |= 0b1000_0000; + } + self.write_all(&buffer)?; } - buf.write_all(&buffer).unwrap(); + Ok(()) } -} -pub fn write_utf_with_len(buf: &mut Vec<u8>, string: &str, len: usize) { - if string.len() > len { - panic!( - "String too big (was {} bytes encoded, max {})", - string.len(), - len - ); + fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error> { + if string.len() > len { + panic!( + "String too big (was {} bytes encoded, max {})", + string.len(), + len + ); + } + self.write_varint(string.len() as i32); + self.write_bytes(string.as_bytes()) } - write_varint(buf, string.len() as i32); - write_bytes(buf, string.as_bytes()); -} -pub fn write_utf(buf: &mut Vec<u8>, string: &str) { - write_utf_with_len(buf, string, MAX_STRING_LENGTH.into()); -} -pub fn write_short(buf: &mut Vec<u8>, n: u16) { - WriteBytesExt::write_u16::<BigEndian>(buf, n).unwrap(); -} + fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error> { + self.write_utf_with_len(string, MAX_STRING_LENGTH.into()) + } + + fn write_short(&mut self, n: u16) -> Result<(), std::io::Error> { + WriteBytesExt::write_u16::<BigEndian>(self, n) + } -pub fn write_byte_array(buf: &mut Vec<u8>, bytes: &[u8]) { - write_varint(buf, bytes.len() as i32); - write_bytes(buf, bytes); + fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { + self.write_varint(bytes.len() as i32); + self.write_bytes(bytes) + } } #[async_trait] @@ -159,11 +175,11 @@ mod tests { #[test] fn test_write_varint() { let mut buf = Vec::new(); - write_varint(&mut buf, 123456); + buf.write_varint(123456); assert_eq!(buf, vec![192, 196, 7]); let mut buf = Vec::new(); - write_varint(&mut buf, 0); + buf.write_varint(0); assert_eq!(buf, vec![0]); } |
