多个客户端 不定时向服务器端发送数据 服务器端好的处理方式
自己写的多线程处理 每次cpu占用太高
private void ClientConn()
{
int threadNum = 0;
while (true)
{
if (threadList.Count() != 0)
{
Client client = threadList[threadNum];
client.islive = true;
string[] str;
client.RecMsgFormClient();
str = client.GetMessage().Split(new char[]{'|'});
switch (str[0])
{
case "Login":
while (true)
{
if (!connIsLock)
{
connIsLock = true;
client.InfoLogSuc(client.GetMessage(), mscn);
client.UpdateClientDetailInfo(mscn);
break;
}
}
connIsLock = false;
SyncRoomsStatus();
continue;
case "RoomNum":
RegisterRoomNum(client,str[1]);
continue;
case "RoomStatus":
while(true)
{
if(!roomUserNumIsLock)
{
roomUserNumIsLock = true;
int i = int.Parse(str[1]) - 1;
roomUserNum[i] += int.Parse(str[2]);
break;
}
}
roomUserNumIsLock = false;
SyncRoomsStatus();
continue;
case "LoginOut":
continue;
case "Pre":
client.bIsPre = bool.Parse(str[1]);
CheckSameRoomIsPre(client);
continue;
case "CancelPre":
client.bIsPre = bool.Parse(str[1]);
CheckRoomBothPre = true;
continue;
case "DiceTally":
SavePlayerDiceTally(client,client.GetMessage());
continue;
case "Exit":
threadList.Remove(client);
client.islive = false;
client.thClient.Abort();
onLineNum--;
continue;
default :
continue;
}
}
threadNum++;
if (threadNum >= threadList.Count())
{
th