// 分发消息 private static void FireMsg(string msgName, JsonMsgBase msgBase) { if (msgListeners.ContainsKey(msgName)) { msgListeners[msgName].Invoke(msgBase); } }
// 分发消息 private static void MsgUpdate() { // 初步判断, 提升效率 if (msgCount == 0) { return; } // 重复处理消息 for (int i = 0; i < MAX_MESSAGE_FIRE; i++) { // 获取第一条消息 JsonMsgBase msgBase = null; lock (msgList) { if (msgList.Count > 0) { msgBase = msgList[0]; msgList.RemoveAt(0); msgCount--; } } // 分发消息 if (msgBase != null) { FireMsg(msgBase.protoName, msgBase); } // 没有消息了 else { break; } } }
// 解码 public static JsonMsgBase Decode(string protoName, byte[] bytes, int offset, int count) { string s = System.Text.Encoding.UTF8.GetString(bytes, offset, count); JsonMsgBase msgBase = (JsonMsgBase)JsonUtility.FromJson(s, Type.GetType(protoName)); return(msgBase); }
// 编码协议名(2字节长度+字符串) public static byte[] EncodeName(JsonMsgBase msgBase) { // 名字bytes和长度 byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(msgBase.protoName); Int16 len = (Int16)nameBytes.Length; // 申请bytes数值 byte[] bytes = new byte[2 + len]; // 组装2字节的长度信息 bytes[0] = (byte)(len % 256); bytes[1] = (byte)(len / 256); // 组装名字bytes Array.Copy(nameBytes, 0, bytes, 2, len); return(bytes); }
// 接受数据处理 private static void OnRecieveData() { // 消息长度 if (readBuff.length <= 2) { return; } // 获取消息体长度 int readIdx = readBuff.readIdx; byte[] bytes = readBuff.bytes; Int16 bodyLength = (Int16)((bytes[readIdx + 1] << 8) | bytes[readIdx]); if (readBuff.length < bodyLength) { return; } readBuff.readIdx += 2; // 解析协议名 int nameCount = 0; string protoName = JsonMsgBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount); if (protoName == "") { Debug.Log("OnRecieveData MsgBase.DecodeName Fail"); return; } readBuff.readIdx += nameCount; // 解析协议体 int bodyCount = bodyLength - nameCount; JsonMsgBase msgBase = JsonMsgBase.Decode(protoName, readBuff.bytes, readBuff.readIdx, bodyCount); readBuff.readIdx += bodyCount; readBuff.CheckAndMoveBytes(); // 添加到消息队列 lock (msgList) { msgList.Add(msgBase); } msgCount++; // 继续读取消息 if (readBuff.length > 2) { OnRecieveData(); } }
// Json 发送数据 public static void Send(JsonMsgBase msg) { // 状态判断 if (socket == null || !socket.Connected) { return; } if (isConnecting || isClosing) { return; } // 数据编码 byte[] nameBytes = JsonMsgBase.EncodeName(msg); byte[] bodyBytes = JsonMsgBase.Encode(msg); int len = nameBytes.Length + bodyBytes.Length; byte[] sendBytes = new byte[2 + len]; // 组装长度 sendBytes[0] = (byte)(len % 256); sendBytes[1] = (byte)(len / 256); // 组装名字 Array.Copy(nameBytes, 0, sendBytes, 2, nameBytes.Length); // 组装消息体 Array.Copy(bodyBytes, 0, sendBytes, 2 + nameBytes.Length, bodyBytes.Length); // 写入队列 ByteArray ba = new ByteArray(sendBytes); int count = 0; // WriteQueue的长度 lock (writeQueue) { writeQueue.Enqueue(ba); count = writeQueue.Count; } // send if (count == 1) { socket.BeginSend(sendBytes, 0, sendBytes.Length, 0, SendCallback, socket); } }
// 监听PONG协议 private static void OnMsgPong(JsonMsgBase msgBase) { lastPongTime = Time.time; }
// 编码 public static byte[] Encode(JsonMsgBase msgBase) { string s = JsonUtility.ToJson(msgBase); return(System.Text.Encoding.UTF8.GetBytes(s)); }