Esempio n. 1
0
        protected virtual void HandlePacket(Packets.Packet packet)
        {
            PacketReceivedEventArgs args = new PacketReceivedEventArgs(packet);

            OnPacketReceived?.BeginInvoke(this, args, PacketReceivedCallback, args);
        }
Esempio n. 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);
            }
        }