Beispiel #1
0
        private async void buttonLogin_Click(object sender, EventArgs e)
        {
            Task <LoginRequestResult> returnedTask = Task.Run(() => LoginRequest());

            LoginRequestResult result = await returnedTask;

            this.textBoxUsername.Text = result.username;
            this.textBoxPassword.Text = result.password;
        }
Beispiel #2
0
        /// <summary>
        /// Starts the login service. Overrides abstract base method.
        /// </summary>
        public override void Run()
        {
            try
            {
                base.Start();
                while (true)
                {
                    TcpClient client = base.AcceptTcpClient();
                    Logger.Log(new LogMessage("Client connect request accepted."));
                    NetworkStream stream     = client.GetStream();
                    byte[]        readBuffer = new byte[1024];

                    if (stream.CanRead)
                    {
                        int numbersOfBytesToRead = 0;
                        do
                        {
                            numbersOfBytesToRead = stream.Read(readBuffer, offset: 0, size: readBuffer.Length);
                        } while(stream.DataAvailable);
                        UserCredentials credentials = Serializer <UserCredentials> .Deserialize(readBuffer);

                        LoginRequestResult result = ( LoginRequestResult )HandleLogInRequest(credentials);
                        if (stream.CanWrite)
                        {
                            byte[] writeBuffer = Serializer <LoginRequestResult> .Serialize(result);

                            stream.Write(writeBuffer, offset: 0, size: writeBuffer.Length);
                            Logger.Log(new LogMessage("Response to client sent."));
                        }
                        client.Close();
                        Logger.Log(new LogMessage("Client connection closed."));
                    }
                }
            }
            catch (Exception e)
            {
                // throw?
                Logger.Log(new LogMessage(e));
            }
        }
        /// <summary>
        /// Attempts to verify the credentials on the server.
        /// </summary>
        /// <param name="credentials">The credentials to verify.</param>
        /// <param name="user">The instance of the User who has logged in. If login fails this output variable is null.</param>
        /// <returns>A value indicating the state of the log in request.</returns>
        /// <exception cref="ControllerException"></exception>
        public LogInRequestStatus TryLogin(UserCredentials credentials, out User user)
        {
            NetworkStream stream = null;

            try
            {
                TcpClient client = new TcpClient(serverEndpoint.Address.ToString(), serverEndpoint.Port);
                stream = client.GetStream();
            }
            catch (SocketException se)
            {
                throw new ControllerException("A networking error occured while atttempting to contact remote server. See inner exception for details.", se);
            }
            catch (ObjectDisposedException ode)
            {
                throw new ControllerException("A networking error occured while atttempting to contact remote server. See inner exception for details.", ode);
            }
            catch (InvalidOperationException ioe)
            {
                throw new ControllerException("A networking error occured while atttempting to contact remote server. See inner exception for details.", ioe);
            }
            catch (ArgumentOutOfRangeException aoore)
            {
                throw new ControllerException("A networking error occured while atttempting to contact remote server. See inner exception for details.", aoore);
            }
            catch (ArgumentNullException ane)
            {
                throw new ControllerException("A networking error occured while atttempting to contact remote server. See inner exception for details.", ane);
            }

            LoginRequestResult result = default(LoginRequestResult);

            if (stream.CanWrite)
            {
                byte[] writeBuffer = Serializer <UserCredentials> .Serialize(credentials);

                stream.Write(writeBuffer, offset: 0, size: writeBuffer.Length);
                byte[] readBuffer = new byte[1024];
                if (stream.CanRead)
                {
                    int numberOfBytesToRead = 0;
                    do
                    {
                        numberOfBytesToRead = stream.Read(readBuffer, offset: 0, size: readBuffer.Length);
                    } while(stream.DataAvailable);
                    result = Serializer <LoginRequestResult> .Deserialize(readBuffer);
                }
                stream.Close();
            }             // The rest could be nicer and cleaner...
            else
            {
                result = new LoginRequestResult(LogInRequestStatus.NetworkError, user: null);
            }

            if (result.Status == LogInRequestStatus.Success)
            {
                user = result.User;
            }
            else
            {
                user = null;
            }
            return(result.Status);
        }