Esempio n. 1
0
        public void Start()
        {
            this.server.Start();

            gsthread = new Thread(new ThreadStart(gameManager.Start));
            gsthread.Name = "GameStateManager";
            gsthread.Start();

            this.running = true;
            while (running)
            {
                // HANDLE INCOMING
                NetIncomingMessage msg;
                while ((msg = server.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                        case NetIncomingMessageType.DiscoveryRequest:
                            server.SendDiscoveryResponse(null, msg.SenderEndpoint);
                            break;
                        case NetIncomingMessageType.VerboseDebugMessage:
                            ServerConsoleMessage(msg.ReadString());
                            break;
                        case NetIncomingMessageType.DebugMessage:
                            ServerConsoleMessage(msg.ReadString());
                            break;
                        case NetIncomingMessageType.WarningMessage:
                            ServerConsoleMessage(msg.ReadString());
                            break;
                        case NetIncomingMessageType.ErrorMessage:
                            ServerConsoleMessage(msg.ReadString());
                            break;
                        case NetIncomingMessageType.StatusChanged:
                            switch ((NetConnectionStatus)msg.ReadByte())
                            {
                                case NetConnectionStatus.Connected:
                                    ServerConsoleMessage("connected");
                                    break;
                                case NetConnectionStatus.Disconnecting:
                                    ServerConsoleMessage("disconnecting");
                                    break;
                                case NetConnectionStatus.Disconnected:
                                    string user = gameManager.RUIDUsernames.GetValue(msg.SenderConnection.RemoteUniqueIdentifier);
                                    ServerConsoleMessage(user + " disconnected");
                                    DisconnectList.Add(user);
                                    gameManager.HandleDisconnect(isLocalGame, user);
                                    if (!isLocalGame)
                                    {
                                        ChatMessageQueue.Enqueue(new ChatMessage("SERVER", user + " has disconnected."));
                                    }
                                    else this.Stop();
                                    break;
                                case NetConnectionStatus.InitiatedConnect:
                                case NetConnectionStatus.RespondedConnect:
                                case NetConnectionStatus.RespondedAwaitingApproval:
                                case NetConnectionStatus.None:
                                    break;
                            }
                            break;
                        case NetIncomingMessageType.Data:
                            HandlePacket(msg);
                            break;
                    }
                }

                // HANDLE OUTGOING
                now = NetTime.Now;
                if (now > nextUpdate)
                {
                    List<ChatMessage> chats = getChatMessageList();

                    // for each connected client
                    foreach (NetConnection connection in server.Connections)
                    {
                        // send all chat messages in queue
                        if (chats.Count > 0)
                        {
                            foreach (ChatMessage chatmsg in chats)
                            {
                                ChatMessagePacket chatpacket = new ChatMessagePacket();
                                chatpacket.username = chatmsg.getRealNameString();
                                chatpacket.message = chatmsg.getChatMessageString();
                                SendReliableData(chatpacket, connection);
                            }
                        }

                        // send notifications of disconnect
                        if (DisconnectList.Count > 0)
                        {
                            foreach (string name in DisconnectList)
                            {
                                if (!name.Equals(""))
                                {
                                    DisconnectPacket disPacket = new DisconnectPacket();
                                    disPacket.username = name;
                                    SendReliableData(disPacket, connection);
                                }
                            }
                            DisconnectList.Clear();
                        }

                        // update all player locations
                        if (gameManager.PlayerEntities.Count > 0)
                        {
                            List<Entity> players = gameManager.PlayerEntities.Values.ToList();
                            foreach (Entity entity in players)
                            {
                                PlayerPositionPacket positionPacket = new PlayerPositionPacket();
                                positionPacket.username = (entity.GetComponent("Username") as Username).UserNm;
                                positionPacket.position = (entity.GetComponent("Position") as Position).Vector2Pos;
                                positionPacket.sequence = (entity.GetComponent("InputSequence") as InputSequence).Sequence;
                                SendUnreliableData(positionPacket, connection);
                            }
                        }

                        // update all mob locations
                        if (gameManager.mobCount > 0)
                        {
                            foreach (Entity mob in gameManager.Mobs)
                            {
                                MobPositionPacket positionPacket = new MobPositionPacket();
                                positionPacket.username = (mob.GetComponent("Username") as Username).UserNm;
                                positionPacket.position = (mob.GetComponent("Position") as Position).Vector2Pos;
                                positionPacket.id = (mob.GetComponent("MobID") as MobID).ID;
                                SendUnreliableData(positionPacket, connection);
                            }
                        }
                    }

                    nextUpdate += (1.0 / GameConstants.SERVER_UPDATE_RATE);
                }

                Thread.Sleep(1);
            }
            ServerConsoleMessage("Stopping Server...");
            this.server.Shutdown("");
        }
        public void HandleNewPlayerPosition(PlayerPositionPacket packet)
        {
            // http://www.gabrielgambetta.com/?p=22
            // See "Server reconciliation"
            CheckForPlayerEntity(packet.username);

            if (Username == packet.username) // remember this only applies to us, the player
            {
                Vector2 newPosition = inputPredictionManager.getReconciledPosition(packet.sequence, packet.position);
                PlayerEntities[packet.username].DoAction("ChangeAbsPosition", new ChangePositionArgs(newPosition));
            }
            else
            {
                Vector2 oldPos = (PlayerEntities[packet.username].GetComponent("Position") as Position).Vector2Pos;
                PlayerEntities[packet.username].DoAction("ChangeAbsPreviousPosition", new ChangePositionArgs(oldPos));
                PlayerEntities[packet.username].DoAction("ChangeAbsPosition", new ChangePositionArgs(packet.position));
                PlayerEntities[packet.username].DoAction("ChangeCurrentSmoothing", new ChangeCurrentSmoothingArgs(1));
            }
        }
Esempio n. 3
0
 private void HandleMessage(NetIncomingMessage msg)
 {
     PacketType type = (PacketType)msg.ReadByte();
     switch (type)
     {
         case PacketType.ChatMessage:
             ChatMessagePacket chatPacket = new ChatMessagePacket();
             chatPacket.Unpack(msg);
             ChatManager.Instance.addMessage(chatPacket.username, chatPacket.message);
             break;
         case PacketType.PlayerPosition:
             PlayerPositionPacket posPacket = new PlayerPositionPacket();
             posPacket.Unpack(msg);
             ClientGameManager.Instance.HandleNewPlayerPosition(posPacket);
             break;
         case PacketType.Disconnect:
             DisconnectPacket disPacket = new DisconnectPacket();
             disPacket.Unpack(msg);
             ClientGameManager.Instance.HandlePlayerDisconnect(disPacket.username);
             break;
         case PacketType.MobPosition:
             MobPositionPacket mpPacket = new MobPositionPacket();
             mpPacket.Unpack(msg);
             ClientGameManager.Instance.HandleMobMovement(mpPacket);
             break;
     }
 }