-3

Hey there I have that piece of code that doesnt work and keeps crashing my emulator..

public byte[] SubArray(byte[] Data, int Offset, int Len)
   {
        byte[] Result = BitConverter.GetBytes(Len);
        Buffer.BlockCopy(Data, Offset, Result, 0, Len);
        return Result;
    }

A first chance exception of type 'System.ArgumentException' occurred in LoginServer.exe

An unhandled exception of type 'System.ArgumentException' occurred in LoginServer.exe

Additional information: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

Im not really experienced in C# but I tried many things and none worked and I did my best in searching but it never worked since I was not able to read different codes than this.

ps: my byte results was

byte[] Result = new byte[Len];

I thought its a problem so I changed it to

byte[] Result = BitConverter.GetBytes(Len);

but now the main problem is from BlockCopy.

EDIT: I went more detailed in the codes and I found the main error.

 public Packet(byte[] Buffer, bool FromServer, out int Length)
    {
        using (Stream stream = new MemoryStream(Buffer))
        using (BinaryReader Reader = new BinaryReader(stream))
        {
            byte SecBytesLen = Reader.ReadByte();
            ushort DataLen = Reader.ReadUInt16();
            Length = SecBytesLen + DataLen + 2;
            Opcode = (ushort)(Reader.ReadUInt16() >> 1);
            Data = SubArray(Buffer, 14, DataLen - 14);
        }
    }

I tried to remove that line and it semi worked without crashing.

Data = SubArray(Buffer, 14, DataLen - 14);

If you need any more details on any variable in that code please tell me...

Just found those too before that.

public ushort Opcode = 0;
    public byte[] Data = new byte[0];

    public int ReaderPosition = 0;

    public Packet(ushort Opcode)
    {
        this.Opcode = Opcode;
    }

1 Answers1

0

Len is an integer and you are getting the bytes which compose that integer, thus those are 4 bytes.

If what you really want is to get a part of the array (I deduct it from the name of the function) then you should do:

public byte[] SubArray(byte[] Data, int Offset, int Len)
{
    byte[] Result = new byte[Len];
    Buffer.BlockCopy(Data, Offset, Result, 0, Len);
    return Result;
}
Gusman
  • 14,905
  • 2
  • 34
  • 50