private async void ManageUserLogin() { try { var changedFilesList = new ThreadSafeList <CustomFileHash>(); var commandResponseBuffer = new BufferBlock <byte[]>(); var tcpClient = EstablishTcpCommunication(); _tcpCommunication = new TcpCommunication(tcpClient, commandResponseBuffer, changedFilesList); var connectionBytes = Encoding.UTF8.GetBytes(_loginViewModel.UserName + ":" + _loginViewModel.UserPassword + ":"); _tcpCommunication.SendCommand(connectionBytes, 0, connectionBytes.Length); await _tcpCommunication.CommandResponseBuffer.OutputAvailableAsync(); var response = _tcpCommunication.CommandResponseBuffer.Receive(); var message = Encoding.UTF8.GetString(response).Split(':'); if (message[0].Equals("Error")) { _tcpCommunication.Dispose(); Messenger.Default.Send(new LoginFailedMsg()); MessageBox.Show(message[1], @"Datele de autentificare sunt invalide.", MessageBoxButton.OK, MessageBoxImage.Warning); } else { RaisePropertyChanged(() => ConnectedUserName); Logger.InitLogger(Helper.TraceEnabled); Logger.WriteInitialSyncBreakLine(); _commandHandler = new CommandHandler(_tcpCommunication); var filesForInitialSync = await DetermineFilesForInitialSync(); _syncProcessor = new SyncProcessor(_commandHandler, changedFilesList); filesForInitialSync.ForEach(_syncProcessor.AddChangedFile); SetupLayout(UiState.LoggedIn); Messenger.Default.Send(new LoginSuccededMsg()); await _syncProcessor.ChangedFileManager(); Logger.WriteSyncBreakLine(); changedFilesList.OnAdd += changedFilesList_OnAdd; _myFsWatcher = new MyFsWatcher(Helper.SyncLocation, _syncProcessor); } } catch (FormatException ex) { LoginVisibility = Visibility.Visible; Messenger.Default.Send(new LoginFailedMsg()); var str = "Message: " + ex.Message + "\nSource: " + ex.Source + "\nStackTrace: " + ex.StackTrace; MessageBox.Show(str, @"Sincronizator de fișiere - Excepție de formatare"); } catch (SocketException ex) { LoginVisibility = Visibility.Visible; Messenger.Default.Send(new LoginFailedMsg()); var str = "Message: " + ex.Message + "\nSource: " + ex.Source + "\nStackTrace: " + ex.StackTrace; MessageBox.Show(str, @"Sincronizator de fișiere - Excepție de socket"); } catch (Exception ex) { LoginVisibility = Visibility.Visible; Messenger.Default.Send(new LoginFailedMsg()); var str = "Message: " + ex.Message + "\nSource: " + ex.Source + "\nException Type: " + ex.GetType() + "\nStackTrace: " + ex.StackTrace; MessageBox.Show(str, @"Sincronizator de fișiere - Excepție"); } }
private async void btnConnectAuto_Click(object sender, EventArgs e) { try { Context.InAutoMode = true; if (Helper.TraceEnabled) { Logger.InitLogger(true); _loggerTask = Task.Factory.StartNew(LoggerAction); } else { Logger.InitLogger(); _loggerTask = null; } var changedFilesList = new ThreadSafeList <CustomFileHash>(); var commandResponseBuffer = new BufferBlock <byte[]>(); var tcpClient = new TcpClient(txtHostAuto.Text, Int32.Parse(txtPortAuto.Text)); _tcpCommunication = new TcpCommunication(tcpClient, commandResponseBuffer, changedFilesList); var connectionBytes = Encoding.UTF8.GetBytes(txtUsername.Text + ":" + txtUserpassword.Text + ":"); _tcpCommunication.SendCommand(connectionBytes, 0, connectionBytes.Length); await _tcpCommunication.CommandResponseBuffer.OutputAvailableAsync(); var response = _tcpCommunication.CommandResponseBuffer.Receive(); var message = Encoding.UTF8.GetString(response).Split(':'); if (message[0].Equals("Error")) { MessageBox.Show(message[1], @"Invalid user or password"); _tcpCommunication.Dispose(); } else { SwitchAutoUiState(UiConnectedState); Context.CurrentUser = txtUsername.Text; _commandHandler = new CommandHandler(_tcpCommunication); Logger.WriteInitialSyncBreakLine(); var filesForInitialSync = await DetermineFilesForInitialSync(); _syncProcessor = new SyncProcessor(_commandHandler, changedFilesList); filesForInitialSync.ForEach(_syncProcessor.AddChangedFile); await _syncProcessor.ChangedFileManager(); Logger.WriteSyncBreakLine(); changedFilesList.OnAdd += changedFilesList_OnAdd; _myFsWatcher = new MyFsWatcher(txtDefaultFolderAuto.Text, _syncProcessor); } } catch (FormatException fex) { var str = "Message: " + fex.Message + "\nSource: " + fex.Source + "\nStackTrace: " + fex.StackTrace; MessageBox.Show(str, @"Syncroniser - FormatException"); SwitchAutoUiState(false); MessageBox.Show(fex.Message); } catch (Exception ex) { var str = "Message: " + ex.Message + "\nSource: " + ex.Source + "\nStackTrace: " + ex.StackTrace; MessageBox.Show(str, @"Syncroniser - Exception"); SwitchAutoUiState(false); MessageBox.Show(ex.Message); } }