示例#1
0
        public virtual void Add(APacket packet)
        {
            Memory <byte> packetMem = new Memory <byte>(new byte[packet.Length + _size]);
            int           length    = packet.Length;

            for (int i = 0; i < _size; ++i)
            {
                packetMem.Span[packet.Length + _size - i - 1] = (byte)(length & 255);
                length >>= 8;
            }
            if (length != 0)
            {
                throw new Exception($"{nameof(Add)}: packet was too big to add!");
            }
            packet.Read().CopyTo(packetMem);
            Write(packetMem);
        }
示例#2
0
        public void ReadPacket(Client client, byte[] buffer, int lengthRead)
        {
            try
            {
                ByteBuffer byteBuffer = new ByteBuffer();

                byteBuffer.Write(buffer, 0, lengthRead);
                byteBuffer.Position = 0;

                while (byteBuffer.Position < lengthRead)
                {
                    int len = byteBuffer.ReadVarInt();
                    int id  = byteBuffer.ReadVarInt();

                    object tmp = client.State.Peek()._ReceiptPackets[id];
                    if (tmp != null)
                    {
                        APacket packet = tmp as APacket;
                        packet = packet.Clone() as APacket;
                        if (packet == null)
                        {
                            throw new NullReferenceException("The server does not handle packet " + id);
                        }

                        packet.Read(client, byteBuffer);
                    }
                    else
                    {
                        Console.WriteLine("Unhandled packet - ID : " + id + " - Length : " + len);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR : " + e.Message);
            }
        }