From fab822c8bbaa9e5d5f6008244586e6163c7f95d6 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Mon, 15 Dec 2014 10:36:50 +0800 Subject: [PATCH] fix #66 --- shadowsocks-csharp/Controller/Local.cs | 19 ++++++++++++++----- shadowsocks-csharp/Controller/PACServer.cs | 20 +++++++++++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index 15649b40..1a749b1f 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -66,16 +66,12 @@ namespace Shadowsocks.Controller public void AcceptCallback(IAsyncResult ar) { + Socket listener = (Socket)ar.AsyncState; try { - Socket listener = (Socket)ar.AsyncState; Socket conn = listener.EndAccept(ar); conn.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); - listener.BeginAccept( - new AsyncCallback(AcceptCallback), - listener); - Handler handler = new Handler(); handler.connection = conn; handler.encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password); @@ -87,6 +83,19 @@ namespace Shadowsocks.Controller { //Console.WriteLine(e.Message); } + finally + { + try + { + listener.BeginAccept( + new AsyncCallback(AcceptCallback), + listener); + } + catch + { + //Console.WriteLine(e.Message); + } + } } } diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index a71fe3de..b64d3ce0 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -78,20 +78,17 @@ namespace Shadowsocks.Controller public void AcceptCallback(IAsyncResult ar) { + Socket listener = (Socket)ar.AsyncState; try { - Socket listener = (Socket)ar.AsyncState; Socket conn = listener.EndAccept(ar); - listener.BeginAccept( - new AsyncCallback(AcceptCallback), - listener); byte[] buf = new byte[2048]; object[] state = new object[] { conn, buf }; - + conn.BeginReceive(buf, 0, 1024, 0, new AsyncCallback(ReceiveCallback), state); } @@ -102,6 +99,19 @@ namespace Shadowsocks.Controller { Console.WriteLine(e); } + finally + { + try + { + listener.BeginAccept( + new AsyncCallback(AcceptCallback), + listener); + } + catch (Exception e) + { + Logging.LogUsefulException(e); + } + } } private string GetPACContent()