/// <summary> /// Executes a MPD command with arguments on the MPD server. /// </summary> /// <param name="command">The command to execute.</param> /// <param name="argument">The arguments of the command.</param> /// <returns>The MPD server response parsed into a basic object.</returns> /// <exception cref="ArgumentException">If the command contains a space of a newline charakter.</exception> public MpdResponse Exec(string command, string[] argument) { if (command == null) { throw new ArgumentNullException("command"); } if (command.Contains(" ")) { throw new ArgumentException("command contains space"); } if (command.Contains("\n")) { throw new ArgumentException("command contains newline"); } if (argument == null) { throw new ArgumentNullException("argument"); } for (int i = 0; i < argument.Length; i++) { if (argument[i] == null) { throw new ArgumentNullException("argument[" + i + "]"); } if (argument[i].Contains("\n")) { throw new ArgumentException("argument[" + i + "] contains newline"); } } //if (m_Commands != null && !m_Commands.Contains(command)) // return new MpdResponse(new ReadOnlyCollection<string>(new List<string>())); try { this.CheckConnected(); m_Mutex.WaitOne(); m_SocketManager.WriteLine(string.Format("{0} {1}", command, string.Join(" ", argument))); MpdResponse res = this.readResponse(); m_Mutex.ReleaseMutex(); return(res); } catch (Exception) { try { this.Disconnect(); } catch (Exception) { } return(new MpdResponse(new ReadOnlyCollection <string>(new List <string>()))); //throw; } }
/// <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) { } } }
/// <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); } }
/// <summary> /// Executes a simple command without arguments on the MPD server and returns the response. /// </summary> /// <param name="command">The command to execute.</param> /// <returns>The MPD server response parsed into a basic object.</returns> /// <exception cref="ArgumentException">If the command contains a space of a newline charakter.</exception> public MpdResponse Exec(string command) { if (command == null) { throw new ArgumentNullException("command"); } if (command.Contains(" ")) { throw new ArgumentException("command contains space"); } if (command.Contains("\n")) { throw new ArgumentException("command contains newline"); } //if (m_Commands != null && !m_Commands.Contains(command)) // return new MpdResponse(new ReadOnlyCollection<string>(new List<string>())); try { this.CheckConnected(); m_Mutex.WaitOne(); m_SocketManager.WriteLine(command); MpdResponse res = this.readResponse(); m_Mutex.ReleaseMutex(); return(res); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format("Exec: {0}", ex.Message)); try { this.Disconnect(); } catch (Exception) { } return(new MpdResponse(new ReadOnlyCollection <string>(new List <string>()))); //throw; } }
/// <summary> /// Creates a new MpdResponseEnumerator. /// </summary> /// <param name="response">The response to enumerate over.</param> protected internal MpdResponseEnumerator(MpdResponse response) { this.response = response; }
/// <summary> /// Builds a list of MpdFile objects from a MpdResponse object. /// </summary> /// <param name="response">The MpdResponse object to build the list of MpdFiles from.</param> /// <returns>A list ob MpdFiles built from the MpdResponse object.</returns> public static List <MpdFile> buildList(MpdResponse response) { if (response == null) { throw new ArgumentNullException("response"); } List <MpdFile> ret = new List <MpdFile>(); string file = null; int time = NO_TIME; string album = NO_ALBUM; string artist = NO_ARTIST; string title = NO_TITLE; string track = NO_TRACK; string name = NO_NAME; string genre = NO_GENRE; string date = NO_DATE; string composer = NO_COMPOSER; string performer = NO_PERFORMER; string comment = NO_COMMENT; int disc = NO_DISC; int pos = NO_POS; int id = NO_ID; foreach (KeyValuePair <string, string> line in response) { if (line.Key != null) { switch (line.Key) { case TAG_FILE: if (file != null) { ret.Add(new MpdFile( file, time, album, artist, title, track, name, genre, date, composer, performer, comment, disc, pos, id)); } file = line.Value; time = NO_TIME; album = NO_ALBUM; artist = NO_ARTIST; title = NO_TITLE; track = NO_TRACK; name = NO_NAME; genre = NO_GENRE; date = NO_DATE; composer = NO_COMPOSER; performer = NO_PERFORMER; comment = NO_COMMENT; disc = NO_DISC; pos = NO_POS; id = NO_ID; break; case TAG_TIME: int tryTime; if (int.TryParse(line.Value, out tryTime)) { time = tryTime; } break; case TAG_ALBUM: album = line.Value; break; case TAG_ARTIST: artist = line.Value; break; case TAG_TITLE: title = line.Value; break; case TAG_TRACK: track = line.Value; /* * int tryTrack; * if (int.TryParse(line.Value, out tryTrack)) * track = tryTrack; */ break; case TAG_NAME: name = line.Value; break; case TAG_GENRE: genre = line.Value; break; case TAG_DATE: date = line.Value; break; case TAG_COMPOSER: composer = line.Value; break; case TAG_PERFORMER: performer = line.Value; break; case TAG_COMMENT: comment = line.Value; break; case TAG_DISC: int tryDisc; if (int.TryParse(line.Value, out tryDisc)) { disc = tryDisc; } break; case TAG_POS: int tryPos; if (int.TryParse(line.Value, out tryPos)) { pos = tryPos; } break; case TAG_ID: int tryId; if (int.TryParse(line.Value, out tryId)) { id = tryId; } break; } } } if (file != null) { ret.Add(new MpdFile( file, time, album, artist, title, track, name, genre, date, composer, performer, comment, disc, pos, id)); } return(ret); }