merged trydisconnecting with other function

This commit is contained in:
arhamGIT committed 2023-01-12 09:45:59 -05:00
1 parent da41a23c0e
commit f3d1569cb3
1 file changed
+31 -31
@@ -49,7 +49,8 @@ namespace bts.websocket_core {
// class SocketsHandler {
// private WebApplication? app = null;
// private WebsocketServerParams? _server_params = null;
// public void start_listening(WebsocketServerParams server_params) {
// public void start_listening(WebsocketServerParams server_params) {
// // start listening, assert that init has been called already
// bts.Log.assert(_server_params == null);
// bts.Log.assert(app == null);
@@ -74,6 +75,7 @@ namespace bts.websocket_core {
// }
// }
/// <summary>
@@ -183,8 +185,34 @@ namespace bts.websocket_core {
/// <para>3. asks sync to handle disconnection</para>
/// </summary>
public void HandleDisconnection(WebsocketClientID id, string optional_message = "") {
TryDisconnect(id, optional_message);
var log = GetLogger($"WebsocketGateway.HandleDisconnection tried closing client_id: {id} with message: {optional_message}");
var socket = Connections.GetSocket(id);
var que = Connections.GetSocketQueue(id);
if (socket == null || que == null) {
log(" failed as could not find socket in dictionary");
Sync.handle_disconnection(id);
return;
}
if (!IsSocketOpen(socket)) {
if (socket.CloseStatus == WebSocketCloseStatus.NormalClosure) {
log(" already closed properly by other thread");
return;
}
log(" already closed ");
Sync.handle_disconnection(id);
Connections.RemoveConnection(id);
return;
}
que.Enqueue(Tuple.Create(MessageType.Close, ""));
log(" added to que");
return;
}
/// <summary>
/// tries to send request to client to disconnect
/// </summary>
/// <param name="id">id of client to whom the Disconnect message should be sent</param>
/// <param name="message"></param>
/// <returns>task that completes if client disconnects or if there is an error</returns>
/// <summary>
/// should be called after init
@@ -310,35 +338,7 @@ namespace bts.websocket_core {
} catch (OperationCanceledException) {//not sure about this read that this should just be ignored
}
}
/// <summary>
/// tries to send request to client to disconnect
/// </summary>
/// <param name="id">id of client to whom the Disconnect message should be sent</param>
/// <param name="message"></param>
/// <returns>task that completes if client disconnects or if there is an error</returns>
public void TryDisconnect(WebsocketClientID id, string message) {
var log = GetLogger($"WebsocketGateway.TryDisconnect tried closing client_id: {id} with message: {message}");
var socket = Connections.GetSocket(id);
var que = Connections.GetSocketQueue(id);
if (socket == null || que == null) {
log(" failed as could not find socket in dictionary");
Sync.handle_disconnection(id);
return;
}
if (!IsSocketOpen(socket)) {
if (socket.CloseStatus == WebSocketCloseStatus.NormalClosure) {
log(" already closed properly by other thread");
return;
}
log(" already closed ");
Sync.handle_disconnection(id);
Connections.RemoveConnection(id);
return;
}
que.Enqueue(Tuple.Create(MessageType.Close, ""));
log(" added to que");
return;
}
public async Task SendDisconnectRequestAsync(WebsocketClientID id, string message) {
var log = GetLogger($"WebsocketGateway.SendDisconnectRequestAsync tried closing client_id: {id} with message: {message}");
var sckt = Connections.GetSocket(id);