Exemplo n.º 1
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)
        {
            logPrefix = string.Format("[{0}/tcp{1}] ", EndPoint.Port, UseTls ? "_tls" : "");
            logName   = "HomeNet.Network.RoleServer";
            log       = new PrefixLogger(logName, 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 ServerRoleForNodes dictionary.
            // If ServerRoleForNodes[R] is true for a role R, it means that that role is intended for nodes.
            // Thus if this server roles only consist of roles, for which ServerRoleForNodes[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) && ServerRoleForNodes[role])
                {
                    this.IsServingClientsOnly = false;
                    break;
                }
            }

            IdBase = ((uint)Roles << 24);
        }
Exemplo n.º 2
0
        /// <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(Client Client, bool IsRelayConnection)
        {
            log.Trace("(Client.Id:'0x{0:X16}',IsRelayConnection:{1})", Client.Id, IsRelayConnection);

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

            await lockObject.WaitAsync();

            bool isCallee = Client == callee;

            if (IsRelayConnection)
            {
                log.Trace("Client ({0}) ID '0x{1:X16}' disconnected, relay '{2}' status {3}.", isCallee ? "callee" : "caller", Client.Id, id, status);
            }
            else
            {
                log.Trace("Client (customer) ID '0x{0:X16}' disconnected, relay '{1}' status {2}.", Client.Id, 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 node. 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.

                    Client 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 node 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:X16}', relay '{1}', maybe it is not connected anymore.", clientToSendMessages.Id, id);
                        break;
                    }
                }
            }


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


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

            log.Trace("(-)");
        }
Exemplo n.º 3
0
        /// <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(Client Client, Message ResponseMessage, Message SenderRequest)
        {
            log.Trace("()");

            bool res          = false;
            bool destroyRelay = false;

            await lockObject.WaitAsync();

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

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

                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 '0x{1:X16}', maybe it is disconnected already, destroying the relay.", id, otherClient.Id);
                    }

                    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"];
                ClientList clientList      = serverComponent.GetClientList();
                await clientList.DestroyNetworkRelay(this);
            }

            log.Trace("(-)");
            return(res);
        }
Exemplo n.º 4
0
        /// <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(Client Client, Message RequestMessage, Guid Token)
        {
            log.Trace("()");

            Message res          = null;
            bool    destroyRelay = false;

            await lockObject.WaitAsync();

            bool isCaller = callerToken.Equals(Token);

            Client otherClient = isCaller ? callee : caller;

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

            switch (status)
            {
            case RelayConnectionStatus.WaitingForFirstInitMessage:
            {
                log.Debug("Received an initialization message from the first client ID '0x{0:X16}' on relay '{1}', waiting for the second client.", Client.Id, 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 '0x{0:X16}' on relay '{1}' probably uses a single connection for two relays. Both relays will be destroyed.", Client.Id, 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 '0x{0:X16}', closing connection to client and destroying the relay.", otherClient.Id);
                        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 '0x{0:X16}' on relay '{1}' sent a message before receiving a reply to its initialization message. Relay will be destroyed.", Client.Id, 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 '0x{0:X16}' has been relayed to other client ID '0x{1:X16}'.", Client.Id, otherClient.Id);
                    }
                    else
                    {
                        log.Warn("Unable to relay message to other client ID '0x{0:X16}', closing connection to client and destroying the relay.", otherClient.Id);
                        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 '0x{0:X16}' mixed relay '{1}' with relay '{2}', closing connection to client and destroying both relays.", otherClient.Id, 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"];
                ClientList clientList      = serverComponent.GetClientList();
                await clientList.DestroyNetworkRelay(this);

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

            log.Trace("(-)");
            return(res);
        }
Exemplo n.º 5
0
        /// <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;
            Client  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 '0x{0:X16}' rejected the call from caller identity ID '0x{1:X16}', relay '{2}'.", callee.Id, caller.Id, id);
                        messageToSend = caller.MessageBuilder.CreateErrorRejectedResponse(pendingMessage);
                    }
                    else
                    {
                        log.Warn("Callee ID '0x{0:X16}' sent error response '{1}' for call request from caller identity ID '0x{2:X16}', relay '{3}'.",
                                 callee.Id, ResponseMessage.Response.Status, caller.Id, 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 '0x{0:X16}'.", clientToSendMessage.Id);
                }
                else
                {
                    log.Debug("Unable to reponse to call initiation request to the caller ID '0x{0:X16}'.", clientToSendMessage.Id);
                }
            }

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

            log.Trace("(-):{0}", res);
            return(res);
        }
Exemplo n.º 6
0
        /// <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 node 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)
        {
            RelayConnectionStatus previousStatus = (RelayConnectionStatus)State;

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

            Client  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 '0x{0:X16}' in relay '{1}', maybe it is not connected anymore.", clientToSendMessage.Id, id);
                }
            }

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

            log.Trace("(-)");
        }
Exemplo n.º 7
0
        public override bool Init()
        {
            log.Info("()");

            bool res   = false;
            bool error = false;

            try
            {
                clientList = new ClientList();

                checkInactiveClientConnectionsTimer = new Timer(CheckInactiveClientConnectionsTimerCallback, null, CheckInactiveClientConnectionsTimerInterval, CheckInactiveClientConnectionsTimerInterval);

                serversMaintenanceThread = new Thread(new ThreadStart(ServersMaintenanceThread));
                serversMaintenanceThread.Start();

                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 implemented.");
                        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();

                if (checkInactiveClientConnectionsTimer != null)
                {
                    checkInactiveClientConnectionsTimer.Dispose();
                }
                checkInactiveClientConnectionsTimer = null;

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

            log.Info("(-):{0}", res);
            return(res);
        }