/// <summary> /// Try to login into user account. /// </summary> /// <param name="login">users login.</param> /// <param name="password">users password.</param> /// <returns>server's response.</returns> /// <exception cref="ConnectionInterruptedException"></exception> /// <exception cref="CertificateException"></exception> public async Task <LoginRegisterResponse> Login(string login, string password) { if (isLoggedIn) { return(LoginRegisterResponse.Error); } // certificate X509Certificate2 cert = SslTools.CreateCertificate(typeof(Client), "CryptoMessenger.Certificate.cert.pfx"); await client.ConnectWithTimeoutAsync(ip, port, cert, Properties.Settings.Default.WaitServerConnectionDelayMsec); LoginRegisterResponseMessage serverResp; try { // send login request client.SendMessage(new LoginRequestMessage { Login = login, Password = password }); // async wait for server's response serverResp = (LoginRegisterResponseMessage)await client.ReceiveMessageAsync(); } catch (ConnectionInterruptedException) { client.Close(); throw; } // dont disconnect if login success if (LoginRegisterResponse.Success.Equals(serverResp.Response)) { isLoggedIn = true; Name = login; } else { client.Close(); } return(serverResp.Response); }
/// <summary> /// Try to register client. Send response about result. /// </summary> /// <param name="client">client.</param> /// <param name="message">client's message.</param> private void RegisterClient(MpClient client, RegisterRequestMessage message) { Message response; if (string.IsNullOrEmpty(message.Login) || string.IsNullOrEmpty(message.Password) || message.Login.Length > 20 || message.Password.Length > 30) { response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Error }; } else { if (DBoperations.Register(message.Login, message.Password)) { log.Info($"Client registered: {message.Login}"); response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Success }; } else { response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Fail }; } } try { // response to client client.SendMessage(response); } catch (ConnectionInterruptedException e) { log.Error("Connection with client brokes.", e); } // close connection log.Info($"Client disconnected. ip {((IPEndPoint)client.tcpClient.Client.RemoteEndPoint).Address.ToString()}"); try { client.Close(); } catch (ConnectionInterruptedException e) { log.Error(e); } }
/// <summary> /// Try to login client. Send response about result. /// </summary> /// <param name="client">client.</param> /// <param name="message">client's message.</param> private void LoginClient(MpClient client, LoginRequestMessage message) { bool isLoggedIn = false; Message response; if (string.IsNullOrEmpty(message.Login) || string.IsNullOrEmpty(message.Password) || message.Login.Length > 20 || message.Password.Length > 30) { response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Error }; } else if (usersHandler.GetOnlineUser(message.Login) != null) { response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.AlreadyLogin }; } else { // client's id in db if (DBoperations.Login(message.Login, message.Password, out int id)) { log.Info($"Client logged in: {message.Login}"); // user is online OnlineUser user = new OnlineUser(id, message.Login, client); usersHandler.AddUser(user); isLoggedIn = true; response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Success }; } else { response = new LoginRegisterResponseMessage { Response = LoginRegisterResponse.Fail }; } } try { // response to client client.SendMessage(response); } catch (ConnectionInterruptedException e) { log.Error("Connection with client broke.", e); } // close connection with client if client not logged in if (!isLoggedIn) { log.Info($"Client disconnected. ip {((IPEndPoint)client.tcpClient.Client.RemoteEndPoint).Address.ToString()}"); try { client.Close(); } catch (ConnectionInterruptedException e) { log.Error(e); } } }