You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the WebSocketServer class uses the Socket.BeginReceive() method to asynchronously receive data from the client's socket. The use of this method is very good because it allows the server to avoid spawning a Thread for every single client that connects to it, which becomes a bit excessive when more than 5000 clients connect to a server at once and it also allows the server to avoid having to loop trough a list of current sockets (we tried that with the WebSocketPool class and it ended up not working too well because it hogged the CPU way too much).
What BeginReceive() does is spawn a new Threadonly when the network card does receive data and triggers an I/O IRQ. This, however, makes it so the event handlers subscribed to the WebSocketServer's events are executed by different threads which makes the library's users' code not thread safe. For instance, let's say a user subscribes the following method to the OnMessage event:
privatestaticvoidOnSocketMessage(objectsender,WebSocketServerEventArgsargs){WebSocketServerserver=(WebSocketServer)sender;
Logger.Log("Thread "+ Thread.CurrentThread.ManagedThreadId+" received message : "+ args.DataFrame.Plaintext, Logger.LogType.Info);}
It will produce the following output
(Note the different thread IDs).
It will be necessary to create a new Thread within the WebSocketServer that checks an 'event Queue' (either periodically or based on a synchronization mechanism) and that will be responsible for executing the event handlers to ensure that the code written by the user is thread safe.
The text was updated successfully, but these errors were encountered:
Currently the
WebSocketServer
class uses theSocket.BeginReceive()
method to asynchronously receive data from the client's socket. The use of this method is very good because it allows the server to avoid spawning aThread
for every single client that connects to it, which becomes a bit excessive when more than 5000 clients connect to a server at once and it also allows the server to avoid having to loop trough a list of current sockets (we tried that with theWebSocketPool
class and it ended up not working too well because it hogged the CPU way too much).What
BeginReceive()
does is spawn a newThread
only when the network card does receive data and triggers an I/O IRQ. This, however, makes it so the event handlers subscribed to theWebSocketServer
's events are executed by different threads which makes the library's users' code not thread safe. For instance, let's say a user subscribes the following method to theOnMessage
event:It will produce the following output
(Note the different thread IDs).
It will be necessary to create a new
Thread
within theWebSocketServer
that checks an 'eventQueue
' (either periodically or based on a synchronization mechanism) and that will be responsible for executing the event handlers to ensure that the code written by the user is thread safe.The text was updated successfully, but these errors were encountered: