Esempio n. 1
0
        /// <summary>
        /// Reads the response from the MPD server and creates a string list from it
        /// </summary>
        /// <returns>Task<List<string>></returns>
        private async Task <IMpdResponse> ReadResponseAsync()
        {
            var response = new MpdResponse();

            string responseLine;

            do
            {
                responseLine = await _reader.ReadLineAsync() ?? string.Empty;

                response.RawResponse.Add(responseLine);
            }while (!(responseLine.Equals(Constants.Ok) || responseLine.StartsWith(Constants.Ack)));

            return(response);
        }
Esempio n. 2
0
        /// <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;
        }
Esempio n. 3
0
        /// <summary>
        /// Returns a MpdFile object from a MpdResponse object.
        /// </summary>
        /// <param name="response">The response of the MPD server.</param>
        /// <returns>A new MpdFile object build from the MpdResponse object.</returns>
        public static MpdFile build(MpdResponse response)
        {
            if (response == null)
                throw new ArgumentNullException("response");

            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;

            StringReader reader = new StringReader(response.ToString());
            List<string> ret = new List<string>();
            string line = reader.ReadLine();
            while (!(line.Equals("OK")  || line.StartsWith("ACK")))
            {
                ret.Add(line);
                line = reader.ReadLine();
                if (String.IsNullOrEmpty(line))
                {
                    line = "OK";
                }
            }

            file = ret[0].Substring(5);
            time = Int32.Parse(ret[1].Substring(5));
            artist = ret[2].Substring(7);
            album = ret[5].Substring(7);
            title = ret[4].Substring(7);
            track = ret[6].Substring(7);
            date = ret[7].Substring(5);
            genre = ret[8].Substring(7);
            disc = Int32.Parse(ret[9].Substring(5));
            pos = Int32.Parse(ret[10].Substring(4));
            id = Int32.Parse(ret[11].Substring(3));
          




           

            if (file == null)
                return null;
            else
                return new MpdFile(
                    file,
                    time,
                    album,
                    artist,
                    title,
                    track,
                    name,
                    genre,
                    date,
                    composer,
                    performer,
                    comment,
                    disc,
                    pos,
                    id);
        }
Esempio n. 4
0
 public ResponseBuilder New()
 {
     _response = new MpdResponse();
     _result   = Constants.Ok;
     return(this);
 }
Esempio n. 5
0
        private async Task <IMpdResponse> ReadBinaryResponseAsync()
        {
            var response = new MpdResponse()
            {
                BinaryChunk = new BinaryChunk()
            };

            try
            {
                int NewLine  = 10;
                var encoding = new UTF8Encoding(false, false);

                int  value;
                bool endReached   = false;
                var  stringBuffer = new MemoryStream();
                var  stream       = _reader.BaseStream;

                while (!endReached && (value = _reader.BaseStream.ReadByte()) != -1)
                {
                    // if it's not the newline, keep buffering the string
                    if (value != NewLine)
                    {
                        stringBuffer.WriteByte((byte)value);
                        continue;
                    }

                    // Got a newline. If there's nothing in the stringBuffer, then just move on
                    if (stringBuffer.Position == 0L)
                    {
                        continue;
                    }

                    // Buffered some string.
                    var line = encoding.GetString(stringBuffer.ToArray());
                    response.RawResponse.Add(line);

                    if (line.StartsWith(Constants.Ack) || line.StartsWith(Constants.Ok))
                    {
                        endReached = true;
                    }

                    var split = line.Split(": ");
                    if (split.Length > 1)
                    {
                        switch (split[0])
                        {
                        case "type":
                            response.BinaryChunk.MimeType = split[1];
                            break;

                        case "offset":
                            response.BinaryChunk.Offset = Convert.ToInt32(split[1]);
                            break;

                        case "size":
                            response.BinaryChunk.FullLength = Convert.ToInt64(split[1]);
                            break;

                        case "binary":
                            // data chunk follows immediately. Let's read it
                            var length = Convert.ToInt32(split[1]);
                            response.BinaryChunk.ChunkLength = length;
                            response.BinaryChunk.Binary      = new byte[length];
                            var chunkOffset = 0;
                            var bytesToRead = length;                                     // this is how many bytes we need to read
                            while (bytesToRead > 0)
                            {
                                // Read will not necessarily read as many bytes as we requested. This is why we're doing it in the loop.
                                var bytesRead = await _reader.BaseStream.ReadAsync(response.BinaryChunk.Binary, chunkOffset, bytesToRead);

                                bytesToRead -= bytesRead;
                                chunkOffset += bytesRead;
                            }
                            break;
                        }
                    }

                    // truncate the stringBuffer stream for the next line
                    stringBuffer.SetLength(0L);
                }

                if (stringBuffer.Position > 0L)
                {
                    // there's some string left in the buffer
                    response.RawResponse.Add(encoding.GetString(stringBuffer.ToArray()));
                }

                stringBuffer.Close();
            }
            catch (Exception e)
            {
                // TODO: communicate the error clearly
                Console.WriteLine(e.Message);
            }

            return(response);
        }