added packet_id to common
This commit is contained in:
parent
7eecea4eb6
commit
42f3da666b
9
src/packet/command.rs
Normal file
9
src/packet/command.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
pub struct Command {
|
||||||
|
pub cookie: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CommandResp {
|
||||||
|
pub cookie: u32,
|
||||||
|
}
|
||||||
@ -9,6 +9,7 @@ use crate::utils::*;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Common<'a> {
|
pub struct Common<'a> {
|
||||||
|
pub packet_id: u16,
|
||||||
pub version: u8,
|
pub version: u8,
|
||||||
pub id: &'a str,
|
pub id: &'a str,
|
||||||
pub ttl: u8,
|
pub ttl: u8,
|
||||||
@ -20,6 +21,7 @@ pub struct Common<'a> {
|
|||||||
impl<'a> Common<'a> {
|
impl<'a> Common<'a> {
|
||||||
pub fn encode(&self) -> Vec<u8> {
|
pub fn encode(&self) -> Vec<u8> {
|
||||||
let mut result = vec![];
|
let mut result = vec![];
|
||||||
|
result.extend_from_slice(&self.packet_id.to_be_bytes());
|
||||||
encode_u8(&mut result, self.version);
|
encode_u8(&mut result, self.version);
|
||||||
|
|
||||||
encode_buf_without_size(&mut result, &self.id.as_bytes(), 32);
|
encode_buf_without_size(&mut result, &self.id.as_bytes(), 32);
|
||||||
@ -38,33 +40,36 @@ impl<'a> Common<'a> {
|
|||||||
pub fn from_slice(value: &'a [u8]) -> Result<(Common<'a>, &'a [u8])> {
|
pub fn from_slice(value: &'a [u8]) -> Result<(Common<'a>, &'a [u8])> {
|
||||||
let id_len = 32;
|
let id_len = 32;
|
||||||
|
|
||||||
if value.len() < 5 + id_len {
|
if value.len() < 7 + id_len {
|
||||||
return Err("common header length error".into());
|
return Err("common header length error".into());
|
||||||
}
|
}
|
||||||
let version = value[0];
|
let packet_id = u16::from_be_bytes(value[0..2].try_into().unwrap());
|
||||||
|
let version = value[2];
|
||||||
|
|
||||||
let v1 = &value[1..1 + id_len];
|
let v1 = &value[3..3 + id_len];
|
||||||
let mut id = match std::str::from_utf8(v1) {
|
let mut id = match std::str::from_utf8(v1) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(e) => return Err(SDLanError::ConvertError(e.to_string())),
|
Err(e) => return Err(SDLanError::ConvertError(e.to_string())),
|
||||||
};
|
};
|
||||||
id = id.trim_end_matches('\0');
|
id = id.trim_end_matches('\0');
|
||||||
let ttl = value[1 + id_len];
|
let ttl = value[3 + id_len];
|
||||||
let flags = BigEndian::read_u16(&value[2 + id_len..4 + id_len]);
|
let flags = BigEndian::read_u16(&value[4 + id_len..6 + id_len]);
|
||||||
let pc = value[4 + id_len];
|
let pc = value[6 + id_len];
|
||||||
|
|
||||||
let common = Self {
|
let common = Self {
|
||||||
|
packet_id,
|
||||||
version,
|
version,
|
||||||
id,
|
id,
|
||||||
ttl,
|
ttl,
|
||||||
pc: pc.into(),
|
pc: pc.into(),
|
||||||
flags: flags,
|
flags: flags,
|
||||||
};
|
};
|
||||||
Ok((common, &value[5 + id_len..]))
|
Ok((common, &value[7 + id_len..]))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(id: &'a str) -> Self {
|
pub fn new(id: &'a str) -> Self {
|
||||||
return Common {
|
return Common {
|
||||||
|
packet_id: 0,
|
||||||
id,
|
id,
|
||||||
version: 1,
|
version: 1,
|
||||||
ttl: 2,
|
ttl: 2,
|
||||||
@ -96,6 +101,9 @@ pub enum PacketType {
|
|||||||
PKTQueryPeer,
|
PKTQueryPeer,
|
||||||
// 服务端向客户端返回消息
|
// 服务端向客户端返回消息
|
||||||
PKTPeerInfo,
|
PKTPeerInfo,
|
||||||
|
// sn send command to other sn or edges
|
||||||
|
PKTCommand,
|
||||||
|
PKTCommandResp,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::convert::From<u8> for PacketType {
|
impl std::convert::From<u8> for PacketType {
|
||||||
@ -112,6 +120,8 @@ impl std::convert::From<u8> for PacketType {
|
|||||||
8 => Self::PKTUnregisterSuper,
|
8 => Self::PKTUnregisterSuper,
|
||||||
9 => Self::PKTQueryPeer,
|
9 => Self::PKTQueryPeer,
|
||||||
10 => Self::PKTPeerInfo,
|
10 => Self::PKTPeerInfo,
|
||||||
|
11 => Self::PKTCommand,
|
||||||
|
12 => Self::PKTCommandResp,
|
||||||
_ => Self::PKTInvalid,
|
_ => Self::PKTInvalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,6 +141,8 @@ impl PacketType {
|
|||||||
Self::PKTUnregisterSuper => 8,
|
Self::PKTUnregisterSuper => 8,
|
||||||
Self::PKTQueryPeer => 9,
|
Self::PKTQueryPeer => 9,
|
||||||
Self::PKTPeerInfo => 10,
|
Self::PKTPeerInfo => 10,
|
||||||
|
Self::PKTCommand => 11,
|
||||||
|
Self::PKTCommandResp => 12,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -143,6 +155,7 @@ mod test {
|
|||||||
|
|
||||||
let id = "hello";
|
let id = "hello";
|
||||||
let common = Common {
|
let common = Common {
|
||||||
|
packet_id: 0,
|
||||||
version: 0,
|
version: 0,
|
||||||
id,
|
id,
|
||||||
ttl: 2,
|
ttl: 2,
|
||||||
@ -169,12 +182,12 @@ pub fn encode_packet_encrypted<T: serde::Serialize>(
|
|||||||
let body = serde_json::to_vec(pkt)?;
|
let body = serde_json::to_vec(pkt)?;
|
||||||
let body = aes_encrypt(header_pass, &body)?;
|
let body = aes_encrypt(header_pass, &body)?;
|
||||||
|
|
||||||
let mut result = Vec::with_capacity(4 + hdr.len() + body.len());
|
let mut result = Vec::with_capacity(2 + hdr.len() + body.len());
|
||||||
let total_size = (2 + hdr.len() + body.len()) as u16;
|
let total_size = (hdr.len() + body.len()) as u16;
|
||||||
// insert total size
|
// insert total size
|
||||||
result.extend_from_slice(&total_size.to_be_bytes());
|
result.extend_from_slice(&total_size.to_be_bytes());
|
||||||
// packet_id
|
// packet_id
|
||||||
result.extend_from_slice(&[0, 0]);
|
// result.extend_from_slice(&[0, 0]);
|
||||||
// insert header
|
// insert header
|
||||||
result.extend_from_slice(&hdr);
|
result.extend_from_slice(&hdr);
|
||||||
// insert body
|
// insert body
|
||||||
@ -199,12 +212,12 @@ pub fn encode_packet_packet(cmn: &Common, pkt: &Packet<'_>) -> Result<Vec<u8>> {
|
|||||||
let hdr = cmn.encode();
|
let hdr = cmn.encode();
|
||||||
let body = pkt.marshal()?;
|
let body = pkt.marshal()?;
|
||||||
|
|
||||||
let mut result = Vec::with_capacity(4 + hdr.len() + body.len());
|
let mut result = Vec::with_capacity(2 + hdr.len() + body.len());
|
||||||
let total_size = (2 + hdr.len() + body.len()) as u16;
|
let total_size = (hdr.len() + body.len()) as u16;
|
||||||
// insert total size
|
// insert total size
|
||||||
result.extend_from_slice(&total_size.to_be_bytes());
|
result.extend_from_slice(&total_size.to_be_bytes());
|
||||||
// packet_id
|
// packet_id
|
||||||
result.extend_from_slice(&[0, 0]);
|
// result.extend_from_slice(&[0, 0]);
|
||||||
// insert header
|
// insert header
|
||||||
result.extend_from_slice(&hdr);
|
result.extend_from_slice(&hdr);
|
||||||
// insert body
|
// insert body
|
||||||
@ -219,12 +232,12 @@ pub fn encode_packet<T: serde::Serialize>(cmn: &Common, pkt: &T) -> Result<Vec<u
|
|||||||
// body
|
// body
|
||||||
let body = serde_json::to_vec(pkt)?;
|
let body = serde_json::to_vec(pkt)?;
|
||||||
|
|
||||||
let mut result = Vec::with_capacity(4 + hdr.len() + body.len());
|
let mut result = Vec::with_capacity(2 + hdr.len() + body.len());
|
||||||
let total_size = (2 + hdr.len() + body.len()) as u16;
|
let total_size = (hdr.len() + body.len()) as u16;
|
||||||
// insert total size
|
// insert total size
|
||||||
result.extend_from_slice(&total_size.to_be_bytes());
|
result.extend_from_slice(&total_size.to_be_bytes());
|
||||||
// packet_id
|
// packet_id
|
||||||
result.extend_from_slice(&[0, 0]);
|
// result.extend_from_slice(&[0, 0]);
|
||||||
// insert header
|
// insert header
|
||||||
result.extend_from_slice(&hdr);
|
result.extend_from_slice(&hdr);
|
||||||
// insert body
|
// insert body
|
||||||
@ -244,7 +257,7 @@ pub fn decode_common<'a>(value: &'a [u8]) -> Result<(Common<'a>, &'a [u8])> {
|
|||||||
return Err(SDLanError::NormalError("decode pkt header size error"));
|
return Err(SDLanError::NormalError("decode pkt header size error"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let value2 = &value[4..2 + size as usize];
|
let value2 = &value[2..2 + size as usize];
|
||||||
let (cmn, value2) = Common::from_slice(value2)?;
|
let (cmn, value2) = Common::from_slice(value2)?;
|
||||||
Ok((cmn, value2))
|
Ok((cmn, value2))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,3 +30,6 @@ pub use query_peer::*;
|
|||||||
|
|
||||||
mod peer_info;
|
mod peer_info;
|
||||||
pub use peer_info::*;
|
pub use peer_info::*;
|
||||||
|
|
||||||
|
mod command;
|
||||||
|
pub use command::*;
|
||||||
|
|||||||
@ -84,6 +84,7 @@ mod test {
|
|||||||
|
|
||||||
fn prepare_data() -> (Common<'static>, RegisterSuper<'static>) {
|
fn prepare_data() -> (Common<'static>, RegisterSuper<'static>) {
|
||||||
let cmn1 = Common {
|
let cmn1 = Common {
|
||||||
|
packet_id: 0,
|
||||||
version: 1,
|
version: 1,
|
||||||
id: "asxalex",
|
id: "asxalex",
|
||||||
ttl: 128,
|
ttl: 128,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user