Files
Ragon/Ragon.Server.DotNetWebSockets/WebSocketServer.cs
T

142 lines
4.0 KiB
C#
Raw Normal View History

2023-03-06 10:06:43 +04:00
/*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-12-20 12:20:52 -08:00
using System.Net;
using System.Net.WebSockets;
using NLog;
2023-03-06 10:06:43 +04:00
using Ragon.Protocol;
2022-12-20 12:20:52 -08:00
2023-03-06 10:06:43 +04:00
namespace Ragon.Server.DotNetWebsockets;
2022-12-20 12:20:52 -08:00
2023-03-06 10:06:43 +04:00
public class DotNetWebSocketServer : INetworkServer
2022-12-20 12:20:52 -08:00
{
2023-03-06 10:06:43 +04:00
public Executor Executor => _executor;
private ILogger _logger = LogManager.GetCurrentClassLogger();
private INetworkListener _networkListener;
private Stack<ushort> _sequencer;
private Executor _executor;
private HttpListener _httpListener;
private WebSocketConnection[] _connections;
private List<WebSocketConnection> _activeConnections;
private CancellationTokenSource _cancellationTokenSource;
2023-03-06 10:06:43 +04:00
public DotNetWebSocketServer()
{
_sequencer = new Stack<ushort>();
_connections = Array.Empty<WebSocketConnection>();
_activeConnections = new List<WebSocketConnection>();
2023-03-06 10:06:43 +04:00
_executor = new Executor();
}
2023-03-06 10:06:43 +04:00
public async void StartAccept(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
2022-12-20 12:20:52 -08:00
{
var context = await _httpListener.GetContextAsync();
2023-03-06 10:06:43 +04:00
if (!context.Request.IsWebSocketRequest)
{
context.Response.StatusCode = 200;
context.Response.ContentLength64 = 0;
context.Response.Close();
continue;
}
2022-12-20 12:20:52 -08:00
var webSocketContext = await context.AcceptWebSocketAsync(null);
var webSocket = webSocketContext.WebSocket;
2022-12-20 12:20:52 -08:00
var peerId = _sequencer.Pop();
var connection = new WebSocketConnection(webSocket, peerId);
_connections[peerId] = connection;
StartListen(connection, cancellationToken);
2022-12-20 12:20:52 -08:00
}
}
async void StartListen(WebSocketConnection connection, CancellationToken cancellationToken)
{
_activeConnections.Add(connection);
_networkListener.OnConnected(connection);
var webSocket = connection.Socket;
var bytes = new byte[2048];
var buffer = new Memory<byte>(bytes);
while (
webSocket.State == WebSocketState.Open ||
!cancellationToken.IsCancellationRequested)
2022-12-20 12:20:52 -08:00
{
try
{
var result = await webSocket.ReceiveAsync(buffer, cancellationToken);
var dataRaw = buffer.Slice(0, result.Count);
2023-03-06 10:06:43 +04:00
if (dataRaw.Length > 0)
_networkListener.OnData(connection, dataRaw.ToArray());
}
catch (Exception ex)
{
break;
}
2022-12-20 12:20:52 -08:00
}
_sequencer.Push(connection.Id);
_activeConnections.Remove(connection);
_networkListener.OnDisconnected(connection);
}
2023-03-06 10:06:43 +04:00
public void Update()
{
Flush();
}
public async void Flush()
{
foreach (var conn in _activeConnections)
await conn.Flush();
}
public void Start(
INetworkListener listener,
NetworkConfiguration configuration
)
{
_networkListener = listener;
_cancellationTokenSource = new CancellationTokenSource();
var limit = (ushort)configuration.LimitConnections;
for (ushort i = limit; i != 0; i--)
_sequencer.Push(i);
_sequencer.Push(0);
_connections = new WebSocketConnection[configuration.LimitConnections];
_httpListener = new HttpListener();
2023-03-06 10:06:43 +04:00
_httpListener.Prefixes.Add($"http://+:{configuration.Port}/");
_httpListener.Start();
_executor.Run(() => StartAccept(_cancellationTokenSource.Token));
var protocolDecoded = RagonVersion.Parse(configuration.Protocol);
2023-03-06 10:06:43 +04:00
_logger.Info($"Listen at http://0.0.0.0:{configuration.Port}/");
_logger.Info($"Protocol: {protocolDecoded}");
}
public void Stop()
{
_cancellationTokenSource.Cancel();
_httpListener.Stop();
}
2022-12-20 12:20:52 -08:00
}