Browse Source

refinement

pull/123/head
notgiven688 3 years ago
parent
commit
10c6bf8cc7
4 changed files with 30 additions and 39 deletions
  1. +1
    -1
      server/Server/Fleck/Interfaces/ISocket.cs
  2. +1
    -2
      server/Server/Fleck/Interfaces/IWebSocketServer.cs
  3. +6
    -3
      server/Server/Fleck/SocketWrapper.cs
  4. +22
    -33
      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> error);
Task<ISocket> Accept(Action<ISocket> callback, Action setHandle, Action<Exception> callbackError, 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);

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

@ -28,7 +28,6 @@ namespace Fleck
public interface IWebSocketServer : IDisposable
{
void Start(Action<IWebSocketConnection> config);
void Stop();
}
}

+ 6
- 3
server/Server/Fleck/SocketWrapper.cs View File

@ -176,13 +176,16 @@ namespace Fleck
}
}
public Task<ISocket> Accept(Action<ISocket> callback, Action setHandle, Action<Exception> error)
public Task<ISocket> Accept(Action<ISocket> callback, Action setHandle, Action<Exception> callbackError, 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 => { setHandle(); callback(t.Result); }, TaskContinuationOptions.OnlyOnRanToCompletion)
.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted); // wmp change
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;
}

+ 22
- 33
server/Server/Fleck/WebSocketServer.cs View File

@ -49,8 +49,6 @@ namespace Fleck {
private readonly IPAddress _locationIP;
private Action<IWebSocketConnection> _config;
private bool _running = true;
private const int BytesPerLong = 4; // 32 / 8
private const int BitsPerByte = 8;
@ -87,10 +85,6 @@ namespace Fleck {
ListenerSocket.Dispose ();
}
public void Stop() {
_running = false;
}
private IPAddress ParseIPAddress (Uri uri) {
string ipStr = uri.Host;
@ -129,44 +123,39 @@ namespace Fleck {
_config = config;
}
private System.Threading.ManualResetEvent acceptDone = new System.Threading.ManualResetEvent (false);
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 () {
// wmp
// free us from unnecessary, complicated and faulty code.
System.Threading.ManualResetEvent acceptDone = new System.Threading.ManualResetEvent (false);
bool running = true;
Task.Run( () => {
Task.Run (() => {
while (_running) {
while (running) {
acceptDone.Reset ();
ListenerSocket.Accept( OnClientConnect, () => acceptDone.Set(),
e => FleckLog.Error ("An error occurred while accepting a client connection", e) );
ListenerSocket.Accept (
OnClientConnect, () => acceptDone.Set (),
e => FleckLog.Error ("An error occurred while accepting a client connection", e),
e => { if(RestartAfterListenError) TryRestart(); running = false; acceptDone.Set(); }
);
acceptDone.WaitOne ();
}
});
/*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(_locationIP.AddressFamily, 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) {

Loading…
Cancel
Save