Skip to content

Commit

Permalink
Merge pull request #181 from jespertheend/restart-listner-with-flag
Browse files Browse the repository at this point in the history
added RestartAfterListenError
  • Loading branch information
Mike Olsen authored Aug 25, 2016
2 parents 9b021ce + 00866bb commit 27e3feb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Fleck/SocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SocketWrapper : ISocket
private Stream _stream;
private CancellationTokenSource _tokenSource;
private TaskFactory _taskFactory;

public string RemoteIpAddress
{
get
Expand Down Expand Up @@ -51,7 +51,7 @@ public Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslPr
_stream = new QueuedStream(ssl);
Func<AsyncCallback, object, IAsyncResult> 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);
Expand All @@ -74,7 +74,7 @@ public bool Connected
{
get { return _socket.Connected; }
}

public Stream Stream
{
get { return _stream; }
Expand Down
20 changes: 19 additions & 1 deletion src/Fleck/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public WebSocketServer(int port, string location)
public X509Certificate2 Certificate { get; set; }
public SslProtocols EnabledSslProtocols { get; set; }
public IEnumerable<string> SupportedSubProtocols { get; set; }
public bool RestartAfterListenError {get; set; }

public bool IsSecure
{
Expand Down Expand Up @@ -99,7 +100,24 @@ public void Start(Action<IWebSocketConnection> 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)
Expand Down

0 comments on commit 27e3feb

Please sign in to comment.