I am making a server-client project. Here the server makes changes in database based on the clients calls. The clients calls are asynchronous. I am invoking some delegates based on the incoming call using events. The below code can give you idea
public delegate void Client_Logging_In_Delegate(WebSocketSession session, string e);
public event Client_Logging_In_Delegate Admin_Logging_In_Event;
public void Client_Logging_In_Function(WebSocketSession session, string e)
{
//Database -> this particular client status= true
}
public void Main()
{
server.NewMessageReceived += new SessionEventHandler<WebSocketSession, string> (server.WebSocketServer_NewMessageReceived);
server.Client_Logging_In_Event = new Client_Logging_In_Delegate(server.Client_Logging_In_Function);
}
public void WebSocketServer_NewMessageReceived(WebSocketSession session, string e)
{
string type = e.Substring(0, e.IndexOf("#"));
if (type == "Client_Logging_In")
Client_Logging_In_Event(session, e);
}
I am running the both in same system. I didnt receive any crash yet. But in real case the clients => 5 -> 20 . So the question is if two events arrived simultaneously then what will happen ?
EDIT:
I am using a third party library “SuperWebsocket” for server part. This one defines the message_Received event. The documentation says that it is thread safe. Mine is a single thread application. There is only one server object. Some functions(called by delegates) have some long codes. For example take this
public void called_By_Some_Delegate(WebSocketSession session, string e)
{
//1) retrieve a big list from database
//2) change the list into a dictionary object
//3) create a string using dictionary object
//4) send this string to the client using the "session" object
}
Now, assume there are two events arrived for the same delegate/function. Now assume the first one will execute the function and it executes in the second point. If the second one accesses the function at the same time it will change the list. So, I think there will be a crash. This is the situation I have to figure out.
When you say simultaneously, I kind of understand that there are more than one thread in the server listening for requests and raising events. In this case each of the thread are going to fire the events and the event handlers are going to run in the respective threads without any problems (provided the event handlers are thread safe).
If not, and the there is only one thread listening to the socket, then anyway you are sequential and should not have issues.
If the listening thread triggers another thread for processing the incoming requests, again you have the event handler run in its own thread context and as long as your handler is thread safe, then you are still safe.
I wrote the below program to simulate the server generating multiple requests and processing the same.
So to answer your question the event handlers will be executed in respective threads simultaneously. However the database locking scheme should be clear enough to handle this scenario.