Exemple #1
0
        /// <summary>
        /// Handles connecting a client to the server, and creates a <see cref="ConnectionHandler" /> on success.
        /// </summary>
        /// <param name="loginDetails">The connection details of the trying-to-connect user.</param>
        /// <param name="connectionHandler">An initialised connection handler on login success.</param>
        /// <returns></returns>
        public LoginResponse ConnectToServer(LoginDetails loginDetails, out ConnectionHandler connectionHandler)
        {
            bool isServerFound = CreateConnection(loginDetails.Address, loginDetails.Port);

            if (!isServerFound)
            {
                connectionHandler = null;
                return(new LoginResponse(LoginResult.ServerNotFound));
            }

            IMessage userRequest = new LoginRequest(loginDetails.Username);

            SendConnectionMessage(userRequest);
            var loginResponse = (LoginResponse)GetConnectionIMessage();

            if (loginResponse.LoginResult == LoginResult.Success)
            {
                BootstrapRepositories(loginResponse.User.Id);

                connectionHandler = new ConnectionHandler(loginResponse.User.Id, serverConnection);

                Log.DebugFormat("Connection process to the server has finished");
            }
            else
            {
                connectionHandler = null;
            }

            return(loginResponse);
        }
        /// <summary>
        /// Connects the Client to the server using the parameters as connection details
        /// and gets the state of <see cref="ClientService" /> up to date with the user status'.
        /// </summary>
        /// <param name="loginDetails">The details used to log in to the Chat Program.</param>
        public LoginResult LogOn(LoginDetails loginDetails)
        {
            var serverLoginHandler = new ServerLoginHandler(messageHandlerRegistry);

            serverLoginHandler.BootstrapCompleted += OnBootstrapCompleted;

            LoginResponse response = serverLoginHandler.ConnectToServer(loginDetails, out connectionHandler);

            switch (response.LoginResult)
            {
            case LoginResult.Success:
                ClientUserId = response.User.Id;
                connectionHandler.MessageReceived += OnNewMessageReceived;
                break;

            case LoginResult.AlreadyConnected:
                Log.WarnFormat($"User {loginDetails.Username} already connected.");
                break;

            case LoginResult.ServerNotFound:
                Log.WarnFormat("Cannot find server.");
                break;
            }

            return(response.LoginResult);
        }
        /// <summary>
        /// Tries to parse the username, ipAddress and port strings to a <see cref="LoginDetails" /> object.
        /// </summary>
        /// <param name="username">The username wanted to be set.</param>
        /// <param name="ipAddress">The IPAddress wanted to be set.</param>
        /// <param name="port">The port wanted to be set.</param>
        /// <param name="loginDetails">An object to store the parsed login details.</param>
        /// <returns>Whether the parse was successful.</returns>
        public bool TryParseLogonDetails(string username, string ipAddress, string port, out LoginDetails loginDetails)
        {
            SetUserName(username);

            if (isParsed)
            {
                SetIPAddress(ipAddress);
            }
            if (isParsed)
            {
                SetPort(port);
            }
            loginDetails = isParsed ? new LoginDetails(targetedUsername, targetedAddress, targetedPort) : null;

            return(isParsed);
        }
        /// <summary>
        /// Tries to parse the command line arguments to a <see cref="LoginDetails" /> object.
        /// </summary>
        /// <param name="commandLineArguments">A collection of command line arguments containing the login details.</param>
        /// <param name="loginDetails">An object to store the parsed login details.</param>
        /// <returns>Whether the parse was successful.</returns>
        public bool TryParseCommandLineArguments(IEnumerable <string> commandLineArguments, out LoginDetails loginDetails)
        {
            var parameterName = "";

            foreach (string argument in commandLineArguments)
            {
                if (argument[0] == '/')
                {
                    parameterName = argument;
                }
                else
                {
                    if (parameterName == "/Username")
                    {
                        SetUserName(argument);
                    }
                    if (parameterName == "/IPAddress" && isParsed)
                    {
                        SetIPAddress(argument);
                    }
                    if (parameterName == "/Port" && isParsed)
                    {
                        SetPort(argument);
                    }
                }
            }

            loginDetails = isParsed ? new LoginDetails(targetedUsername, targetedAddress, targetedPort) : null;

            Log.Info(isParsed
                ? "Command line arguments successfully parsed"
                : "Command line arguments incomplete. Going to manual entry of Username, Server and Port");

            return(isParsed);
        }