mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-09-08 20:20:40 +00:00
feat[rust]: rust protocol
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
#![allow(unused_imports)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_camel_case_types)]
|
||||
use std::any::Any;
|
||||
use crate::${protocol_root_path}::i_byte_buffer::IByteBuffer;
|
||||
use crate::${protocol_root_path}::i_byte_buffer::IPacket;
|
||||
use crate::${protocol_root_path}::protocol_manager::write;
|
||||
use crate::${protocol_root_path}::protocol_manager::readByProtocolId;
|
||||
|
||||
@@ -29,6 +32,25 @@ impl ByteBuffer {
|
||||
#[allow(dead_code)]
|
||||
#[allow(unused_parens)]
|
||||
impl IByteBuffer for ByteBuffer {
|
||||
fn adjustPadding(&mut self, predictionLength: i32, beforeWriteIndex: i32) {
|
||||
let currentWriteIndex = self.getWriteOffset();
|
||||
let predictionCount = self.writeIntCount(predictionLength);
|
||||
let length = currentWriteIndex - beforeWriteIndex - predictionCount;
|
||||
let lengthCount = self.writeIntCount(length);
|
||||
let padding = lengthCount - predictionCount;
|
||||
if (padding == 0) {
|
||||
self.setWriteOffset(beforeWriteIndex);
|
||||
self.writeInt(length);
|
||||
self.setWriteOffset(currentWriteIndex);
|
||||
} else {
|
||||
let mut bytes: Vec<i8> = Vec::with_capacity(length as usize);
|
||||
bytes.extend(&self.buffer[(currentWriteIndex - length) as usize..currentWriteIndex as usize]);
|
||||
self.setWriteOffset(beforeWriteIndex);
|
||||
self.writeInt(length);
|
||||
self.writeBytes(bytes.as_slice());
|
||||
}
|
||||
}
|
||||
|
||||
fn getBuffer(&self) -> &Vec<i8> {
|
||||
return &self.buffer;
|
||||
}
|
||||
@@ -398,7 +420,7 @@ impl IByteBuffer for ByteBuffer {
|
||||
return f64::from_bits(self.readRawLong() as u64);
|
||||
}
|
||||
|
||||
fn writeString(&mut self, value: &String) {
|
||||
fn writeString(&mut self, value: String) {
|
||||
if (value == "" || value.is_empty()) {
|
||||
self.writeInt(0);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#![allow(unused_imports)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_camel_case_types)]
|
||||
use std::any::Any;
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub trait IPacket {
|
||||
fn protocolId(&self) -> i16;
|
||||
}
|
||||
@@ -9,6 +12,7 @@ pub trait IPacket {
|
||||
#[allow(dead_code)]
|
||||
#[allow(unused_parens)]
|
||||
pub trait IByteBuffer {
|
||||
fn adjustPadding(&mut self, predictionLength: i32, beforeWriteIndex: i32);
|
||||
fn getBuffer(&self) -> &Vec<i8>;
|
||||
fn getWriteOffset(&self) -> i32;
|
||||
fn setWriteOffset(&mut self, writeIndex: i32);
|
||||
@@ -43,7 +47,7 @@ pub trait IByteBuffer {
|
||||
fn readFloat(&mut self) -> f32;
|
||||
fn writeDouble(&mut self, value: f64);
|
||||
fn readDouble(&mut self) -> f64;
|
||||
fn writeString(&mut self, value: &String);
|
||||
fn writeString(&mut self, value: String);
|
||||
fn readString(&mut self) -> String;
|
||||
fn writePacket(&mut self, packet: &dyn Any, protocolId: i16);
|
||||
fn readPacket(&mut self, protocolId: i16) -> Box<dyn Any>;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
${protocol_note}
|
||||
#[derive(Clone)]
|
||||
pub struct ${protocol_name} {
|
||||
${protocol_field_definition}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
#![allow(unused_imports)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_camel_case_types)]
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use crate::${protocol_root_path}::i_byte_buffer::{IByteBuffer, IPacket};
|
||||
use crate::${protocol_root_path}::i_byte_buffer::IByteBuffer;
|
||||
${protocol_imports}
|
||||
|
||||
pub fn write(buffer: &mut dyn IByteBuffer, packet: &dyn Any, protocolId: i16) {
|
||||
|
||||
@@ -6,7 +6,7 @@ impl IPacket for ${protocol_name} {
|
||||
|
||||
impl ${protocol_name} {
|
||||
pub fn new() -> ${protocol_name} {
|
||||
let mut packet = ${protocol_name} {
|
||||
let packet = ${protocol_name} {
|
||||
${protocol_field_definition}
|
||||
};
|
||||
return packet;
|
||||
@@ -21,12 +21,12 @@ pub fn write${protocol_name}(buffer: &mut dyn IByteBuffer, packet: &dyn Any) {
|
||||
pub fn read${protocol_name}(buffer: &mut dyn IByteBuffer) -> Box<dyn Any> {
|
||||
let length = buffer.readInt();
|
||||
let mut packet = ${protocol_name}::new();
|
||||
if (length == 0) {
|
||||
if length == 0 {
|
||||
return Box::new(packet);
|
||||
}
|
||||
let beforeReadIndex = buffer.getReadOffset();
|
||||
${protocol_read_deserialization}
|
||||
if (length > 0) {
|
||||
if length > 0 {
|
||||
buffer.setReadOffset(beforeReadIndex + length);
|
||||
}
|
||||
return Box::new(packet);
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_mut)]
|
||||
#![allow(unused_variables)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_camel_case_types)]
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
|
||||
Reference in New Issue
Block a user