public void CreateBattle() { int randSeed = UnityEngine.Random.Range(0, 100); ThreadPool.QueueUserWorkItem((obj) => { dic_battleUserUid = new Dictionary <int, int>(); dic_udp = new Dictionary <int, ServerClientUdp>(); dic_battleReady = new Dictionary <int, bool>(); int userBattleID = 1; TcpEnterBattleMessage bm = new TcpEnterBattleMessage(randSeed); foreach (MatchUserInfo info in group) { dic_battleUserUid[info.uid] = userBattleID; ServerClientUdp clientUdp = new ServerClientUdp(info.conn.GetIP(), info.uid, HandleMsg); clientUdp.StartClientUdp(); dic_udp[userBattleID] = clientUdp; dic_battleReady[userBattleID] = false; BattleUserInfo _bUser = new BattleUserInfo(info.uid, userBattleID); bm.Add(_bUser); userBattleID++; } foreach (MatchUserInfo info in group) { // 发送BattleEnterMessage Protocol p = new Protocol(bm); info.conn.Send(new Protocol(bm)); } }); }
private void HandleMsg(ServerClientUdp udp, Protocol proto) { switch (proto.ClassName) { case nameof(UdpBattleReadyMessage): { UdpBattleReadyMessage msg = proto.Decode <UdpBattleReadyMessage>(); Debug.Log("客户端完成战斗加载"); // 接收战斗准备 CheckBattleBegin(msg.BattleID); dic_udp[msg.BattleID].RecvClientReady(msg.UID); } break; case nameof(UdpUpPlayerOperation): { UdpUpPlayerOperation msg = proto.Decode <UdpUpPlayerOperation>(); //Debug.Log("服务器接收到玩家的操作信息"); UpdatePlayerOperation(msg.PlayerOperation, msg.MsgID); } break; case nameof(UdpUpDeltaFrames): { UdpUpDeltaFrames msg = proto.Decode <UdpUpDeltaFrames>(); UdpDownDeltaFrames _downData = new UdpDownDeltaFrames(); for (int i = 0; i < msg.Frames.Count; i++) { int frameIndex = msg.Frames[i]; UdpDownFrameOperations _downOps = new UdpDownFrameOperations(); _downOps.FrameID = frameIndex; _downOps.Ops = dic_gameOperation[frameIndex]; _downData.FramesData.Add(_downOps); } dic_udp[msg.BattleID].SendMessage(new Protocol(_downData)); } break; default: { Debug.LogError("未知客户端UDP信息"); } break; } }