Пример #1
0
        public void Update()
        {
            NetIncomingMessage msg;

            while ((msg = _client.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.Data:
                {
                    //Read Packet Header (Byte) and Convert It To PacketTypes Enum
                    byte          PacketHeader = msg.ReadByte();
                    Packets.Types PacketType   = (Packets.Types)Enum.ToObject(typeof(Packets.Types), PacketHeader);

                    //Switch Read Functionality Based on Packet Header
                    switch (PacketType)
                    {
                    case Packets.Types.MOVE:
                    {
                        //Read Packet
                        Packets.MovePacket movePacket = new Packets.MovePacket();
                        Packets.ReadPacket(ref msg, ref movePacket);

                        //Update Local Simulation
                        InvokePlayerMove(new NetworkManager.PlayerMoveEventArgs(movePacket.uid, movePacket.posX, movePacket.posY, movePacket.facingDirection));

                        break;
                    }

                    case Packets.Types.ATTACK:
                    {
                        // Read packet.
                        Packets.AttackPacket attackPacket = new Packets.AttackPacket();
                        Packets.ReadPacket(ref msg, ref attackPacket);

                        // Update local simulation.
                        InvokePlayerAttack(new PlayerAttackEventArgs(attackPacket.uid, attackPacket.posX, attackPacket.posY, attackPacket.facingDirection));

                        break;
                    }

                    case Packets.Types.LOGIN_RESPONCE:
                    {
                        //Read Packet
                        Packets.LoginResponcePacket loginPacket = new Packets.LoginResponcePacket();
                        Packets.ReadPacket(ref msg, ref loginPacket);

                        // Invoke Event Handler
                        InvokeOnPlayerConnected(new PlayerConnectedEventArgs()
                                {
                                    playerPacket = loginPacket.playerPacket
                                });

                        break;
                    }

                    case Packets.Types.WORLDSTATE:
                    {
                        //Read Packet
                        Packets.WorldPacket worldPacket = new Packets.WorldPacket();
                        worldPacket.playerPackets = new List <Packets.PlayerPacket>();

                        Packets.ReadPacket(ref msg, ref worldPacket);

                        //Clear Engine Players
                        Player.ClearAllPlayersExcept(ControllablePlayer.ControllablePlayerInstance);

                        //Read Players
                        foreach (Packets.PlayerPacket p in worldPacket.playerPackets)
                        {
                            //Get Player Object From Packet
                            //Update Last Sync DesiredPosition If Login Packet Of Local Player
                            if (p.uid == _client.UniqueIdentifier)
                            {
                                InvokeOnLocalPlayerConnected(new PlayerConnectedEventArgs()
                                        {
                                            playerPacket = p
                                        });
                            }

                            else
                            {
                                InvokeOnPlayerConnected(new PlayerConnectedEventArgs()
                                        {
                                            playerPacket = p
                                        });
                            }
                        }

                        break;
                    }

                    case Packets.Types.DISCONNECT:
                    {
                        //Read Packet
                        Packets.DisconnectPacket disconnectPacket = new Packets.DisconnectPacket();
                        Packets.ReadPacket(ref msg, ref disconnectPacket);

                        // Invoke Event Handler
                        InvokeOnPlayerDisconnected(new PlayerDisconnectedEventArgs(disconnectPacket.uid));

                        break;
                    }
                    }

                    /*
                     *
                     * //Process Specific Handling Of Packet Types
                     * switch (Packet.GetPacketType())
                     * {
                     *     case PacketTypes.MOVE:
                     *         {
                     *             // ------------------ Read Move Packet -----------------------//
                     *
                     *             //Cast Packet to Login Type
                     *             MovePacket movePacket = (MovePacket)Packet;
                     *
                     *             InvokePlayerMove(new PlayerMoveEventArgs()
                     *             {
                     *                 uniqueID = movePacket.uniqueID,
                     *                 posX = movePacket.posX,
                     *                 posY = movePacket.posY
                     *             });
                     *
                     *             break;
                     *         }
                     *     case PacketTypes.WORLDSTATE:
                     *         {
                     *             /*
                     *             // ------------------ Read World Packet -----------------------//
                     *
                     *             //Cast Packet to World Type
                     *             WorldPacket packet = (WorldPacket)Packet;
                     *
                     *             //Load All Players
                     *             for (int i = 1; i <= packet.playerCount; i++)
                     *             {
                     *                 Player player = new Player();
                     *
                     *                 msg.ReadAllFields(player);
                     *                 msg.ReadAllProperties(player);
                     *
                     *                 //Update Last Sync DesiredPosition If Login Packet Of Local Player
                     *                 bool _local = (player.UniqueIdentifier == _client.UniqueIdentifier);
                     *                 if (_local)
                     *                 {
                     *                     _lastUpdateX = player.DesiredPosition.X;
                     *                     _lastUpdateY = player.DesiredPosition.Y;
                     *                 }
                     *
                     *                 // Invoke Event Handler
                     *                 InvokeOnPlayerConnected(new PlayerConnectedEventArgs()
                     *                 {
                     *                     player = player,
                     *                     local = _local
                     *                 });
                     *             }
                     *             break;
                     *         }
                     */
                }
                break;

                case NetIncomingMessageType.StatusChanged:
                {
                    int a = 0;
                    break;
                }
                }
                _client.Recycle(msg);
            }
        }
        private void ProcessMessages()
        {
            NetIncomingMessage msg;

            while ((msg = _server.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                    #region Data
                case NetIncomingMessageType.Data:
                {
                    //Read Packet Header (Byte) and Convert It To PacketTypes Enum
                    byte          PacketHeader = msg.ReadByte();
                    Packets.Types PacketType   = (Packets.Types)Enum.ToObject(typeof(Packets.Types), PacketHeader);

                    //Switch Read Functionality Based on Packet Header
                    switch (PacketType)
                    {
                    case Packets.Types.MOVE:
                    {
                        //Read Move Packet
                        Packets.MovePacket movePacket = new Packets.MovePacket();
                        Packets.ReadPacket(ref msg, ref movePacket);

                        //Update Local Simulation
                        InvokePlayerMove(new PlayerMoveEventArgs(movePacket.uid, movePacket.posX, movePacket.posY, movePacket.facingDirection));
                        //Foward Move Packet
                        NetOutgoingMessage outMsg = _server.CreateMessage();
                        Packets.WritePacket(ref outMsg, ref movePacket);
                        SendPacketExcept(outMsg, msg.SenderConnection, NetDeliveryMethod.UnreliableSequenced, 1);
                        break;
                    }

                    case Packets.Types.ATTACK:
                    {
                        // Read attack packet.
                        Packets.AttackPacket attackPacket = new Packets.AttackPacket();
                        Packets.ReadPacket(ref msg, ref attackPacket);

                        // Update local simulation.
                        InvokePlayerAttack(new PlayerAttackEventArgs(attackPacket.uid, attackPacket.posX, attackPacket.posY, attackPacket.facingDirection));

                        // Forward Attack packet.
                        NetOutgoingMessage outMsg = _server.CreateMessage();
                        Packets.WritePacket(ref outMsg, attackPacket);
                        SendPacketExcept(outMsg, msg.SenderConnection, NetDeliveryMethod.ReliableUnordered);
                        break;
                    }
                    }
                    break;
                }
                    #endregion Data

                // If incoming message is Request for connection approval
                // This is the very first packet/message that is sent from client
                // Here you can do new player initialisation stuff
                case NetIncomingMessageType.ConnectionApproval:
                {
                    //Read Packet Header (Byte) and Convert It To PacketTypes Enum
                    byte          PacketHeader = msg.ReadByte();
                    Packets.Types PacketType   =
                        (Packets.Types)Enum.ToObject(typeof(Packets.Types), PacketHeader);

                    //Switch Read Functionality Based on Packet Header
                    if (PacketType == Packets.Types.LOGIN_REQUEST)
                    {
                        Packets.PlayerPacket packet;

                        //LoginRequestPacket
                        {
                            //Create Packet
                            Packets.LoginRequestPacket loginPacket = new Packets.LoginRequestPacket();

                            //Read Packet Data From Message
                            Packets.ReadPacket(ref msg, ref loginPacket);

                            // Approve clients connection ( Its sort of agreement. "You can be my client and i will host you" )
                            msg.SenderConnection.Approve();

                            //Create new Player Instance
                            packet = new Packets.PlayerPacket(loginPacket.name, msg.SenderConnection.RemoteUniqueIdentifier, 50, 50, Player.FacingDirections.N);

                            // Invoke Event Handler
                            InvokeOnPlayerConnected(new PlayerConnectedEventArgs()
                                {
                                    playerPacket = packet
                                });
                        }


                        //LoginResponcePacket
                        {
                            Packets.LoginResponcePacket loginResponcePacket = new Packets.LoginResponcePacket(0, packet);

                            NetOutgoingMessage outMsg = _server.CreateMessage();
                            Packets.WritePacket(ref outMsg, ref loginResponcePacket);

                            _loginResponceQueue.Add(new MessageQueue()
                                {
                                    connection = msg.SenderConnection,
                                    msg        = outMsg,
                                    target     = MessageTarget.GLOBAL_EXCEPT
                                });
                        }

                        //World Packet
                        {
                            Packets.WorldPacket worldPacket = new Packets.WorldPacket(0);

                            foreach (var player in Player.AllPlayers)
                            {
                                worldPacket.playerCount++;
                                worldPacket.playerPackets.Add(Packets.CreatePlayerPacket(player));
                            }

                            NetOutgoingMessage outMsg = _server.CreateMessage();
                            Packets.WritePacket(ref outMsg, ref worldPacket);

                            _loginResponceQueue.Add(new MessageQueue()
                                {
                                    connection = msg.SenderConnection,
                                    msg        = outMsg,
                                    target     = MessageTarget.DIRECT
                                });
                        }
                    }        //if request pecket
                }
                break;

                case NetIncomingMessageType.ConnectionLatencyUpdated:
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.DiscoveryRequest:
                case NetIncomingMessageType.DiscoveryResponse:
                case NetIncomingMessageType.Error:
                case NetIncomingMessageType.ErrorMessage:
                case NetIncomingMessageType.NatIntroductionSuccess:
                case NetIncomingMessageType.Receipt:
                case NetIncomingMessageType.StatusChanged:
                {
                    NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                    switch (status)
                    {
                    case NetConnectionStatus.Connected:
                    {
                        //Inform Clients of Connection Event
                        NetOutgoingMessage    outMsg        = _server.CreateMessage();
                        Packets.ConnectPacket connectPacket = new Packets.ConnectPacket();
                        connectPacket.uid = msg.SenderConnection.RemoteUniqueIdentifier;

                        //Write Packet
                        Packets.WritePacket(ref outMsg, ref connectPacket);
                        SendPacketExcept(outMsg, msg.SenderConnection, NetDeliveryMethod.ReliableOrdered);

                        break;
                    }

                    case NetConnectionStatus.Disconnected:
                    {
                        //Inform Clients of Disconnection Event
                        NetOutgoingMessage       outMsg           = _server.CreateMessage();
                        Packets.DisconnectPacket disconnectPacket = new Packets.DisconnectPacket();
                        disconnectPacket.uid = msg.SenderConnection.RemoteUniqueIdentifier;

                        //Write Packet
                        Packets.WritePacket(ref outMsg, ref disconnectPacket);
                        SendPacketExcept(outMsg, msg.SenderConnection, NetDeliveryMethod.ReliableOrdered);

                        //Invoke Disconnected Event Handler
                        InvokeOnPlayerDisconnected(new PlayerDisconnectedEventArgs(msg.SenderConnection.RemoteUniqueIdentifier));
                        break;
                    }

                    case NetConnectionStatus.Disconnecting:
                    {
                        break;
                    }

                    case NetConnectionStatus.InitiatedConnect:
                    {
                        break;
                    }

                    case NetConnectionStatus.None:
                    {
                        break;
                    }

                    case NetConnectionStatus.RespondedConnect:
                    {
                        break;
                    }
                    }
                    break;
                }

                case NetIncomingMessageType.UnconnectedData:
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.WarningMessage:
                default:
                {
                    //Console.WriteLine("Unhandled type: " + msg.MessageType);
                    break;
                }
                }
                _server.Recycle(msg);
            }
        }