Exemplo n.º 1
0
        /// <summary>
        /// Obtains the client list.
        /// </summary>
        /// <returns>List of all server's clients.</returns>
        public IncomingClientList GetClientList()
        {
            log.Trace("()");

            IncomingClientList res = clientList;

            log.Trace("(-)");
            return(res);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new TCP server to listen on specific IP endpoint.
        /// </summary>
        /// <param name="EndPoint">Specification of the interface and TCP port on which the TCP server should listen. IPAddress.Any is a valid value for the interface.</param>
        /// <param name="UseTls">Indication of whether to use TLS for this TCP server.</param>
        /// <param name="Roles">One or more roles of this server.</param>
        public TcpRoleServer(IPEndPoint EndPoint, bool UseTls, ServerRole Roles)
        {
            string logPrefix = string.Format("[{0}/tcp{1}] ", EndPoint.Port, UseTls ? "_tls" : "");

            log = new PrefixLogger("ProfileServer.Network.TcpRoleServer", logPrefix);

            this.UseTls   = UseTls;
            this.Roles    = Roles;
            this.EndPoint = EndPoint;

            ShutdownSignaling = new ComponentShutdown(Base.Components.GlobalShutdown);

            serverComponent = (Server)Base.ComponentDictionary["Network.Server"];
            clientList      = serverComponent.GetClientList();

            IsRunning = false;
            Listener  = new TcpListener(this.EndPoint);
            Listener.Server.LingerState = new LingerOption(true, 0);
            Listener.Server.NoDelay     = true;

            // We want to determine what types of clients do connect to this server.
            // This information is stored in ServerRoleForServers dictionary.
            // If ServerRoleForServers[R] is true for a role R, it means that that role is intended for servers.
            // Thus if this server roles only consist of roles, for which ServerRoleForServers[x] is false,
            // it means that the server is intended for clients use.
            IsServingClientsOnly = true;
            foreach (ServerRole role in Enum.GetValues(typeof(ServerRole)))
            {
                if (this.Roles.HasFlag(role) && ServerRoleForServers[role])
                {
                    this.IsServingClientsOnly = false;
                    break;
                }
            }

            IdBase = ((uint)Roles << 24);
        }
Exemplo n.º 3
0
        public override bool Init()
        {
            log.Info("()");

            bool res   = false;
            bool error = false;

            try
            {
                serverId   = ProfileServerCrypto.Crypto.Sha256(Base.Configuration.Keys.PublicKey);
                clientList = new IncomingClientList();

                foreach (RoleServerConfiguration roleServer in Base.Configuration.ServerRoles.RoleServers.Values)
                {
                    if (roleServer.IsTcpServer)
                    {
                        IPEndPoint    endPoint = new IPEndPoint(Base.Configuration.ServerInterface, roleServer.Port);
                        TcpRoleServer server   = new TcpRoleServer(endPoint, roleServer.Encrypted, roleServer.Roles);
                        tcpServers.Add(server.EndPoint.Port, server);
                    }
                    else
                    {
                        log.Fatal("UDP servers are not supported.");
                        error = true;
                        break;
                    }
                }


                foreach (TcpRoleServer server in tcpServers.Values)
                {
                    if (!server.Start())
                    {
                        log.Error("Unable to start TCP server {0}.", server.EndPoint);
                        error = true;
                        break;
                    }
                }

                if (!error)
                {
                    res         = true;
                    Initialized = true;
                }
            }
            catch (Exception e)
            {
                log.Error("Exception occurred: {0}", e.ToString());
            }

            if (!res)
            {
                ShutdownSignaling.SignalShutdown();

                foreach (TcpRoleServer server in tcpServers.Values)
                {
                    if (server.IsRunning)
                    {
                        server.Stop();
                    }
                }
                tcpServers.Clear();
            }

            log.Info("(-):{0}", res);
            return(res);
        }
        /// <summary>
        /// Handles situation when a client connected to a relay disconnected.
        /// However, the closed connection might be either connection to clCustomer/clNonCustomer port,
        /// or it might be connection to clAppService port.
        /// </summary>
        /// <param name="Client">Client that disconnected.</param>
        /// <param name="IsRelayConnection">true if the closed connection was to clAppService port, false otherwise.</param>
        public async Task HandleDisconnectedClient(IncomingClient Client, bool IsRelayConnection)
        {
            log.Trace("(Client.Id:{0},IsRelayConnection:{1})", Client.Id.ToHex(), IsRelayConnection);

            IncomingClient clientToSendMessages = null;
            List <Message> messagesToSend       = new List <Message>();
            IncomingClient clientToClose        = null;

            await lockObject.WaitAsync();

            bool isCallee = Client == callee;

            if (IsRelayConnection)
            {
                log.Trace("Client ({0}) ID {1} disconnected, relay '{2}' status {3}.", isCallee ? "callee" : "caller", Client.Id.ToHex(), id, status);
            }
            else
            {
                log.Trace("Client (customer) ID {0} disconnected, relay '{1}' status {2}.", Client.Id.ToHex(), id, status);
            }

            bool destroyRelay = false;

            switch (status)
            {
            case RelayConnectionStatus.WaitingForCalleeResponse:
            {
                if (isCallee)
                {
                    // The client is callee in a relay that is being initialized. The caller is waiting for callee's response and the callee has just disconnected
                    // from the profile server. This is situation 1) from the comment in ProcessMessageCallIdentityApplicationServiceRequestAsync.
                    // We have to send ERROR_NOT_AVAILABLE to the caller and destroy the relay.
                    log.Trace("Callee disconnected from clCustomer port of relay '{0}', message will be sent to the caller and relay destroyed.", id);
                    clientToSendMessages = caller;
                    messagesToSend.Add(caller.MessageBuilder.CreateErrorNotAvailableResponse(pendingMessage));
                    destroyRelay = true;
                }
                else
                {
                    // The client is caller in a relay that is being initialized. The caller was waiting for callee's response, but the caller disconnected before
                    // the callee replied. The callee is now expected to reply and either accept or reject the call. If the call is rejected, everything is OK,
                    // and we do not need to take any action. If the call is accepted, the callee will establish a new connection to clAppService port and will
                    // send us initial ApplicationServiceSendMessageRequest message. We will now destroy the relay so that the callee is disconnected
                    // as its token used in the initial message will not be found.
                    log.Trace("Caller disconnected from clCustomer port or clNonCustomer port of relay '{0}', relay will be destroyed.", id);
                    destroyRelay = true;
                }
                break;
            }

            case RelayConnectionStatus.WaitingForFirstInitMessage:
            {
                // In this relay status we do not care about connection to other than clAppService port.
                if (IsRelayConnection)
                {
                    // This should never happen because client's Relay is initialized only after
                    // its initialization message is received and that would upgrade the relay to WaitingForSecondInitMessage.
                }

                break;
            }

            case RelayConnectionStatus.WaitingForSecondInitMessage:
            {
                // In this relay status we do not care about connection to other than clAppService port.
                if (IsRelayConnection)
                {
                    // One of the clients has sent its initialization message to clAppService port
                    // and is waiting for the other client to do the same.
                    bool isWaitingClient = (callee == Client) || (caller == Client);

                    if (isWaitingClient)
                    {
                        // The client that disconnected was the waiting client. We destroy the relay.
                        // The other client is not connected yet or did not sent its initialization message yet.
                        log.Trace("First client on clAppService port of relay '{0}' closed its connection, destroying the relay.", id);
                        destroyRelay = true;
                    }
                    else
                    {
                        // The client that disconnected was the client that the first client is waiting for.
                        // We do not need to destroy the relay as the client may still connect again
                        // and send its initialization message on time.
                        log.Trace("Second client (that did not sent init message yet) on clAppService port of relay '{0}' closed its connection, no action taken.", id);
                    }
                }

                break;
            }

            case RelayConnectionStatus.Open:
            {
                // In this relay status we do not care about connection to other than clAppService port.
                if (IsRelayConnection)
                {
                    // Both clients were connected. We disconnect the other client and destroy the relay.
                    // However, there might be some unfinished ApplicationServiceSendMessageRequest requests
                    // that we have to send responses to.

                    IncomingClient otherClient = isCallee ? caller : callee;
                    log.Trace("{0} disconnected from relay '{1}', closing connection of {2}.", isCallee ? "Callee" : "Caller", id, isCallee ? "caller" : "callee");
                    clientToSendMessages = otherClient;
                    clientToClose        = otherClient;

                    // Find all unfinished requests from this relay.
                    // When a client sends ApplicationServiceSendMessageRequest, the profile server creates ApplicationServiceReceiveMessageNotificationRequest
                    // and adds it as an unfinished request with context set to RelayMessageContext, which contains the sender's ApplicationServiceSendMessageRequest.
                    // This unfinished message is in the list of unfinished message of the recipient.
                    List <UnfinishedRequest> unfinishedRelayRequests = Client.GetAndRemoveUnfinishedRequests();
                    foreach (UnfinishedRequest unfinishedRequest in unfinishedRelayRequests)
                    {
                        // Find ApplicationServiceReceiveMessageNotificationRequest request messages sent to the client who closed the connection.
                        if ((unfinishedRequest.RequestMessage.MessageTypeCase == Message.MessageTypeOneofCase.Request) &&
                            (unfinishedRequest.RequestMessage.Request.ConversationTypeCase == Request.ConversationTypeOneofCase.SingleRequest) &&
                            (unfinishedRequest.RequestMessage.Request.SingleRequest.RequestTypeCase == SingleRequest.RequestTypeOneofCase.ApplicationServiceReceiveMessageNotification))
                        {
                            // This unfinished request's context holds ApplicationServiceSendMessageRequest message of the client that is still connected.
                            RelayMessageContext ctx           = (RelayMessageContext)unfinishedRequest.Context;
                            Message             responseError = clientToSendMessages.MessageBuilder.CreateErrorNotFoundResponse(ctx.SenderRequest);
                            messagesToSend.Add(responseError);
                        }
                    }

                    destroyRelay = true;
                }

                break;
            }

            case RelayConnectionStatus.Destroyed:
                // Nothing to be done.
                break;
            }

            lockObject.Release();


            if (messagesToSend.Count > 0)
            {
                foreach (Message messageToSend in messagesToSend)
                {
                    if (!await clientToSendMessages.SendMessageAsync(messageToSend))
                    {
                        log.Warn("Unable to send message to the client ID {0}, relay '{1}', maybe it is not connected anymore.", clientToSendMessages.Id.ToHex(), id);
                        break;
                    }
                }
            }


            if (clientToClose != null)
            {
                await clientToClose.CloseConnectionAsync();
            }


            if (destroyRelay)
            {
                Server             serverComponent = (Server)Base.ComponentDictionary["Network.Server"];
                IncomingClientList clientList      = serverComponent.GetClientList();
                await clientList.DestroyNetworkRelay(this);
            }

            log.Trace("(-)");
        }
        /// <summary>
        /// Processes incoming confirmation from the message recipient over the relay.
        /// </summary>
        /// <param name="Client">Client that sent the response.</param>
        /// <param name="ResponseMessage">Full response message.</param>
        /// <param name="SenderRequest">Sender request message that the recipient confirmed.</param>
        /// <returns>true if the connection to the client that sent the response should remain open, false if the client should be disconnected.</returns>
        public async Task <bool> RecipientConfirmedMessage(IncomingClient Client, Message ResponseMessage, Message SenderRequest)
        {
            log.Trace("()");

            bool res          = false;
            bool destroyRelay = false;

            await lockObject.WaitAsync();

            if (status == RelayConnectionStatus.Open)
            {
                bool isCaller = Client == caller;

                IncomingClient otherClient = isCaller ? callee : caller;
                log.Trace("Over relay '{0}', received confirmation (status code {1}) from client ID {2} of a message sent by client ID {3}.",
                          id, ResponseMessage.Response.Status, Client.Id.ToHex(), otherClient.Id.ToHex());

                if (ResponseMessage.Response.Status == Status.Ok)
                {
                    // We have received a confirmation from the recipient, so we just complete the sender's request to inform it that the message was delivered.
                    Message otherClientResponse = otherClient.MessageBuilder.CreateApplicationServiceSendMessageResponse(SenderRequest);
                    if (await otherClient.SendMessageAsync(otherClientResponse))
                    {
                        res = true;
                    }
                    else
                    {
                        log.Warn("Unable to send message to other client ID '0x{0:X16}' on relay '{1}', closing connection to client and destroying the relay.", id, otherClient.Id);
                        destroyRelay = true;
                    }
                }
                else
                {
                    // We have received error from the recipient, so we forward it to the sender and destroy the relay.
                    Message errorResponse = otherClient.MessageBuilder.CreateErrorNotFoundResponse(SenderRequest);

                    if (!await otherClient.SendMessageAsync(errorResponse))
                    {
                        log.Warn("In relay '{0}', unable to send error response to the sender client ID {1}, maybe it is disconnected already, destroying the relay.", id, otherClient.Id.ToHex());
                    }

                    destroyRelay = true;
                }
            }
            else
            {
                // This should never happen unless the relay is destroyed already.
                log.Debug("Relay '{0}' status is {1} instead of Open, destroying relay if it is still active.", id, status);
                destroyRelay = status != RelayConnectionStatus.Destroyed;
            }

            lockObject.Release();

            if (destroyRelay)
            {
                Server             serverComponent = (Server)Base.ComponentDictionary["Network.Server"];
                IncomingClientList clientList      = serverComponent.GetClientList();
                await clientList.DestroyNetworkRelay(this);
            }

            log.Trace("(-)");
            return(res);
        }
        /// <summary>
        /// Processes ApplicationServiceSendMessageRequest message from a client.
        /// <para>
        /// Relay received message from one client and sends it to the other one. If this is the first request
        /// a client sends after it connects to clAppService port, the request's message is ignored and the reply is sent
        /// to the client as the other client is confirmed to join the relay.</para>
        /// </summary>
        /// <param name="Client">Client that sent the message.</param>
        /// <param name="RequestMessage">Full request message.</param>
        /// <param name="Token">Sender's relay token.</param>
        /// <returns>Response message to be sent to the client.</returns>
        public async Task <Message> ProcessIncomingMessage(IncomingClient Client, Message RequestMessage, Guid Token)
        {
            log.Trace("()");

            Message res          = null;
            bool    destroyRelay = false;

            await lockObject.WaitAsync();

            bool isCaller = callerToken.Equals(Token);

            IncomingClient otherClient = isCaller ? callee : caller;

            log.Trace("Received message over relay '{0}' in status {1} with client ID {2} being {3} and the other client ID {4} is {5}.",
                      id, status, Client.Id.ToHex(), isCaller ? "caller" : "callee", otherClient != null ? otherClient.Id.ToHex() : "N/A", isCaller ? "callee" : "caller");

            switch (status)
            {
            case RelayConnectionStatus.WaitingForCalleeResponse:
            {
                if (!isCaller)
                {
                    // We have received a message from callee, but we did not receive its IncomingCallNotificationResponse.
                    // This may be OK if this message has been sent by callee and it just not has been processed before
                    // the callee connected to clAppService port and sent us the initialization message.
                    // In this case we will try to wait a couple of seconds and see if we receive IncomingCallNotificationResponse.
                    // If yes, we continue as if we processed the message in the right order.
                    // In all other cases, this is a fatal error and we have to destroy the relay.
                    lockObject.Release();

                    bool statusChanged = false;
                    log.Warn("Callee sent initialization message before we received IncomingCallNotificationResponse. We will wait to see if it arrives soon.");
                    for (int i = 0; i < 5; i++)
                    {
                        log.Warn("Attempt #{0}, waiting 1 second.", i + 1);
                        await Task.Delay(1000);

                        await lockObject.WaitAsync();

                        log.Warn("Attempt #{0}, checking relay status.", i + 1);
                        if (status != RelayConnectionStatus.WaitingForCalleeResponse)
                        {
                            log.Warn("Attempt #{0}, relay status changed to {1}.", i + 1, status);
                            statusChanged = true;
                        }

                        lockObject.Release();

                        if (statusChanged)
                        {
                            break;
                        }
                    }

                    await lockObject.WaitAsync();

                    if (statusChanged)
                    {
                        // Status of relay has change, which means either it has been destroyed already, or the IncomingCallNotificationResponse
                        // message we were waiting for arrived. In any case, we call this method recursively, but it can not happen that we would end up here again.
                        lockObject.Release();

                        log.Trace("Calling ProcessIncomingMessage recursively.");
                        res = await ProcessIncomingMessage(Client, RequestMessage, Token);

                        await lockObject.WaitAsync();
                    }
                    else
                    {
                        log.Trace("Message received from caller and relay status is WaitingForCalleeResponse and IncomingCallNotificationResponse did not arrive, closing connection to client, destroying relay.");
                        res = Client.MessageBuilder.CreateErrorNotFoundResponse(RequestMessage);
                        Client.ForceDisconnect = true;
                        destroyRelay           = true;
                    }
                }
                else
                {
                    log.Trace("Message received from caller and relay status is WaitingForCalleeResponse, closing connection to client, destroying relay.");
                    res = Client.MessageBuilder.CreateErrorNotFoundResponse(RequestMessage);
                    Client.ForceDisconnect = true;
                    destroyRelay           = true;
                }
                break;
            }

            case RelayConnectionStatus.WaitingForFirstInitMessage:
            {
                log.Debug("Received an initialization message from the first client ID '{0}' on relay '{1}', waiting for the second client.", Client.Id.ToHex(), id);
                CancelTimeoutTimerLocked();

                if (Client.Relay == null)
                {
                    Client.Relay = this;

                    // Other peer is not connected yet, so we put this request on hold and wait for the other client.
                    if (isCaller)
                    {
                        caller = Client;
                    }
                    else
                    {
                        callee = Client;
                    }

                    status = RelayConnectionStatus.WaitingForSecondInitMessage;
                    log.Trace("Relay '{0}' status changed to {1}.", id, status);

                    pendingMessage = RequestMessage;
                    timeoutTimer   = new Timer(TimeoutCallback, status, SecondAppServiceInitializationMessageDelayMaxSeconds * 1000, Timeout.Infinite);

                    // res remains null, which is OK as the request is put on hold until the other client joins the channel.
                }
                else
                {
                    // Client already sent us the initialization message, this is protocol violation error, destroy the relay.
                    // Since the relay should be upgraded to WaitingForSecondInitMessage status, this can happen
                    // only if a client does not use a separate connection for each clAppService session, which is forbidden.
                    log.Debug("Client ID {0} on relay '{1}' probably uses a single connection for two relays. Both relays will be destroyed.", Client.Id.ToHex(), id);
                    res          = Client.MessageBuilder.CreateErrorNotFoundResponse(RequestMessage);
                    destroyRelay = true;
                }
                break;
            }

            case RelayConnectionStatus.WaitingForSecondInitMessage:
            {
                log.Debug("Received an initialization message from the second client on relay '{0}'.", id);
                CancelTimeoutTimerLocked();

                if (Client.Relay == null)
                {
                    Client.Relay = this;

                    // Other peer is connected already, so we just inform it by sending response to its initial ApplicationServiceSendMessageRequest.
                    if (isCaller)
                    {
                        caller = Client;
                    }
                    else
                    {
                        callee = Client;
                    }

                    status = RelayConnectionStatus.Open;
                    log.Trace("Relay '{0}' status changed to {1}.", id, status);

                    Message otherClientResponse = otherClient.MessageBuilder.CreateApplicationServiceSendMessageResponse(pendingMessage);
                    pendingMessage = null;
                    if (await otherClient.SendMessageAsync(otherClientResponse))
                    {
                        // And we also send reply to the second client that the channel is now ready for communication.
                        res = Client.MessageBuilder.CreateApplicationServiceSendMessageResponse(RequestMessage);
                    }
                    else
                    {
                        log.Warn("Unable to send message to other client ID {0}, closing connection to client and destroying the relay.", otherClient.Id.ToHex());
                        res = Client.MessageBuilder.CreateErrorNotFoundResponse(RequestMessage);
                        Client.ForceDisconnect = true;
                        destroyRelay           = true;
                    }
                }
                else
                {
                    // Client already sent us the initialization message, this is error, destroy the relay.
                    log.Debug("Client ID {0} on relay '{1}' sent a message before receiving a reply to its initialization message. Relay will be destroyed.", Client.Id.ToHex(), id);
                    res          = Client.MessageBuilder.CreateErrorNotFoundResponse(RequestMessage);
                    destroyRelay = true;
                }

                break;
            }


            case RelayConnectionStatus.Open:
            {
                if (Client.Relay == this)
                {
                    // Relay is open, this means that all incoming messages are sent to the other client.
                    byte[]              messageForOtherClient = RequestMessage.Request.SingleRequest.ApplicationServiceSendMessage.Message.ToByteArray();
                    Message             otherClientMessage    = otherClient.MessageBuilder.CreateApplicationServiceReceiveMessageNotificationRequest(messageForOtherClient);
                    RelayMessageContext context = new RelayMessageContext(this, RequestMessage);
                    if (await otherClient.SendMessageAndSaveUnfinishedRequestAsync(otherClientMessage, context))
                    {
                        // res is null, which is fine, the sender is put on hold and we will get back to it once the recipient confirms that it received the message.
                        log.Debug("Message from client ID {0} has been relayed to other client ID {1}.", Client.Id.ToHex(), otherClient.Id.ToHex());
                    }
                    else
                    {
                        log.Warn("Unable to relay message to other client ID {0}, closing connection to client and destroying the relay.", otherClient.Id.ToHex());
                        res = Client.MessageBuilder.CreateErrorNotFoundResponse(RequestMessage);
                        Client.ForceDisconnect = true;
                        destroyRelay           = true;
                    }
                }
                else
                {
                    // This means that the client used a single clAppService port connection for two different relays, which is forbidden.
                    log.Warn("Client ID {0} mixed relay '{1}' with relay '{2}', closing connection to client and destroying both relays.", otherClient.Id.ToHex(), Client.Relay.id, id);
                    res = Client.MessageBuilder.CreateErrorNotFoundResponse(RequestMessage);
                    Client.ForceDisconnect = true;
                    destroyRelay           = true;
                }

                break;
            }

            case RelayConnectionStatus.Destroyed:
            {
                log.Trace("Relay has been destroyed, closing connection to client.");
                res = Client.MessageBuilder.CreateErrorNotFoundResponse(RequestMessage);
                Client.ForceDisconnect = true;
                break;
            }

            default:
                log.Trace("Relay status is '{0}', closing connection to client, destroying relay.", status);
                res = Client.MessageBuilder.CreateErrorNotFoundResponse(RequestMessage);
                Client.ForceDisconnect = true;
                destroyRelay           = true;
                break;
            }

            lockObject.Release();

            if (destroyRelay)
            {
                Server             serverComponent = (Server)Base.ComponentDictionary["Network.Server"];
                IncomingClientList clientList      = serverComponent.GetClientList();
                await clientList.DestroyNetworkRelay(this);

                if ((this != Client.Relay) && (Client.Relay != null))
                {
                    await clientList.DestroyNetworkRelay(Client.Relay);
                }
            }

            log.Trace("(-)");
            return(res);
        }
        /// <summary>
        /// Handles situation when the callee replied to the incoming call notification request.
        /// </summary>
        /// <param name="ResponseMessage">Full response message from the callee.</param>
        /// <param name="Request">Unfinished call request message of the caller that corresponds to the response message.</param>
        /// <returns></returns>
        public async Task <bool> CalleeRepliedToIncomingCallNotification(Message ResponseMessage, UnfinishedRequest Request)
        {
            log.Trace("()");

            bool res = false;

            bool           destroyRelay        = false;
            IncomingClient clientToSendMessage = null;
            Message        messageToSend       = null;

            await lockObject.WaitAsync();


            if (status == RelayConnectionStatus.WaitingForCalleeResponse)
            {
                CancelTimeoutTimerLocked();

                // The caller is still connected and waiting for an answer to its call request.
                if (ResponseMessage.Response.Status == Status.Ok)
                {
                    // The callee is now expected to connect to clAppService with its token.
                    // We need to inform caller that the callee accepted the call.
                    // This is option 4) from ProcessMessageCallIdentityApplicationServiceRequestAsync.
                    messageToSend       = caller.MessageBuilder.CreateCallIdentityApplicationServiceResponse(pendingMessage, callerToken.ToByteArray());
                    clientToSendMessage = caller;
                    pendingMessage      = null;

                    caller = null;
                    callee = null;
                    status = RelayConnectionStatus.WaitingForFirstInitMessage;
                    log.Debug("Relay '{0}' status has been changed to {1}.", id, status);

                    /// Install timeoutTimer to expire if the first client does not connect to clAppService port
                    /// and send its initialization message within reasonable time.
                    timeoutTimer = new Timer(TimeoutCallback, RelayConnectionStatus.WaitingForFirstInitMessage, FirstAppServiceInitializationMessageDelayMaxSeconds * 1000, Timeout.Infinite);

                    res = true;
                }
                else
                {
                    // The callee rejected the call or reported other error.
                    // These are options 3) and 2) from ProcessMessageCallIdentityApplicationServiceRequestAsync.
                    if (ResponseMessage.Response.Status == Status.ErrorRejected)
                    {
                        log.Debug("Callee ID '{0}' rejected the call from caller identity ID '{1}', relay '{2}'.", callee.Id.ToHex(), caller.Id.ToHex(), id);
                        messageToSend = caller.MessageBuilder.CreateErrorRejectedResponse(pendingMessage);
                    }
                    else
                    {
                        log.Warn("Callee ID '0} sent error response '{1}' for call request from caller identity ID {2}, relay '{3}'.",
                                 callee.Id.ToHex(), ResponseMessage.Response.Status, caller.Id.ToHex(), id);

                        messageToSend = caller.MessageBuilder.CreateErrorNotAvailableResponse(pendingMessage);
                    }

                    clientToSendMessage = caller;
                    destroyRelay        = true;
                }
            }
            else
            {
                // The relay has probably been destroyed, or something bad happened to it.
                // We take no action here regardless of what the callee's response is.
                // If it rejected the call, there is nothing to be done since we do not have
                // any connection to the caller anymore.
                log.Debug("Relay status is {0}, nothing to be done.", status);
            }

            lockObject.Release();


            if (messageToSend != null)
            {
                if (await clientToSendMessage.SendMessageAsync(messageToSend))
                {
                    log.Debug("Response to call initiation request sent to the caller ID {0}.", clientToSendMessage.Id.ToHex());
                }
                else
                {
                    log.Debug("Unable to reponse to call initiation request to the caller ID {0}.", clientToSendMessage.Id.ToHex());
                }
            }

            if (destroyRelay)
            {
                Server             serverComponent = (Server)Base.ComponentDictionary["Network.Server"];
                IncomingClientList clientList      = serverComponent.GetClientList();
                await clientList.DestroyNetworkRelay(this);
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
        /// <summary>
        /// Callback routine that is called once the timeoutTimer expires.
        /// <para>
        /// If relay status is WaitingForCalleeResponse, the callee has to reply to the incoming call notification
        /// within a reasonable time. If it does the timer is cancelled. If it does not, the timeout occurs.
        /// </para>
        /// <para>
        /// If relay status is WaitingForFirstInitMessage, both clients are expected to connect to clAppService port
        /// and send an initial message over that service. The timeoutTimer expires when none of the clients
        /// connects to clAppService port and sends its initialization message within a reasonable time.
        /// </para>
        /// <para>
        /// Then if relay status is WaitingForSecondInitMessage, the profile server receives a message from the first client
        /// on clAppService port, it starts the timer again, which now expires if the second client does not connect
        /// and send its initial message within a reasonable time.
        /// </para>
        /// </summary>
        /// <param name="state">Status of the relay when the timer was installed.</param>
        private async void TimeoutCallback(object State)
        {
            LogDiagnosticContext.Start();

            RelayConnectionStatus previousStatus = (RelayConnectionStatus)State;

            log.Trace("(State:{0})", previousStatus);

            IncomingClient clientToSendMessage = null;
            Message        messageToSend       = null;
            bool           destroyRelay        = false;

            await lockObject.WaitAsync();

            if (timeoutTimer != null)
            {
                switch (status)
                {
                case RelayConnectionStatus.WaitingForCalleeResponse:
                {
                    // The caller requested the call and the callee was notified.
                    // The callee failed to send us response on time, this is situation 2)
                    // from ProcessMessageCallIdentityApplicationServiceRequestAsync.
                    // We send ERROR_NOT_AVAILABLE to the caller and destroy the relay.
                    log.Debug("Callee failed to reply to the incoming call notification, closing relay.");

                    clientToSendMessage = caller;
                    messageToSend       = caller.MessageBuilder.CreateErrorNotAvailableResponse(pendingMessage);
                    break;
                }

                case RelayConnectionStatus.WaitingForFirstInitMessage:
                {
                    // Neither client joined the channel on time, nothing to do, just destroy the relay.
                    log.Debug("None of the clients joined the relay on time, closing relay.");
                    break;
                }

                case RelayConnectionStatus.WaitingForSecondInitMessage:
                {
                    // One client is waiting for the other one to join, but the other client failed to join on time.
                    // We send ERROR_NOT_FOUND to the waiting client and close its connection.
                    log.Debug("{0} failed to join the relay on time, closing relay.", callee != null ? "Caller" : "Callee");

                    clientToSendMessage = callee != null ? callee : caller;
                    messageToSend       = clientToSendMessage.MessageBuilder.CreateErrorNotFoundResponse(pendingMessage);
                    break;
                }

                default:
                    log.Debug("Time out triggered while the relay status was {0}.", status);
                    break;
                }

                // In case of any timeouts, we just destroy the relay.
                destroyRelay = true;
            }
            else
            {
                log.Debug("Timeout timer of relay '{0}' has been destroyed, no action taken.", id);
            }

            lockObject.Release();


            if (messageToSend != null)
            {
                if (!await clientToSendMessage.SendMessageAsync(messageToSend))
                {
                    log.Warn("Unable to send message to the client ID {0} in relay '{1}', maybe it is not connected anymore.", clientToSendMessage.Id.ToHex(), id);
                }
            }

            if (destroyRelay)
            {
                Server             serverComponent = (Server)Base.ComponentDictionary["Network.Server"];
                IncomingClientList clientList      = serverComponent.GetClientList();
                await clientList.DestroyNetworkRelay(this);
            }

            log.Trace("(-)");

            LogDiagnosticContext.Stop();
        }