static void Main(string[] args) { while (true) { String typed = Console.ReadLine(); //if ping query if (typed == "ping") { long actualTime = DateTime.UtcNow.Ticks; ClientPacketPing clientPing = new ClientPacketPing(actualTime); Packet answer = SendPacket(NetworkHelper.SerializePacket(clientPing)); Console.WriteLine("Answer:" + answer); ServerPacketPing serverPing = answer as ServerPacketPing; if (serverPing == null) { break; } Console.WriteLine("Packet infos: " + serverPing.PacketSendTime); long answerTime = serverPing.PacketSendTime; Console.WriteLine("Ping : " + ((answerTime - actualTime) / 1000) + " ticks"); } if (typed == "user") { PLFUser newUser = new PLFUser(); newUser.UserNickName = "UserTest"; newUser.UserName = "******"; newUser.UserSurname = "UserTest Surname"; newUser.UserEMail = "*****@*****.**"; ClientPacketUserRegister clientPacketUserRegister = new ClientPacketUserRegister(newUser, "mdp"); Packet answer = SendPacket(NetworkHelper.SerializePacket(clientPacketUserRegister)); Console.WriteLine("Answer:" + answer); ServerPacketUserRegister serverPacketUserRegister = answer as ServerPacketUserRegister; if (serverPacketUserRegister == null) { break; } int userID = serverPacketUserRegister.UserID; Console.WriteLine( $"Register: {serverPacketUserRegister.RegisterSuccess} - {userID} - {serverPacketUserRegister.ErrorMessage}"); } System.Threading.Thread.Sleep(1000); Console.ReadKey(); Console.Clear(); } }
/// <summary> /// Registers the user. /// </summary> /// <returns>The user.</returns> /// <param name="user">User.</param> /// <param name="userPassword">User password.</param> public static ServerPacketUserRegister RegisterUser(PLFUser user, String userPassword) { //hash user password String hashPassword = userPassword.HashSHA256(); //new user register packet ClientPacketUserRegister clientPacketUserRegister = new ClientPacketUserRegister(user, hashPassword); //send packet to server ServerPacketUserRegister serverPacketUserRegister = TCPClient.SendPacket(clientPacketUserRegister) as ServerPacketUserRegister; //if no answer if (serverPacketUserRegister == null) { return(new ServerPacketUserRegister(false, -1, NetworkError.SERVER_UNAVAILABLE)); } return(serverPacketUserRegister); }
/// <summary> /// Register this instance. /// </summary> void Register() { //if fields are empty if (String.IsNullOrEmpty(tfNickNameField.Text) || String.IsNullOrEmpty(tfEmailField.Text) || String.IsNullOrEmpty(tfPasswordField.Text)) { BarHelper.DisplayErrorBar(uivMainView, MSGBank.ERROR_TITLE, MSGBank.ERROR_FILL_ALL_FIELDS); return; } //if invalid email if (!tfEmailField.Text.IsValidEmail()) { BarHelper.DisplayErrorBar(uivMainView, MSGBank.ERROR_TITLE, MSGBank.ERROR_WRONG_EMAIL); return; } //if not same password if (tfPasswordField.Text != tfPasswordConfirmField.Text) { BarHelper.DisplayErrorBar(uivMainView, MSGBank.ERROR_TITLE, MSGBank.ERROR_NOT_SAME_PASSWORD); return; } //init new user with register properties PLFUser user = new PLFUser(); user.UserName = tfNameField.Text; user.UserSurname = tfSurnameField.Text; user.UserEMail = tfEmailField.Text; user.UserNickName = tfNickNameField.Text; //send register packet ServerPacketUserRegister serverPacketUserRegister = ServerHelper.RegisterUser(user, tfPasswordField.Text); //if register success if (serverPacketUserRegister.RegisterSuccess) { //Set user id with received user id user.ID = serverPacketUserRegister.UserID; //set the actual user Application.ActualUser = user; Application.UserPassword = tfPasswordField.Text; //load user pet list Application.PetManager.LoadUserPetList(user); BarHelper.DisplayInfoBar(uivMainView, "Inscription", $"Super ! Vous vous êtes bien inscrit sous le nom de {user.UserNickName} !", 3, delegate { ConnectionHelper.SaveConnectionProfile(tfNickNameField.Text, tfPasswordField.Text); PerformSegue("RegisterConfirmAccountSegue", this); //reset register fields tfNameField.Text = string.Empty; tfSurnameField.Text = string.Empty; tfEmailField.Text = string.Empty; tfNickNameField.Text = string.Empty; tfPasswordField.Text = string.Empty; tfPasswordConfirmField.Text = string.Empty; }, delegate { ConnectionHelper.SaveConnectionProfile(tfNickNameField.Text, tfPasswordField.Text); PerformSegue("RegisterConfirmAccountSegue", this); //reset register fields tfNameField.Text = string.Empty; tfSurnameField.Text = string.Empty; tfEmailField.Text = string.Empty; tfNickNameField.Text = string.Empty; tfPasswordField.Text = string.Empty; tfPasswordConfirmField.Text = string.Empty; }); return; } //chose right error message String messageError = string.Empty; switch (serverPacketUserRegister.NetworkError) { case NetworkError.GLOBAL_UNKNOWN: goto default; case NetworkError.SERVER_UNAVAILABLE: messageError = MSGBank.ERROR_NO_SERVER; break; case NetworkError.SQL_USER_EXIST: messageError = "Ce nom d'utilisateur est deja pris !"; break; default: messageError = MSGBank.ERROR_UNKNOWN; break; } BarHelper.DisplayErrorBar(uivMainView, MSGBank.ERROR_TITLE, messageError); }
public override Packet OnPacketReceive(Packet receivedPacket) { //get packet ClientPacketUserRegister clientPacketUserRegister = receivedPacket as ClientPacketUserRegister; ConsoleHelper.Write("Receive - ClientPacketUserRegister"); //read packet infos PLFUser newUser = clientPacketUserRegister.User; String userPassword = clientPacketUserRegister.UserPassword.ToSQL(); String userSecretKey = KeyHelper.CreateRandomKey(); //insert new profile into DB command MySqlCommand registerNewUserCommand = new MySqlCommand(); registerNewUserCommand.Connection = Program.mySQLManager.MySQLConnection.MysqlConnection; registerNewUserCommand.CommandText = //register profile $"INSERT INTO `T_User`(`userPassword`, `userNick`, `userName`, `userSurname`, `userEmail`) VALUES " + $"('{userPassword}','{newUser.UserNickName.ToSQL()}','{newUser.UserName.ToSQL()}','{newUser.UserSurname.ToSQL()}','{newUser.UserEMail.ToSQL()}');" + //register key $"INSERT INTO `T_RegisterKey`(`regUserMail`, `regKey`, `regKeyDeliver`) VALUES ('{newUser.UserEMail}','{userSecretKey}','{DateTime.Now.Ticks}');" + //get user id $"SELECT LAST_INSERT_ID()"; MySqlDataReader mysqlDataReader = null; //command reader ServerPacketUserRegister serverPacketUserRegister; //register packet answer try { int userID = 0; //Execute reader mysqlDataReader = registerNewUserCommand.ExecuteReader(); //open reader and read ID while (mysqlDataReader.Read()) { userID = mysqlDataReader.GetInt32(0); } //new answer, register success serverPacketUserRegister = new ServerPacketUserRegister(true, userID, NetworkError.NONE); //send confirmation mail MailHelper.SendRegisterConfirmationMail(newUser, userSecretKey); } catch (MySqlException e) { //write sql error Console.WriteLine(e.Number + " - " + e.Message); NetworkError networkError; //switch between sql errors switch (e.Number) { case 1062: networkError = NetworkError.SQL_USER_EXIST; break; case 1064: networkError = NetworkError.GLOBAL_UNKNOWN; break; default: networkError = NetworkError.GLOBAL_UNKNOWN; break; } //new answer, register fail serverPacketUserRegister = new ServerPacketUserRegister(false, -1, networkError); } catch (Exception e) { //new answer, register fail serverPacketUserRegister = new ServerPacketUserRegister(false, -1, NetworkError.GLOBAL_UNKNOWN); Console.WriteLine(e.Message); } //close reader if (mysqlDataReader != null && !mysqlDataReader.IsClosed) { mysqlDataReader.Close(); } ConsoleHelper.Write("Send - ServerPacketUserRegister"); return(serverPacketUserRegister); }