From e53873276f02e69d3c752fe94c915dc207bf9011 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 17 Dec 2014 17:20:08 +0800 Subject: [PATCH] translate Microsoft language into human language --- shadowsocks-csharp/Controller/PACServer.cs | 11 +- .../Controller/ShadowsocksController.cs | 100 ++++++++++++------ shadowsocks-csharp/Program.cs | 4 +- shadowsocks-csharp/View/MenuViewController.cs | 6 ++ 4 files changed, 86 insertions(+), 35 deletions(-) diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index b64d3ce0..0b2934b4 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -58,8 +58,11 @@ namespace Shadowsocks.Controller public void Stop() { - _listener.Close(); - _listener = null; + if (_listener != null) + { + _listener.Close(); + _listener = null; + } } public string TouchPACFile() @@ -107,6 +110,10 @@ namespace Shadowsocks.Controller new AsyncCallback(AcceptCallback), listener); } + catch (ObjectDisposedException) + { + // do nothing + } catch (Exception e) { Logging.LogUsefulException(e); diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 464cc131..e2a761ff 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Text; using System.Threading; +using System.Net.Sockets; namespace Shadowsocks.Controller { @@ -37,33 +38,34 @@ namespace Shadowsocks.Controller // when user clicked Edit PAC, and PAC file has already created public event EventHandler PACFileReadyToOpen; + public event ErrorEventHandler Errored; + public ShadowsocksController() { _config = Configuration.Load(); - polipoRunner = new PolipoRunner(); - polipoRunner.Start(_config); - local = new Local(_config); - try - { - local.Start(); - } - catch (Exception e) - { - Console.WriteLine(e); - } - try - { - pacServer = new PACServer(); - pacServer.PACFileChanged += pacServer_PACFileChanged; - pacServer.Start(_config); - } - catch (Exception e) + } + + public void Start() + { + Reload(); + } + + protected void ReportError(Exception e) + { + if (Errored != null) { - Console.WriteLine(e); + // translate Microsoft language into human language + // i.e. 以一种访问权限不允许的方式做了一个访问套接字的尝试 => Port is already used + if (e is SocketException) + { + SocketException se = (SocketException)e; + if (se.SocketErrorCode == SocketError.AccessDenied) + { + e = new Exception("Port is already used", e); + } + } + Errored(this, new ErrorEventArgs(e)); } - - UpdateSystemProxy(); - StartReleasingMemory(); } public Server GetCurrentServer() @@ -128,8 +130,14 @@ namespace Shadowsocks.Controller return; } stopped = true; - local.Stop(); - polipoRunner.Stop(); + if (local != null) + { + local.Stop(); + } + if (polipoRunner != null) + { + polipoRunner.Stop(); + } if (_config.enabled) { SystemProxy.Disable(); @@ -153,36 +161,64 @@ namespace Shadowsocks.Controller return "ss://" + base64; } - - protected void SaveConfig(Configuration newConfig) + protected void Reload() { - Configuration.Save(newConfig); // some logic in configuration updated the config when saving, we need to read it again _config = Configuration.Load(); + if (polipoRunner == null) + { + polipoRunner = new PolipoRunner(); + } + if (pacServer == null) + { + pacServer = new PACServer(); + pacServer.PACFileChanged += pacServer_PACFileChanged; + } + pacServer.Stop(); - local.Stop(); + + if (local != null) + { + local.Stop(); + } // don't put polipoRunner.Start() before pacServer.Stop() // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1 // though UseShellExecute is set to true now // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open polipoRunner.Stop(); - polipoRunner.Start(_config); + try + { + polipoRunner.Start(_config); - local = new Local(_config); - local.Start(); - pacServer.Start(_config); + local = new Local(_config); + local.Start(); + pacServer.Start(_config); + } + catch (Exception e) + { + Logging.LogUsefulException(e); + ReportError(e); + } if (ConfigChanged != null) { ConfigChanged(this, new EventArgs()); } + UpdateSystemProxy(); Util.Util.ReleaseMemory(); } + protected void SaveConfig(Configuration newConfig) + { + Configuration.Save(newConfig); + Reload(); + } + + private void UpdateSystemProxy() { if (_config.enabled) diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 6b15ea9c..716e0df7 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -41,7 +41,9 @@ namespace Shadowsocks ShadowsocksController controller = new ShadowsocksController(); MenuViewController viewController = new MenuViewController(controller); - Util.Util.ReleaseMemory(); + + controller.Start(); + Application.Run(); } } diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index a91e48f7..0d46ba5b 100755 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -53,6 +53,7 @@ namespace Shadowsocks.View controller.PACFileReadyToOpen += controller_PACFileReadyToOpen; controller.ShareOverLANStatusChanged += controller_ShareOverLANStatusChanged; controller.EnableGlobalChanged += controller_EnableGlobalChanged; + controller.Errored += controller_Errored; this.updateChecker = new UpdateChecker(); updateChecker.NewVersionFound += updateChecker_NewVersionFound; @@ -68,6 +69,11 @@ namespace Shadowsocks.View } } + void controller_Errored(object sender, System.IO.ErrorEventArgs e) + { + MessageBox.Show(e.GetException().ToString(), "Error: " + e.GetException().Message); + } + private void LoadTrayIcon() { int dpi;