Exemplo n.º 1
0
    public bool Parse()
    {
        if (this.isOK)
        {
            return(true);
        }

        bool finish = false;

        while (!finish)
        {
            switch (this.state)
            {
            case ParserState.PacketSize:
                if (this.buffer.Length < 2)
                {
                    finish = true;
                }
                else
                {
                    this.buffer.Read(this.packet.Bytes, 0, 2);
                    this.packetSize = BytesHelper.ToUInt16(this.packet.Bytes, 0);
                    if (packetSize > 60000)
                    {
                        throw new Exception($"packet too large, size: {this.packetSize}");
                    }
                    this.state = ParserState.PacketBody;
                }
                break;

            case ParserState.PacketBody:
                if (this.buffer.Length < this.packetSize)
                {
                    finish = true;
                }
                else
                {
                    this.buffer.Read(this.packet.Bytes, 0, this.packetSize);
                    this.packet.Length = this.packetSize;
                    this.isOK          = true;
                    this.state         = ParserState.PacketSize;
                    finish             = true;
                }
                break;
            }
        }
        return(this.isOK);
    }