Ejemplo n.º 1
0
        /// <summary>
        ///     Attempt to connect to a server
        /// </summary>
        /// <param name="clientConfig">Client config params</param>
        /// <returns>True if the client is connected to the server, false if not</returns>
        public bool Connect(ClientConfig clientConfig)
        {
            // Let the user know that we are trying to connect to a server
            _logger.Info($"Attempting to connect to server at {clientConfig.HostAddress}:{clientConfig.Port}...");

            // if we are currently trying to connect, cancel
            // and try again.
            if (Status == ClientStatus.Connecting)
            {
                _logger.Info("Current status is 'connecting', attempting to disconnect first.");
                Disconnect();
            }

            // The client is already connected so we need to
            // disconnect.
            if (Status == ClientStatus.Connected)
            {
                _logger.Info("Current status is 'connected', attempting to disconnect first.");
                Disconnect();
            }

            // Set the configuration
            Config = clientConfig;

            // Start the client, if client setup fails, return out and
            // tell the user
            var result = _netClient.Start();

            if (!result)
            {
                _logger.Error("The client failed to start.");
                ConnectionMessage = "The client failed to start.";
                Disconnect(); // make sure we are fully disconnected
                return(false);
            }

            _logger.Info("Set status to 'connecting'...");

            // Try connect to server, update the status to say that
            // we are trying to connect.
            try
            {
                _netClient.Connect(Config.HostAddress, Config.Port, "CSM");
            }
            catch (Exception ex)
            {
                ConnectionMessage = "Failed to connect.";
                _logger.Error(ex, $"Failed to connect to {Config.HostAddress}:{Config.Port}");
                ChatLogPanel.PrintGameMessage(ChatLogPanel.MessageType.Error, $"Failed to connect: {ex.Message}");
                Disconnect();
                return(false);
            }

            // Start processing networking
            Status   = ClientStatus.Connecting;
            ClientId = 0;

            // We need to wait in a loop for 30 seconds (waiting 500ms each time)
            // while we wait for a successful connection (Status = Connected) or a
            // failed connection (Status = Disconnected).
            var waitWatch = new Stopwatch();

            waitWatch.Start();

            // Try connect for 30 seconds
            while (waitWatch.Elapsed < TimeSpan.FromSeconds(30))
            {
                // If we connect, exit the loop and return true
                if (Status == ClientStatus.Connected)
                {
                    _logger.Info("Client has connected.");
                    return(true);
                }

                // The client cannot connect for some reason, the ConnectionMessage
                // variable will contain why.
                if (Status == ClientStatus.Disconnected)
                {
                    _logger.Warn("Client disconnected while in connecting loop.");
                    Disconnect(); // make sure we are fully disconnected
                    return(false);
                }

                // Wait 500ms
                Thread.Sleep(500);
            }

            // We have timed out
            ConnectionMessage = "Could not connect to server, timed out.";
            _logger.Warn("Connection timeout!");

            // Did not connect
            Disconnect(); // make sure we are fully disconnected
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Attempt to connect to a server
        /// </summary>
        /// <param name="clientConfig">Client config params</param>
        /// <returns>True if the client is connected to the server, false if not</returns>
        public bool Connect(ClientConfig clientConfig)
        {
            // if we are currently trying to connect, cancel
            // and try again.
            if (Status == ClientStatus.Connecting)
            {
                Disconnect();
            }

            // The client is already connected so we need to
            // disconnect.
            if (Status == ClientStatus.Connected)
            {
                Disconnect();
            }

            // Set the config
            _clientConfig = clientConfig;

            // Let the user know that we are trying to connect to a server
            CSM.Log($"Attempting to connect to server at {_clientConfig.HostAddress}:{_clientConfig.Port}...");

            // Start the client, if client setup fails, return out and
            // tell the user
            var result = _netClient.Start();

            if (!result)
            {
                ConnectionMessage = "The client failed to start.";
                Disconnect(); // make sure we are fully disconnected
                return(false);
            }

            // Try connect to server, update the status to say that
            // we are trying to connect.
            _netClient.Connect(_clientConfig.HostAddress, _clientConfig.Port);

            // Start processing networking
            Status = ClientStatus.Connecting;

            // Setup processing thread
            _clientProcessingThread = new Thread(ProcessEvents);
            _clientProcessingThread.Start();

            // We need to wait in a loop for 30 seconds (waiting 500ms each time)
            // while we wait for a successful connection (Status = Connected) or a
            // failed connection (Status = Disconnected).
            var waitWatch = new Stopwatch();

            waitWatch.Start();

            while (waitWatch.Elapsed < TimeSpan.FromSeconds(30))
            {
                // If we connect, exit the loop and return true
                if (Status == ClientStatus.Connected)
                {
                    return(true);
                }

                // The client cannot connect for some reason, the ConnectionMessage
                // variable will contain why.
                if (Status == ClientStatus.Disconnected)
                {
                    Disconnect(); // make sure we are fully disconnected
                    return(false);
                }

                // Wait 500ms
                Thread.Sleep(500);
            }

            // We have timed out
            ConnectionMessage = "Could not connect to server, timed out.";

            // Did not connect
            Disconnect(); // make sure we are fully disconnected
            return(false);
        }