Exemplo n.º 1
0
    private void OnReceive(byte[] buffer, int size, IPEndPoint remotePoint)
    {
        if (remotePoint.Address.Equals(IPAddress.Parse("192.168.1.129")))
        {
            return;
        }

        Log4Debug("OnReceive() " + remotePoint + "/长度->" + size);

        udpUser info = null;

        if (ListUserKCP.ContainsKey(remotePoint))
        {
            info = ListUserKCP[remotePoint];
        }
        else//配表没有该用户信息
        {
            info = ListUserKCP.GetOrAdd(remotePoint, new udpUser()
            {
                lastPoint = remotePoint, roomId = -1
            });
        }

        byte[] sendBytes = SelectMessage(info, buffer);
        if (sendBytes != null)
        {
            SendMessage(info.lastPoint, sendBytes);
        }
    }
Exemplo n.º 2
0
 public void UpdateUDP(int unique, udpUser user)
 {
     Log4Debug("站位:" + unique + " udp地址:" + user.lastPoint);
     lock (udpUserInfo)
     {
         udpUserInfo[unique] = user;
     }
 }
Exemplo n.º 3
0
    /// <summary>
    /// udp
    /// </summary>
    /// <param name="data"></param>
    /// <param name="user"></param>
    /// <returns></returns>
    public byte[] SelectMessage(udpDatas data, udpUser user)
    {
        byte[]       newBuffer = null;
        MessageXieYi xieyi     = MessageXieYi.FromBytes(data.byteRecv);

        if (xieyi == null)
        {
            return(newBuffer);
        }

        byte[]             tempMessageContent = xieyi.MessageContent;
        ActorMoveDirection moveDirection      = null;
        int        roomID = user.roomId;
        SingleRoom room   = GetSingleRoomByID(roomID);
        UDPLogin   login  = null;

        switch ((MessageConvention)xieyi.XieYiFirstFlag)
        {
        case MessageConvention.setUDP:
            login       = SerializeHelper.Deserialize <UDPLogin>(tempMessageContent);
            login.login = user.lastPoint.ToString();
            user.roomId = login.roomID;
            user.unique = login.unique;
            Log4Debug("UDP login 房间号:" + login.roomID);
            newBuffer = SerializeHelper.Serialize <UDPLogin>(login);
            break;

        case MessageConvention.moveDirection:
            moveDirection = SerializeHelper.Deserialize <ActorMoveDirection>(tempMessageContent);
            if (room.ActorList[moveDirection.userIndex].CurState != RoomActorState.Dead)
            {
                //Log4Debug("将历史帧:" + moveDirection.frameIndex + "保存到" + (moveDirection.frameIndex + room.RoomInfo.frameInterval) + "/" + room.RoomInfo.FrameIndex);
                room.SetRecondFrame(xieyi.ToBytes());
                //Log4Debug("站位:" + moveDirection.userIndex + " 更新了方向:" + "["
                //    + moveDirection.direction.x + ","
                //    + moveDirection.direction.y + ","
                //    + moveDirection.direction.z + "]"
                //    + "/速度:" + moveDirection.speed);
            }
            else
            {
                Log4Debug("死亡用户不更新移动。");
            }
            break;

        case MessageConvention.rotateDirection:
            ActorRotateDirection netRotation = SerializeHelper.Deserialize <ActorRotateDirection>(tempMessageContent);
            if (room.ActorList[netRotation.userIndex].CurState != RoomActorState.Dead)
            {
                room.SetRecondFrame(xieyi.ToBytes());
                //Log4Debug("站位:" + netRotation.userIndex + " 更新了旋转:" + netRotation.rotateY);
            }
            else
            {
                Log4Debug("死亡用户不更新旋转。");
            }
            break;

        case MessageConvention.jump:
            ActorJump netJump = SerializeHelper.Deserialize <ActorJump>(tempMessageContent);
            if (room.ActorList[netJump.userIndex].CurState != RoomActorState.Dead)
            {
                room.SetRecondFrame(xieyi.ToBytes());
            }
            else
            {
                Log4Debug("死亡用户不更新跳跃。");
            }
            break;

        case MessageConvention.shootBullet:
            ShootInfo shootInfo = SerializeHelper.Deserialize <ShootInfo>(tempMessageContent);
            if (room.ActorList[shootInfo.userIndex].CurState != RoomActorState.Dead)
            {
                room.SetRecondFrame(xieyi.ToBytes());
                //Log4Debug("站位:" + netRotation.userIndex + " 更新了旋转:" + netRotation.rotateY);
            }
            else
            {
                Log4Debug("死亡用户不更新射击。");
            }
            break;

        case MessageConvention.bulletInfo:
            //
            BulletInfo bulletInfo = SerializeHelper.Deserialize <BulletInfo>(xieyi.MessageContent);
            room.UpdateBulletInfo(bulletInfo);    //更新
            //room.SetRecondFrame(xieyi.ToBytes());
            break;

        default:
            Log4Debug("检查协议->" + (MessageConvention)xieyi.XieYiFirstFlag);
            break;
        }

        byte[] sendBuffer = null;
        if (newBuffer != null)//用户需要服务器返回值给自己的话
        {
            xieyi      = new MessageXieYi(xieyi.XieYiFirstFlag, xieyi.XieYiSecondFlag, newBuffer);
            sendBuffer = xieyi.ToBytes();
        }
        return(sendBuffer);
    }