diff --git a/src/Fleck/SocketWrapper.cs b/src/Fleck/SocketWrapper.cs index ba975404..50324dfa 100644 --- a/src/Fleck/SocketWrapper.cs +++ b/src/Fleck/SocketWrapper.cs @@ -16,7 +16,7 @@ public class SocketWrapper : ISocket private Stream _stream; private CancellationTokenSource _tokenSource; private TaskFactory _taskFactory; - + public string RemoteIpAddress { get @@ -51,7 +51,7 @@ public Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslPr _stream = new QueuedStream(ssl); Func begin = (cb, s) => ssl.BeginAuthenticateAsServer(certificate, false, enabledSslProtocols, false, cb, s); - + Task task = Task.Factory.FromAsync(begin, ssl.EndAuthenticateAsServer, null); task.ContinueWith(t => callback(), TaskContinuationOptions.NotOnFaulted) .ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted); @@ -74,7 +74,7 @@ public bool Connected { get { return _socket.Connected; } } - + public Stream Stream { get { return _stream; } diff --git a/src/Fleck/WebSocketServer.cs b/src/Fleck/WebSocketServer.cs index eb5cda3a..03a609c9 100644 --- a/src/Fleck/WebSocketServer.cs +++ b/src/Fleck/WebSocketServer.cs @@ -43,6 +43,7 @@ public WebSocketServer(int port, 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 { @@ -99,7 +100,24 @@ public void Start(Action config) private void ListenForClients() { - ListenerSocket.Accept(OnClientConnect, e => FleckLog.Error("Listener socket is closed", e)); + 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(AddressFamily.InterNetwork, 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); + } + } + }); } private void OnClientConnect(ISocket clientSocket)