public Watcher(String path, Client client) { this.client = client; this.path = path; lastRead = DateTime.MinValue; this.Run(); }
private static void Main(string[] args) { var sw = new Stopwatch(); sw.Start(); var timer = new Timer(obj => { Console.WriteLine(sw.Elapsed); }, null, TimeSpan.Zero, TimeSpan.FromSeconds(5)); var c = new Client(); var t = c.Get(); var r = t.Result; Console.Write("Press ENTER to exit."); Console.ReadLine(); }
private void register(String address, String port, String user, String pwd1, String pwd2) { if (address.Equals(String.Empty) || port.Equals(String.Empty) || user.Equals(String.Empty) || pwd1.Equals(String.Empty) || pwd2.Equals(String.Empty) || address == null || port == null || user == null || pwd1 == null || pwd2 == null) return; if (!(pwd1.Equals(pwd2))) { String msg = "Create account"; String title = "Registration Failed"; String msgBanner = "The passwords inserted are different"; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_regBanner), msg, title, msgBanner); return; } if (user.Length > 16) { String msg = "Create account"; String title = "Registration Failed"; String msgBanner = "The username must be up to 16 characters"; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_regBanner), msg, title, msgBanner); return; } try { client = new Client(); Int32 portInt = Int32.Parse(port); String msg = "Trying to connect to the server . . ."; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegate(updateUI_progressBarReg), msg); TcpClient tcpClient = new TcpClient(); tcpClient.Connect(address, portInt); tcpClient.ReceiveTimeout = Networking.TIME_OUT_SHORT; tcpClient.SendTimeout = Networking.TIME_OUT_SHORT; client.TcpClient = tcpClient; if (client.keyExchangeTcpClient()) { int ret = client.registrationTcpClient(user, pwd1); msg = "Create account"; String title = "Registration result"; //String bannerMsg = "" + ret; String bannerMsg=""; switch (ret) { case 1: bannerMsg="Success"; break; case 0: bannerMsg = "Registration failed. User already exists"; break; case -1: bannerMsg = "Registration failed due to technical reason"; break; default: bannerMsg = "" + ret; break; } this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_regBanner), msg, title, bannerMsg); } } catch (SocketException se) { String msg = "Create Account"; StreamWriter sw = new StreamWriter("client_log.txt", true); sw.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")); sw.WriteLine(" ***Fatal Error*** " + se.Message); sw.WriteLine(se.StackTrace); sw.Close(); this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_regBanner), msg, "Server Unreachable", se.Message); } catch (Exception exc) { String msg = "Create Account"; StreamWriter sw = new StreamWriter("client_log.txt", true); sw.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")); sw.WriteLine(" ***Fatal Error*** " + exc.Message); sw.WriteLine(exc.StackTrace); sw.Close(); this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_regBanner), msg, "An error occurred", exc.Message); } finally { if (client.TcpClient != null) { client.TcpClient.Close(); } } }
private void connect(String address, String port, String username, String pwd) { if (address.Equals(String.Empty) || port.Equals(String.Empty) || username.Equals(String.Empty) || pwd.Equals(String.Empty) || address == null || port == null || username == null || pwd == null) return; if (connected) { connected = false; client.closeConnectionTcpClient(); if (sync_worker.IsBusy) sync_worker.CancelAsync(); sync_worker.Dispose(); if (restore_worker.IsBusy) restore_worker.CancelAsync(); restore_worker.Dispose(); if (watcher != null) watcher.Stop(); String msg = "Log in to the remote server"; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegate(updateUI), msg); } else { try { client = new Client(); Int32 portInt = Int32.Parse(port); connected = true; String msg = "Trying to connect to the server . . ."; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegate(updateUI_progressBar), msg); client.TcpClient = new TcpClient(); TcpClient tcpClient = client.TcpClient; tcpClient.Connect(address, portInt); tcpClient.ReceiveTimeout = Networking.TIME_OUT_SHORT; tcpClient.SendTimeout = Networking.TIME_OUT_SHORT; if (client.keyExchangeTcpClient()) { Int64 sessionId = client.authenticationTcpClient(username, pwd); if (sessionId <= 0) { if (sessionId == -1) { //network or other error msg = "Log in to the remote server"; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, "Server Unreachable", "A network error occurred"); } if (sessionId == -2 || sessionId == -3) { //username or password not correct msg = "Log in to the remote server"; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, "Log in incorrect", "The combination username/password you provided is incorrect"); } } else { client.Server = new IPEndPoint(IPAddress.Parse(address), portInt); client.UserId = username; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => { this.Grid_initial.Visibility = Visibility.Collapsed; this.Grid_logged.Visibility = Visibility.Visible; this.file_grid.Visibility = Visibility.Hidden; this.dir_grid.Visibility = Visibility.Hidden; this.fileSnap_grid.Visibility = Visibility.Hidden; this.Back_button.Visibility = Visibility.Hidden; this.Back_button_2.Visibility = Visibility.Collapsed; this.Refresh_button.Visibility = Visibility.Hidden; this.Home_button.Visibility = Visibility.Hidden; this.Sync_init_button.Visibility = Visibility.Visible; this.Restore_init_button.Visibility = Visibility.Visible; this.Label_log.Visibility = Visibility.Visible; this.Label_log.Content = "Welcome back, " + client.UserId + "! Choose one of the following options"; })); } } } catch (SocketException se) { connected = false; if (client.TcpClient != null && client.TcpClient.Connected) client.TcpClient.Close(); String msg = "Log in to the remote server"; StreamWriter sw = new StreamWriter("client_log.txt", true); sw.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")); sw.WriteLine(" ***Fatal Error*** " + se.Message); sw.WriteLine(se.StackTrace); sw.Close(); this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, "Server Unreachable", se.Message); } catch (Exception exc) { connected = false; if (client.TcpClient != null && client.TcpClient.Connected) client.TcpClient.Close(); String msg = "Log in to the remote server"; StreamWriter sw = new StreamWriter("client_log.txt", true); sw.Write(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")); sw.WriteLine(" ***Fatal Error*** " + exc.Message); sw.WriteLine(exc.StackTrace); sw.Close(); this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDelegateAsync(updateUI_banner), msg, "An error occurred", exc.Message); } } }