public override CMD_Base_RespNtf Deserialize(ByteBuffer buf) { CMD_GP_AdditionScore t = this; // 读取buf填充结构体 todo t.dwLottery = buf.ReadInt(); t.dwIngot = buf.ReadInt(); t.dwVip = buf.ReadInt(); t.dwCalCardTime = buf.ReadInt(); t.dwFreeLuckyTime = buf.ReadInt(); t.dwVipOverTimes = buf.ReadInt(); t.dwHornCount = buf.ReadInt(); t.VIP_Value = buf.ReadInt(); t.dwMinimum = buf.ReadInt(); t.szVipGift = buf.ReadString(10); t.bVipOverTime = buf.ReadByte(); t.bVipGetScore = buf.ReadByte(); t.dwRecommondScore = buf.ReadInt(); t.dwRecommondGet = buf.ReadInt(); t.bPhone = buf.ReadByte(); buf.ReadBytes(3); t.dwRoomCard = buf.ReadInt(); t.bCardShared = buf.ReadBool(); buf.ReadBytes(3); return this; }
public static FieldNode Read(byte[] bytes) { FieldNode node = new FieldNode(0, null); ByteBuffer bfs = new ByteBuffer(bytes); node.Access = (AccessMode)bfs.ReadByte(); node.Domain = (DomainMode)bfs.ReadByte(); node.OutRect = AccessRect.Read(bfs.ReadBytes()); node.OutRect.Target = node; node.InRect = AccessRect.Read(bfs.ReadBytes()); node.InRect.Target = node; node.Type = bfs.ReadByte(); node.Name = bfs.ReadString(); node.Value = bfs.ReadString(); node.Index = bfs.ReadByte(); node.HashID = bfs.ReadInt32(); if (bfs.ReadBool()) { node.PrevHashID = bfs.ReadInt32(); } if (bfs.ReadBool()) { node.NextHashID = bfs.ReadInt32(); } return(node); }
public void Process(byte[] buffer, Connection connection) { var msg = new ByteBuffer(buffer); int length; length = msg.ReadInt32(); var key = msg.ReadBytes(length); length = msg.ReadInt32(); var iv = msg.ReadBytes(length); var rsa = connection.RSAKey.GetKey(); var _key = RSACryptography.RSADecrypt(key, rsa.GetPrivateKey(), false); var _iv = RSACryptography.RSADecrypt(iv, rsa.GetPrivateKey(), false); connection.AesKey.SetClientKey(_key); connection.AesKey.SetClientIv(_iv); // Encria e envia de volta para o cliente. _key = connection.AesKey.GetKey(); _iv = connection.AesKey.GetIv(); key = RSACryptography.RSAEncrypt(_key, rsa.GetClientPublicKey(), false); iv = RSACryptography.RSAEncrypt(_iv, rsa.GetClientPublicKey(), false); var aes = new SpAESKey(key, iv); aes.Send(connection, false); }
public void DealMsg(ReceiveMessageData _data) { if (_data == null) { return; } byte[] _dataBytes = _data.ReceiveBytes; ByteBuffer _buff = new ByteBuffer(_dataBytes); int length = _buff.ReadInt32(); //收到的消息结构长度 byte[] _bytes = _buff.ReadBytes(length); int dataLength = _data.ReceiveLength - 4; //去掉包长度的字节,剩下的为内容长度 if (dataLength > length) //有多余的字节,黏包 { int leftLength = dataLength - length; _data.ReceiveBytes = _buff.ReadBytes(leftLength); _data.ReceiveLength = leftLength; DealMsg(_data); } else if (dataLength == length) { MessageData data = ProtoBufTools.DeSerialize <MessageData>(_bytes); data.Head.ReceivePoint = _data.ReceivePoint; if (DealMsgEvent != null) { DealMsgEvent(data); } } }
public void DealMsg(ReceiveMsgData _data) { if (_data == null) { return; } byte[] _dataBytes = _data.receiveBytes; IPEndPoint receivePoint = _data.receivePoint; ByteBuffer _buff = new ByteBuffer(_dataBytes); int length = _buff.ReadInt32(); //收到的消息结构实际长度 if (length > 0) { if (length <= _data.receiveLength) { byte[] _bytes = _buff.ReadBytes(length); MessageData data = ProtoBufTools.DeSerialize <MessageData>(_bytes); data.receivePoint = receivePoint; DealMsgSwitch(data); } //除本次包内容外还有其它数据,有多余的字节,黏包,再处理一次 int dataLength = _data.receiveLength - 4; if (dataLength > length) { int leftLength = dataLength - length; _data.receiveBytes = _buff.ReadBytes(leftLength); _data.receiveLength = leftLength; DealMsg(_data); } } }
public override void In(ByteBuffer buffer) { this.networkId = buffer.ReadInt32(); this.targetNetworkId = buffer.ReadInt32(); this.position = buffer.ReadInt32(); if (this.position == 0) { this.finalPacketId = buffer.ReadInt32(); this.totalBytes = buffer.ReadInt32(); this.checksum = buffer.ReadBytes(); } this.data = buffer.ReadBytes(); }
public void OnSocket(int key, ByteBuffer byteBuffer) { UnityTools.Log(key); if (key == 11) { loadData(); } else if (key == 133) { byteBuffer.ReadShort(); BattleStartResponse battleStartResponse = new BattleStartResponse(); battleStartResponse.MergeFrom(byteBuffer.ReadBytes()); foreach (var hero in battleStartResponse.Heros) { UnityTools.Log(hero.ToString()); } GameData.g_battleView.InitHeros(battleStartResponse.Heros.ToArrayList()); } else if (key == 135) { byteBuffer.ReadShort(); BattleResultResponse battleResultResponse = new BattleResultResponse(); battleResultResponse.MergeFrom(byteBuffer.ReadBytes()); UnityTools.Log(battleResultResponse.Earn); GameData.g_battleView.ShowResult(GameData.g_battleResult, battleResultResponse.Earn, battleResultResponse.Level, battleResultResponse.Exp, battleResultResponse.LevelUpExp); } else if (key == 111) { byteBuffer.ReadShort(); SkillResponse skillResponse = new SkillResponse(); skillResponse.MergeFrom(byteBuffer.ReadBytes()); foreach (var skill in skillResponse.Skills) { UnityTools.Log(skill.ToString()); } GameData.g_battleView.InitSkills(skillResponse.Skills.ToArrayList()); } else if (key == 113) { byteBuffer.ReadShort(); EquipResponse itemResponse = new EquipResponse(); itemResponse.MergeFrom(byteBuffer.ReadBytes()); foreach (var item in itemResponse.Equips) { UnityTools.Log(item.ToString()); } GameData.g_battleView.InitItems(itemResponse.Equips.ToArrayList()); } }
private string ReadMsgTypeFromByteBuffer(ByteBuffer buf, int len) { byte[] array = buf.ReadBytes(len); string result = Encoding.UTF8.GetString(array); return(result); }
unsafe static decimal DecodeDecimal32(ByteBuffer buffer) { byte[] bytes = new byte[FixedWidth.Decimal32]; buffer.ReadBytes(bytes, 0, bytes.Length); int sign = 1; int exponent = 0; sign = (bytes[0] & 0x80) != 0 ? -1 : 1; if ((bytes[0] & 0x60) != 0x60) { // s 8-bit-exponent (0)23-bit-significant exponent = ((bytes[0] & 0x7F) << 1) | ((bytes[1] & 0x80) >> 7); bytes[0] = 0; bytes[1] &= 0x7F; } else if ((bytes[0] & 0x78) != 0) { // handle NaN and Infinity } else { // s 11 8-bit-exponent (100)21-bit-significant exponent = ((bytes[0] & 0x1F) << 3) | ((bytes[1] & 0xE0) >> 5); bytes[0] = 0; bytes[1] &= 0x1F; bytes[1] |= 0x80; } int low = (int)AmqpBitConverter.ReadUInt(bytes, 0, bytes.Length); return(CreateDecimal(low, 0, 0, sign, exponent - Decimal32Bias)); }
public IMessage decode(byte[] bytes) { ByteBuffer buffer = new ByteBuffer(bytes); int mainId = buffer.ReadShort(); int pbDataLen = bytes.Length - 2; byte[] pbData = buffer.ReadBytes(pbDataLen); if (!m_ProtoIdList.Contains(mainId)) { Debug.LogError("未知协议号"); return(null); } Type protoType = GetTypeByProtoId(mainId); try { MessageParser messageParser = GetParserByType(protoType); IMessage toc = messageParser.ParseFrom(pbData); return(toc); } catch { Debug.LogError("decode Error:" + protoType.ToString()); } return(null); }
private void UnPackProtocol(byte[] msg_bytes) { ByteBuffer buffer = new ByteBuffer(Encrypt.Decode(msg_bytes, Encrypt.DefaultKey)); //读出附加位 int addition = buffer.ReadInt(); //读出用户ID列表 int list_lenght = buffer.ReadInt(); //读出协议体 byte[] protocol_bytes = buffer.ReadBytes((int)buffer.RemainingBytes()); IMessage protocol = Protocol.Decode(protocol_bytes); buffer.Close(); if (protocol != null) { Debug.Log("客户端接收消息:" + protocol.GetType() + " 数据:" + protocol.ToString()); lock (msg_lock) { this.protocol_list.Add(protocol); } } }
unsafe static decimal DecodeDecimal128(ByteBuffer buffer) { byte[] bytes = new byte[FixedWidth.Decimal128]; buffer.ReadBytes(bytes, 0, bytes.Length); int sign = 1; int exponent = 0; sign = (bytes[0] & 0x80) != 0 ? -1 : 1; if ((bytes[0] & 0x60) != 0x60) { // s 14-bit-exponent (0)113-bit-significant exponent = ((bytes[0] & 0x7F) << 7) | ((bytes[1] & 0xFE) >> 1); bytes[0] = 0; bytes[1] &= 0x1; } else if ((bytes[0] & 0x78) != 0) { // handle NaN and Infinity } else { // s 11 14-bit-exponent (100)111-bit-significant // it is out of the valid range already. Should not be used return(0); } int high = (int)AmqpBitConverter.ReadUInt(bytes, 4, 4); int middle = (int)AmqpBitConverter.ReadUInt(bytes, 8, 4); int low = (int)AmqpBitConverter.ReadUInt(bytes, 12, 4); return(CreateDecimal(low, middle, high, sign, exponent - Decimal128Bias)); }
public void OnRecvHit(byte[] Byte) { ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_hit_reply reply = PackCodec.Deserialize <s2c_hit_reply>(buffer.ReadBytes(size - sizeof(Int32))); UInt32 attId = reply.id; UInt32 defId = reply.enemy_id; float hurt = (float)(reply.damage * 1.0 / 1000); //取得BattleTank if (!list.ContainsKey(attId)) { Debug.Log("OnRecvHit attBt == null " + attId); return; } BattleTank attBt = list[attId]; if (!list.ContainsKey(defId)) { Debug.Log("OnRecvHit defBt == null " + defId); return; } BattleTank defBt = list[defId]; //被击中的坦克 defBt.tank.NetBeAttacked(hurt, attBt.tank.gameObject); }
// 发送消息 public void Send(ByteBuffer bytes, short nMessageID) { ByteBuffer temp = new ByteBuffer(bytes.ToBytes()); byte[] buffer = temp.ReadBytes(); m_clientSocket.Send(buffer, nMessageID); }
/// <summary> /// 接收指定客户端Socket的消息 /// </summary> /// <param name="clientSocket"></param> private static void RecieveMessage(object clientSocket) { Socket mClientSocket = (Socket)clientSocket; while (true) { try { int receiveNumber = mClientSocket.Receive(result); Console.WriteLine("接收客户端{0}消息, 长度为{1}", mClientSocket.RemoteEndPoint.ToString(), receiveNumber); ByteBuffer buff = new ByteBuffer(result); int datalength = buff.ReadShort(); int typeId = buff.ReadInt(); byte[] pbdata = buff.ReadBytes(); //通过协议号判断选择的解析类 if (typeId == (int)EnmCmdID.CS_LOGIN_REQ) { CSLoginReq clientReq = PackCodec.Deserialize <CSLoginReq>(pbdata); string user_name = clientReq.LoginInfo.UserName; string pass_word = clientReq.LoginInfo.Password; Console.WriteLine("数据内容:UserName={0},Password={1}", user_name, pass_word); } } catch (Exception ex) { Console.WriteLine(ex.Message); mClientSocket.Shutdown(SocketShutdown.Both); mClientSocket.Close(); break; } } }
public override byte[] Decrypt(byte[] data) { lock (this) { using (ByteBuffer buffer = new ByteBuffer(data)) { byte[] header = buffer.ReadBytes(4); if (!this.Decryptograph.IsValidPacket(header)) { throw new CryptographyException("Invalid header."); } byte[] content = buffer.GetContent(); int length = AesCryptograph.RetrieveLength(header); if (content.Length == length) { this.Decryptograph.Crypt(content); BlurCryptograph.Decrypt(content); return(content); } else { throw new CryptographyException(string.Format("Packet length not matching ({0} != {1}).", content.Length, length)); } } } }
public void OnRecvHeartbeat(byte[] Byte) { ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_heartbeat_reply reply = PackCodec.Deserialize <s2c_heartbeat_reply>(buffer.ReadBytes(size - sizeof(Int32))); }
public void OnRecvShooting(byte[] Byte) { ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_shooting_reply reply = PackCodec.Deserialize <s2c_shooting_reply>(buffer.ReadBytes(size - sizeof(Int32))); UInt32 id = reply.id; Vector3 pos; Vector3 rot; pos.x = (float)(reply.pos_x * 1.0 / 1000); pos.y = (float)(reply.pos_y * 1.0 / 1000); pos.z = (float)(reply.pos_z * 1.0 / 1000); rot.x = (float)(reply.rot_x * 1.0 / 1000); rot.y = (float)(reply.rot_y * 1.0 / 1000); rot.z = (float)(reply.rot_z * 1.0 / 1000); //处理 if (!list.ContainsKey(id)) { Debug.Log("OnRecvShooting bt == null"); return; } BattleTank bt = list[id]; //跳过自己的同步信息 if (id == GameMgr.instance.id) { return; } bt.tank.NetShoot(pos, rot); }
static decimal DecodeDecimal64(ByteBuffer buffer) { byte[] bytes = new byte[FixedWidth.Decimal64]; buffer.ReadBytes(bytes, 0, bytes.Length); int sign = 1; int exponent = 0; sign = (bytes[0] & 0x80) != 0 ? -1 : 1; if ((bytes[0] & 0x60) != 0x60) { // s 10-bit-exponent (0)53-bit-significant exponent = ((bytes[0] & 0x7F) << 3) | ((bytes[1] & 0xE0) >> 5); bytes[0] = 0; bytes[1] &= 0x1F; } else if ((bytes[0] & 0x78) != 0) { // handle NaN and Infinity } else { // s 11 10-bit-exponent (100)51-bit-significant exponent = ((bytes[0] & 0x1F) << 8) | ((bytes[1] & 0xF8) >> 3); bytes[0] = 0; bytes[1] &= 0x7; bytes[1] |= 0x20; } int middle = (int)AmqpBitConverter.ReadUInt(bytes, 0, 4); int low = (int)AmqpBitConverter.ReadUInt(bytes, 4, 4); return(CreateDecimal(low, middle, 0, sign, exponent - Decimal64Bias)); }
public void OnRecvResult(byte[] Byte) { ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_result_reply reply = PackCodec.Deserialize <s2c_result_reply>(buffer.ReadBytes(size - sizeof(Int32))); Int32 team = reply.camp; //弹出胜负面板 UInt32 id = GameMgr.instance.id; BattleTank bt = list[id]; if (bt.camp == team) { PanelMgr.instance.OpenPanel <WinPanel>("", 1); } else { PanelMgr.instance.OpenPanel <WinPanel>("", 0); } //取消监听 NetMgr.servConn.msgDist.DelListener("s2c_update_unit_reply", OnRecvUpdateUnitInfo); NetMgr.servConn.msgDist.DelListener("s2c_shooting_reply", OnRecvShooting); NetMgr.servConn.msgDist.DelListener("s2c_hit_reply", OnRecvHit); NetMgr.servConn.msgDist.DelListener("s2c_result_reply", OnRecvResult); }
private void Start() { pb.PlayerSnapShootMsg msg = new pb.PlayerSnapShootMsg(); msg.username = "******"; msg.playerId = 9000001; byte[] data = ProtobufSerializer.Serialize(msg); Debug.LogError("data len " + data.Length); ByteBuffer buffer = ByteBuffer.Allocate(1024); buffer.WriteShort(1001); buffer.WriteShort(1002); buffer.WriteShort(1003); buffer.WriteLong(10055555); buffer.WriteBytes(data); short s1 = buffer.ReadShort(); short s2 = buffer.ReadShort(); short s3 = buffer.ReadShort(); long l1 = buffer.ReadLong(); byte[] readData = new byte[buffer.ReadableBytes()]; buffer.ReadBytes(readData, 0, buffer.ReadableBytes()); Debug.LogError("readData len " + readData.Length); pb.PlayerSnapShootMsg dmsg = ProtobufSerializer.DeSerialize <pb.PlayerSnapShootMsg>(readData); Debug.LogError("user name " + dmsg.username); //socket receive bytes // }
void OnRecieveMessageDeal(ByteBuffer buffer, UInt32 len = 0) { UInt32 length = 0; UInt32 mainId = 0; if (len != 0) { length = len; } else { length = buffer.ReadUInt32(); //数据字节长度 } mainId = buffer.ReadUInt32(); //msgid byte[] b = buffer.ReadBytes((int)length); //数据字节 NetLogic((int)mainId, b); int next = (int)buffer.ReadUInt32(); if (next != 0) { OnRecieveMessageDeal(buffer, (UInt32)next); } }
/// <summary> /// 获取消息 /// </summary> public static T GetMessage <T>(ByteBuffer buffer) { byte[] data = buffer.ReadBytes(); using (var stream = new MemoryStream(data)) { return((T)Serializer.Deserialize <T>(stream)); } }
public void ByteBufferCheck() { var _byte = 203; var _bytes = new byte[] { 109, 22, 34, 9, 57 }; var _int16 = 1045; var _int32 = 57329; var buffer = ByteBuffer.Allocate(30); buffer.WriteByte((byte)_byte); buffer.WriteBytes(_bytes); buffer.WriteInt16((short)_int16); buffer.WriteInt32(_int32); var buffer2 = new ByteBuffer(buffer.Bytes); var _byte2 = buffer2.ReadByte(); var _bytes2 = buffer2.ReadBytes(5); var _int16_2 = buffer2.ReadInt16(); var _int32_2 = buffer2.ReadInt32(); if (_byte != _byte2) { Assert.Fail(); } if (!_bytes.SequenceEqual(_bytes2)) { Assert.Fail(); } if (_int16 != _int16_2) { Assert.Fail(); } if (_int32 != _int32_2) { Assert.Fail(); } }
/// <summary> /// Deserialize an ip end point. /// </summary> public static IPEndPoint Deserialize(ByteBuffer reader) { // read the port var port = reader.ReadInt32(); // read the address type var addressFamily = (System.Net.Sockets.AddressFamily)reader.ReadByte(); byte[] addressBytes; // read the required bytes addressBytes = addressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 ? reader.ReadBytes(16) : reader.ReadBytes(4); // return the end point return(new IPEndPoint(new IPAddress(addressBytes), port)); }
public void Process(byte[] buffer, Connection connection) { var key = connection.AesKey.GetClientKey(); var iv = connection.AesKey.GetClientIv(); var aes = new AesCryptography() { CipherMode = System.Security.Cryptography.CipherMode.CBC, KeySize = AesKeySize.KeySize128, PaddingMode = System.Security.Cryptography.PaddingMode.PKCS7 }; var decrypted = aes.Decrypt(buffer, key, iv, out var sucess); if (sucess) { var msg = new ByteBuffer(decrypted); var fileName = msg.ReadString(); var fileLength = msg.ReadInt64(); var length = msg.ReadInt32(); var bytes = msg.ReadBytes(length); FileDownload handler; if (Authentication.FileDownloader.ContainsKey(connection.UniqueKey)) { handler = Authentication.FileDownloader[connection.UniqueKey]; if (handler.FileName != fileName) { handler.SetFileData(connection.UniqueKey, fileName, fileLength); handler.Close(); handler.Reset(); } } else { handler = new FileDownload(); handler.SetFileData(connection.UniqueKey, fileName, fileLength); Authentication.AddDownloadHandler(connection.UniqueKey, handler); } if (!handler.Completed) { if (!handler.IsOpen) { handler.Open(); } handler.Save(bytes); } } else { connection.Disconnect(); Global.WriteLog($"Failed to decrypt file packet.", "Black"); } }
public void ReadBytesTest() { ByteBuffer byteBuffer = new ByteBuffer(4); Assert.ThrowsException <IndexOutOfRangeException>(() => { byteBuffer.ReadBytes(5); }); byteBuffer.WriteInt(2147483647); byte[] buff = byteBuffer.ReadBytes(4); Assert.AreEqual(4, byteBuffer.ReaderIndex); Assert.AreEqual(127, buff[0]); Assert.AreEqual(255, buff[1]); Assert.AreEqual(255, buff[2]); Assert.AreEqual(255, buff[3]); Assert.AreEqual(4, buff.Length); }
public static void HandleData(byte[] data) { byte[] Buffer; Buffer = (byte[])data.Clone(); if (playerBuffer == null) { playerBuffer = new ByteBuffer(); } playerBuffer.WriteBytes(Buffer); if (playerBuffer.Count() == 0) { playerBuffer.Clear(); return; } if (playerBuffer.Length() >= 8) { pLength = playerBuffer.ReadLong(false); if (pLength <= 0) { playerBuffer.Clear(); return; } } if (playerBuffer.Length() >= 8) { pLength = playerBuffer.ReadLong(false); if (pLength <= 0) { playerBuffer.Clear(); return; } } while (pLength > 0 & pLength <= playerBuffer.Length() - 8) { if (pLength <= playerBuffer.Length() - 8) { playerBuffer.ReadLong(); data = playerBuffer.ReadBytes((int)pLength); HandleDataPackets(data); } pLength = 0; if (playerBuffer.Length() >= 8) { pLength = playerBuffer.ReadLong(false); if (pLength < 0) { playerBuffer.Clear(); return; } } } }
public void Receive(UserToken token, ByteBuffer msg) { if (msg.ReadableBytes() < 10) { return; } token.Send(msg.ReadBytes(10)); }
public void ShouldReadBytes() { byte[] messageBytes = Encoding.ASCII.GetBytes(messageInputString); byteBuffer.WriteBytes(messageBytes); byte[] byteRead = byteBuffer.ReadBytes(messageBytes.Length); Assert.Equal(messageBytes, byteRead); }