base websockets working

This commit is contained in:
arhamGIT committed 2023-01-04 04:34:44 -05:00
1 parent 320427c9c5
commit bebbf51c6a
6 files changed
+414 -26

No files matched your search

+4 -1
View File
@@ -4,13 +4,16 @@
class Program {
static int Main(string[] args) {
Console.WriteLine("Starting spark_api_server...");
Console.WriteLine("Starting spark_api_server... just checking build");
// TODO
bts.websocket_core.WebsocketDataSync sync =
new bts.websocket_core.WebsocketDataSync();
bts.websocket_core.WebsocketConnectionGateway gwy =
new bts.websocket_core.WebsocketConnectionGateway();
bts.websocket_core.WebsocketServerParams prms = new bts.websocket_core.WebsocketServerParams();
gwy.init(sync);
sync.init(gwy);
gwy.start_listening(prms);
while (true) {
System.Threading.Thread.Sleep(100);
}
@@ -3,26 +3,23 @@ using System.Collections.Generic;
using System.Reflection;
namespace bts.websocket_core {
// Represents a unique client
// Represents a unique client, a little redundant
public class WebsocketClientID {
readonly string ip;
readonly string port;
// not sure if we need this, lets go with this for now
readonly string uuid;
public WebsocketClientID(string ip, string port, string uuid) {
this.ip = ip;
this.port = port;
this.uuid = uuid;
public Guid Id { get; }
public WebsocketClientID() {
Id = Guid.NewGuid();
}
public override int GetHashCode() {
return uuid.GetHashCode();
return this.Id.GetHashCode();
}
public override string ToString() {
return uuid;
return this.Id.ToString();
}
public override bool Equals(object? obj) {
var other = obj == null ? null : obj as WebsocketClientID;
return other != null && other.uuid == uuid;
return other != null && other.Id == Id;
}
}
}
@@ -1,28 +1,259 @@
using System;
using System.Collections.Generic;
using System.Reflection;
// #nullable enable
using System.Net.WebSockets;
using System.Text;
using System.Collections.Concurrent;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
namespace bts.websocket_core {
/// <summary>
/// Handles all websocket related processing, interfaces with datasync, gets initial paramters from WebsocketServerParams
/// </summary>
public class WebsocketConnectionGateway : IWebsocketConnectionGateway {
private IWebsocketDataSync? _sync = null;
private WebsocketServerParams? _server_params = null;
private WebApplication? app = null;
// IDictionary<int, WebSocket> clients = new Dictionary<int, WebSocket>();
private ConcurrentDictionary<WebsocketClientID, WebSocket>? clients = null;
/// <summary>
/// To Avoid null reference warning, only called when object is not null, should only be called inside getNonNull, is separate function so can be changed later on
/// </summary>
/// <returns> non nullable version of object </returns>
private T getObject<T>(T? obj) {
return obj!;
}
/// <summary>
/// as explained in c# documentation this can be used to instruct the compiler that obj is not null, for now this is redundant
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private static bool IsNotNull([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) => obj != null;
/// <summary>
/// will assert that object is not null and return the non nullable version
/// </summary>
/// <returns> non nullable version of object </returns>
private object GetNonNull(object? obj) {
if (IsNotNull(obj)) {
return getObject<object>(obj);
} else {
bts.Log.assert(false);
return new Object();
}
}
/// <summary>
/// Should be called in the start, stores pointer for sync and creates dictionary for clients, does not start server
/// </summary>
/// <param name="sync"></param>
public void init(IWebsocketDataSync sync) {
// first call
bts.Log.assert(_sync == null);
bts.Log.assert(clients == null);
_sync = sync;
clients = new ConcurrentDictionary<WebsocketClientID, WebSocket>();
}
/// <summary>
/// stores the current clients, one client corresponds to one open socket which corresponds to one request, will update getter to c# convention
/// </summary>
private ConcurrentDictionary<WebsocketClientID, WebSocket> GetClients() => (ConcurrentDictionary<WebsocketClientID, WebSocket>)GetNonNull(clients);
/// <summary>
/// will update getter to c# convention
/// </summary>
private IWebsocketDataSync GetSync() => (IWebsocketDataSync)GetNonNull(_sync);
/// <summary>
/// creates ID and adds it to clients, in case of collision just updates the previous value
/// </summary>
WebsocketClientID createID(WebSocket ws) {
var id = new WebsocketClientID();
var check = GetClients().TryAdd(id, ws);
if (!check) {
var log_txt = $"WebsocketDataSync.createID client_id: {id.ToString()} Already exits";
bts.Log.Gen.warn(log_txt);
GetClients()[id] = ws;
}
return id;
}
/// <summary>
/// wrapper to gets socket from clients dictionary against given id, returns null if not found
/// </summary>
private WebSocket? GetSocket(WebsocketClientID id) {
return GetClients().ContainsKey(id) ? GetClients()[id] : null;
}
/// <summary>
/// wrapper to remove socket from clients dictionary against given id, cleans up removed socket as well
/// </summary>
private void RemoveSocket(WebsocketClientID id) {
if (GetClients().ContainsKey(id)) {
if (GetClients().TryRemove(id, out WebSocket? ws)) {
ws.Dispose();
}
}
}
/// <summary>
/// Manages client disconnection, does the following three things:
/// <para>1. tries sending close request to client </para>
/// <para>2. removes client from dictionary of active clients</para>
/// <para>3. asks sync to handle disconnection</para>
/// </summary>
public async Task HandleDisconnection(WebsocketClientID id, string optional_message = "") {
await SendDisconnectRequestAsync(id, optional_message);
RemoveSocket(id);
GetSync().handle_disconnection(id);
}
/// <summary>
/// should be called after init
/// creates server using the server params provided and starts listening for socket based requests on that URI
/// keeps running async
/// </summary>
/// <param name="server_params"></param>
public void start_listening(WebsocketServerParams server_params) {
// start listening, assert that init has been called already
bts.Log.assert(_sync != null);
bts.Log.assert(_server_params == null);
bts.Log.assert(clients != null);
bts.Log.assert(app == null);
_server_params = server_params;
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls(_server_params.Uri);
app = builder.Build();
app.UseWebSockets(_server_params.get_ws_options());
app.Map(_server_params.Default_end_point, async context => {
if (context.WebSockets.IsWebSocketRequest) {
using (var ws = await context.WebSockets.AcceptWebSocketAsync()) {
await ProcessWebSocketConnectionAsync(ws);
}
} else {
context.Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
}
});
app.RunAsync();
}
// Messages are sent to the client through this
/// <summary>
/// sends the list of messafes to respectives sockets, does not wait for messages to be sent
/// </summary>
/// <param name="messages">list of messages each containing a string based message and client ID</param>
public void send_to_clients(
IEnumerable<WebsocketOutgoingMessage> messages) {
var tasks = new List<Task>();
foreach (var om in messages) {
tasks.Add(SendWebSocketRequestAsync(om.receiver_client_id, om.message_body));
}
}
/// <summary>
/// After adding new socket to dictionary send the new client id to sync to store and process
/// while socket stays open this keeps waiting for requests
/// </summary>
/// <param name="ws"></param>
/// <returns>Task that keeps waiting for requests until the socket is closed</returns>
public async Task ProcessWebSocketConnectionAsync(WebSocket ws) {
var request = createID(ws);
GetSync().handle_connection(request, null);//for now thinking this is async/ no IO
while (ws != null && ws.State == WebSocketState.Open) {
await ProcessWebSocketRequestAsync(request);
}
}
/// <summary>
/// 1. makes sure state is consistent
/// 2. listens for request
/// 3. if request is the close, calls handleDisconnection
/// 4. otherwise sends request to sync to process
/// catches exception of client closes without handshake
/// </summary>
/// <param name="id">id of client from whom the message is to be recieved</param>
/// <returns>Task that runs until socket is open</returns>
public async Task ProcessWebSocketRequestAsync(WebsocketClientID id) {
var socket = GetSocket(id);
if (socket == null || socket.State != WebSocketState.Open) {
await HandleDisconnection(id, "WebsocketGateway.ProcessWebSocketRequestAsync could not receive Request as client closed");
return;
}
try {
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[256]);
WebSocketReceiveResult result;
string request = "";
do {
result = await socket.ReceiveAsync(buffer, CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close) {
var log_txt = $"WebsocketGateway.ProcessWebSocketRequestAsync connection closed client_id: {id}";
await HandleDisconnection(id, log_txt);
return;
}
if (buffer.Array != null)// added because of warning
{
request += Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
} else {
bts.Log.assert(false);//this should never happen
}
if (result.EndOfMessage) {
GetSync().handle_client_message(new WebsocketIncomingMessage(id, request));
request = "";
}
} while (socket.State == WebSocketState.Open);
} catch (WebSocketException ex) {
var ex_str = ex.ToString();
var log_txt = $"WebsocketGateway.ProcessWebSocketRequestAsync connection closed client_id: {id} Error: {ex_str}";
await HandleDisconnection(id, log_txt);
} catch (OperationCanceledException) {//not sure about this read that this should just be ignored
}
}
/// <summary>
/// tries to send request to client to disconnect
/// </summary>
/// <param name="id">id of client to whom the Disconnect message should be sent</param>
/// <param name="message"></param>
/// <returns>task that completes if client disconnects or if there is an error</returns>
public async Task SendDisconnectRequestAsync(WebsocketClientID id, string message) {
var log_txt = $"WebsocketGateway.SendDisconnectRequestAsync tried closing client_id: {id} with message: {message}";
var socket = GetSocket(id);
if (socket != null && socket.State == WebSocketState.Open) {
try {
await socket.CloseAsync((WebSocketCloseStatus)1000, "", CancellationToken.None);
log_txt += " done succesfully";
} catch {
log_txt += " failed as client didnt respond";
}
} else if (socket == null) {
log_txt += " failed as could not find socket in dictionary";
} else if (socket.State != WebSocketState.Open) {
log_txt += " Socket already closed";
}
bts.Log.Gen.info(log_txt);
Console.WriteLine(log_txt);
}
/// <summary>
/// sends message iver socket correspoding to id
/// </summary>
/// <param name="id">id of client to whom the message should be sent</param>
/// <param name="message">message to be sent</param>
/// <returns></returns>
public async Task SendWebSocketRequestAsync(WebsocketClientID id, string message) {
var socket = GetSocket(id);
if (socket == null || socket.State != WebSocketState.Open) {
await HandleDisconnection(id, "WebsocketGateway.SendWebSocketRequestAsync could not send Request as client closed");
return;
}
try {
await socket.SendAsync(System.Text.Encoding.ASCII.GetBytes(message), System.Net.WebSockets.WebSocketMessageType.Text, true, CancellationToken.None);
} catch (WebSocketException ex) {
var ex_str = ex.ToString();
var log_txt = $"WebsocketGateway.SendWebSocketRequestAsync connection closed client_id: {id} Error: {ex_str}";
await HandleDisconnection(id, log_txt);
return;
}
}
@@ -1,30 +1,161 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace bts.websocket_core {
// This does all the bookkeeping for conenctions and data, and passes
// it over to the business layer
/// <summary>
/// Immutable to store the state of the client
/// </summary>
public class ClientState {
public UInt64 MessageCount { get; }
public ClientState(UInt64 messageCount) {
MessageCount = messageCount;
}
public ClientState IncrementMessages(ClientState st) => new ClientState(st.MessageCount + 1);
}
/// <summary>
/// This does all the bookkeeping for conenctions and data, and passes
/// it over to the business layer CURRENT VERSION IS JUST TO SHOW HOW TO USE GATEWAY
/// </summary>
public class WebsocketDataSync : IWebsocketDataSync {
WebsocketConnectionGateway? Gateway;
/// <summary>
/// stores the current connected clients, can be converted the concurrent dictionary
/// </summary>
private Dictionary<WebsocketClientID, ClientState>? clients;
/// <summary>
/// to access the dictionary
/// </summary>
private static object _clientsAccessLock = new object();
/// <summary>
/// Thread that will broadcast messages to all connected messages
/// </summary>
private Thread? broadcast;
/// <summary>
/// number of messages after which broadcast thread will disconnect that particular socket
/// </summary>
public uint BROADCAST_LIMIT { get; } = 20;
/// <summary>
/// will update getter to c# convention
/// </summary>
/// <returns>non nullable version</returns>
private WebsocketConnectionGateway GetGateway() => (WebsocketConnectionGateway)GetNonNull(Gateway);
/// <summary>
/// will update getter to c# convention
/// </summary>
/// <returns>non nullable version</returns>
private Dictionary<WebsocketClientID, ClientState> GetClients() => (Dictionary<WebsocketClientID, ClientState>)GetNonNull(clients);
/// <summary>
/// adds client to dictionary is thread safe
/// </summary>
/// <param name="client_id"></param>
void AddClient(WebsocketClientID client_id) {
lock (_clientsAccessLock) {
GetClients().Add(client_id, new ClientState(0));
}
}
/// <summary>
/// removes client from dictionary is thread safe
/// </summary>
/// <param name="client_id"></param>
void RemoveClient(WebsocketClientID client_id) {
lock (_clientsAccessLock) {
if (GetClients().ContainsKey(client_id))
GetClients().Remove(client_id);
}
}
/// <summary>
/// will update getter to c# convention
/// </summary>
/// <returns>non nullable version</returns>
private Thread GetBroadcast() => (Thread)GetNonNull(broadcast);
/// <summary>
/// To Avoid null reference warning, only called when object is not null, should only be called inside getNonNull, is separate function so can be changed later on
/// </summary>
/// <returns> non nullable version of object </returns>
private T getObject<T>(T? obj) {
return obj!;
}
/// <summary>
/// as explained in c# documentation this can be used to instruct the compiler that obj is not null, for now this is redundant
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private static bool IsNotNull([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) => obj != null;
/// <summary>
/// will assert that object is not null and return the non nullable version
/// </summary>
/// <returns> non nullable version of object </returns>
private object GetNonNull(object? obj) {
if (IsNotNull(obj)) {
return getObject<object>(obj);
} else {
bts.Log.assert(false);
return new Object();
}
}
/// <summary>
/// Should be called in the start, stores pointer for gtwy and creates dictionary for clients, starts broadcast thread
/// </summary>
/// <param name="gtwy"></param>
public void init(WebsocketConnectionGateway gtwy) {
Gateway = gtwy;
clients = new Dictionary<WebsocketClientID, ClientState>();
broadcast = new Thread(() => BroadcastToClients());
broadcast.Start();
}
/// <summary>
/// Runs on different thread, wakes after given duration sends a message to all connected sockets, will close sockets that have been sent messages equal to limit
/// </summary>
/// <param name="duration">wakes up after given duration in ms</param>
private void BroadcastToClients(int duration = 1000) {
while (true) {
List<WebsocketOutgoingMessage> messages = new List<WebsocketOutgoingMessage>();
List<WebsocketClientID> toRemove = new List<WebsocketClientID>();
lock (_clientsAccessLock) {
foreach (var client in GetClients()) {
messages.Add(new WebsocketOutgoingMessage(client.Key, $"New Message number: {client.Value.MessageCount}, clients connected: {GetClients().Count}"));
GetClients()[client.Key] = client.Value.IncrementMessages(client.Value);
if (GetClients()[client.Key].MessageCount == BROADCAST_LIMIT) {
toRemove.Add(client.Key);
}
}
}
//example on how to send multiple messages to multiple sockets
GetGateway().send_to_clients(messages);
foreach (var key in toRemove) {
///example on how to disconnect, this should ideally happen in its own thread
GetGateway().HandleDisconnection(key, "closing client").Wait();
}
Thread.Sleep(duration);
}
}
// Called when a new client is connected
public void handle_connection(WebsocketClientID client_id,
WebsocketIncomingMessage? optional_message) {
AddClient(client_id);
var log_txt = $"WebsocketDataSync.handle_connection client_id: {client_id} " +
$"message: {(optional_message == null ? "null" : optional_message.ToString())}";
bts.Log.Gen.info(log_txt);
System.Console.WriteLine(log_txt);
if (IsNotNull(optional_message)) {
handle_client_message(optional_message);
}
}
// Called when a client is disconnected
public void handle_disconnection(WebsocketClientID client_id) {
RemoveClient(client_id);
var log_txt = $"WebsocketDataSync.handle_disconnection client_id: {client_id}";
bts.Log.Gen.info(log_txt);
System.Console.WriteLine(log_txt);
}
// Called when a client sends data
// Called when a client sends data sends the same message back
public void handle_client_message(WebsocketIncomingMessage message) {
var log_txt = $"WebsocketDataSync.handle_client_message message: {message}";
bts.Log.Gen.info(log_txt);
System.Console.WriteLine(log_txt);
//example to show how to send messages to clients
GetGateway().send_to_clients(new List<WebsocketOutgoingMessage>() { new WebsocketOutgoingMessage(message.sender_client_id, message.message_body + " RESPONSE") });
}
}
}
@@ -2,9 +2,32 @@ using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
namespace bts.websocket_core {
// Placeholder for whatever is needed for the server to start
// listening to clients (port, security etc)
public class WebsocketServerParams {
public string Port { get; }
public string Host { get; }
public string Scheme { get; }
public string Default_end_point { get; }
public double Keep_alive_interval_seconds { get; }
public string Uri { get; }
public WebsocketServerParams() {
//will later do this from conf file
this.Default_end_point = "/";
this.Host = "localhost";
this.Scheme = "http";
this.Port = "6666";
this.Keep_alive_interval_seconds = 0.1;
this.Uri = this.Scheme + "://" + this.Host + ":" + this.Port; ;
}
public WebSocketOptions get_ws_options() {
return new WebSocketOptions() {
KeepAliveInterval = TimeSpan.FromSeconds(this.Keep_alive_interval_seconds)
};
}
}
}
@@ -8,9 +8,12 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>$(major).$(minor).$(patch)</Version>
<Version Condition="'$(version)'=='..'">0.0.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Net.WebSockets" Version="4.3.0"/>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<ProjectReference Include="../spark_core/spark_core.csproj" />
</ItemGroup>