Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › http › Server › WebSocket › readSmallMessage Public Function readSmallMessage Doc Source 706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753 pub fn readSmallMessage (ws : *WebSocket ) ReadSmallTextMessageError !SmallMessage {
const in = ws .input ;
while (true ) {
const header = try in .takeArray (2 );
const h0 : Header0 = @bitCast (header [0 ]);
const h1 : Header1 = @bitCast (header [1 ]);
switch (h0 .opcode ) {
.text , .binary , .pong , .ping => {},
.connection_close => return error .ConnectionClose ,
.continuation => return error .UnexpectedOpCode ,
_ => return error .UnexpectedOpCode ,
}
if (!h0 .fin ) return error .MessageOversize ;
if (!h1 .mask ) return error .MissingMaskBit ;
const len : usize = switch (h1 .payload_len ) {
.len16 => try in .takeInt (u16 , .big ),
.len64 => std .math .cast (usize , try in .takeInt (u64 , .big )) orelse return error .MessageOversize ,
else => @backingInt (h1 .payload_len ),
};
if (len > in .buffer .len ) return error .MessageOversize ;
const mask : [4 ]u8 = (try in .takeArray (4 )).*;
const payload = try in .take (len );
// Skip pongs.
if (h0 .opcode == .pong ) continue ;
// The last item may contain a partial word of unused data.
const floored_len = (payload .len / 4 ) * 4 ;
const payload_chunks : [][4 ]u8 = @ptrCast (payload [0 ..floored_len ]);
for (payload_chunks ) |*chunk | {
const mask_i : u32 = @bitCast (mask );
const chunk_i : u32 = @bitCast (chunk .*);
chunk .* = @bitCast (chunk_i ^ mask_i );
}
for (payload [floored_len ..], mask [0 .. payload .len - floored_len ]) |*leftover , m | {
leftover .* ^= m ;
}
return .{
.opcode = h0 .opcode ,
.data = payload ,
};
}
}
Reads the next message from the WebSocket stream, failing if the message does not fit into the input buffer. The returned memory points into the input buffer and is invalidated on the next read.