コード例 #1
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 = new byte[length - packetIdLength];
                                     * int read = 0;
                                     * while (read < packetData.Length)
                                     * {
                                     *      read += ms.Read(packetData, read, packetData.Length - read);
                                     *
                                     *      if (CancellationToken.IsCancellationRequested) throw new OperationCanceledException();
                                     * }*/
                                    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);
                                    byte[] decompressed;
                                    DecompressData(data, out decompressed);

                                    using (MemoryStream b = new MemoryStream(decompressed))
                                    {
                                        using (MinecraftStream a = new MinecraftStream(b))
                                        {
                                            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)
                            {
                                HandlePacketQueue.Add(new TemporaryPacketData(packet, packetData));
                            }
                            else
                            {
                                packet.Decode(new MinecraftStream(new MemoryStream(packetData)));
                                HandlePacket(packet);
                            }
                        }
                    }
                }
            }
            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);
            }
        }
コード例 #2
0
        private void ProcessNetwork()
        {
            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);

                                if (length - packetIdLength > 0)
                                {
                                    /*packetData = new byte[length - packetIdLength];
                                     * int read = 0;
                                     * while (read < packetData.Length)
                                     * {
                                     *      read += ms.Read(packetData, read, packetData.Length - read);
                                     *
                                     *      if (CancellationToken.IsCancellationRequested) throw new OperationCanceledException();
                                     * }*/
                                    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);
                                    packetData = ms.Read(packetLength - (br + readMore));
                                }
                                else
                                {
                                    byte[] data = ms.Read(packetLength - br);
                                    byte[] decompressed;
                                    DecompressData(data, out decompressed);

                                    using (MemoryStream b = new MemoryStream(decompressed))
                                    {
                                        using (MinecraftStream a = new MinecraftStream(b))
                                        {
                                            int l;
                                            packetId   = a.ReadVarInt(out l);
                                            packetData = a.Read(dataLength - l);
                                        }
                                    }
                                }
                            }

                            packet = MCPacketFactory.GetPacket(ConnectionState, packetId);
                            if (packet == null)
                            {
                                Log.Warn($"Unhandled package! 0x{packetId.ToString("x2")}");
                                continue;
                            }
                            packet.Decode(new MinecraftStream(new MemoryStream(packetData)));
                            HandlePacket(packet);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Warn("OH NO", ex);
                if (ex is OperationCanceledException)
                {
                    return;
                }
                if (ex is EndOfStreamException)
                {
                    return;
                }
                if (ex is IOException)
                {
                    return;
                }

                Log.Error("An unhandled exception occured while processing network!", ex);
            }
            finally
            {
                Disconnected(false);
            }
        }
コード例 #3
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);
        }
コード例 #4
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);
            }
        }