예제 #1
0
    /// <summary>
    /// tcp
    /// </summary>
    /// <param name="xieyi"></param>
    /// <param name="userToken"></param>
    /// <returns></returns>
    public byte[] SelectMessage(MessageXieYi xieyi, AsyncUserToken userToken)
    {
        JObject json = null;

        byte[]             newBuffer          = null;
        byte[]             tempMessageContent = xieyi.MessageContent;
        int                roomID             = userToken.userInfo.RoomID;
        SingleRoom         room            = GetSingleRoomByID(roomID);
        Register           login           = null;
        RoomActorUpdate    roomActorUpdate = null;
        ActorMoveDirection moveDirection   = null;

        if (userToken == null)
        {
            Log4Debug("该用户已被清理,不处理接收数据。");
            return(newBuffer);
        }
        if (allRoom.RoomList.ContainsKey(userToken.userInfo.RoomID))
        {
            room = allRoom.RoomList[userToken.userInfo.RoomID];
        }

        //Log4Debug("/" + xieyi.MessageContentLength);
        //try
        {
            switch ((MessageConvention)xieyi.XieYiFirstFlag)
            {
            case MessageConvention.login:
                login     = SerializeHelper.Deserialize <Register>(tempMessageContent);
                newBuffer = Login(login, userToken);
                break;

            case MessageConvention.getHeartBeatTime:
                HeartbeatTime hbTime = new HeartbeatTime()
                {
                    time = AsyncIOCPServer.HeartbeatSecondTime
                };
                newBuffer = SerializeHelper.Serialize <HeartbeatTime>(hbTime);
                break;

            case MessageConvention.reConnectCheck:
                //检查是否是掉线用户
                ReconnctInfo rcInfo = new ReconnctInfo();
                if (OffLineRooms.ContainsKey(userToken.userInfo.Register.userID))
                {
                    int offRoomID = OffLineRooms[userToken.userInfo.Register.userID];
                    userToken.userInfo.RoomID = offRoomID;
                    allRoom.RoomList[offRoomID].ReConnect(userToken);

                    int removeCount = 0;
                    OffLineRooms.TryRemove(userToken.userInfo.Register.userID, out removeCount);
                    rcInfo.isReconnect = true;
                }
                else
                {
                    rcInfo.isReconnect = false;
                }
                newBuffer = SerializeHelper.Serialize <ReconnctInfo>(rcInfo);
                break;

            case MessageConvention.reConnectIndex:
                room.GetReConnectFrameData(userToken.userInfo.UniqueID);
                break;

            case MessageConvention.heartBeat:
                //心跳检测客户端传递服务器时间
                //Log4Debug("接收到 " + userToken.userInfo.Register.name + " 的心跳检测");
                //newBuffer = SerializeHelper.DateTimeToBytes(DateTime.Now);
                //newBuffer = SerializeHelper.Serialize<HeartbeatTime>(new HeartbeatTime() { time = AsyncIOCPServer.HeartbeatSecondTime });
                break;

            case MessageConvention.updateName:
                string updateName = SerializeHelper.ConvertToString(tempMessageContent);
                Log4Debug("修改人物信息:" + updateName);
                newBuffer = UpdateName(userToken, updateName);
                break;

            case MessageConvention.createRoom:
                json      = JObject.Parse(SerializeHelper.ConvertToString(xieyi.MessageContent));
                newBuffer = allRoom.CreateNewRoom(json, userToken);
                break;

            case MessageConvention.joinRoom:
                json      = JObject.Parse(SerializeHelper.ConvertToString(xieyi.MessageContent));
                newBuffer = allRoom.JoinRoom(json, userToken);
                break;

            case MessageConvention.updateRoom:
                Log4Debug("更新房间");
                json      = JObject.Parse(SerializeHelper.ConvertToString(xieyi.MessageContent));
                newBuffer = allRoom.UpdateRoom(json);
                break;

            case MessageConvention.getRoomInfo:
                newBuffer = SerializeHelper.Serialize <RoomInfo>(room.RoomInfo);
                break;

            case MessageConvention.getRoommateInfo:
                Log4Debug("请求房间人物信息:" + SerializeHelper.ConvertToString(tempMessageContent));
                roomActorUpdate = new RoomActorUpdate();
                roomActorUpdate.SetSendInfo(SerializeHelper.ConvertToString(tempMessageContent));
                //
                newBuffer = SerializeHelper.Serialize <List <RoomActor> >(new List <RoomActor>(room.ActorList.Values));
                break;

            case MessageConvention.quitRoom:
                QuitInfo qInfo = SerializeHelper.Deserialize <QuitInfo>(tempMessageContent);
                if (room != null)
                {
                    room.CheckQuit(userToken, qInfo);
                }
                break;

            case MessageConvention.updateActorAnimation:
                ActorNetAnimation netAnimation = new ActorNetAnimation();
                netAnimation.SetSendInfo(SerializeHelper.ConvertToString(tempMessageContent));
                //Log4Debug("set->" + netAnimation.animationIndex + "");
                room.UpdateAnimation(netAnimation);    //更新
                break;

            case MessageConvention.updateActorState:
                roomActorUpdate = new RoomActorUpdate();
                roomActorUpdate.SetSendInfo(SerializeHelper.ConvertToString(tempMessageContent));
                Log4Debug("站位:" + userToken.userInfo.UniqueID + "/" + userToken.userInfo.Register.name
                          + "用户站位:" + roomActorUpdate.userIndex +
                          "请求修改状态为->" + (RoomActorState)int.Parse(roomActorUpdate.update));
                //
                room.UpdateState(roomActorUpdate);    //更新
                break;

            case MessageConvention.prepareLocalModel:
                roomActorUpdate = new RoomActorUpdate();
                roomActorUpdate.SetSendInfo(SerializeHelper.ConvertToString(tempMessageContent));
                //
                room.UpdatePrepare(roomActorUpdate, userToken);    //更新
                break;

            case MessageConvention.getPreGameData:

                break;

            case MessageConvention.startGaming:

                break;

            case MessageConvention.endGaming:

                break;

            case MessageConvention.frameData:
                FrameInfo frame = SerializeHelper.Deserialize <FrameInfo>(tempMessageContent);
                newBuffer = room.GetBoardFrame(frame.frameIndex);
                //Log4Debug("用户" + userToken.userInfo.Register.name + "/请求帧数据:" + frame.frameIndex + "/" + room.RoomInfo.FrameIndex + "数据总长:" + newBuffer.Length);
                break;

            //case MessageConvention.setUDP:
            //    UDPLogin loginUDP = SerializeHelper.Deserialize<UDPLogin>(tempMessageContent);
            //    Log4Debug("收到登录UDP账号:" + loginUDP.login);
            //    room.UpdateUDP(userToken.userInfo.UniqueID, loginUDP);
            //    break;
            default:
                Log4Debug("TCP是否判断该协议:" + (MessageConvention)xieyi.XieYiFirstFlag);
                break;
            }
        }
        //catch (Exception error)
        //{
        //    Log4Debug("处理逻辑错误:" + error.Message);
        //}
        //  创建一个发送缓冲区。
        byte[] sendBuffer = null;
        if (newBuffer != null)//用户需要服务器返回值给自己的话
        {
            xieyi      = new MessageXieYi(xieyi.XieYiFirstFlag, xieyi.XieYiSecondFlag, newBuffer);
            sendBuffer = xieyi.ToBytes();
        }
        return(sendBuffer);
    }