Browse Source

listener-fix

pull/123/head
notgiven688 3 years ago
parent
commit
d7efdc08f1
3 changed files with 43 additions and 35 deletions
  1. +1
    -1
      server/Server/Fleck/Interfaces/ISocket.cs
  2. +4
    -7
      server/Server/Fleck/SocketWrapper.cs
  3. +38
    -27
      server/Server/Fleck/WebSocketServer.cs

+ 1
- 1
server/Server/Fleck/Interfaces/ISocket.cs View File

@ -40,7 +40,7 @@ namespace Fleck
bool NoDelay { get; set; }
EndPoint LocalEndPoint { get; }
Task<ISocket> Accept(Action<ISocket> callback, Action setHandle, Action<Exception> callbackError, Action<Exception> error);
Task Accept(Action<ISocket> callback, Action<Exception> error);
Task Send(byte[] buffer, Action callback, Action<Exception> error);
Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset = 0);
Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error);

+ 4
- 7
server/Server/Fleck/SocketWrapper.cs View File

@ -176,17 +176,14 @@ namespace Fleck
}
}
public Task<ISocket> Accept(Action<ISocket> callback, Action setHandle, Action<Exception> callbackError, Action<Exception> error)
public Task Accept(Action<ISocket> callback, Action<Exception> error)
{
Func<IAsyncResult, ISocket> end = r => _tokenSource.Token.IsCancellationRequested ? null : new SocketWrapper(_socket.EndAccept(r));
var task = _taskFactory.FromAsync(_socket.BeginAccept, end, null);
task.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
task.ContinueWith(t => { setHandle(); callback(t.Result); }, TaskContinuationOptions.OnlyOnRanToCompletion)
.ContinueWith(t => callbackError(t.Exception), TaskContinuationOptions.OnlyOnFaulted); // wmp change
return task;
return task.ContinueWith(t => callback(t.Result), TaskContinuationOptions.OnlyOnRanToCompletion);
}
public void Dispose()

+ 38
- 27
server/Server/Fleck/WebSocketServer.cs View File

@ -40,6 +40,7 @@ using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Fleck.Helpers;
@ -123,42 +124,52 @@ namespace Fleck {
_config = config;
}
private void TryRestart () {
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 socket could not be restarted", ex);
private void TryRestart()
{
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 socket could not be restarted", ex);
}
}
private void ListenForClients () {
private void ListenForClients()
{
ManualResetEvent acceptDone = new ManualResetEvent(false);
bool running = true;
System.Threading.ManualResetEvent acceptDone = new System.Threading.ManualResetEvent (false);
Task.Run(() => {
bool running = true;
while (running)
{
Task.Run (() => {
acceptDone.Reset();
while (running) {
acceptDone.Reset ();
var task = ListenerSocket.Accept(
s => {
running = (s != null);
acceptDone.Set();
OnClientConnect(s);
},
e => {
FleckLog.Error("Error while listening for new clients", e);
if (RestartAfterListenError) TryRestart();
running = false; acceptDone.Set();
}
);
ListenerSocket.Accept (
OnClientConnect, () => acceptDone.Set (),
e => FleckLog.Error ("An error occurred while accepting a client connection", e),
e =>
{
FleckLog.Error ("Error while listening for new clients", e);
if(RestartAfterListenError) TryRestart();
running = false; acceptDone.Set();
}
);
task.ContinueWith((t) => FleckLog.Warn("Error during client connect", t.Exception),
TaskContinuationOptions.OnlyOnFaulted);
acceptDone.WaitOne ();
acceptDone.WaitOne();
}
});
}

Loading…
Cancel
Save