예제 #1
0
        /// <summary>
        /// Handles messages the server has received from clients.
        /// </summary>
        private static void handleIncomingMessages()
        {
            while ((msg = server.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                    case NetIncomingMessageType.Data:
                        handleDataMessage(msg);
                        break;
                    case NetIncomingMessageType.DiscoveryRequest:
                        handleDiscoveryRequestMessage(msg);
                        break;
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                        log.Info("Lidgren message: " + msg.ReadString());
                        break;
                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                        if (status == NetConnectionStatus.Connected)
                        {
                            log.Info("====> {0} connected ({1})", Helper.getRemoteTag(msg), msg.ReadString());

                            // Add the new player to the player list.
                            Player player = new Player(msg.SenderConnection.RemoteUniqueIdentifier);
                            player.Position = new Vector2(gameWidth / 2, gameHeight / 2);
                            playerManager.Add(msg.SenderConnection.RemoteUniqueIdentifier, player);

                            // Send them a list of all players in the game.
                            // They will already know their unique identifier.
                            NetOutgoingMessage om = server.CreateMessage();
                            S_PlayerListMessage playerListMessage = new S_PlayerListMessage()
                            {
                                PlayerCount = playerManager.Count,
                                Players = playerManager.Players,
                            };
                            playerListMessage.Write(om);
                            server.SendMessage(om, msg.SenderConnection, NetDeliveryMethod.ReliableUnordered);

                            // Tell all other players about the newly joined player.
                            om = server.CreateMessage();
                            S_PlayerJoinMessage playerJoinMessage = new S_PlayerJoinMessage()
                            {
                                PlayerRUI = player.RUI,
                                Color = player.Color
                            };
                            playerJoinMessage.Write(om);
                            server.SendToAll(om, msg.SenderConnection, NetDeliveryMethod.ReliableUnordered, 0);
                        }
                        else if (status == NetConnectionStatus.Disconnected || status == NetConnectionStatus.Disconnecting)
                        {
                            log.Info("<==== {0} disconnected ({1})", Helper.getRemoteTag(msg), msg.ReadString());
                        }
                        break;
                    case NetIncomingMessageType.ConnectionApproval:
                        // Just aprove everything for now.
                        msg.SenderConnection.Approve();
                        log.Info("Approved connection for {0}", Helper.getRemoteTag(msg));
                        break;
                    default:
                        log.Error("Unhandled message type: " + msg.MessageType);
                        break;
                }
            }
        }
예제 #2
0
        private void handleDataMessage(NetIncomingMessage msg)
        {
            //log.Info("Got data message: {0}", msg);

            MessageType type = (MessageType)msg.ReadByte();
            switch (type)
            {
                case MessageType.PlayerPosition:
                    // Batch player position message.
                    S_PlayerPositionMessage playerPositionMessage = new S_PlayerPositionMessage()
                    {
                        RUIList = new List<long>(),
                        PositionList = new List<Vector2>()
                    };
                    playerPositionMessage.Read(msg);
                    playerManager.UpdatePositions(playerPositionMessage.PlayerCount,
                        playerPositionMessage.RUIList, playerPositionMessage.PositionList);
                    break;
                case MessageType.PlayerJoin:
                    // A new player has joined, so add it to the local dictionary.
                    S_PlayerJoinMessage playerJoinMessage = new S_PlayerJoinMessage();
                    playerJoinMessage.Read(msg);
                    PlayerManager.Add(playerJoinMessage.PlayerRUI,
                        new Player(playerJoinMessage.PlayerRUI, Resources.playerTexture, playerJoinMessage.Color));
                    break;
                case MessageType.PlayerList:
                    S_PlayerListMessage playerListMessage = new S_PlayerListMessage();
                    playerListMessage.playerTexture = Resources.playerTexture;
                    playerListMessage.Read(msg);
                    log.Info("playersCount: {0}", playerListMessage.PlayerCount);
                    playerManager.AddAll(playerListMessage.Players);
                    // Find and set the local player from the players list.
                    if (!playerManager.Players.TryGetValue(client.UniqueIdentifier, out localPlayer))
                    {
                        // This should never happen.
                        log.Error("Unable to find local player in player list");
                        client.Disconnect("Error");
                    }
                    localPlayer.Sprite = new Sprite(Resources.playerTexture);
                    break;
                default:
                    log.Error("Unknown message type: {0}", type);
                    break;
            }
        }