public static NetMsgRecvData Read(int msgId, byte[] bts, NetMsgEventHandle handle) { MemoryStream ms = new MemoryStream(bts); ms.SetLength(bts.Length); NetMsgRecvData data = null; try { IMessage md = handle.mMsgParser.ParseFrom(bts); data = new NetMsgRecvData(handle.mCallBack, md); } catch (Exception ex) { Logger.Log("[NetPacketHelper.Read() => 反序列化数据出错,ex:" + ex + "]"); } ms.Close(); return(data); }
private void OnReceiveData(S2C_MSG_DATA value) { if (value.ErrorCode < 0) { Action RunOnMainThread = () => { NetErrorHelper.DoErrorCode(value.ErrorCode); }; Loom.QueueOnMainThread(RunOnMainThread); _recvStream.Clear(); return; } byte[] bytes = value.Data.ToByteArray(); _recvStream.AddBytes(bytes); while (_recvStream.BytesAvailable >= 4) { int msgId = _recvStream.ReadInt(); //消息id int msgLen = _recvStream.ReadInt(); //消息长度 byte[] msgData = _recvStream.ReadBytes(msgLen); if (_dictNetHandle.ContainsKey(msgId)) { NetMsgEventHandle handle = _dictNetHandle[msgId]; NetMsgRecvData data = NetPacketHelper.Read(msgId, msgData, handle); if (data != null) { _lstRecvDatas.Add(data); } } else { Logger.LogWarning("[GameServer.OnReceiveData() => 消息号:" + msgId + "未注册]"); } } _recvStream.Clear(); }