public bool SendToClient(GameClient cl, CSharpGame.Message mes) { try { Byte[] outbytes = CSharpGame.SerializationUnit.SerializeObject(mes); Socket s = cl.Sock; if (s.Connected) { System.Console.WriteLine("send seat to {0}, {1}", cl.Name, (string)mes.userSender); // s.Send(outbytes, outbytes.Length, 0); //加密 Byte[] enbytes = DES.Enbyte(outbytes, outbytes.Length); //ns.Write(outbytes, 0, outbytes.Length); s.Send(enbytes, enbytes.Length, 0); } return(true); } catch (Exception e) { Console.WriteLine(e.ToString()); return(false); } }
private void BroadcastClient(CSharpGame.Message msg) { foreach (GameClient cl in clients) { SendToClient(cl, msg); } }
private void SentToUser(CSharpGame.Message msg, string user) { int find = findGameClient(user); GameClient gl = (GameClient)clients[find]; SendToClient(gl, msg); }
private void BroadcastRoom(CSharpGame.Message msg) { foreach (GameClient cl in clients) { if (cl.TableIdx == (int)msg.Num) { SendToClient(cl, msg); } } }
private void ProcessGamMsg(CSharpGame.Message gamMsg) { // 指向同一房间内的玩家广播 BroadcastRoom(gamMsg); // 服务器处理客户端的游戏情况 MsgGame msgGame = (MsgGame)gamMsg.msgContent; int tabIdx = msgGame.tableIdx; int seatIdx = msgGame.seatIdx; int ret = tables[tabIdx].PlayerCleanPair(seatIdx); if (ret == 2) { // players win MsgSys sysGameOver = new MsgSys(); sysGameOver.sysType = MsgSysType.GameOver; sysGameOver.sysContent = tabIdx; CSharpGame.Message m = new CSharpGame.Message(sysGameOver); m.userSender = msgGame.userName; BroadcastClient(m); // 一些房间的游戏信息清空 tables[tabIdx].readycount = 0; //tables[tabIdx].usercount = 0; tables[tabIdx].tabelEable = false; } else if (ret == 1) { // player 完成一幅牌 // tables[tabIdx].gameRound[seatIdx]++; MsgSys sysReset = new MsgSys(); sysReset.sysType = MsgSysType.NewRound; sysReset.sysContent = tables[tabIdx].GetGameForPlayer(seatIdx); CSharpGame.Message m = new CSharpGame.Message(sysReset); m.userSender = msgGame.userName; m.Num = tabIdx; m.reciType = ReciveType.Room; BroadcastRoom(m); //SentToUser(m, msgGame.userName); } }
private void sendCurrentTables(string username) { GameClient toUser = null; foreach (GameClient gc in clients) { if (gc.Name == username) { toUser = gc; break; } } if (toUser == null) { return; } foreach (GameClient gcc in clients) { if (gcc.Name == username) { continue; } if (gcc.TableIdx != -1) { MsgSys sysMsg = new MsgSys(); sysMsg.sysType = MsgSysType.Seat; sysMsg.sysContent = new int[] { gcc.TableIdx, gcc.SeatIdx }; CSharpGame.Message msg = new CSharpGame.Message(sysMsg); msg.userSender = gcc.Name; SendToClient(toUser, msg); //BroadcastClient(msg); } } }
private void ServiceClient() { Socket client = clientsocket; bool keepalive = true; //各个客户Socket线程存活的标识 Hashtable duizhan = new Hashtable(); while (keepalive) { try { Byte[] LInfor = new Byte[1024]; int msglen = client.Receive(LInfor, LInfor.Length, 0); Byte[] realDate = new Byte[msglen]; System.Buffer.BlockCopy(LInfor, 0, realDate, 0, msglen); Byte[] realDateDeCode = DES.Debyte(realDate, msglen);//解密 // CSharpGame.Message clientMsg = (CSharpGame.Message)(CSharpGame.SerializationUnit.DeserializeObject(realDate)); CSharpGame.Message clientMsg = (CSharpGame.Message)(CSharpGame.SerializationUnit.DeserializeObject(realDateDeCode)); switch (clientMsg.msgType) { case MsgType.Sys: { ProcessSysMsg(clientMsg, client); } break; case MsgType.Game: { ProcessGamMsg(clientMsg); } break; case MsgType.Chat: { } break; } } catch (System.Exception ex) { //用户意外掉线的处理,从hashtable中找到用户,然后在clients中干掉用户 string duanxian = (string)userandsocket[client]; if (duanxian != null) { userandsocket.Remove(client); MsgSys sysBroadcast = new MsgSys(); sysBroadcast.sysType = MsgSysType.Exit; sysBroadcast.sysContent = duanxian; BroadcastClient(new CSharpGame.Message(sysBroadcast)); int remove = findGameClient((string)sysBroadcast.sysContent); if (remove != -1) { GameClient gc = (GameClient)clients[remove]; if (gc.TableIdx != -1) { if (tables[gc.TableIdx].readycount != 0)//用户意外掉线后清空用户所在桌子数据 { tables[gc.TableIdx].readycount--; } if (tables[gc.TableIdx].usercount != 0) { tables[gc.TableIdx].usercount--; } } clients.RemoveAt(remove); } client.Close(); } } } }
private void ProcessSysMsg(CSharpGame.Message _sysMsg, Socket client) { MsgSys sysMsg = (MsgSys)_sysMsg.msgContent; string curr_user = _sysMsg.userSender; switch (sysMsg.sysType) { case MsgSysType.Login: { MsgSys s = new MsgSys(); s.sysType = MsgSysType.Judge; string[] user_pwd = (string[])sysMsg.sysContent; if (user_pwd == null || user_pwd.Length != 2 || JudgeUserLogin(user_pwd[0], user_pwd[1]) == false) { s.sysContent = false; GameClient newGC = new GameClient(user_pwd[0], null, clientservice, client); SendToClient(newGC, new CSharpGame.Message(s)); } else if (findGameClient(user_pwd[0]) != -1) //重名登录 { s.sysContent = false; GameClient newGC = new GameClient(user_pwd[0], null, clientservice, client); SendToClient(newGC, new CSharpGame.Message(s)); } else { s.sysContent = true; GameClient newGC = new GameClient(user_pwd[0], null, clientservice, client); clients.Add(newGC); userandsocket[client] = user_pwd[0]; SendToClient(newGC, new CSharpGame.Message(s)); } } break; case MsgSysType.Online: { GameClient newGC = new GameClient((string)sysMsg.sysContent, null, clientservice, client); if (clients.Count != 0) { // 写个 广播函数 // 因为会有很多 广播操作。。 MsgSys sysBroadcast = new MsgSys(); sysBroadcast.sysType = MsgSysType.Join; sysBroadcast.sysContent = sysMsg.sysContent; // BroadcastClient(new CSharpGame.Message(sysBroadcast)); foreach (GameClient cl in clients) { if (!cl.Name.Equals((string)sysMsg.sysContent)) { SendToClient(cl, new CSharpGame.Message(sysBroadcast)); } } Thread.Sleep(100); MsgSys sendback = new MsgSys(); sendback.sysType = MsgSysType.List; List <string> userList = GetUserNameList(); sendback.sysContent = userList; SendToClient(newGC, new CSharpGame.Message(sendback)); } Thread.Sleep(300); sendCurrentTables((string)sysMsg.sysContent); for (int i = 0; i < 9; i++) { if (tables[i].tabelEable) { MsgSys sysGameOn = new MsgSys(); sysGameOn.sysType = MsgSysType.GameOn; sysGameOn.sysContent = i; SendToClient(newGC, new CSharpGame.Message(sysGameOn)); } } } break; case MsgSysType.Offline: { MsgSys sysBroadcast = new MsgSys(); sysBroadcast.sysType = MsgSysType.Exit; sysBroadcast.sysContent = sysMsg.sysContent; //BroadcastClient(new CSharpGame.Message(sysBroadcast)); foreach (GameClient cl in clients) { if (!cl.Name.Equals((string)sysMsg.sysContent)) { SendToClient(cl, new CSharpGame.Message(sysBroadcast)); } } int remove = findGameClient((string)sysMsg.sysContent); if (remove != -1) { clients.RemoveAt(remove); } client.Close(); } break; case MsgSysType.Ready: { //客户端还要传桌子号过来 int find = findGameClient(curr_user); GameClient gl = (GameClient)clients[find]; total = (int)sysMsg.sysContent; //int tableIndex = (int)sysMsg.sysContent; //gl.TableIdx = tableIndex; int tableIndex = gl.TableIdx; tables[tableIndex].readycount++; ////发回玩家准备消息,显示界面 //MsgSys readyMsg = new MsgSys(); //readyMsg.sysType = MsgSysType.ReadyMsg; ////sysBroadcast.sysContent = //gameResetStatus[0]; //readyMsg.sysContent = curr_user; //CSharpGame.Message readymsg = new CSharpGame.Message(readyMsg); //readymsg.Num = tableIndex; //BroadcastRoom(readymsg); if (tables[tableIndex].readycount == tables[tableIndex].usercount) { //首先发送消息通知客户端,某房间游戏已经开始 MsgSys sysGameOn = new MsgSys(); sysGameOn.sysType = MsgSysType.GameOn; sysGameOn.sysContent = tableIndex; BroadcastClient(new CSharpGame.Message(sysGameOn)); // 完成牌数的功能 在InitGame前 设置tableInfo中的totalRound 以最后一个设置的为准 tables[tableIndex].totalRound = total; tables[tableIndex].tabelEable = true; //发送开始 InitTableGame(tableIndex); MsgSys sysBroadcast = new MsgSys(); sysBroadcast.sysType = MsgSysType.Begin; //sysBroadcast.sysContent = //gameResetStatus[0]; sysBroadcast.sysContent = tables[tableIndex].gameResetStatus[0]; CSharpGame.Message m = new CSharpGame.Message(sysBroadcast); m.Num = tableIndex; BroadcastRoom(m); } } break; case MsgSysType.GameStart: { //服务器生成初始数据,图片数组,副数广播 InitGameStatus(); //MsgSys sysBegin = new MsgSys(); //sysBegin.sysType = MsgSysType.Begin; //sysBegin.sysContent = gameResetStatus[0]; //SendToClient(newGC, new CSharpGame.Message(sysBegin)); MsgSys sysBroadcast = new MsgSys(); sysBroadcast.sysType = MsgSysType.Begin; sysBroadcast.sysContent = gameResetStatus[0]; BroadcastClient(new CSharpGame.Message(sysBroadcast)); } break; case MsgSysType.Seat: { // // 首先更新当前发生者gc的状态 // int[] temp = (int[])sysMsg.sysContent; tables[temp[0]].usercount++; string userSend = (string)_sysMsg.userSender; foreach (GameClient gc in clients) { if (gc.Name == userSend) { gc.TableIdx = temp[0]; gc.SeatIdx = temp[1]; } } // 把消息转发给所有人 通知客户端更新seat的信息 BroadcastClient(_sysMsg); } break; case MsgSysType.LeaveRoom: { // // 首先更新当前发生者gc的状态 // int[] temp = (int[])sysMsg.sysContent; if (tables[temp[0]].usercount != 0) { tables[temp[0]].usercount--; } string userSend = (string)_sysMsg.userSender; foreach (GameClient gc in clients) { if (gc.Name == userSend) { gc.TableIdx = -1; } } // 把消息转发给所有人 通知客户端更新seat的信息 BroadcastClient(_sysMsg); //if (tables[temp[0]].tabelEable) //{ // tables[temp[0]].readycount--; //} if (tables[temp[0]].readycount != 0) { tables[temp[0]].readycount--; } // 如果是在游戏中退出的,且如果是该游戏的最后一名玩家, // 则通知所有人该房间游戏结束 if (tables[temp[0]].readycount == 0 && tables[temp[0]].tabelEable) { MsgSys sysGameOver = new MsgSys(); sysGameOver.sysType = MsgSysType.GameOver; sysGameOver.sysContent = temp[0]; CSharpGame.Message m = new CSharpGame.Message(sysGameOver); m.userSender = "Server"; BroadcastClient(m); tables[temp[0]].tabelEable = false; } } break; case MsgSysType.TableNoSeat: { ; } break; case MsgSysType.FreshGameArea: { BroadcastRoom(_sysMsg); } break; } }