Beispiel #1
0
        public MinecraftPacket(MinecraftClient client, Stream stream)
        {
            Client = client;
            Data   = new MinecraftStream((RecyclableMemoryStream)RMSManager.Get().GetStream(GetType().FullName));

            Length = Client.CompressionEnabled ? (int)stream.Length : new VarInt(stream).Value;

            long _pos = stream.Position;

            Id = new VarInt(stream).Value;

            byte[] data = new byte[Length - (stream.Position - _pos)];
            stream.Read(data, 0, data.Length);

            Data.Write(data);
            Data.Seek(0, SeekOrigin.Begin);
        }
Beispiel #2
0
        private bool TryReadPacket(MinecraftStream stream, out int lastPacketId)
        {
            Packets.Packet packet = null;
            int            packetId;

            byte[] packetData;

            if (!CompressionEnabled)
            {
                int length = stream.ReadVarInt();

                int packetIdLength;
                packetId     = stream.ReadVarInt(out packetIdLength);
                lastPacketId = packetId;

                if (length - packetIdLength > 0)
                {
                    packetData = stream.Read(length - packetIdLength);
                }
                else
                {
                    packetData = new byte[0];
                }
            }
            else
            {
                int packetLength = stream.ReadVarInt();

                int br;
                int dataLength = stream.ReadVarInt(out br);

                int readMore;

                if (dataLength == 0)
                {
                    packetId     = stream.ReadVarInt(out readMore);
                    lastPacketId = packetId;
                    packetData   = stream.Read(packetLength - (br + readMore));
                }
                else
                {
                    var data = stream.ReadToSpan(packetLength - br);

                    using (MinecraftStream a = new MinecraftStream())
                    {
                        using (ZlibStream outZStream = new ZlibStream(
                                   a, CompressionMode.Decompress, CompressionLevel.Default, true))
                        {
                            outZStream.Write(data);
                            //  outZStream.Write(data, 0, data.Length);
                        }

                        a.Seek(0, SeekOrigin.Begin);

                        int l;
                        packetId     = a.ReadVarInt(out l);
                        lastPacketId = packetId;
                        packetData   = a.Read(dataLength - l);
                    }
                }
            }

            packet = MCPacketFactory.GetPacket(PacketDirection, ConnectionState, packetId);

            Interlocked.Increment(ref PacketsIn);
            Interlocked.Add(ref PacketSizeIn, packetData.Length);

            if (packet == null)
            {
                if (UnhandledPacketsFilter[ConnectionState].TryAdd(packetId, 1))
                {
                    Log.Debug(
                        $"Unhandled packet in {ConnectionState}! 0x{packetId.ToString("x2")} = {(ConnectionState == ConnectionState.Play ? MCPacketFactory.GetPlayPacketName(packetId) : "Unknown")}");
                }
                else
                {
                    UnhandledPacketsFilter[ConnectionState][packetId] =
                        UnhandledPacketsFilter[ConnectionState][packetId] + 1;
                }

                return(false);
            }

            //    Log.Info($"Received: {packet}");

            if (ConnectionState == ConnectionState.Play)
            {
                if (ShouldAddToProcessing(packet))
                {
                    // Interlocked.Increment(ref _queued);

                    // ThreadPool.QueueUserWorkItem(
                    //	    () =>
                    //	    {
                    ProcessPacket(packet, packetData);
                    //   Interlocked.Decrement(ref _queued);
                    //	    });

                    return(true);
                }
            }
            else
            {
                ProcessPacket(packet, packetData);

                return(true);
            }

            return(false);
        }
Beispiel #3
0
        private void ProcessNetwork()
        {
            int lastPacketId = 0;

            try
            {
                using (NetworkStream ns = new NetworkStream(Socket))
                {
                    using (MinecraftStream ms = new MinecraftStream(ns))
                    {
                        _readerStream = ms;

                        while (!CancellationToken.IsCancellationRequested)
                        {
                            Packets.Packet packet = null;
                            int            packetId;
                            byte[]         packetData;

                            if (!CompressionEnabled)
                            {
                                int length = ms.ReadVarInt();

                                int packetIdLength;
                                packetId     = ms.ReadVarInt(out packetIdLength);
                                lastPacketId = packetId;
                                if (length - packetIdLength > 0)
                                {
                                    packetData = ms.Read(length - packetIdLength);
                                }
                                else
                                {
                                    packetData = new byte[0];
                                }
                            }
                            else
                            {
                                int packetLength = ms.ReadVarInt();

                                int br;
                                int dataLength = ms.ReadVarInt(out br);

                                int readMore;
                                if (dataLength == 0)
                                {
                                    packetId     = ms.ReadVarInt(out readMore);
                                    lastPacketId = packetId;
                                    packetData   = ms.Read(packetLength - (br + readMore));
                                }
                                else
                                {
                                    byte[] data = ms.Read(packetLength - br);
                                    using (MinecraftStream a = new MinecraftStream())
                                    {
                                        using (ZlibStream outZStream = new ZlibStream(a,
                                                                                      CompressionMode.Decompress, CompressionLevel.Default, true))
                                        {
                                            outZStream.Write(data, 0, data.Length);
                                        }

                                        a.Seek(0, SeekOrigin.Begin);

                                        int l;
                                        packetId     = a.ReadVarInt(out l);
                                        lastPacketId = packetId;
                                        packetData   = a.Read(dataLength - l);
                                    }
                                }
                            }

                            packet = MCPacketFactory.GetPacket(Direction, ConnectionState, packetId);
                            if (packet == null)
                            {
                                if (UnhandledPacketsFilter[ConnectionState]
                                    .TryAdd(packetId, 1))
                                {
                                    Log.Debug(
                                        $"Unhandled packet in {ConnectionState}! 0x{packetId.ToString("x2")} = {(ConnectionState == ConnectionState.Play ? MCPacketFactory.GetPlayPacketName(packetId) : "Unknown")}");
                                }
                                else
                                {
                                    UnhandledPacketsFilter[ConnectionState][packetId] =
                                        UnhandledPacketsFilter[ConnectionState][packetId] + 1;
                                }

                                continue;
                            }

                            if (ConnectionState == ConnectionState.Play)
                            {
                                if (ShouldAddToProcessing(packet))
                                {
                                    Interlocked.Increment(ref _queued);
                                    ThreadPool.QueueUserWorkItem(() =>
                                    {
                                        ProcessPacket(packet, packetData);
                                        Interlocked.Decrement(ref _queued);
                                    });
                                }
                            }
                            else
                            {
                                ProcessPacket(packet, packetData);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is OperationCanceledException)
                {
                    return;
                }
                if (ex is EndOfStreamException)
                {
                    return;
                }
                if (ex is IOException)
                {
                    return;
                }

                if (LogExceptions)
                {
                    Log.Warn(
                        $"Failed to process network (Last packet: 0x{lastPacketId:X2} State: {ConnectionState}): " +
                        ex);
                }
            }
            finally
            {
                Disconnected(false);
            }
        }