Re: how to close secure websocket properly
Posted: Tue Mar 16, 2021 2:26 pm
Thanks Jon, I set m_fd_tcp to public and rebuild system files.
My thought to close ssl_fd first is incorrect. close the ssl_fd before close ws_fd in the same task doesn't solve the problem in 2.9.5, but work fine in 2.9.3
Interestingly, if I close the ssl_fd in a different task, application work completely fine. (false in 2.9.5, true in 2.9.3)
One thing I notice by using your code, whenever the return ws is null, I am able to close the ws_fd. If the ws is not null, and it's a secure ws, it's always fail to close the ws.
task to monitor ws fd
2.9.5 debug output
My thought to close ssl_fd first is incorrect. close the ssl_fd before close ws_fd in the same task doesn't solve the problem in 2.9.5, but work fine in 2.9.3
Interestingly, if I close the ssl_fd in a different task, application work completely fine. (false in 2.9.5, true in 2.9.3)
One thing I notice by using your code, whenever the return ws is null, I am able to close the ws_fd. If the ws is not null, and it's a secure ws, it's always fail to close the ws.
task to monitor ws fd
Code: Select all
void wsTask(void *pd)
{
char msg[ETHER_BUFFER_SIZE];
fd_set read_fds;
fd_set error_fds;
FD_ZERO( &read_fds );
FD_ZERO( &error_fds );
printf("------------wsTask running----------------\r\n");
while(true)
{
OSTimeDly(TICKS_PER_SECOND/8);
for(int i =0; i < ws_vector.size(); i++)
{
FD_SET(ws_vector[i], &read_fds);
FD_SET(ws_vector[i], &error_fds);
select(1, &read_fds, NULL, &error_fds, 0);
if (FD_ISSET(ws_vector[i], &error_fds))
{
printf("webpage is closed by user, removed ws = %d from ws_vector\r\n", ws_vector[i]);
int sslFd = 0;
NB::WebSocket* ws = NB::WebSocket::GetWebSocketRecord( ws_vector[i] );
if( ws != nullptr )
{
sslFd = ws->m_fd_tcp;
printf("ws is not null\r\n");
// if(IsSSLfd(sslFd))
// {
// printf("sslFd = %d\r\n", sslFd);
// close(sslFd);
// printf("close sslFd succeed\r\n");
// }
}
else
printf("ws is null\r\n");
close(ws_vector[i]);
printf("close ws= %d\r\n\r\n", ws_vector[i]);
ws_vector[i] = -1;
ws_vector.erase(ws_vector.begin() + i);
i--;
FD_ZERO( &read_fds );
FD_ZERO( &error_fds );
continue;
}
...
}
}
}
added ws=38 to ws_vector, associate sock = 25, sock is not ssl fd //non secure webpage open
webpage is closed by user, removed ws = 38 from ws_vector
ws is not null
close ws= 38
added ws=39 to ws_vector, associate sock = 38, sock is ssl fd //secure webpage open
webpage is closed by user, removed ws = 39 from ws_vector
ws is null
close ws= 39
added ws=39 to ws_vector, associate sock = 38, sock is ssl fd //secure webpage open
webpage is closed by user, removed ws = 39 from ws_vector
ws is null
close ws= 39
added ws=39 to ws_vector, associate sock = 38, sock is ssl fd //secure webpage open
webpage is closed by user, removed ws = 39 from ws_vector
ws is not null
//fail to close. no print out