2

So basically here is how I do it with C++:

enum ServerOpcode : uint16_t{
    SERVER_AUTH_CONNECTION_RESPONSE                    = 0x001,
    SERVER_LOGIN_REQUEST_RESPONSE                      = 0x002,
    SERVER_NUM_MSG_TYPES                               = 0x003,
};

uint8_t* Vibranium::Packet::PreparePacket(ServerOpcode& serverOpcode, flatbuffers::FlatBufferBuilder& builder) {
    size_t size = builder.GetSize();
    uint8_t *buf = builder.GetBufferPointer();
    uint8_t *actualBuffer = new uint8_t[size + 2];
    actualBuffer[1] = (serverOpcode >> 8);
    actualBuffer[0] = (serverOpcode&0xFF);
    memcpy(actualBuffer + 2, buf, size);
    return actualBuffer;
}

I know that uint16_t is exactly 2 bytes and that is why i add +2.

Can someone give example in C# of how can I cast the ByteBuffer to byte[] and than prefix it with:

public enum ServerOpcode : ushort
{
    SERVER_AUTH_CONNECTION_RESPONSE                    = 0x001,
    SERVER_LOGIN_REQUEST_RESPONSE                      = 0x002,
    SERVER_NUM_MSG_TYPES                               = 0x003,

}

In C# I found out that the equivalent of uint16_t is ushort.

So my question is how can I convert ByteBuffer into byte[] and prefix it with ushort?

Can anyone make an answer showing and equivalent of PreparePacket in C# ?

P.S. Note that I am familiar with the file_identifier but I would like to do that manually. Hope someone can provide an example in C#

Venelin
  • 2,905
  • 7
  • 53
  • 117

1 Answers1

1

Following is the solution:

    public static Byte[] PrependServerOpcode(ByteBuffer byteBuffer, ServerOpcode code)
    {
        var originalArray = byteBuffer.ToArray(0, byteBuffer.Length);
        byte[] buffer = new byte[originalArray.Length + 2];
        buffer[0] = (byte)((ushort)code / 0x0100); 
        buffer[1] = (byte)code;
        Array.Copy(originalArray, 0, buffer, 2, originalArray.Length);
        return buffer;
    }

        public enum ServerOpcode : ushort
        {
            SERVER_AUTH_CONNECTION_RESPONSE = 0x001,
            SERVER_LOGIN_REQUEST_RESPONSE = 0x002,
            SERVER_NUM_MSG_TYPES = 0x003
        }

Or alternative:

public static ByteBuffer PrependServerOpcode(ByteBuffer byteBuffer, ServerOpcode code)
{
    var originalArray = byteBuffer.ToArray(0, byteBuffer.Length);
    byte[] buffer = new byte[originalArray.Length + 2];
    buffer[0] = (byte)((ushort)code / 0x0100);
    buffer[1] = (byte)code;
    Array.Copy(originalArray, 0, buffer, 2, originalArray.Length);
    return new ByteBuffer(buffer);
}

Usage:

    static void Main(string[] args)
    {
        var bb = new ByteBuffer(new byte[] { 0x01 });
        var result = PrependServerOpcode(bb, ServerOpcode.SERVER_NUM_MSG_TYPES);
    }
PK.
  • 588
  • 1
  • 4
  • 12
  • `ByteBuffer` is flatbuffers class. And I am not reading from stream. I am reading from flatbuffers's builder in the C++ example. – Venelin Nov 09 '20 at 20:59
  • @Venelin I edited my answer to utilize flatbuffers/ByteBuffer API – PK. Nov 10 '20 at 10:56
  • Thank you @PK however I think to the function `ToByteArray` instead of `Stream` you should pass the ByteBuffer or the builder as I am doing in C++. That way it will be answering the question. – Venelin Nov 10 '20 at 11:16
  • @Venelin There you go – PK. Nov 10 '20 at 11:19
  • amazing. Thank you sir. I accept the answer as it looks complete. Later on I'll test and let you know if there is any issue. – Venelin Nov 10 '20 at 11:23
  • A little update: buffer[0] = (byte)((ushort)code / 0x0100); That should be it. – PK. Nov 10 '20 at 11:29