Thread overview
question about bitfields to decode websocket header
Nov 07
test
Nov 08
test
November 07
I am confused about the bitfields order.

mixin(bitfields!(
	bool, 	"fin",     	1,
	bool,   "rsv1", 	1,
	bool,   "rsv2", 	1,
	bool,   "rsv3", 	1,
	Opcode, "opcode", 	4,
		
	bool,   "mask", 	1,
	ubyte,  "_size", 	7,
));


output for first byte is 10000001 ,  the 2st byte 10000011

my code output:  opcode=8 mask=true size=65

the real size is 3 byte, and opcode is 1;

how to fix this ?

November 07
On Wednesday, 7 November 2018 at 13:05:49 UTC, test wrote:
> I am confused about the bitfields order.
>
> mixin(bitfields!(
> 	bool, 	"fin",     	1,
> 	bool,   "rsv1", 	1,
> 	bool,   "rsv2", 	1,
> 	bool,   "rsv3", 	1,
> 	Opcode, "opcode", 	4,
> 		
> 	bool,   "mask", 	1,
> 	ubyte,  "_size", 	7,
> ));
>
>
> output for first byte is 10000001 ,  the 2st byte 10000011
>
> my code output:  opcode=8 mask=true size=65
>
> the real size is 3 byte, and opcode is 1;
>
> how to fix this ?

The bitfields start with the least significant bits:
fin -> 1
rsv1 -> 0
rsv2 -> 0
rsv3 -> 0
opcode -> 1000 = 8

mask -> 1
_size -> 1000001 = 65

This order will likely be what you want:
mixin(bitfields!(
    opcode, "opcode", 4,
    bool,   "rsv3",   1,
    bool,   "rsv2",   1,
    bool,   "rsv1",   1,
    bool,   "fin",    1,

    ubyte,  "_size",  7,
    bool,   "mask",   1,
));

Also beware of endianness when mapping bytes to it.
November 08
On Wednesday, 7 November 2018 at 14:22:43 UTC, lithium iodate wrote:
> On Wednesday, 7 November 2018 at 13:05:49 UTC, test wrote:
>> I am confused about the bitfields order.
>>
> The bitfields start with the least significant bits:
> fin -> 1
> rsv1 -> 0
> rsv2 -> 0
> rsv3 -> 0
> opcode -> 1000 = 8
>
> mask -> 1
> _size -> 1000001 = 65
>
> This order will likely be what you want:
> mixin(bitfields!(
>     opcode, "opcode", 4,
>     bool,   "rsv3",   1,
>     bool,   "rsv2",   1,
>     bool,   "rsv1",   1,
>     bool,   "fin",    1,
>
>     ubyte,  "_size",  7,
>     bool,   "mask",   1,
> ));
>
> Also beware of endianness when mapping bytes to it.

After I use your code it working now.

my other question is: if the opcode bit cross byte, how do we define the bitfields ?

for example if the opcode is a 6 bit number instead 4bit :  F1|R1|R1|R1|opcode6|Mask1|Size5

I has to split the opcode here ?

mixin(bitfields!(
     opcode, "opcode4", 4,
     bool,   "rsv3",   1,
     bool,   "rsv2",   1,
     bool,   "rsv1",   1,
     bool,   "fin",    1,

     ubyte,  "_size",  5,
     bool,   "mask",   1,
     bool,   "opcode2",   1,
 ));