public bool HandlePacket(PacketIncomingType packetType, List <byte> packetData) { int chunkX = dataTypes.ReadNextInt(packetData); int chunkZ = dataTypes.ReadNextInt(packetData); bool chunksContinuous = dataTypes.ReadNextBool(packetData); ushort chunkMask = protocolversion >= (int)McVersion.V19 ? (ushort)dataTypes.ReadNextVarInt(packetData) : dataTypes.ReadNextUShort(packetData); if (protocolversion < (int)McVersion.V18) { ushort addBitmap = dataTypes.ReadNextUShort(packetData); int compressedDataSize = dataTypes.ReadNextInt(packetData); byte[] compressed = dataTypes.ReadData(compressedDataSize, packetData); byte[] decompressed = ZlibUtils.Decompress(compressed); pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, addBitmap, worldInfo.dimension == 0, chunksContinuous, worldInfo.dimension, new List <byte>(decompressed)); } else { if (protocolversion >= (int)McVersion.V114) { dataTypes.ReadNextNbt(packetData); // Heightmaps - 1.14 and above } int dataSize = dataTypes.ReadNextVarInt(packetData); pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, 0, false, chunksContinuous, worldInfo.dimension, packetData); } return(true); }
public void SendPacket(IPacket packet) { PacketBuffer sendBuffer = new PacketBuffer(); packet.Send(sendBuffer); byte[] packetIdVI = ByteUtils.ToVarInt(packet.GetId()); byte[] packetData = ByteUtils.Concat(packetIdVI, sendBuffer.ToArray()); if (compressionThreshold > 0) { if (packetData.Length > compressionThreshold) { byte[] uncompressed_length = ByteUtils.ToVarInt(packetData.Length); byte[] compressed_packet = ZlibUtils.Compress(packetData); byte[] compressed_packet_length = ByteUtils.ToVarInt(compressed_packet.Length); packetData = ByteUtils.Concat(compressed_packet_length, compressed_packet); } else { byte[] uncompressed_length = ByteUtils.ToVarInt(0); packetData = ByteUtils.Concat(uncompressed_length, packetData); } } byte[] lengthVI = ByteUtils.ToVarInt(packetData.Length); var l = ByteUtils.Concat(lengthVI, packetData); Console.WriteLine("Sent " + l.Length + " bytes"); SendBytes(l); }
public void SendPacket(MemoryStream data, int id, bool connectionNeed = true) { if (connectionNeed && !_connected) { return; } if (_threshold > 0) { MemoryStream newData = new MemoryStream(); newData.WriteVarInt(id); newData.Write(data); int uncompressedSize = (int)newData.Length; if (uncompressedSize >= _threshold) { newData = ZlibUtils.Compress(newData, Ionic.Zlib.CompressionLevel.BestSpeed); } else { uncompressedSize = 0; } int size = (int)newData.Length + VarIntSize(uncompressedSize); data.SetLength(0); data.WriteVarInt(size); data.WriteVarInt(uncompressedSize); data.Write(newData); newData.Dispose(); _socket.Send(data.ToArray()); data.Dispose(); } else { SendUncompressPacket(data, id); } }
/// <summary> /// 生成发送给服务端的包 /// </summary> /// <param name="compress">数据包压缩的阚值</param> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <exception cref="IndexOutOfRangeException"></exception> public virtual byte[] ToBytes(int compress = -1) { if (Empty) { throw new PacketException("Packet is empty"); } byte[] PacketData; if (compress > 0) { /* * 压缩的数据包: * * 名称 类型 备注 * 数据包长度 Varint 等于下方两者解压前的总字节数 * 解压后长度 Varint 若为0则表示本包未被压缩 * 被压缩的数据 经Zlib压缩.开头是一个VarInt字段,代表数据包ID,然后是数据包数据. */ if (Data.Count >= compress) { //拼接PacketID(VarInt)和PacketData(ByteArray)并塞入ZlibUtils.Compress去压缩 byte[] uncompressed = new byte[Length]; Data.CopyTo(uncompressed, VarInt.WriteTo(ID, uncompressed)); byte[] compressed = ZlibUtils.Compress(uncompressed); PacketData = new byte[VarInt.GetLength(VarInt.GetLength(uncompressed.Length) + compressed.Length) + VarInt.GetLength(uncompressed.Length) + compressed.Length]; //写入第一个VarInt(解压长度+压缩后的长度) int offset = VarInt.WriteTo(VarInt.GetLength(uncompressed.Length) + compressed.Length, PacketData); //写入第二个VarInt(未压缩长度) offset += VarInt.WriteTo(uncompressed.Length, PacketData.AsSpan().Slice(offset)); //写入被压缩的数据 compressed.CopyTo(PacketData, offset); } else { PacketData = new byte[VarInt.GetLength(Length + 1) + Length + 1]; int offset = VarInt.WriteTo(Length + 1, PacketData); PacketData[offset++] = 0; offset += VarInt.WriteTo(ID, PacketData.AsSpan().Slice(offset)); if (Data.Count > 0) { Data.CopyTo(PacketData, offset); } } } else { PacketData = new byte[VarInt.GetLength(Length) + Length]; int offset = VarInt.WriteTo(Length, PacketData); offset += VarInt.WriteTo(ID, PacketData.AsSpan().Slice(offset)); if (Data.Count > 0) { Data.CopyTo(PacketData, offset); } } return(PacketData); }
private PacketBuffer ReadNextPacket(ref int packetID) { int size = ReadNextVarIntRAW(); PacketBuffer buffer = new PacketBuffer(ReadDataRAW(size)); if (manager.compressionThreshold > 0) { int sizeUncompressed = buffer.ReadVarInt(); if (sizeUncompressed != 0) // != 0 means compressed, let's decompress { buffer = new PacketBuffer(ZlibUtils.Decompress(buffer.ReadToEnd(), sizeUncompressed)); } } packetID = buffer.ReadVarInt(); return(buffer); }
public bool HandlePacket(PacketIncomingType packetType, List <byte> packetData) { int chunkCount; bool hasSkyLight; List <byte> chunkData = packetData; //Read global fields if (protocolversion < (int)McVersion.V18) { chunkCount = dataTypes.ReadNextShort(packetData); int compressedDataSize = dataTypes.ReadNextInt(packetData); hasSkyLight = dataTypes.ReadNextBool(packetData); byte[] compressed = dataTypes.ReadData(compressedDataSize, packetData); byte[] decompressed = ZlibUtils.Decompress(compressed); chunkData = new List <byte>(decompressed); } else { hasSkyLight = dataTypes.ReadNextBool(packetData); chunkCount = dataTypes.ReadNextVarInt(packetData); } //Read chunk records int[] chunkXs = new int[chunkCount]; int[] chunkZs = new int[chunkCount]; ushort[] chunkMasks = new ushort[chunkCount]; ushort[] addBitmaps = new ushort[chunkCount]; for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++) { chunkXs[chunkColumnNo] = dataTypes.ReadNextInt(packetData); chunkZs[chunkColumnNo] = dataTypes.ReadNextInt(packetData); chunkMasks[chunkColumnNo] = dataTypes.ReadNextUShort(packetData); addBitmaps[chunkColumnNo] = protocolversion < (int)McVersion.V18 ? dataTypes.ReadNextUShort(packetData) : (ushort)0; } //Process chunk records for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++) { pTerrain.ProcessChunkColumnData(chunkXs[chunkColumnNo], chunkZs[chunkColumnNo], chunkMasks[chunkColumnNo], addBitmaps[chunkColumnNo], hasSkyLight, true, worldInfo.dimension, chunkData); } return(true); }
public static Packet ReceivePacket(Socket tcp, int compressionThreshold) { Packet recPacket = new Packet(); int PacketLength = VarInt.Read(tcp); recPacket.WriteBytes(ReceiveData(0, PacketLength, tcp)); if (compressionThreshold > 0) { int DataLength = ReadVarInt(recPacket.Data); if (DataLength != 0) //如果是0的话就代表这个数据包没有被压缩 { byte[] uncompressed = ZlibUtils.Decompress(recPacket.Data.ToArray(), DataLength); recPacket.Data.Clear(); recPacket.Data.AddRange(uncompressed); } } recPacket.ID = ReadVarInt(recPacket.Data); return(recPacket); }
public void WritePacket(int id, IEnumerable <byte> data) { byte[] the_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(id), data.ToArray()); if (connectionInfo.compressionThreshold > 0) //Compression enabled? { if (the_packet.Length >= connectionInfo.compressionThreshold) //Packet long enough for compressing? { byte[] compressed_packet = ZlibUtils.Compress(the_packet); the_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(the_packet.Length), compressed_packet); } else { byte[] uncompressed_length = dataTypes.GetVarInt(0); //Not compressed (short packet) the_packet = dataTypes.ConcatBytes(uncompressed_length, the_packet); } } connectionInfo.socketWrapper.SendDataRAW(dataTypes.ConcatBytes(dataTypes.GetVarInt(the_packet.Length), the_packet)); }
/// <summary> /// 生成发送给服务端的包 /// </summary> /// <param name="compress">数据包压缩的阚值</param> /// <exception cref="ArgumentOutOfRangeException"></exception> public virtual List <byte> ToList(int compress = -1) { if (Empty) { throw new PacketException("Packet is empty"); } List <byte> PacketData = new List <byte>(); PacketData.Capacity = Length; if (compress > 0) { if (Data.Count >= compress) { PacketData.AddRange(VarInt.GetBytes(Length)); int IdLength = VarInt.GetLength(ID); byte[] buffer = new byte[IdLength + Data.Count]; Array.Copy(VarInt.GetBytes(ID), 0, buffer, 0, IdLength); Data.CopyTo(buffer, IdLength); PacketData.AddRange(ZlibUtils.Compress(buffer)); PacketData.InsertRange(0, VarInt.GetBytes(PacketData.Count)); } else { PacketData.AddRange(VarInt.GetBytes(Length + 1)); PacketData.Add(0); PacketData.AddRange(VarInt.GetBytes(ID)); PacketData.AddRange(Data); } } else { PacketData.AddRange(VarInt.GetBytes(Length)); PacketData.AddRange(VarInt.GetBytes(ID)); PacketData.AddRange(Data); } return(PacketData); }
public Packet ReadNext() { Packet packet = new Packet(); int size = dataTypes.ReadNextVarIntRAW(connectionInfo.socketWrapper); packet.data.AddRange(connectionInfo.socketWrapper.ReadDataRAW(size)); //Handle packet decompression if (protocolVersion >= (int)McVersion.V18 && connectionInfo.compressionThreshold > 0) { int sizeUncompressed = dataTypes.ReadNextVarInt(packet.data); if (sizeUncompressed != 0) // != 0 means compressed, let's decompress { byte[] toDecompress = packet.data.ToArray(); byte[] uncompressed = ZlibUtils.Decompress(toDecompress, sizeUncompressed); packet.data.Clear(); packet.data.AddRange(uncompressed); } } packet.id = dataTypes.ReadNextVarInt(packet.data); return(packet); }
public IEnumerator ReadPacket(MemoryStream packet) { if (_threshold > 0) { int dataLength = packet.ReadVarInt(); if (dataLength != 0) { if (dataLength >= PARTIAL_DECOMPRESS_MIN_SIZE) { MemoryStream outMs = new MemoryStream(); byte[] buffer = new byte[PARTIAL_DECOMPRESS_BUFFER_SIZE]; var zOut = new ZlibStream(packet, CompressionMode.Decompress); int len; while ((len = zOut.Read(buffer, 0, buffer.Length)) > 0) { outMs.Write(buffer, 0, len); yield return(new PriorityYieldInstruction(PriorityYieldInstruction.Priority.High)); } zOut.Close(); outMs.Position = 0; packet.Dispose(); packet = outMs; } else { byte[] decompressed = ZlibUtils.Decompress(packet, dataLength); packet.Dispose(); packet = new MemoryStream(decompressed); } } } ReadUncompressPacket(packet); }
public override byte[] ToBytes(int compress = -1) { byte[] channel = Encoding.UTF8.GetBytes(Channel); byte[] PacketData; //14w31a: The length prefix on the payload of Plugin Message has been removed (Both ways) if (_protocolVersion <= ProtocolVersionNumbers.V14w31a) { if (HasForge) { PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(channel.Length), channel, VarShort.GetBytes(Data.Count), Data.ToArray()); } else { PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(channel.Length), channel, ProtocolHandler.GetBytes((short)Data.Count), Data.ToArray()); } } else { PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(channel.Length), channel, Data.ToArray()); } int DataLength = PacketData.Length; PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(ID), PacketData); if (compress > 0) { if (DataLength >= compress) { PacketData = ProtocolHandler.ConcatBytes(VarInt.GetBytes(PacketData.Length), ZlibUtils.Compress(PacketData)); } else { PacketData = ProtocolHandler.ConcatBytes(new byte[] { 0 }, PacketData); } } return(ProtocolHandler.ConcatBytes(VarInt.GetBytes(PacketData.Length), PacketData)); }