/// <summary> /// Removes the tunnel from the server and closes all connections. /// </summary> /// <param name="tunnel">The tunnel to be removed.</param> /// <param name="notifyClient">if set to <c>true</c> then notify the client of the closure.</param> protected virtual async Task RemoveTunnelAsync(Tunnel tunnel, bool notifyClient) { var connections = Connections.Values.Where(a => a.TunnelId == tunnel.Id).ToList(); foreach (var connection in connections) { try { if (Connections.TryRemove(connection.Id, out var _)) { await connection.CloseLocalAsync(); } } catch { /* Intentionally ignored so that we close all connections. */ } } await tunnel.CloseLocalAsync(); if (notifyClient) { var message = new Message { Type = MessageType.Notification, Code = MessageCode.TunnelClosedNotification }; message.Values["tunnel_id"] = tunnel.Id; SendNotification(message); } }