refactor: Use events to avoid circular deps for ws sync/gateway
This commit is contained in:
1 parent
fd59682b28
commit
82128be9ca
4 files changed
+15
-26
No files matched your search
@@ -43,7 +43,6 @@
|
||||
Port = "8080",
|
||||
KeepAliveIntervalSecs = 0.1,
|
||||
};
|
||||
gwy.init(sync);
|
||||
await sync.initAsync(gwy);
|
||||
var errors = Errors.None;
|
||||
Log.Api.info("Starting websocket server...");
|
||||
|
||||
@@ -14,10 +14,10 @@ namespace bts.websocket_core {
|
||||
// 2.
|
||||
// Sends data to the client
|
||||
public interface IWebsocketConnectionGateway {
|
||||
// This is the first call after construction. This
|
||||
// will only be called once, and the provided sync will be used
|
||||
// to send connection, disconnection and data back to the sync
|
||||
public void init(IWebsocketDataSync sync);
|
||||
event Action<WebsocketClientID, WebsocketIncomingMessage?>? Connected;
|
||||
event Action<WebsocketClientID>? Disconnected;
|
||||
event Action<WebsocketIncomingMessage>? MessageReceived;
|
||||
|
||||
// This function tells the class to start listening to client connections.
|
||||
// All the parameters (ports to open, security parameters etc) are
|
||||
// passed through the WebsocketServerParams arguments
|
||||
|
||||
@@ -329,19 +329,13 @@ namespace bts.websocket_core {
|
||||
|
||||
}
|
||||
|
||||
public event Action<WebsocketClientID, WebsocketIncomingMessage?>? Connected;
|
||||
public event Action<WebsocketClientID>? Disconnected;
|
||||
public event Action<WebsocketIncomingMessage>? MessageReceived;
|
||||
|
||||
private WebApplication? app = null;
|
||||
private WebsocketServerConfig? _server_params = null;
|
||||
private WebSocketConnectionStore Connections { get; } = new();
|
||||
private IWebsocketDataSync? _sync = null;
|
||||
private IWebsocketDataSync Sync {
|
||||
get => Log.notnull<IWebsocketDataSync>(_sync);
|
||||
set {
|
||||
bts.Log.isnull(_sync);
|
||||
_sync = value;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// creates a new connections dictionary and a new WebSocketHandler.
|
||||
/// </summary>
|
||||
@@ -350,13 +344,6 @@ namespace bts.websocket_core {
|
||||
_WebSocketHandler = new WebSocketHandler(this, Connections);
|
||||
}
|
||||
/// <summary>
|
||||
/// Initializes the websocket gateway with a sync object
|
||||
/// </summary>
|
||||
/// <param name="sync">The object that synchronizes the data between the websocket server and other parts of the system</param>
|
||||
public void init(IWebsocketDataSync sync) {
|
||||
Sync = sync;
|
||||
}
|
||||
/// <summary>
|
||||
/// Creates a unique identifier for a websocket client and adds the client's connection to the list of active connections.
|
||||
/// </summary>
|
||||
/// <param name="conn">The connection of the client</param>
|
||||
@@ -370,14 +357,15 @@ namespace bts.websocket_core {
|
||||
/// forwards an incoming message from a websocket client to sync.
|
||||
/// </summary>
|
||||
/// <param name="message">The incoming message to be handled</param>
|
||||
public void HandleMessage(WebsocketIncomingMessage message) => Sync.handle_client_message(message);
|
||||
public void HandleMessage(WebsocketIncomingMessage message) =>
|
||||
MessageReceived?.Invoke(message);
|
||||
|
||||
/// <summary>
|
||||
/// Notifies of a client disconnection and removes the client's connection from the list of active connections.
|
||||
/// </summary>
|
||||
/// <param name="id">The unique identifier of the disconnected client</param>
|
||||
public void NotifyDisconnection(WebsocketClientID id) {
|
||||
Sync.handle_disconnection(id);
|
||||
Disconnected?.Invoke(id);
|
||||
Connections.RemoveConnection(id);
|
||||
}
|
||||
|
||||
@@ -389,7 +377,7 @@ namespace bts.websocket_core {
|
||||
/// <returns>A unique identifier for the client</returns>
|
||||
public async Task<WebsocketClientID> HandleConnection(Connection conn, WebsocketIncomingMessage? optional_message) {
|
||||
var id = createID(conn);
|
||||
await Task.Run(() => Sync.handle_connection(id, optional_message));
|
||||
await Task.Run(() => Connected?.Invoke(id, optional_message));
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -406,7 +394,7 @@ namespace bts.websocket_core {
|
||||
if (conn == null) {
|
||||
// If the client connection is not found, log the error and notify sync about the disconnection through the Sync object
|
||||
logger.warn(" could not find socket in dictionary");
|
||||
Sync.handle_disconnection(id);
|
||||
Disconnected?.Invoke(id);
|
||||
return;
|
||||
}
|
||||
// Check if the connection needs to be disconnected
|
||||
@@ -425,7 +413,6 @@ namespace bts.websocket_core {
|
||||
/// <param name="server_params">The parameters for the websocket server includind the URL and endpoint,</param>
|
||||
public Task start_listening(WebsocketServerConfig server_params) {
|
||||
// start listening, assert that init has been called already
|
||||
bts.Log.notnull(_sync);
|
||||
bts.Log.isnull(_server_params);
|
||||
bts.Log.isnull(app);
|
||||
|
||||
|
||||
@@ -169,6 +169,9 @@ namespace bts.websocket_core {
|
||||
set {
|
||||
Log.isnull(gateway);
|
||||
gateway = value;
|
||||
gateway.Connected += handle_connection;
|
||||
gateway.Disconnected += handle_disconnection;
|
||||
gateway.MessageReceived += handle_client_message;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user