Skip to content

Commit ca00a01

Browse files
author
Jesper
committed
added RestartAfterListenError
use server.ListenerSocket. RestartAfterListenError to automatically restart the listener after it closes because of an error
1 parent 9f56c80 commit ca00a01

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/Fleck/Interfaces/ISocket.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public interface ISocket
1515
int RemotePort { get; }
1616
Stream Stream { get; }
1717
bool NoDelay { get; set; }
18+
bool RestartAfterListenError { get; set; }
1819
EndPoint LocalEndPoint { get; }
1920

2021
Task<ISocket> Accept(Action<ISocket> callback, Action<Exception> error);

src/Fleck/SocketWrapper.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class SocketWrapper : ISocket
1616
private Stream _stream;
1717
private CancellationTokenSource _tokenSource;
1818
private TaskFactory _taskFactory;
19-
19+
private bool _restartAfterListenError;
20+
2021
public string RemoteIpAddress
2122
{
2223
get
@@ -51,7 +52,7 @@ public Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslPr
5152
_stream = new QueuedStream(ssl);
5253
Func<AsyncCallback, object, IAsyncResult> begin =
5354
(cb, s) => ssl.BeginAuthenticateAsServer(certificate, false, enabledSslProtocols, false, cb, s);
54-
55+
5556
Task task = Task.Factory.FromAsync(begin, ssl.EndAuthenticateAsServer, null);
5657
task.ContinueWith(t => callback(), TaskContinuationOptions.NotOnFaulted)
5758
.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
@@ -74,7 +75,7 @@ public bool Connected
7475
{
7576
get { return _socket.Connected; }
7677
}
77-
78+
7879
public Stream Stream
7980
{
8081
get { return _stream; }
@@ -86,6 +87,12 @@ public bool NoDelay
8687
set { _socket.NoDelay = value; }
8788
}
8889

90+
public bool RestartAfterListenError
91+
{
92+
get { return _restartAfterListenError; }
93+
set { _restartAfterListenError = value; }
94+
}
95+
8996
public EndPoint LocalEndPoint
9097
{
9198
get { return _socket.LocalEndPoint; }

src/Fleck/WebSocketServer.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,24 @@ public void Start(Action<IWebSocketConnection> config)
9999

100100
private void ListenForClients()
101101
{
102-
ListenerSocket.Accept(OnClientConnect, e => FleckLog.Error("Listener socket is closed", e));
102+
ListenerSocket.Accept(OnClientConnect, e => {
103+
FleckLog.Error("Listener socket is closed", e);
104+
if(ListenerSocket.RestartAfterListenError){
105+
FleckLog.Info("Listener socket restarting");
106+
try
107+
{
108+
ListenerSocket.Dispose();
109+
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
110+
ListenerSocket = new SocketWrapper(socket);
111+
Start(_config);
112+
FleckLog.Info("Listener socket restarted");
113+
}
114+
catch (Exception ex)
115+
{
116+
FleckLog.Error("Listener could not be restarted", ex);
117+
}
118+
}
119+
});
103120
}
104121

105122
private void OnClientConnect(ISocket clientSocket)

0 commit comments

Comments
 (0)