Reputation: 257
I have a simple iocp server which need to send 1024 (or more) bytes to browser through websocket. Acorrding to this RFC about websocket:
+-+-+-+-+-------+-+-------------+-------------------------------+
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| | Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
i build websocket head this way:
char BufferToSend[1028]; //+4 bytes for websocket head
BufferToSend[0]=130; // 10000010
BufferToSend[1]=126; // 01111110 first bit 0 means unmask message
BufferToSend[2] = // 2 bytes for payload size
BufferToSend[3] = //
BufferToSend[4] = // payload starts
.....
// sending data to browser
WSASend(Socket, &wsabuf,1,&dwSendNumBytes..); //wsabuf refers to BufferToSend
The first byte (10000010) consist of first bit 'fin' set to 1, three reserved bits and the opcode '0010' for binary frames. I always set 'fin' bit to 1, which means it is final frame, because i assume that WSAsend send buffer to browser at once in one piece. But what if WSAsend needs to be call more then one time to send all buffer? If two Wsasend calls going to happen, in first call should i set fin to 0(part of message) and in second call to 1(final frame)?. Futhermore I build websocket head before calling WSasend(I don't know one or more times), how do i know when set fin bit to 1 or 0?
thanks in advance for help.
Upvotes: 2
Views: 534
Reputation: 2788
The FIN bit has nothing to do with TCP frames. It just indicates that the frame is the last web socket frame. Indeed, it is possible that the payload is sent in two or more TCP frames, but this has no effect on the web socket protocol.
Web socket implementations tend to have a max web socket frame size (which cannot be derived from the handshake). Whenever a frame is too big, you will receive an error code indicating that there is too much data. But in most cases, web socket implementations can handle any web socket frame size (depends on the available memory of course).
Upvotes: 2