question about bitfields to decode websocket header

test test at gmail.com
Thu Nov 8 01:45:35 UTC 2018


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,
  ));



More information about the Digitalmars-d-learn mailing list