public ProtocolItem(int protocolId, byte[] bytes) { ProtocolId = protocolId; byte[] enryptByteArray; MBinaryWriter mbw = new MBinaryWriter(); if (!Game.GameConfig.instance.encrypt) { if (bytes == null) { mbw.Write(ProtocolConf.PROTOCOL_HEAD_LENGTH); } else { mbw.Write(bytes.Length + ProtocolConf.PROTOCOL_HEAD_LENGTH); } } mbw.Write(ProtocolProxy.instance.Version); mbw.Write(ProtocolProxy.instance.UserID0); mbw.Write(ProtocolProxy.instance.UserID1); mbw.Write(ProtocolProxy.instance.UserID2); mbw.Write(ProtocolProxy.instance.UserID3); mbw.Write(ProtocolProxy.instance.UserID4); mbw.Write(ProtocolProxy.instance.UserID5); mbw.Write(TimeController.instance.ServerTimeTicksMillisecondAfter9); mbw.Write(ProtocolProxy.instance.UserID7); mbw.Write(ProtocolId); if (bytes != null) { mbw.Write(bytes); } if (Game.GameConfig.instance.encrypt) { enryptByteArray = Common.Util.EncryptUtil.AESEncryptBytes(mbw.ToArray(), Game.GameConfig.instance.aesEncryptKey); mbw.Clear(); mbw.Write(enryptByteArray.Length); mbw.Write(enryptByteArray); } ProtocolItemBytes = mbw.ToArray(); mbw.Close(); mbw = null; if (ProtocolConf.NeedShowMask(ProtocolId)) { Logic.UI.Mask.Contorller.MaskController.instance.ShowMask(); } }
public ProtocolItem(int protocolId, ProtoBuf.IExtensible protocol) { if (protocol != null) { if (ProtocolProxy.instance.IsLoginServer) { ProtocolId = (int)ProtocolConf.GetLoginServerIdByType(protocol.GetType()); } else { ProtocolId = protocolId; } Protocol = protocol; ProtocolItemBytes = ParseItem(); if (ProtocolConf.NeedShowMask(ProtocolId)) { Logic.UI.Mask.Contorller.MaskController.instance.ShowMask(); } } }
private void ParseProtocol() { if (!Connected) { return; } _publicInt = _socket.Receive(_recvBuff, SocketFlags.None); if (_publicInt <= 0) { _gameServerErr = true; return; } _recvStream.Position = _recvStream.Length; _recvStream.Write(_recvBuff, 0, _publicInt); _recvStream.Position = _recvStreamPos; byte[] bytes = new byte[ProtocolConf.PACKGE_LEN]; while (_recvStream.Length - _recvStream.Position >= ProtocolConf.PACKGE_LEN) { //long t1 = Common.Util.TimeUtil.GetTimeStamp(); _recvStreamPos = _recvStream.Position; _recvStream.Read(bytes, 0, ProtocolConf.PACKGE_LEN); _mbr = new MBinaryReader(bytes); _publicInt = _mbr.ReadInt32(); if (_recvStream.Length - _recvStream.Position >= _publicInt) { _recvStreamPos = 0; bytes = new byte[_publicInt]; _recvStream.Read(bytes, 0, _publicInt); if (Game.GameConfig.instance.encrypt) { byte[] decryptBytes = Common.Util.EncryptUtil.AESDecryptBytes(bytes, Game.GameConfig.instance.aesEncryptKey); _publicInt = decryptBytes.Length; _mbr = new MBinaryReader(decryptBytes); } else { _mbr = new MBinaryReader(bytes); } ProtocolProxy.instance.Version = _mbr.ReadInt32(); ProtocolProxy.instance.UserID0 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID1 = _mbr.ReadInt32(); _userID2 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID3 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID4 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID5 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID6 = _mbr.ReadInt32(); ProtocolProxy.instance.UserID7 = _mbr.ReadInt32(); int protocolId = _mbr.ReadInt32(); _publicInt -= ProtocolConf.PROTOCOL_HEAD_LENGTH; byte[] bytes2 = new byte[_publicInt]; _mbr.Read(bytes2, 0, _publicInt); if (ProtocolConf.instance.ExistInLuaProcotols(protocolId)) { if (_recvQueue != null) { lock (_recvQueue) { _recvQueue.Enqueue(new ProtocolItem() { ProtocolId = protocolId, ProtocolBytes = bytes2 }); } } } else { _protocolItem = new ProtocolItem(); _protocolItem.ProtocolId = protocolId; using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes2)) { System.Type t = ProtocolProxy.instance.IsLoginServer ? ProtocolConf.GetLoginServerTypeByID(_protocolItem.ProtocolId) : ProtocolConf.GetTypeByID(_protocolItem.ProtocolId); if (t == null) { Debugger.LogError("未配置ID为" + _protocolItem.ProtocolId + "的协议 , 不含包头长:" + bytes.Length + "字节"); } else { if (ProtocolProxy.instance.IsLoginServer) { _protocolItem.Protocol = Activator.CreateInstance(t) as IProtocol; (_protocolItem.Protocol as IProtocol).FromBytes(new MBinaryReader(ms)); } else { if (t != typeof(EmptyMessage)) { _protocolItem.Protocol = ProtoBuf.Serializer.NonGeneric.Deserialize(t, ms) as ProtoBuf.IExtensible; } } } } if (_recvQueue != null) { lock (_recvQueue) { _recvQueue.Enqueue(_protocolItem); } } } } else { _recvStream.Position = _recvStreamPos; break; } _mbr.Close(); _mbr = null; } if (_recvStream.Position == _recvStream.Length) { _recvStream.SetLength(0); } }