Manual decoding of size representation in CodedInputStream

So, I have a dotNet CoreCLR application for hololens, and I understand that standard sockets are not supported in CoreCLR, and it is recommended to use StreamSocket.

I also understand that CodedInputStreamthey StreamSocketare incompatible.

So, I manually tried to decode the size preload, but this is varInt32, which can be anything from 2-4 bytes.

This is as long as I received, but a variable size problem creates problems.

int bytenum = 0;
int num = 0;
while((bytes[bytenum] & 0x80) == 0x80){
    bytenum++;
}
while(bytenum >= 0)
{
    int tem = bytes[bytenum] & 0x7F;
    num = num << 7;
    num = num | tem;
    bytenum--;
}

Any suggestions on how I can fix this code or alternatives that I could use?

Edit: I also looked at CodedInputStream.cs SlowReadRawVarint32 () and tried this without success ... I Think DataReader is doing something non-standard.

var dr = new DataReader(socket.InputStream);

await dr.LoadAsync(1);

int result = -1;
int tmp = dr.ReadByte();

if (tmp >= 128)
{
    result = tmp & 0x7f;
    await dr.LoadAsync(1);
    if ((tmp = dr.ReadByte()) < 128)
    {
        result |= tmp << 7;
    }
    else
    {

        result |= (tmp & 0x7f) << 7;
        await dr.LoadAsync(1);
        if ((tmp = dr.ReadByte()) < 128)
        {
            result |= tmp << 14;
        }
        else
        {
            result |= (tmp & 0x7f) << 14;
            await dr.LoadAsync(1);
            if ((tmp = dr.ReadByte()) < 128)
            {
                result |= tmp << 21;
            }
            else
            {
                result |= (tmp & 0x7f) << 21;
                await dr.LoadAsync(1);
                result |= (tmp = dr.ReadByte()) << 28;
                if (tmp >= 128)
                {
                    // Discard upper 32 bits.
                    for (int i = 0; i < 5; i++)
                    {
                        await dr.LoadAsync(1);
                        if (dr.ReadByte() < 128)
                        {
                        }
                    }
                }
            }
        }
    }
}
else
{
    result = tmp;
}
+6
1

, (https://github.com/deephacks/westty/blob/master/westty-protobuf/src/main/java/org/deephacks/westty/protobuf/Varint32.java) ? ?

public int read() throws IOException {

byte tmp = bytes.get();
if (tmp >= 0) {
        return tmp;
    }
    int result = tmp & 0x7f;
    if ((tmp = bytes.get()) >= 0) {
        result |= tmp << 7;
    } else {
        result |= (tmp & 0x7f) << 7;
        if ((tmp = bytes.get()) >= 0) {
            result |= tmp << 14;
        } else {
            result |= (tmp & 0x7f) << 14;
            if ((tmp = bytes.get()) >= 0) {
                result |= tmp << 21;
            } else {
                result |= (tmp & 0x7f) << 21;
                result |= (tmp = bytes.get()) << 28;
                if (tmp < 0) {
                    // Discard upper 32 bits.
                    for (int i = 0; i < 5; i++) {
                        if (bytes.get() >= 0) {
                            return result;
                        }
                    }
                    throw new IllegalArgumentException();
                }
            }
        }
    }
    return result;
}

, , . 40 , , , . , , , !

+3

All Articles