tags:

views:

60

answers:

1

Hi, I am developing some additional stuff to the game server but I dont understand the code I recieved (however it works). I would be really happy if somebody could explain to me, why (in the recievedata method) there is socket list clearing and then again filling. I just cant see the point.

this.mReceiveDataThread = new System.Threading.Thread(this.ReceiveData);

           protected void ReceiveData()
        {
            List<System.Net.Sockets.Socket> sockets = new List<System.Net.Sockets.Socket>();
            bool noClients = false;
            while (true)
            {
                if (noClients)
                {
                    System.Threading.Thread.Sleep(RECEIVE_DATA_NO_CLIENT_WAIT_TIME);
                    noClients = false;
                }
                this.mClientsSynchronization.AcquireReaderLock(-1);
                try
                {
                    if (this.mClients == null || this.mClients.Count == 0)
                    {
                        noClients = true;
                        continue;
                    }
                    sockets.Clear(); 
                    for (int i = 0; i < this.mClientsValues.Count; i++)
                    {
                        Client c = this.mClientsValues[i];
                        if (!c.IsDisconnected && !c.ReadingInProgress)
                        {
                            sockets.Add(c.Socket);
                        }
                    }
                    if (sockets.Count == 0)
                    {
                        // noClients = true;
                        continue;
                    }
                }
                finally
                {
                    this.mClientsSynchronization.ReleaseReaderLock();
                }
                try
                {
                    //System.Net.Sockets.Socket.Select(sockets, null, null, RECEIVE_DATA_TIMEOUT);
                    SocketSelect.Select(sockets, null, null, RECEIVE_DATA_TIMEOUT);
                    foreach (System.Net.Sockets.Socket s in sockets)
                    {
                        Client c = this.mClients[s]; //mClients-dictionary<socket,client>
                        if (!c.SetReadingInProgress())
                        {
                            System.Threading.ThreadPool.QueueUserWorkItem(c.ReadData);
                        }
                    }
                }
                catch (Exception e)
                {
                    this.OnExceptionCaught(this, new ExceptionCaughtEventArgs(e, "Exception when reading data."));
                }
            }
        }
+1  A: 

The sockets list is being used within the loop to extract the still connected Client objects from mClients. The sockets list gets cleared on each loop iteration so that mClients can be checked to exclude any disconnected clients or clients with a read already in progress.

Taking out the thread locking mechanism and SocketSelect call the loop boils down to the code below which may be easier to understand.

mClients.Where(c => !c.IsDisconnected && !c.ReadingInProgress).ToList().ForEach(c=> {
    if (!c.SetReadingInProgress())  {
        ThreadPool.QueueUserWorkItem(c.ReadData);                        
    }
});
sipwiz