Пример #1
0
        /// <summary>
        /// Puts the client in idle mode for the given subsystems
        /// </summary>
        /// <param name="subsystems">The subsystems to listen to.</param>
        public void Idle(Mpc.Subsystems subsystems)
        {
            StringBuilder subs = new StringBuilder();

            foreach (Mpc.Subsystems s in Enum.GetValues(typeof(Mpc.Subsystems)))
            {
                if (s != Mpc.Subsystems.All && (subsystems & s) != 0)
                {
                    subs.AppendFormat(" {0}", s.ToString());
                }
            }
            string command = string.Format("idle {0}", subs.ToString());

            try
            {
                while (true)
                {
                    this.CheckConnected();
                    m_SocketManager.WriteLine(command);
                    MpdResponse res = this.readResponse();

                    Mpc.Subsystems eventSubsystems = Mpc.Subsystems.None;
                    foreach (string m in res.Message)
                    {
                        List <string> values = res.getValueList();
                        foreach (string sub in values)
                        {
                            Mpc.Subsystems s = Mpc.Subsystems.None;
                            if (Enum.TryParse <Mpc.Subsystems>(sub, out s))
                            {
                                eventSubsystems |= s;
                            }
                        }
                    }

                    if (eventSubsystems != Mpc.Subsystems.None && this.OnSubsystemsChanged != null)
                    {
                        this.OnSubsystemsChanged(this, eventSubsystems);
                    }
                }
            }
            catch (Exception)
            {
                try
                {
                    this.Disconnect();
                }
                catch (Exception) { }
            }
        }
Пример #2
0
        /// <summary>
        /// Connects to the MPD server who's IPEndPoint was set in the Server property.
        /// </summary>
        /// <exception cref="InvalidOperationException">If no IPEndPoint was set to the Server property.</exception>
        public void Connect()
        {
            if (this.ipEndPoint == null)
            {
                throw new InvalidOperationException("Server IPEndPoint not set.");
            }

            if (this.Connected)
            {
                throw new AlreadyConnectedException();
            }

            if (m_SocketManager != null)
            {
                m_SocketManager.Dispose();
            }

            m_SocketManager = new SocketManager();
            m_SocketManager.Connect(ipEndPoint);

            string firstLine = m_SocketManager.ReadLine();

            if (!firstLine.StartsWith(FIRST_LINE_PREFIX))
            {
                this.Disconnect();
                throw new InvalidDataException("Response of mpd does not start with \"" + FIRST_LINE_PREFIX + "\".");
            }
            this.version = firstLine.Substring(FIRST_LINE_PREFIX.Length);

            //m_SocketManager.WriteLine(string.Empty);
            //this.readResponse();

            MpdResponse response = Exec("commands");

            m_Commands = response.getValueList();

            if (this.OnConnected != null)
            {
                this.OnConnected.Invoke(this);
            }
        }