I am developing a game emu in c# using async sockets. Currently, the program uses a List to store the clients.
If a client joins a room, a local list is created storing all the users from that specific room (I am using FindAll(x => x.room == this.room)). That list is needed because when a client changes rooms, the server must send a certain packet to announce all the other players in the room that a client joined.
Something like this:
var localist = Clients.FindAll(x=> x.room == this.room);
foreach(stateObj client_in_room in locallist)
{
client_in_room.sendPacket(...); //what if the client_in_room disconnected in the meanwhile?
}
When a client disconnects, it (stateObj) gets removed from the original client list (and the client socket closes), and a client volatile boolean called ConnectionFired changes to false (it is meant to check if the user is still connected before doing socket operations like BeginSend, etc.). What if a client disconnects from room X while some other client joins a room X? It will still be in the local list for sure but will the ConnectionFired bool change its value? I guess not. It only affects the main/original list.
My concerns are that if that^ happens then the server could try send data to a closed socket (since the bool that checks for this, ConnectionFire, will not change its value when required).
To avoid this kind of stuff, should I avoid doing clones of the list, and use a for on the main list directly? (I initially did this locallist thing to avoid the collection was modified exception).