diff --git a/Example/Example.pidb b/Example/Example.pidb index b582a647..3d04d80d 100644 Binary files a/Example/Example.pidb and b/Example/Example.pidb differ diff --git a/Example/bin/Debug/example.exe b/Example/bin/Debug/example.exe index c1a95eef..5e9241f9 100755 Binary files a/Example/bin/Debug/example.exe and b/Example/bin/Debug/example.exe differ diff --git a/Example/bin/Debug/example.exe.mdb b/Example/bin/Debug/example.exe.mdb index 98579731..9be971b0 100644 Binary files a/Example/bin/Debug/example.exe.mdb and b/Example/bin/Debug/example.exe.mdb differ diff --git a/Example/bin/Debug/websocket-sharp.dll b/Example/bin/Debug/websocket-sharp.dll index 2c81ac0e..543e4cf7 100755 Binary files a/Example/bin/Debug/websocket-sharp.dll and b/Example/bin/Debug/websocket-sharp.dll differ diff --git a/Example/bin/Debug/websocket-sharp.dll.mdb b/Example/bin/Debug/websocket-sharp.dll.mdb index f0adb074..77552c37 100644 Binary files a/Example/bin/Debug/websocket-sharp.dll.mdb and b/Example/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index e186c51d..d5ed725c 100755 Binary files a/Example/bin/Debug_Ubuntu/example.exe and b/Example/bin/Debug_Ubuntu/example.exe differ diff --git a/Example/bin/Debug_Ubuntu/example.exe.mdb b/Example/bin/Debug_Ubuntu/example.exe.mdb index d9d98ab3..0df28e0f 100644 Binary files a/Example/bin/Debug_Ubuntu/example.exe.mdb and b/Example/bin/Debug_Ubuntu/example.exe.mdb differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll b/Example/bin/Debug_Ubuntu/websocket-sharp.dll index b9830e4a..f06b8384 100755 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index c2dc67bc..672db19b 100644 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Release/example.exe b/Example/bin/Release/example.exe index f119b36a..eea4ca69 100755 Binary files a/Example/bin/Release/example.exe and b/Example/bin/Release/example.exe differ diff --git a/Example/bin/Release/websocket-sharp.dll b/Example/bin/Release/websocket-sharp.dll index e96204cf..7f0d1d96 100755 Binary files a/Example/bin/Release/websocket-sharp.dll and b/Example/bin/Release/websocket-sharp.dll differ diff --git a/Example/bin/Release_Ubuntu/example.exe b/Example/bin/Release_Ubuntu/example.exe index e3ea75df..fd423595 100755 Binary files a/Example/bin/Release_Ubuntu/example.exe and b/Example/bin/Release_Ubuntu/example.exe differ diff --git a/Example/bin/Release_Ubuntu/websocket-sharp.dll b/Example/bin/Release_Ubuntu/websocket-sharp.dll index f5d4ff6c..5f94f3a7 100755 Binary files a/Example/bin/Release_Ubuntu/websocket-sharp.dll and b/Example/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/example1.exe b/Example1/bin/Debug/example1.exe index 95a297e5..7dc0e25a 100755 Binary files a/Example1/bin/Debug/example1.exe and b/Example1/bin/Debug/example1.exe differ diff --git a/Example1/bin/Debug/example1.exe.mdb b/Example1/bin/Debug/example1.exe.mdb index f0aae89b..f80ee0f5 100644 Binary files a/Example1/bin/Debug/example1.exe.mdb and b/Example1/bin/Debug/example1.exe.mdb differ diff --git a/Example1/bin/Debug/websocket-sharp.dll b/Example1/bin/Debug/websocket-sharp.dll index 2c81ac0e..543e4cf7 100755 Binary files a/Example1/bin/Debug/websocket-sharp.dll and b/Example1/bin/Debug/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/websocket-sharp.dll.mdb b/Example1/bin/Debug/websocket-sharp.dll.mdb index f0adb074..77552c37 100644 Binary files a/Example1/bin/Debug/websocket-sharp.dll.mdb and b/Example1/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index 13febc90..d83f214e 100755 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe and b/Example1/bin/Debug_Ubuntu/example1.exe differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe.mdb b/Example1/bin/Debug_Ubuntu/example1.exe.mdb index 7879bae7..2d8b8c1a 100644 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe.mdb and b/Example1/bin/Debug_Ubuntu/example1.exe.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll index b9830e4a..f06b8384 100755 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index c2dc67bc..672db19b 100644 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Release/example1.exe b/Example1/bin/Release/example1.exe index 97d21b47..4359d271 100755 Binary files a/Example1/bin/Release/example1.exe and b/Example1/bin/Release/example1.exe differ diff --git a/Example1/bin/Release/websocket-sharp.dll b/Example1/bin/Release/websocket-sharp.dll index e96204cf..7f0d1d96 100755 Binary files a/Example1/bin/Release/websocket-sharp.dll and b/Example1/bin/Release/websocket-sharp.dll differ diff --git a/Example1/bin/Release_Ubuntu/example1.exe b/Example1/bin/Release_Ubuntu/example1.exe index 34e83457..b76383a6 100755 Binary files a/Example1/bin/Release_Ubuntu/example1.exe and b/Example1/bin/Release_Ubuntu/example1.exe differ diff --git a/Example1/bin/Release_Ubuntu/websocket-sharp.dll b/Example1/bin/Release_Ubuntu/websocket-sharp.dll index f5d4ff6c..5f94f3a7 100755 Binary files a/Example1/bin/Release_Ubuntu/websocket-sharp.dll and b/Example1/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/Chat.cs b/Example2/Chat.cs index 7199b8f0..fb0c13ca 100644 --- a/Example2/Chat.cs +++ b/Example2/Chat.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using WebSocketSharp; using WebSocketSharp.Server; @@ -6,8 +7,7 @@ namespace Example2 { public class Chat : WebSocketService { - private static object _forNum = new object(); - private static uint _num = 0; + private static int _num = 0; private string _name; @@ -18,12 +18,9 @@ namespace Example2 { : "anon#" + getNum(); } - private uint getNum() + private int getNum() { - lock (_forNum) - { - return ++_num; - } + return Interlocked.Increment(ref _num); } protected override void OnOpen(object sender, EventArgs e) diff --git a/Example2/Example2.pidb b/Example2/Example2.pidb index b6060cc1..ecc3a5f8 100644 Binary files a/Example2/Example2.pidb and b/Example2/Example2.pidb differ diff --git a/Example2/Program.cs b/Example2/Program.cs index d2aad13b..8438dcdd 100644 --- a/Example2/Program.cs +++ b/Example2/Program.cs @@ -20,6 +20,7 @@ namespace Example2 //var wssv = new WebSocketServiceHost(4649); //var wssv = new WebSocketServiceHost(4649, "/Chat"); //var wssv = new WebSocketServiceHost(4649, "/チャット"); + //wssv.Sweeped = false; // Stop the Sweep inactive session Timer. wssv.Start(); Console.WriteLine( @@ -27,12 +28,13 @@ namespace Example2 wssv.Uri, wssv.Address, wssv.Port); */ - // Multi services server + /// Multi services server var wssv = new WebSocketServer(4649); wssv.AddService("/Echo"); wssv.AddService("/エコー"); wssv.AddService("/Chat"); wssv.AddService("/チャット"); + //wssv.Sweeped = false; // Must be set after any AddService methods done. wssv.Start(); Console.WriteLine( diff --git a/Example2/bin/Debug/example2.exe b/Example2/bin/Debug/example2.exe index 2fba62f8..32826dfa 100755 Binary files a/Example2/bin/Debug/example2.exe and b/Example2/bin/Debug/example2.exe differ diff --git a/Example2/bin/Debug/example2.exe.mdb b/Example2/bin/Debug/example2.exe.mdb index 8836aa84..9399c662 100644 Binary files a/Example2/bin/Debug/example2.exe.mdb and b/Example2/bin/Debug/example2.exe.mdb differ diff --git a/Example2/bin/Debug/websocket-sharp.dll b/Example2/bin/Debug/websocket-sharp.dll index 2c81ac0e..543e4cf7 100755 Binary files a/Example2/bin/Debug/websocket-sharp.dll and b/Example2/bin/Debug/websocket-sharp.dll differ diff --git a/Example2/bin/Debug/websocket-sharp.dll.mdb b/Example2/bin/Debug/websocket-sharp.dll.mdb index f0adb074..77552c37 100644 Binary files a/Example2/bin/Debug/websocket-sharp.dll.mdb and b/Example2/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index 63f1a58b..7039eba6 100755 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe and b/Example2/bin/Debug_Ubuntu/example2.exe differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe.mdb b/Example2/bin/Debug_Ubuntu/example2.exe.mdb index d70053a6..53dd27f3 100644 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe.mdb and b/Example2/bin/Debug_Ubuntu/example2.exe.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll index b9830e4a..f06b8384 100755 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index c2dc67bc..672db19b 100644 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Release/example2.exe b/Example2/bin/Release/example2.exe index 433a2a0b..504e5d07 100755 Binary files a/Example2/bin/Release/example2.exe and b/Example2/bin/Release/example2.exe differ diff --git a/Example2/bin/Release/websocket-sharp.dll b/Example2/bin/Release/websocket-sharp.dll index e96204cf..7f0d1d96 100755 Binary files a/Example2/bin/Release/websocket-sharp.dll and b/Example2/bin/Release/websocket-sharp.dll differ diff --git a/Example2/bin/Release_Ubuntu/example2.exe b/Example2/bin/Release_Ubuntu/example2.exe index fff2d150..0589fc35 100755 Binary files a/Example2/bin/Release_Ubuntu/example2.exe and b/Example2/bin/Release_Ubuntu/example2.exe differ diff --git a/Example2/bin/Release_Ubuntu/websocket-sharp.dll b/Example2/bin/Release_Ubuntu/websocket-sharp.dll index f5d4ff6c..5f94f3a7 100755 Binary files a/Example2/bin/Release_Ubuntu/websocket-sharp.dll and b/Example2/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/Chat.cs b/Example3/Chat.cs index aec3475a..2e96c16e 100644 --- a/Example3/Chat.cs +++ b/Example3/Chat.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using WebSocketSharp; using WebSocketSharp.Server; @@ -6,8 +7,7 @@ namespace Example3 { public class Chat : WebSocketService { - private static object _forNum = new object(); - private static uint _num = 0; + private static int _num = 0; private string _name; @@ -18,12 +18,9 @@ namespace Example3 { : "anon#" + getNum(); } - private uint getNum() + private int getNum() { - lock (_forNum) - { - return ++_num; - } + return Interlocked.Increment(ref _num); } protected override void OnOpen(object sender, EventArgs e) diff --git a/Example3/Example3.pidb b/Example3/Example3.pidb index e3b926b0..5a095f9c 100644 Binary files a/Example3/Example3.pidb and b/Example3/Example3.pidb differ diff --git a/Example3/Program.cs b/Example3/Program.cs index 0368abb1..7da77afd 100644 --- a/Example3/Program.cs +++ b/Example3/Program.cs @@ -14,6 +14,7 @@ namespace Example3 _httpsv = new HttpServer(4649); _httpsv.AddService("/Echo"); _httpsv.AddService("/Chat"); + //_httpsv.Sweeped = false; // Must be set after any AddService methods done. _httpsv.OnGet += (sender, e) => { diff --git a/Example3/bin/Debug/Example3.exe b/Example3/bin/Debug/Example3.exe index 92d8f12f..ce8daefa 100755 Binary files a/Example3/bin/Debug/Example3.exe and b/Example3/bin/Debug/Example3.exe differ diff --git a/Example3/bin/Debug/Example3.exe.mdb b/Example3/bin/Debug/Example3.exe.mdb index d78a5546..b91048f4 100644 Binary files a/Example3/bin/Debug/Example3.exe.mdb and b/Example3/bin/Debug/Example3.exe.mdb differ diff --git a/Example3/bin/Debug/websocket-sharp.dll b/Example3/bin/Debug/websocket-sharp.dll index 2c81ac0e..543e4cf7 100755 Binary files a/Example3/bin/Debug/websocket-sharp.dll and b/Example3/bin/Debug/websocket-sharp.dll differ diff --git a/Example3/bin/Debug/websocket-sharp.dll.mdb b/Example3/bin/Debug/websocket-sharp.dll.mdb index f0adb074..77552c37 100644 Binary files a/Example3/bin/Debug/websocket-sharp.dll.mdb and b/Example3/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe index b82ecdd8..567afa7d 100755 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe and b/Example3/bin/Debug_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb index b921cf5d..c11cafa9 100644 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb and b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll index b9830e4a..f06b8384 100755 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index c2dc67bc..672db19b 100644 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Release/Example3.exe b/Example3/bin/Release/Example3.exe index 7098e3cd..90c74882 100755 Binary files a/Example3/bin/Release/Example3.exe and b/Example3/bin/Release/Example3.exe differ diff --git a/Example3/bin/Release/websocket-sharp.dll b/Example3/bin/Release/websocket-sharp.dll index e96204cf..7f0d1d96 100755 Binary files a/Example3/bin/Release/websocket-sharp.dll and b/Example3/bin/Release/websocket-sharp.dll differ diff --git a/Example3/bin/Release_Ubuntu/Example3.exe b/Example3/bin/Release_Ubuntu/Example3.exe index 11695f83..783e67b0 100755 Binary files a/Example3/bin/Release_Ubuntu/Example3.exe and b/Example3/bin/Release_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Release_Ubuntu/websocket-sharp.dll b/Example3/bin/Release_Ubuntu/websocket-sharp.dll index f5d4ff6c..5f94f3a7 100755 Binary files a/Example3/bin/Release_Ubuntu/websocket-sharp.dll and b/Example3/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp.userprefs b/websocket-sharp.userprefs index a6d2e3e8..9b182064 100644 --- a/websocket-sharp.userprefs +++ b/websocket-sharp.userprefs @@ -1,11 +1,6 @@  - - - - - - + diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index 0640e351..b54c4535 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -63,12 +63,22 @@ namespace WebSocketSharp.Server { #endregion - #region Property + #region Properties public int Port { get { return _port; } } + public bool Sweeped { + get { + return _services.Sweeped; + } + + set { + _services.Sweeped = value; + } + } + #endregion #region Events diff --git a/websocket-sharp/Server/IServiceHost.cs b/websocket-sharp/Server/IServiceHost.cs index ba77772b..07d3b583 100644 --- a/websocket-sharp/Server/IServiceHost.cs +++ b/websocket-sharp/Server/IServiceHost.cs @@ -32,6 +32,7 @@ namespace WebSocketSharp.Server { public interface IServiceHost { + bool Sweeped { get; set; } void BindWebSocket(WebSocket socket); void Broadcast(string data); void Start(); diff --git a/websocket-sharp/Server/ServiceManager.cs b/websocket-sharp/Server/ServiceManager.cs index 5cf2f21f..4ca8e3de 100644 --- a/websocket-sharp/Server/ServiceManager.cs +++ b/websocket-sharp/Server/ServiceManager.cs @@ -36,6 +36,7 @@ namespace WebSocketSharp.Server { #region Field private Dictionary _services; + private bool _sweeped; #endregion @@ -44,11 +45,12 @@ namespace WebSocketSharp.Server { public ServiceManager() { _services = new Dictionary(); + _sweeped = true; } #endregion - #region Property + #region Properties public int Count { get { @@ -56,6 +58,21 @@ namespace WebSocketSharp.Server { } } + public bool Sweeped { + get { + return _sweeped; + } + + set { + if (value ^ _sweeped) + { + _sweeped = value; + foreach (var svcHost in _services.Values) + svcHost.Sweeped = value; + } + } + } + #endregion #region Public Methods diff --git a/websocket-sharp/Server/SessionManager.cs b/websocket-sharp/Server/SessionManager.cs index 0f702064..0cfaf816 100644 --- a/websocket-sharp/Server/SessionManager.cs +++ b/websocket-sharp/Server/SessionManager.cs @@ -28,6 +28,8 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Timers; using WebSocketSharp.Frame; namespace WebSocketSharp.Server { @@ -36,8 +38,11 @@ namespace WebSocketSharp.Server { #region Private Fields - private bool _isStopped; + private object _forSweep; + private volatile bool _isStopped; + private volatile bool _isSweeping; private Dictionary _sessions; + private Timer _sweepTimer; private object _syncRoot; #endregion @@ -46,15 +51,32 @@ namespace WebSocketSharp.Server { public SessionManager() { - _isStopped = false; - _sessions = new Dictionary(); - _syncRoot = new object(); + _forSweep = new object(); + _isStopped = false; + _isSweeping = false; + _sessions = new Dictionary(); + _sweepTimer = new Timer(30 * 1000); + _sweepTimer.Elapsed += (sender, e) => + { + Sweep(); + }; + _syncRoot = new object(); + + startSweepTimer(); } #endregion #region Properties + public IEnumerable ActiveID { + get { + return from result in Broadping(String.Empty) + where result.Value + select result.Key; + } + } + public int Count { get { lock (_syncRoot) @@ -64,6 +86,37 @@ namespace WebSocketSharp.Server { } } + public IEnumerable InactiveID { + get { + return from result in Broadping(String.Empty) + where !result.Value + select result.Key; + } + } + + public IEnumerable ID { + get { + lock (_syncRoot) + { + return _sessions.Keys; + } + } + } + + public bool Sweeped { + get { + return _sweepTimer.Enabled; + } + + set { + if (value && !_isStopped) + startSweepTimer(); + + if (!value) + stopSweepTimer(); + } + } + public object SyncRoot { get { return _syncRoot; @@ -82,11 +135,23 @@ namespace WebSocketSharp.Server { } } - private string getNewID() + private string createID() { return Guid.NewGuid().ToString("N"); } + private void startSweepTimer() + { + if (!Sweeped) + _sweepTimer.Start(); + } + + private void stopSweepTimer() + { + if (Sweeped) + _sweepTimer.Stop(); + } + #endregion #region Public Methods @@ -98,7 +163,7 @@ namespace WebSocketSharp.Server { if (_isStopped) return null; - var id = getNewID(); + var id = createID(); _sessions.Add(id, service); return id; @@ -110,7 +175,10 @@ namespace WebSocketSharp.Server { lock (_syncRoot) { foreach (var service in _sessions.Values) - service.SendAsync(data); + if (_isStopped || _isSweeping) + service.Send(data); + else + service.SendAsync(data); } } @@ -119,7 +187,10 @@ namespace WebSocketSharp.Server { lock (_syncRoot) { foreach (var service in _sessions.Values) - service.SendAsync(data); + if (_isStopped || _isSweeping) + service.Send(data); + else + service.SendAsync(data); } } @@ -132,21 +203,10 @@ namespace WebSocketSharp.Server { return result; } - public IEnumerable GetIDs() - { - lock (_syncRoot) - { - return _sessions.Keys; - } - } - public bool Remove(string id) { lock (_syncRoot) { - if (_isStopped) - return false; - return _sessions.Remove(id); } } @@ -166,16 +226,43 @@ namespace WebSocketSharp.Server { public void Stop(CloseStatusCode code, string reason) { + stopSweepTimer(); lock (_syncRoot) { if (_isStopped) return; _isStopped = true; - foreach (var service in _sessions.Values) + foreach (var service in copySessions().Values) service.Stop(code, reason); + } + } + + public void Sweep() + { + if (_isStopped || _isSweeping || Count == 0) + return; + + lock (_forSweep) + { + _isSweeping = true; + foreach (var id in InactiveID) + { + lock (_syncRoot) + { + if (_isStopped) + { + _isSweeping = false; + return; + } + + WebSocketService service; + if (TryGetByID(id, out service)) + service.Stop(CloseStatusCode.ABNORMAL, String.Empty); + } + } - _sessions.Clear(); + _isSweeping = false; } } diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index 25d3e2c9..8d753cc4 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -74,6 +74,20 @@ namespace WebSocketSharp.Server { #endregion + #region Property + + public bool Sweeped { + get { + return _services.Sweeped; + } + + set { + _services.Sweeped = value; + } + } + + #endregion + #region Private Method private void init() diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs index a4e1b4ec..455f2f91 100644 --- a/websocket-sharp/Server/WebSocketService.cs +++ b/websocket-sharp/Server/WebSocketService.cs @@ -47,9 +47,8 @@ namespace WebSocketSharp.Server { public WebSocketService() { - ID = String.Empty; - IsBound = false; - IsStopped = false; + ID = String.Empty; + IsBound = false; } #endregion @@ -72,9 +71,8 @@ namespace WebSocketSharp.Server { #region Public Properties - public string ID { get; private set; } - public bool IsBound { get; private set; } - public bool IsStopped { get; private set; } + public string ID { get; private set; } + public bool IsBound { get; private set; } #endregion @@ -89,8 +87,7 @@ namespace WebSocketSharp.Server { _socket.OnClose += (sender, e) => { - if (!IsStopped) - _sessions.Remove(ID); + _sessions.Remove(ID); }; } @@ -202,6 +199,7 @@ namespace WebSocketSharp.Server { { Send(data); }; + ThreadPool.QueueUserWorkItem(sendCb); } @@ -211,6 +209,7 @@ namespace WebSocketSharp.Server { { Send(data); }; + ThreadPool.QueueUserWorkItem(sendCb); } @@ -242,15 +241,22 @@ namespace WebSocketSharp.Server { public void Stop() { - Stop(CloseStatusCode.NORMAL, String.Empty); + if (!IsBound) + return; + + _socket.Close(); } public void Stop(CloseStatusCode code, string reason) { - if (!IsBound || IsStopped) + Stop((ushort)code, reason); + } + + public void Stop(ushort code, string reason) + { + if (!IsBound) return; - - IsStopped = true; + _socket.Close(code, reason); } diff --git a/websocket-sharp/Server/WebSocketServiceHost.cs b/websocket-sharp/Server/WebSocketServiceHost.cs index 4b6ff987..3be9fe72 100644 --- a/websocket-sharp/Server/WebSocketServiceHost.cs +++ b/websocket-sharp/Server/WebSocketServiceHost.cs @@ -79,7 +79,17 @@ namespace WebSocketSharp.Server { #endregion - #region Property + #region Properties + + public bool Sweeped { + get { + return _sessions.Sweeped; + } + + set { + _sessions.Sweeped = value; + } + } public Uri Uri { get { diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 95ab0aba..89d2e011 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -418,19 +418,19 @@ namespace WebSocketSharp { return; } - // Whether a close status code that must not be set for send is used ? - if (!canSendAsCloseFrame(data)) - { - onClose(new CloseEventArgs(data)); - return; - } - _readyState = WsState.CLOSING; } + // Whether a close status code that must not be set for send is used ? + if (!canSendAsCloseFrame(data)) + { + onClose(new CloseEventArgs(data)); + return; + } + closeHandshake(data); #if DEBUG - Console.WriteLine("WS: Info@close: Exit close method."); + Console.WriteLine("WS: Info@close: Exits close method."); #endif } @@ -451,7 +451,7 @@ namespace WebSocketSharp { var payloadData = new PayloadData(data.ToArray()); if (payloadData.Length > 125) { - var msg = "Close frame must have a payload length of 125 bytes or less."; + var msg = "A Close frame must have a payload length of 125 bytes or less."; onError(msg); return; } @@ -497,10 +497,7 @@ namespace WebSocketSharp { { var args = new CloseEventArgs(data); var frame = createFrame(Fin.FINAL, Opcode.CLOSE, data); - if (send(frame) && !Thread.CurrentThread.IsBackground) - if (!_exitMessageLoop.IsNull()) - _exitMessageLoop.WaitOne(5 * 1000); - + send(frame); onClose(args); } @@ -738,6 +735,10 @@ namespace WebSocketSharp { private void onClose(CloseEventArgs eventArgs) { + if (!Thread.CurrentThread.IsBackground) + if (!_exitMessageLoop.IsNull()) + _exitMessageLoop.WaitOne(5 * 1000); + if (closeConnection()) eventArgs.WasClean = true; @@ -772,7 +773,7 @@ namespace WebSocketSharp { var buffer = Encoding.UTF8.GetBytes(message); if (buffer.Length > 125) { - var msg = "Ping frame must have a payload length of 125 bytes or less."; + var msg = "A Ping frame must have a payload length of 125 bytes or less."; onError(msg); return false; } @@ -800,7 +801,7 @@ namespace WebSocketSharp { var frame = _wsStream.ReadFrame(); if (frame.IsNull()) { - var msg = "WebSocket data frame can not be read from network stream."; + var msg = "The WebSocket frame can not be read from network stream."; close(CloseStatusCode.ABNORMAL, msg); } @@ -846,7 +847,7 @@ namespace WebSocketSharp { if (frame.Opcode == Opcode.CLOSE) {// FINAL & CLOSE #if DEBUG - Console.WriteLine("WS: Info@receive: Start closing handshake."); + Console.WriteLine("WS: Info@receive: Starts closing handshake."); #endif close(frame.PayloadData); return null; @@ -855,7 +856,7 @@ namespace WebSocketSharp { if (frame.Opcode == Opcode.PING) {// FINAL & PING #if DEBUG - Console.WriteLine("WS: Info@receive: Return Pong."); + Console.WriteLine("WS: Info@receive: Returns Pong."); #endif pong(frame.PayloadData); return null; @@ -863,10 +864,14 @@ namespace WebSocketSharp { if (frame.Opcode == Opcode.PONG) {// FINAL & PONG + #if DEBUG + Console.WriteLine("WS: Info@receive: Receives Pong."); + #endif _receivePong.Set(); + return null; } - // FINAL & (TEXT | BINARY | PONG) + // FINAL & (TEXT | BINARY) return new MessageEventArgs(frame.Opcode, frame.PayloadData); } @@ -889,7 +894,7 @@ namespace WebSocketSharp { } #if DEBUG - Console.WriteLine("WS: Info@receiveFragmented: Start closing handshake."); + Console.WriteLine("WS: Info@receiveFragmented: Starts closing handshake."); #endif close(CloseStatusCode.INCORRECT_DATA, String.Empty); return null; @@ -904,7 +909,7 @@ namespace WebSocketSharp { if (frame.Opcode == Opcode.CLOSE) {// FINAL & CLOSE #if DEBUG - Console.WriteLine("WS: Info@receiveFragmented: Start closing handshake."); + Console.WriteLine("WS: Info@receiveFragmented: Starts closing handshake."); #endif close(frame.PayloadData); return null; @@ -913,7 +918,7 @@ namespace WebSocketSharp { if (frame.Opcode == Opcode.PING) {// FINAL & PING #if DEBUG - Console.WriteLine("WS: Info@receiveFragmented: Return Pong."); + Console.WriteLine("WS: Info@receiveFragmented: Returns Pong."); #endif pong(frame.PayloadData); continue; @@ -921,14 +926,16 @@ namespace WebSocketSharp { if (frame.Opcode == Opcode.PONG) {// FINAL & PONG + #if DEBUG + Console.WriteLine("WS: Info@receiveFragmented: Receives Pong."); + #endif _receivePong.Set(); - OnMessage.Emit(this, new MessageEventArgs(frame.Opcode, frame.PayloadData)); continue; } // FINAL & (TEXT | BINARY) #if DEBUG - Console.WriteLine("WS: Info@receiveFragmented: Start closing handshake."); + Console.WriteLine("WS: Info@receiveFragmented: Starts closing handshake."); #endif close(CloseStatusCode.INCORRECT_DATA, String.Empty); return null; @@ -964,7 +971,7 @@ namespace WebSocketSharp { if (_readyState == WsState.CONNECTING || _readyState == WsState.CLOSED) { - var msg = "Connection isn't established or has been closed."; + var msg = "The WebSocket connection isn't established or has been closed."; onError(msg); return false; } @@ -1011,7 +1018,7 @@ namespace WebSocketSharp { { if (_readyState != WsState.OPEN) { - var msg = "Connection isn't established or has been closed."; + var msg = "The WebSocket connection isn't established or has been closed."; onError(msg); return; } @@ -1215,7 +1222,7 @@ namespace WebSocketSharp { { if (_readyState == WsState.OPEN) { - Console.WriteLine("WS: Info@Connect: Connection has been established already."); + Console.WriteLine("WS: Info@Connect: The WebSocket connection has been established already."); return; } @@ -1278,7 +1285,9 @@ namespace WebSocketSharp { if (message.IsNull()) message = String.Empty; - return ping(message, 5 * 1000); + return _isClient + ? ping(message, 5 * 1000) + : ping(message, 1 * 1000); } /// diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index 2c81ac0e..543e4cf7 100755 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll and b/websocket-sharp/bin/Debug/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb index f0adb074..77552c37 100644 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index b9830e4a..f06b8384 100755 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index c2dc67bc..672db19b 100644 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Release/websocket-sharp.dll b/websocket-sharp/bin/Release/websocket-sharp.dll index e96204cf..7f0d1d96 100755 Binary files a/websocket-sharp/bin/Release/websocket-sharp.dll and b/websocket-sharp/bin/Release/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll index f5d4ff6c..5f94f3a7 100755 Binary files a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/websocket-sharp.pidb b/websocket-sharp/websocket-sharp.pidb index 9815e7a3..130e0515 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ