Beispiel #1
0
        /// <summary>
        /// Adds a new connection to the session.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="token">The token.</param>
        /// <exception cref="System.InvalidOperationException">Can not add connection to unknown tunnel.</exception>
        public virtual async Task AddConnection(Connection connection, CancellationToken token)
        {
            if (!Tunnels.TryGetValue(connection.TunnelId, out var _))
            {
                throw new InvalidOperationException("Can not add connection to unknown tunnel.");
            }

            Connections.TryAdd(connection.Id, connection);

            var message = new Message
            {
                Type = MessageType.Message,
                Code = MessageCode.NewConnectionMessage,
            };

            message.Values["connection_id"] = connection.Id;
            message.Values["tunnel_id"]     = connection.TunnelId;

            var response = await SendMessageAsync(message, token);

            if (!response.Success)
            {
                throw new Exception("Could not add connection.");
            }
        }
Beispiel #2
0
        /// <summary>
        /// Adds a new connection to the session.
        /// </summary>
        /// <param name="connection">The connection.</param>
        public virtual void AddConnection(Connection connection)
        {
            if (!Tunnels.TryGetValue(connection.TunnelId, out var _))
            {
                throw new InvalidOperationException("Can not add connection to unknown tunnel.");
            }

            Connections.TryAdd(connection.Id, connection);
        }
Beispiel #3
0
 /// <summary>
 /// A tunnel has been closed by the server.
 /// </summary>
 /// <param name="tunnelId">The tunnel identifier.</param>
 protected virtual async Task TunnelClosedAsync(Guid tunnelId)
 {
     if (Tunnels.TryGetValue(tunnelId, out var tunnel))
     {
         try
         {
             await RemoveTunnelAsync(tunnel, false);
         }
         catch (Exception ex)
         {
             System.Diagnostics.Debug.WriteLine($"Error closing local tunnel: {ex.Message}");
         }
     }
 }
Beispiel #4
0
        /// <summary>
        /// A new connection has been initiated for the given tunnel.
        /// </summary>
        /// <param name="messageId">The message identifier.</param>
        /// <param name="tunnelId">The tunnel identifier.</param>
        /// <param name="connectionId">The connection identifier.</param>
        /// <returns></returns>
        protected virtual async Task <Response> NewConnectionAsync(Guid messageId, Guid tunnelId, Guid connectionId)
        {
            try
            {
                if (Tunnels.TryGetValue(tunnelId, out var tunnel))
                {
                    var connection = await tunnel.CreateConnectionAsync(this, connectionId);

                    AddConnection(connection);

                    return(new Response(messageId, true, "Connection ready."));
                }
                else
                {
                    return(new Response(messageId, false, "Tunnel not found."));
                }
            }
            catch (Exception ex)
            {
                return(new Response(messageId, false, ex.Message));
            }
        }