use std::hash::Hash; use crate::service::ServiceKey; use self::protocol_v1::ProtocolV1Reader; pub mod protocol_v1; #[derive(PartialEq, Debug)] pub enum MsgType { HeartBit, //心跳 ChannelOn, // 通道在线了 AckChannelOn, //回应消息 DXCOn, // 代理上限 AckDXCOn, // 回应 dxc 下线 DXCOff, // dxc下线无需回应 NormalMsg, // 普通消息 RspNormalMsg, // 回应普通消息 ExceptionMsg, // 异常消息 TimeOutMsg, // 超时时间 AckNormalMsg, //确认消息 RefuseMsg, // 拒绝消息 TimerTask, //定时任务 ParallelTask, //并行任务结果 ApiGateWayOn, // 网关上线 BackApiGateNodeId, //给网关分配节点信息 AckBackGateNodeId, //返回的 LoadService, // 加载 Service AskNodeId, // 要分分配NodeId AckNodeId, //回应NodeId RspNodeConnId, // 回应节点连接信息的 Login, // 登录消息 AckLogin, // 登录消息 ReLogin, // 重登陆 Redirect, // 重定向 LoginBad, // 登录信息残缺 LoginFail, // 登录失败 ReLoginFail, // 重登陆失败 LoginSucceed, // 登录成功 LoginMultiLogin, // 重复登录 KickOff, // 重复登录踢下线 NotSupport, // 不支持的协议 SetEnvId, // 设置环境ID EraseEnvId, // 清空环境ID //------------设置通道不需要ID------------ SetChannelNOId, // 设置通道不需要ID //-----------direct的消息状态---------------- DirectLogin, // 直连登录 DirectLoginSucceed, // 登录成功 DirectLoginFail, // 登录失败 System, // 系统消息 Sentinel, //哨兵 } impl MsgType { fn from(value: i32) -> Option { match value { 0 => Some(MsgType::HeartBit), 1 => Some(MsgType::ChannelOn), 2 => Some(MsgType::AckChannelOn), 3 => Some(MsgType::DXCOn), 4 => Some(MsgType::AckDXCOn), 5 => Some(MsgType::DXCOff), 6 => Some(MsgType::NormalMsg), 7 => Some(MsgType::RspNormalMsg), 8 => Some(MsgType::ExceptionMsg), 9 => Some(MsgType::TimeOutMsg), 10 => Some(MsgType::AckNormalMsg), 11 => Some(MsgType::RefuseMsg), 12 => Some(MsgType::TimerTask), 13 => Some(MsgType::ParallelTask), 14 => Some(MsgType::ApiGateWayOn), 15 => Some(MsgType::BackApiGateNodeId), 16 => Some(MsgType::AckBackGateNodeId), 17 => Some(MsgType::LoadService), 18 => Some(MsgType::AskNodeId), 19 => Some(MsgType::AckNodeId), 20 => Some(MsgType::RspNodeConnId), 21 => Some(MsgType::Login), 22 => Some(MsgType::AckLogin), 23 => Some(MsgType::ReLogin), 24 => Some(MsgType::Redirect), 25 => Some(MsgType::LoginBad), 26 => Some(MsgType::LoginFail), 27 => Some(MsgType::ReLoginFail), 28 => Some(MsgType::LoginSucceed), 29 => Some(MsgType::LoginMultiLogin), 30 => Some(MsgType::KickOff), 31 => Some(MsgType::NotSupport), 32 => Some(MsgType::SetEnvId), 33 => Some(MsgType::EraseEnvId), 34 => Some(MsgType::SetChannelNOId), 35 => Some(MsgType::DirectLogin), 36 => Some(MsgType::DirectLoginSucceed), 37 => Some(MsgType::DirectLoginFail), 38 => Some(MsgType::System), 38 => Some(MsgType::Sentinel), _ => None, } } } #[derive(Copy, Clone)] pub struct XID { pub conn_id: i64, pub request_id: i64, } impl Hash for XID { fn hash(&self, state: &mut H) { self.conn_id.hash(state); self.request_id.hash(state); } } impl PartialEq for XID { fn eq(&self, other: &Self) -> bool { self.conn_id == other.conn_id && self.request_id == other.request_id } } impl Eq for XID {} impl Default for XID { fn default() -> XID { XID { conn_id: 0, request_id: 0, } } } impl XID { pub fn new(conn_id: i64, msg_id: i64) -> Self { XID { conn_id, request_id: msg_id, } } pub fn is_empty(&self) -> bool { self.request_id == 0 } } /** * ======= 读取内容 ====== */ pub fn get_version(message: &[u8]) -> u16 { unsafe { let ptr = message.as_ptr().offset(4); let bytes = *(ptr as *const [u8; 2]); u16::from_le_bytes(bytes) } } pub fn get_sender_service_key(message: &[u8]) -> Option { let version = get_version(message); match version { 1 => { let v1_reader = ProtocolV1Reader::new(message); Some(v1_reader.sender()) } _ => None, } } pub fn get_receiver_service_key(message: &[u8]) -> Option { let version = get_version(message); match version { 1 => { let v1_reader = ProtocolV1Reader::new(message); Some(v1_reader.receiver()) } _ => None, } }