Thank you Mr. Mozny,
Looking at the MQX http_ses_flush function I think I've found a possible bug! It is written in the function that it should return the number of bytes flushed. However it always returns zero!
Here is the original code:
uint32_t httpsrv_ses_flush(HTTPSRV_SESSION_STRUCT *session)
{
uint32_t length = 0;
while (length != session->buffer.offset)
{
uint32_t remaining;
length = send(session->sock, session->buffer.data, session->buffer.offset, 0);
if (length == RTCS_ERROR)
{
session->state = HTTPSRV_SES_CLOSE;
length = 0;
break;
}
remaining = session->buffer.offset - length;
if (remaining > 0)
{
_mem_zero(session->buffer.data, length);
memmove(session->buffer.data, session->buffer.data+length, remaining);
session->buffer.offset = remaining;
}
else
{
_mem_zero(session->buffer.data, session->buffer.offset);
session->buffer.offset = 0;
}
}
return(length);
}
Let's suppose we call the flush function when there is 9 bytes in the buffer, so session->buffer.offset == 9
The send call returns 9 on line 09., so length = 9. The if condition on line 11. does not hold as there is no error. As the remaining value is 9, the 'else' case is executed on the next condition, cleaning the buffer and putting session->buffer.offset to 0.
I suppose the function should return at this point, but the while condition is then verified again, and as the buffer.offset is set to 0 and length var holds 9, it executes the while loop again! The send call returns 0 (as session->buffer.offset is 0), length assumes 0 value and as a consequence the while condition holds and the function returns 0 (length). This is obviously hiding the quantity of data sent, and as it returns 0 regardless of what happened, I can't know when the send call "fails"! (the original reason of this question)
I've changed the code, putting a break; after line 29., and now I can know when the connection has dropped (the flush call returns 0). The new code is:
uint32_t httpsrv_ses_flush(HTTPSRV_SESSION_STRUCT *session)
{
uint32_t length = 0;
while (length != session->buffer.offset)
{
uint32_t remaining;
length = send(session->sock, session->buffer.data, session->buffer.offset, 0);
if (length == RTCS_ERROR)
{
session->state = HTTPSRV_SES_CLOSE;
length = 0;
break;
}
remaining = session->buffer.offset - length;
if (remaining > 0)
{
_mem_zero(session->buffer.data, length);
memmove(session->buffer.data, session->buffer.data+length, remaining);
session->buffer.offset = remaining;
}
else
{
_mem_zero(session->buffer.data, session->buffer.offset);
session->buffer.offset = 0;
break; /* I've inserted this line */
}
}
return(length);
}
About websockets: when is the next MQX release up to? Is there any way to get the websockets code earlier? Maybe it is even more adequate to my application than HTML5 SSE!
Thank you very much for your response!
Luiz Fernando