add a way to run in blocking more, which is useful for server mode to have N*thread instead of 2N*thread for N connections

This commit is contained in:
Benjamin Sergeant
2019-01-01 21:25:15 -08:00
parent 946d7015a2
commit 1bc5bc7f1c
5 changed files with 54 additions and 18 deletions

View File

@ -84,13 +84,16 @@ namespace ix
void WebSocket::stop()
{
bool automaticReconnection = _automaticReconnection;
// This value needs to be forced when shutting down, it is restored later
_automaticReconnection = false;
close();
if (!_thread.joinable())
{
_automaticReconnection = true;
_automaticReconnection = automaticReconnection;
return;
}
@ -98,7 +101,7 @@ namespace ix
_thread.join();
_stop = false;
_automaticReconnection = true;
_automaticReconnection = automaticReconnection;
}
WebSocketInitResult WebSocket::connect()
@ -241,6 +244,11 @@ namespace ix
WebSocket::invokeTrafficTrackerCallback(msg.size(), true);
});
// 4. In blocking mode, getting out of this function is triggered by
// an explicit disconnection from the callback, or by the remote end
// closing the connection, ie isConnected() == false.
if (!_thread.joinable() && !isConnected() && !_automaticReconnection) return;
}
}
@ -332,4 +340,14 @@ namespace ix
case WebSocket_ReadyState_Closed: return "CLOSED";
}
}
void WebSocket::enableAutomaticReconnection()
{
_automaticReconnection = true;
}
void WebSocket::disableAutomaticReconnection()
{
_automaticReconnection = false;
}
}