//分发消息 private static void FireMsg(string msgName, NetMsgBase msgBase) { if (msgListeners.ContainsKey(msgName)) { msgListeners[msgName](msgBase); } }
//更新消息 public static void MsgUpdate() { //初步判断,提升效率 if (msgCount == 0) { return; } //重复处理消息 for (int i = 0; i < MAX_MESSAGE_FIRE; i++) { //获取第一条消息 NetMsgBase 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 NetMsgBase Decode(string protoName, byte[] bytes, int offset, int count) { string s = System.Text.Encoding.UTF8.GetString(bytes, offset, count); NetMsgBase msgBase = (NetMsgBase)JsonUtility.FromJson(s, Type.GetType(protoName)); return(msgBase); }
private static void OnReceiveData() { //消息长度 if (buffCount <= 2) { return; } byte[] bytes = readBuff.bytes; //获取消息体长度 int readIdx = readBuff.readIdx; short bodyLength = (short)((bytes[readIdx + 1] << 8) | bytes[readIdx]); if (readBuff.length < bodyLength) { return; } readBuff.readIdx += 2; //解析协议名 int nameCount = 0; string protoName = NetMsgBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount); if (protoName == "") { Debug.Log("OnReceiveData MsgBase.DecodeName fail"); return; } readBuff.readIdx += nameCount; //解析协议体 int bodyCount = bodyLength - nameCount; NetMsgBase msgBase = NetMsgBase.Decode(protoName, readBuff.bytes, readBuff.readIdx, bodyCount); readBuff.readIdx += bodyCount; readBuff.CheckAndMoveBytes(); //添加到消息队列 lock (msgList) { msgList.Add(msgBase); } msgCount++; //继续读取消息 if (readBuff.length > 2) { OnReceiveData(); } }
//编码协议名(2字节长度+字符串) public static byte[] EncodeName(NetMsgBase msgBase) { //名字bytes和长度 byte[] nameBytes = System.Text.Encoding.UTF8.GetBytes(msgBase.protoName); short len = (short)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); }
//发送数据 public static void Send(NetMsgBase msg) { //状态判断 if (socket == null || !socket.Connected) { return; } if (isConnecting) { return; } if (isClosing) { return; } //数据编码 byte[] nameBytes = NetMsgBase.EncodeName(msg); byte[] bodyBytes = NetMsgBase.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; } if (count == 1) { socket.BeginSend(sendBytes, 0, sendBytes.Length, 0, SendCallback, socket); } }
//监听PONG协议 private static void OnMsgPong(NetMsgBase msgBase) { lastPongTime = Time.time; }
//编码 public static byte[] Encode(NetMsgBase msgBase) { string s = JsonUtility.ToJson(msgBase); return(System.Text.Encoding.UTF8.GetBytes(s)); }