예제 #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("");
        }
예제 #2
0
        public override void HandleInput(InputState input)
        {
            inputs.resetStates();

            if (input.IsNewKeyPress(Keys.Enter))
            {
                if (!IsTypingMessage)
                    IsTypingMessage = true;
                else if (IsTypingMessage)
                {
                    IsTypingMessage = false;
                    string chat = keyboardInput.ToString();
                    if (!chat.Equals(""))  // no empty strings in chat
                    {
                        ChatMessagePacket msgPacket = new ChatMessagePacket();
                        msgPacket.message = chat;
                        NetworkManager.Instance.SendReliableData(msgPacket);
                        keyboardInput.Clear();
                    }
                }
            }
            if (input.IsNewKeyPress(Keys.Escape))
            {
                if (IsTypingMessage)
                {
                    IsTypingMessage = false;
                    keyboardInput.Clear();
                }
                else
                {
                    screenManager.AddScreen(new MainGameOptionsScreen(this));
                }
            }
            if (!IsTypingMessage)
            {
                inputs.Up = (input.CurrentKeyboardState.IsKeyDown(Keys.Up)) ? true : false;
                inputs.Down = (input.CurrentKeyboardState.IsKeyDown(Keys.Down)) ? true : false;
                inputs.Left = (input.CurrentKeyboardState.IsKeyDown(Keys.Left)) ? true : false;
                inputs.Right = (input.CurrentKeyboardState.IsKeyDown(Keys.Right)) ? true : false;
                inputs.Space = (input.CurrentKeyboardState.IsKeyDown(Keys.Space)) ? true : false;
            }
        }
예제 #3
0
        private void HandlePacket(NetIncomingMessage msg)
        {
            PacketType type = (PacketType)msg.ReadByte();
            switch (type)
            {
                case PacketType.UsernameKeywordCombo:
                    UsernameKeywordComboPacket unkwPacket = new UsernameKeywordComboPacket();
                    unkwPacket.Unpack(msg);
                    if (gameManager.PlayerSaves.ContainsKey(unkwPacket.username))
                    {
                        LoginVerificationPacket packet = new LoginVerificationPacket();
                        packet.message = "You are already logged in.";
                        ServerConsoleMessage(unkwPacket.username + " login error: Already logged in.");
                        SendUnconnectedMessage(packet, msg.SenderEndpoint);
                        //server.Connections.Remove(server.GetConnection(msg.SenderEndpoint));  // try
                    }
                    else
                    {
                        PlayerSave save = FileGrabber.getPlayerSave(isLocalGame, unkwPacket.username, unkwPacket.keyword);
                        if (!unkwPacket.keyword.Equals(save.Keyword))
                        {
                            LoginVerificationPacket packet = new LoginVerificationPacket();
                            packet.message = "Keyword does not match.";
                            ServerConsoleMessage(unkwPacket.username + " login error: Bad keyword.");
                            SendUnconnectedMessage(packet, msg.SenderEndpoint);
                            //server.Connections.Remove(server.GetConnection(msg.SenderEndpoint));  // try
                        }
                        else
                        {
                            LoginVerificationPacket packet = new LoginVerificationPacket();
                            packet.message = "verified";
                            gameManager.PlayerSaves.Add(unkwPacket.username, save);
                            gameManager.createPlayerEntityFromSave(unkwPacket.username);
                            gameManager.RUIDUsernames.Add(unkwPacket.username, msg.SenderConnection.RemoteUniqueIdentifier);
                            SendUnconnectedMessage(packet, msg.SenderEndpoint);
                            ServerConsoleMessage(unkwPacket.username + " has logged into the game.");
                            if (!isLocalGame)
                            {
                                ChatMessageQueue.Enqueue(new ChatMessage("SERVER", unkwPacket.username + " has connected."));
                            }
                        }
                    }
                    break;

                case PacketType.ChatMessage:
                    ChatMessagePacket chatPacket = new ChatMessagePacket();
                    chatPacket.Unpack(msg);
                    string username = gameManager.RUIDUsernames.GetValue(msg.SenderConnection.RemoteUniqueIdentifier);
                    if (!username.Equals(""))
                    {
                        ChatMessage cmsg = new ChatMessage(username, chatPacket.message);
                        ChatMessageQueue.Enqueue(cmsg);
                        ServerConsoleMessage(cmsg.getChatString());
                    }
                    break;

                case PacketType.InputsPacket:
                    InputsPacket inputsPacket = new InputsPacket();
                    inputsPacket.Unpack(msg);
                    string un = gameManager.RUIDUsernames.GetValue(msg.SenderConnection.RemoteUniqueIdentifier);
                    if (!un.Equals(""))
                    {
                        gameManager.HandlePlayerMoving(un, inputsPacket);
                    }
                    break;
            }
        }
예제 #4
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;
     }
 }