/** * This method reads to configuration file and save its values. */ public static bool config(sill2_client_form form) { IniFile localConfigFile; // Try to read the configuration file for database connection try { localConfigFile = new IniFile(@"" + Directory.GetCurrentDirectory() + @"\sill2-client-config.ini"); dbServer = localConfigFile.IniReadValue("Config", "DbServer"); database = localConfigFile.IniReadValue("Config", "Database"); port = localConfigFile.IniReadValue("Config", "Port"); user = localConfigFile.IniReadValue("Config", "User"); password = localConfigFile.IniReadValue("Config", "Password"); // Sets the connection string /*using (connection = new MySqlConnection("server=" + dbServer + "; port=" + port + "; database=" + database + "; uid=" + user + "; pwd=" + password + "")) * { * connection.Close(); * connection.Dispose(); * }*/ return(true); } catch (FileNotFoundException ex) { if (lastErrorCode != 3) { form.SetTrayIconError("sill2-client: 3 - The configuration file could not be found.\n" + ex.Message); lastErrorCode = 3; } Verification.connectionLost = true; return(false); } catch (DirectoryNotFoundException ex) { if (lastErrorCode != 4) { form.SetTrayIconError("sill2-client: 4 - The specified path is invalid.\n" + ex.Message); lastErrorCode = 4; } Verification.connectionLost = true; return(false); } catch (IOException ex) { if (lastErrorCode != 5) { form.SetTrayIconError("sill2-client: 5 - The path includes an incorrect or invalid syntax for file name, directory name, or volume label.\n" + ex.Message); lastErrorCode = 5; } Verification.connectionLost = true; return(false); } }
/** * This method reads and parses the configuration file and creates the MySqlConnection, returning it in the end. */ public static MySqlConnection create(sill2_client_form form) { /*try * { * serverConfigFile = new IniFile(@"" + fileServer + @"\sill2-client-config.ini"); * * // Sets the connection variables * // Compare DbServer * if (String.Compare( * localConfigFile.IniReadValue("Config", "DbServer"), * serverConfigFile.IniReadValue("Config", "DbServer"), * false) == 0) * { * dbServer = localConfigFile.IniReadValue("Config", "DbServer"); * } * else * { * dbServer = serverConfigFile.IniReadValue("Config", "DbServer"); * localConfigFile.IniWriteValue("Config", "DbServer", dbServer); * } * * // Compare Database * if (String.Compare( * localConfigFile.IniReadValue("Config", "Database"), * serverConfigFile.IniReadValue("Config", "Database"), * false) == 0) * { * database = localConfigFile.IniReadValue("Config", "Database"); * } * else * { * database = serverConfigFile.IniReadValue("Config", "Database"); * localConfigFile.IniWriteValue("Config", "Database", database); * } * * // Compare Port * if (String.Compare( * localConfigFile.IniReadValue("Config", "Port"), * serverConfigFile.IniReadValue("Config", "Port"), * false) == 0) * { * port = localConfigFile.IniReadValue("Config", "Port"); * } * else * { * port = serverConfigFile.IniReadValue("Config", "Port"); * localConfigFile.IniWriteValue("Config", "Port", port); * } * * // Compare User * if (String.Compare( * localConfigFile.IniReadValue("Config", "User"), * serverConfigFile.IniReadValue("Config", "User"), * false) == 0) * { * user = localConfigFile.IniReadValue("Config", "User"); * } * else * { * user = serverConfigFile.IniReadValue("Config", "User"); * localConfigFile.IniWriteValue("Config", "User", user); * } * * // Compare Password * if (String.Compare( * localConfigFile.IniReadValue("Config", "Password"), * serverConfigFile.IniReadValue("Config", "Password"), * false) == 0) * { * password = localConfigFile.IniReadValue("Config", "Password"); * } * else * { * password = serverConfigFile.IniReadValue("Config", "Password"); * localConfigFile.IniWriteValue("Config", "Password", password); * } * } * catch (Exception ex) * { * dbServer = localConfigFile.IniReadValue("Config", "DbServer"); * database = localConfigFile.IniReadValue("Config", "Database"); * port = localConfigFile.IniReadValue("Config", "Port"); * user = localConfigFile.IniReadValue("Config", "User"); * password = localConfigFile.IniReadValue("Config", "Password"); * // MessageBox.Show("sill2-client: Could not be possible to write the INI file.\n" + ex.Message); * }*/ // Create the MySQL connection try { // If connection is null or closed or broken, close and dispose it to create another if (connection == null || connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken) { if (connection != null) { connection.Close(); } connection = new MySqlConnection("server=" + dbServer + "; port=" + port + "; database=" + database + "; uid=" + user + "; pwd=" + password + ""); connection.Close(); return(connection); } else // If the connection is already open, return it { connection.Close(); return(connection); } } catch (MySqlException ex) { if (lastErrorCode != 1) { form.SetTrayIconError("sill2-client: 1 - The MySQL connection could not be created.\n" + ex.Message); lastErrorCode = 1; } Verification.connectionLost = true; } catch (Exception ex) { if (lastErrorCode != 1) { form.SetTrayIconError("sill2-client: 2 - The MySQL connection could not be created.\n" + ex.Message); lastErrorCode = 2; } Verification.connectionLost = true; } return(connection); }
public static void Start(sill2_client_form f) { // Get a form instance form = f; // Verify if another sill2.exe is running if (IsProcessOpen("sill2-client") > 1) { Environment.Exit(0); } // Execute the initial config of the MySQL Coonection if (DBConnection.config(form)) { // Send date/time NOW() to database infinitely using (dbConn = DBConnection.create(form)) { // Executes the initial procedures when user logs on Update(true); while (true) { try { while (dbConn.State != ConnectionState.Open) { // Delay for 10 seconds... System.Threading.Thread.Sleep(10000); // Checks if connection is closed if (dbConn.State == ConnectionState.Closed) { dbConn.Open(); } else if (dbConn.State == ConnectionState.Broken) { dbConn.Close(); dbConn.Open(); } } // Verifies in database if the user is already logged string sqlVerifiesLog = "SELECT `user`, `computer` FROM `sill2`.`logged` WHERE `user`='" + userLogin + "' AND `computer`='" + computerName + "';"; using (MySqlCommand verifiesLog = new MySqlCommand(sqlVerifiesLog, dbConn)) { using (MySqlDataReader loggedResult = verifiesLog.ExecuteReader()) { // If user is already logged... if (loggedResult.HasRows) { // Close DataReader loggedResult.Close(); // Update the logged current time string logUpQuery = "UPDATE `sill2`.`logged` SET `lognow`=NOW() WHERE `user`='" + userLogin + "' AND `computer`='" + computerName + "';"; using (MySqlCommand logUpComm = new MySqlCommand(logUpQuery, dbConn)) { // Executes the update query if (logUpComm.ExecuteNonQuery() > 0) { connectionLost = false; // Executes the initial procedures when user logs on Update(false); } else { // Do all the tests if the user is logged on another computers TestUserLogged(false); } } } else { // Close DataReader loggedResult.Close(); // Executes the initial procedures when user logs on Update(false); } } // Close the database connection dbConn.Close(); } // Delay for 3 seconds... System.Threading.Thread.Sleep(3000); } catch (Exception ex) { connectionLost = true; //MessageBox.Show("sill2-client: Error while trying to update the actual time.\n" + ex.Message); if (lastErrorCode != 6) { form.SetTrayIconError("sill2-client: 6 - Erro enquanto tentava atualizar a hora atual.\n" + ex.Message); lastErrorCode = 6; // saveException(ex.Message + "\n" + ex.StackTrace); } } } } } else { connectionLost = true; Environment.Exit(0); } }
/* Executes the initial procudures when user logs on. */ private static void Update(bool firstLoop) { /* Send logon time to database */ // Open MySQL connection try { // Checks if connection is closed if (dbConn.State == ConnectionState.Closed) { dbConn.Open(); } else if (dbConn.State == ConnectionState.Broken) { dbConn.Close(); dbConn.Open(); } } catch (Exception ex) { connectionLost = true; if (lastErrorCode != 8) { form.SetTrayIconError("sill2-client: 8 - Não pôde ser estabelecida conexão com o banco de dados.\n" + ex.Message); lastErrorCode = 8; // saveException(ex.Message + "\n" + ex.StackTrace); } } // Verifies if the connection is open if (dbConn.State == ConnectionState.Open) { // Do all the tests if the user is logged on another computers TestUserLogged(firstLoop); // Insert the logon in the database try { string sqlSameCompLog = "SELECT `user`, `computer` FROM `sill2`.`logged` WHERE `user`='" + userLogin + "' AND `computer`='" + computerName + "';"; using (MySqlCommand sameCompLog = new MySqlCommand(sqlSameCompLog, dbConn)) using (MySqlDataReader sameCompLogResult = sameCompLog.ExecuteReader()) { string logOnQuery = ""; // If user is still logged on the same computer as before if (sameCompLogResult.HasRows) { logOnQuery = "UPDATE `sill2`.`logged` SET `logon`=NOW(), `lognow`=NOW() WHERE `user`='" + userLogin + "' AND `computer`='" + computerName + "';"; } else if (firstLoop || connectionLost) // Connection lost { logOnQuery = "INSERT INTO `sill2`.`logged`(`computer`, `user`, `logon`, `lognow`) VALUES('" + computerName + "','" + userLogin + "', NOW(), NOW());"; } else // User removed from Logged list { logoff(true); } // Close DataReader sameCompLogResult.Close(); // Executes the logon query using (MySqlCommand logOnComm = new MySqlCommand(logOnQuery, dbConn)) { if (logOnComm.ExecuteNonQuery() > 0) { connectionLost = false; } } } } catch (Exception ex) { connectionLost = true; if (lastErrorCode != 10) { form.SetTrayIconError("sill2-client: 10 - Erro enquanto tentava executar a query de LogOn.\n" + ex.Message); lastErrorCode = 10; // saveException(ex.Message + "\n" + ex.StackTrace); } } } }