Skip to content

Commit

Permalink
added RestartAfterListenError
Browse files Browse the repository at this point in the history
use server.ListenerSocket. RestartAfterListenError to automatically
restart the listener after it closes because of an error
  • Loading branch information
Jesper committed Aug 23, 2016
1 parent 9f56c80 commit ca00a01
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Fleck/Interfaces/ISocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface ISocket
int RemotePort { get; }
Stream Stream { get; }
bool NoDelay { get; set; }
bool RestartAfterListenError { get; set; }
EndPoint LocalEndPoint { get; }

Task<ISocket> Accept(Action<ISocket> callback, Action<Exception> error);
Expand Down
13 changes: 10 additions & 3 deletions src/Fleck/SocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class SocketWrapper : ISocket
private Stream _stream;
private CancellationTokenSource _tokenSource;
private TaskFactory _taskFactory;

private bool _restartAfterListenError;

public string RemoteIpAddress
{
get
Expand Down Expand Up @@ -51,7 +52,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 +75,7 @@ public bool Connected
{
get { return _socket.Connected; }
}

public Stream Stream
{
get { return _stream; }
Expand All @@ -86,6 +87,12 @@ public bool NoDelay
set { _socket.NoDelay = value; }
}

public bool RestartAfterListenError
{
get { return _restartAfterListenError; }
set { _restartAfterListenError = value; }
}

public EndPoint LocalEndPoint
{
get { return _socket.LocalEndPoint; }
Expand Down
19 changes: 18 additions & 1 deletion src/Fleck/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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(ListenerSocket.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 ca00a01

Please sign in to comment.