Beispiel #1
0
        private void NetworkManagerMessageArrived(object sender, NetMessageArgs args)
        {
            NetIncomingMessage message = args.RawMessage;

            switch (message.MessageType)
            {
            case NetIncomingMessageType.StatusChanged:
                var statMsg = (NetConnectionStatus)message.ReadByte();
                if (statMsg == NetConnectionStatus.Disconnected)
                {
                    string disconnectMessage = message.ReadString();
                    UserInterfaceManager.AddComponent(new DisconnectedScreenBlocker(StateManager,
                                                                                    UserInterfaceManager,
                                                                                    ResourceCache,
                                                                                    disconnectMessage));
                }
                break;

            case NetIncomingMessageType.Data:
                var messageType = (NetMessages)message.ReadByte();
                switch (messageType)
                {
                case NetMessages.LobbyChat:
                    //TODO: Send player messages to a lobby chat
                    break;

                case NetMessages.PlayerList:
                    HandlePlayerList(message);
                    break;

                case NetMessages.WelcomeMessage:
                    HandleWelcomeMessage(message);
                    break;

                case NetMessages.ChatMessage:
                    HandleChatMessage(message);
                    break;

                case NetMessages.JoinGame:
                    HandleJoinGame();
                    break;
                }
                break;
            }
        }
        private void NetMgr_MessageArrived(object sender, NetMessageArgs e)
        {
            //Make sure we reset the position - we might receive this message after the gamestates.
            if (e.RawMessage.Position > 0)
            {
                e.RawMessage.Position = 0;
            }

            if (e.RawMessage.MessageType != NetIncomingMessageType.Data)
            {
                return;
            }

            switch ((NetMessages)e.RawMessage.PeekByte())
            {
            case NetMessages.ConsoleCommandReply:
                e.RawMessage.ReadByte();
                AddLine("< " + e.RawMessage.ReadString(), new Color4(65, 105, 225, 255));
                break;

            case NetMessages.ConsoleCommandRegister:
                e.RawMessage.ReadByte();
                for (ushort amount = e.RawMessage.ReadUInt16(); amount > 0; amount--)
                {
                    string commandName = e.RawMessage.ReadString();
                    // Do not do duplicate commands.
                    if (commands.ContainsKey(commandName))
                    {
                        AddLine("Server sent console command {0}, but we already have one with the same name. Ignoring." + commandName, Color4.White);
                        continue;
                    }

                    string description = e.RawMessage.ReadString();
                    string help        = e.RawMessage.ReadString();

                    var command = new ServerDummyCommand(commandName, help, description);
                    commands[commandName] = command;
                }
                break;
            }

            //Again, make sure we reset the position - we might get it before the gamestate and then that would break.
            e.RawMessage.Position = 0;
        }