From 71965150e4cbaee2844f6d92eff7b0369ab8b2d9 Mon Sep 17 00:00:00 2001 From: wmpadmin Date: Thu, 26 Apr 2018 10:55:39 +0200 Subject: [PATCH 1/3] listener logic change --- src/Fleck/Interfaces/ISocket.cs | 2 +- src/Fleck/Interfaces/IWebSocketServer.cs | 3 +- src/Fleck/SocketWrapper.cs | 4 +-- src/Fleck/WebSocketServer.cs | 42 ++++++++++++++---------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/Fleck/Interfaces/ISocket.cs b/src/Fleck/Interfaces/ISocket.cs index b6481a0a..f8c4308d 100644 --- a/src/Fleck/Interfaces/ISocket.cs +++ b/src/Fleck/Interfaces/ISocket.cs @@ -17,7 +17,7 @@ public interface ISocket bool NoDelay { get; set; } EndPoint LocalEndPoint { get; } - Task Accept(Action callback, Action error); + Task Accept(Action callback, Action setHandle, Action error); Task Send(byte[] buffer, Action callback, Action error); Task Receive(byte[] buffer, Action callback, Action error, int offset = 0); Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action error); diff --git a/src/Fleck/Interfaces/IWebSocketServer.cs b/src/Fleck/Interfaces/IWebSocketServer.cs index ee5f889f..eb63bce9 100644 --- a/src/Fleck/Interfaces/IWebSocketServer.cs +++ b/src/Fleck/Interfaces/IWebSocketServer.cs @@ -5,5 +5,6 @@ namespace Fleck public interface IWebSocketServer : IDisposable { void Start(Action config); + void Stop(); } -} +} diff --git a/src/Fleck/SocketWrapper.cs b/src/Fleck/SocketWrapper.cs index eb8a2894..ba3e1aa9 100644 --- a/src/Fleck/SocketWrapper.cs +++ b/src/Fleck/SocketWrapper.cs @@ -130,11 +130,11 @@ public Task Receive(byte[] buffer, Action callback, Action } } - public Task Accept(Action callback, Action error) + public Task Accept(Action callback, Action setHandle, Action error) { Func end = r => _tokenSource.Token.IsCancellationRequested ? null : new SocketWrapper(_socket.EndAccept(r)); var task = _taskFactory.FromAsync(_socket.BeginAccept, end, null); - task.ContinueWith(t => callback(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion) + task.ContinueWith(t => { setHandle(); callback(t.Result); }, TaskContinuationOptions.OnlyOnRanToCompletion) .ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted); task.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted); return task; diff --git a/src/Fleck/WebSocketServer.cs b/src/Fleck/WebSocketServer.cs index c5ce5721..c49ce5bc 100644 --- a/src/Fleck/WebSocketServer.cs +++ b/src/Fleck/WebSocketServer.cs @@ -4,6 +4,7 @@ using System.Security.Cryptography.X509Certificates; using System.Collections.Generic; using System.Security.Authentication; +using System.Threading.Tasks; using Fleck.Helpers; namespace Fleck @@ -13,6 +14,7 @@ public class WebSocketServer : IWebSocketServer private readonly string _scheme; private readonly IPAddress _locationIP; private Action _config; + private bool _running; public WebSocketServer(string location) { @@ -38,7 +40,6 @@ public WebSocketServer(string location) public X509Certificate2 Certificate { get; set; } public SslProtocols EnabledSslProtocols { get; set; } public IEnumerable SupportedSubProtocols { get; set; } - public bool RestartAfterListenError {get; set; } public bool IsSecure { @@ -47,6 +48,7 @@ public bool IsSecure public void Dispose() { + Stop(); ListenerSocket.Dispose(); } @@ -89,30 +91,34 @@ public void Start(Action config) FleckLog.Debug("Using default TLS 1.0 security protocol."); } } + + _running = true; ListenForClients(); _config = config; } + + public void Stop() + { + _running = false; + } private void ListenForClients() { - ListenerSocket.Accept(OnClientConnect, e => { - FleckLog.Error("Listener socket is closed", e); - if(RestartAfterListenError){ - FleckLog.Info("Listener socket restarting"); - try - { - ListenerSocket.Dispose(); - var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP); - ListenerSocket = new SocketWrapper(socket); - Start(_config); - FleckLog.Info("Listener socket restarted"); - } - catch (Exception ex) - { - FleckLog.Error("Listener could not be restarted", ex); - } - } + System.Threading.ManualResetEvent acceptDone = new System.Threading.ManualResetEvent (false); + + Task.Run( () => { + + while (_running) { + acceptDone.Reset (); + + ListenerSocket.Accept( OnClientConnect, () => acceptDone.Set(), + e => FleckLog.Error ("An error occurred while accepting a client connection", e) ); + + acceptDone.WaitOne (); + } + }); + } private void OnClientConnect(ISocket clientSocket) From 432090e33f791e5961740ed55354f9b8413406fd Mon Sep 17 00:00:00 2001 From: wmpadmin Date: Thu, 26 Apr 2018 11:51:12 +0200 Subject: [PATCH 2/3] removed ListenForClients() --- src/Fleck/WebSocketServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fleck/WebSocketServer.cs b/src/Fleck/WebSocketServer.cs index c49ce5bc..1ba02dd0 100644 --- a/src/Fleck/WebSocketServer.cs +++ b/src/Fleck/WebSocketServer.cs @@ -126,7 +126,7 @@ private void OnClientConnect(ISocket clientSocket) if (clientSocket == null) return; // socket closed FleckLog.Debug(String.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString())); - ListenForClients(); + //ListenForClients(); WebSocketConnection connection = null; From 401af12220b2179ad4a09992210488739c6948c9 Mon Sep 17 00:00:00 2001 From: Elad Zelingher Date: Fri, 27 Apr 2018 15:43:53 -0400 Subject: [PATCH 3/3] Made tests compile and pass --- src/Fleck.Tests/SocketWrapperTests.cs | 2 +- src/Fleck.Tests/WebSocketServerTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Fleck.Tests/SocketWrapperTests.cs b/src/Fleck.Tests/SocketWrapperTests.cs index 990e01b5..647f7393 100644 --- a/src/Fleck.Tests/SocketWrapperTests.cs +++ b/src/Fleck.Tests/SocketWrapperTests.cs @@ -29,7 +29,7 @@ public void Setup() [Test] public void ShouldCompleteAcceptTaskOnDispose() { - Task task = _wrapper.Accept(socket => { }, exception => { }); + Task task = _wrapper.Accept(socket => { }, () => { }, exception => { }); _wrapper.Dispose(); Assert.DoesNotThrow(task.Wait); diff --git a/src/Fleck.Tests/WebSocketServerTests.cs b/src/Fleck.Tests/WebSocketServerTests.cs index 471e2e3d..dec0a571 100644 --- a/src/Fleck.Tests/WebSocketServerTests.cs +++ b/src/Fleck.Tests/WebSocketServerTests.cs @@ -43,7 +43,7 @@ public void ShouldStart() _server.Start(connection => { }); socketMock.Verify(s => s.Bind(It.Is(i => i.Port == 8000))); - socketMock.Verify(s => s.Accept(It.IsAny>(), It.IsAny>())); + socketMock.Verify(s => s.Accept(It.IsAny>(), It.IsAny(), It.IsAny>())); } [Test]