void GameMain_Exiting(object sender, System.EventArgs e) { if (cue != null && cue.IsPlaying) { cue.Stop(AudioStopOptions.Immediate); } while (gameHandler != null && gameHandler.receiveThread != null && gameHandler.receiveThread.IsAlive) { if (gameHandler.currentState() == GameState.none || gameHandler.currentState() == GameState.map_warp) { gameHandler.currentState(GameState.map_backtologin); } gameHandler.receiveThread.Abort(); } long startExit = DateTime.Now.Ticks; network.Send("LOGOUT:0;"); bool isLoggedOut = false; while (!isLoggedOut && network.isConnected()) { String responseLogoutMsg = ""; while (responseLogoutMsg.Length <= 0) { responseLogoutMsg = network.Receive(); } String[] LogoutLine = responseLogoutMsg.Split(';'); for (int l = 0; l < LogoutLine.Length; ++l) { String[] LogoutMsg = LogoutLine[l].Split(':'); if (LogoutMsg[0].Equals("LOGOUT") && LogoutMsg.Length == 2) { isLoggedOut = true; break; } } } network.Close(); }
private void loginThread() { String responseMsg = ""; String statusMsg = ""; String idMsg = ""; try { // Try connect to server if (!network.isConnected()) { network.Connect(serverConfig.getIP(), serverConfig.getPort()); } // Starting ping... // Send login message network.Send("LOGIN:"******" " + password.Text + ";"); // Receive message while (responseMsg.Length <= 0) { responseMsg = network.Receive(); } } catch (Exception e) { // Has any error come here Debug.WriteLine(e.ToString()); username.Enabled = true; password.Enabled = true; loginButton.Enabled = true; game.messageDialog.setTitle("ERROR!!"); game.messageDialog.setMessage("Can't connect to server."); game.messageDialog.Visible = true; game.messageDialog.CloseButtonVisible = true; LoggedUserID = 0; state = GameState.none; } // Split message from end of line String[] line = responseMsg.Split(';'); for (int i = 0; i < line.Length; ++i) { // If has message // Split message name(LOGIN) and value({STATUS}|{LOGINID}) // LOGIN:{STATUS}|{LOGINID}; String[] msg = line[i].Split(':'); if (msg[0].Equals("LOGIN") && msg.Length == 2) { // Split message value String[] value = msg[1].Split(' '); if (value.Length == 2) { statusMsg = value[0]; idMsg = value[1]; } if (statusMsg.Equals("OK") && !idMsg.Equals("0")) { // if status is OK try { username.Enabled = true; password.Enabled = true; loginButton.Enabled = true; game.hideMessageDialog(); LoggedUserID = Convert.ToInt32(idMsg); state = GameState.login_loggedin; } catch (Exception e) { // Has any error come here // I was expect that NumberFormatException can occur Debug.WriteLine(e.ToString()); username.Enabled = true; password.Enabled = true; loginButton.Enabled = true; game.messageDialog.setTitle("ERROR!!"); game.messageDialog.setMessage("Wrong username or password."); game.messageDialog.Visible = true; game.messageDialog.CloseButtonVisible = true; LoggedUserID = 0; state = GameState.none; } } else { // if status isn't OK network.Send("LOGOUT:0;"); bool isLoggedOut = false; while (!isLoggedOut && network.isConnected()) { String responseLogoutMsg = ""; while (responseLogoutMsg.Length <= 0) { responseLogoutMsg = network.Receive(); } String[] LogoutLine = responseLogoutMsg.Split(';'); for (int l = 0; l < LogoutLine.Length; ++l) { String[] LogoutMsg = LogoutLine[l].Split(':'); if (LogoutMsg[0].Equals("LOGOUT") && LogoutMsg.Length == 2) { isLoggedOut = true; break; } } } network.Close(); username.Enabled = true; password.Enabled = true; loginButton.Enabled = true; if (idMsg.Equals(0)) { game.messageDialog.setTitle("ERROR!!"); game.messageDialog.setMessage("Wrong username or password."); game.messageDialog.Visible = true; game.messageDialog.CloseButtonVisible = true; } else { game.messageDialog.setTitle("ERROR!!"); game.messageDialog.setMessage("This user is already login."); game.messageDialog.Visible = true; game.messageDialog.CloseButtonVisible = true; } LoggedUserID = 0; break; } } } }