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."));
}
}
}