1

I have been a fan for several years of MPD & GMPC on Linux. Recently took up the task to build a website with similar look & feel to GMPC. As a teacher I need a nice example of an Angular website and this is a nice 'pet project'.

All is going very well up until I used the command list Artist to list all the artists. If I use the MPC command line tool like this:

mpc list Artist

I get a lot of lines as expected. If I do a line count I get e.g. 1500 artists. However, when using PHP and a socket (fsockopen) only 16384 are received at max. And this leads up to about 600-650 artists being listed. I detect an EOF (feof function used) and stop reading. Reopening the socket does not help.

I tried a lot and even installed the last MPD version (0.21) on my dev-machine from source (hurrah!). Changed the MPD config for max_output_buffer_size to no avail. Checked if the new version was really started (from /usr/local/bin/mpd) and specified the right config file (/etc/mpd.conf).

Music Player Daemon 0.21.13 (0.21.13)
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.3 LTS
Release:    18.04
Codename:   bionic

I switched from a higher level PHP function to low-level sockets. This is my code:

<?php

  namespace MPD\Readers;

  const BUFFER_LENGTH = 8192 * 1024;

  class MPDConnectionReader
  {

    public $status;
    public $errNo;
    public $errStr;
    public $nrOfBytesRead;
    public $version;

    public function __construct()
    {
      $this->status = "";
      $this->errStr = "";
      $this->errNo = 0;
      $this->nrOfBytesRead = 0;
    }

    public function sendCommand(String $command)
    {
      return null;
    }

    public function readResponse()
    {
      return false;
    }
  }


  class MPDFileReader extends MPDConnectionReader
  {
    private $foldername;

    /**
     * MPDFileReader constructor.
     */
    public function __construct(String $foldername)
    {
      parent::__construct();
      $this->foldername = $foldername;
    }//constructor

    public function sendCommand(String $command)
    {
      return true;
    }
  }


  class MPDHTTPReader extends MPDConnectionReader
  {
    private $_socket;
    private $_host;
    private $_port;

    /**
     * MPDHTTPReader constructor.
     * @param $_host
     * @param $_port
     */
    public function __construct($host, $port)
    {
      parent::__construct();

      $this->_host = $host;
      $this->_port = $port;

      $this->openSocket();
    }//constructor

    private function openSocket()
    {
      $this->_socket = @socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));

      if ($this->_socket === FALSE) $this->handleSocketError("Could not connet");

      $status = @socket_connect($this->_socket, $this->_host, $this->_port);
      if ($status === FALSE) $this->handleSocketError("Could not connect socket");

      // after connect, MPD will send "MPD OK" + version number
      $this->version = socket_read($this->_socket, 2048, PHP_NORMAL_READ);

    }//openSocket()

    private function handleSocketError($functionalDescription)
    {
      $this->errNo = socket_last_error();
      $this->errStr = socket_strerror($this->errNo);
      throw (new \Exception($functionalDescription . "(" . $this->_host . ":" . $this->_port . ") ==> " . $this->errStr, $this->errNo));
    }//handleSocketError()

    public function __destruct()
    {
      if ($this->_socket !== false) socket_close($this->_socket);
    }//__destruct()

    public function sendCommand(String $command)
    {
      $buf = $command . "\n";
      $status = socket_write($this->_socket, $buf);

      if ($status === false) $this->handleSocketError("Could not send to socket");
      else $this->status = "ok";
    }//sendCommand()

    public function readResponse()
    {
      $response = "";
      $end_of_stream = false;

      do {
        $buf = socket_read($this->_socket, BUFFER_LENGTH, PHP_BINARY_READ);

        if ($buf === false) $this->handleSocketError("Could not read from socket");
        elseif ($buf === "") $end_of_stream = true;
        else {
          $response .= $buf;
          $this->nrOfBytesRead += strlen($buf);
        }
      } while (!$end_of_stream);

      return $response;
    }//readResponse()

  }//class

Invoked like this:

$httpreader = new \MPD\Readers\MPDHTTPReader("localhost","6600");
$api = new mpdInterface($httpreader);
$api->sendCommand($cmd . "\n");

$response = $api->connectionReader->readResponse();
$bytesRead = $api->connectionReader->nrOfBytesRead;

There is no error. After 16384 (16Kb?) exactly the data stops coming. If I keep on reading I get a socket error 104 (Connection reset by peer).

So what is wrong here?

Greetings Martin

Pianoman
  • 327
  • 2
  • 10

1 Answers1

1

After a tip from the MPD Forum (great thanks to Max) I got it working. There was a second \n in the command, breaking the protocol. The new readResponse function is below. Notice the use of the socket_read() function instead of the socket_recv.There is work for improvement, but for the sake of answering this question here it is:

public function readResponse()
{
  $response = "";
  $end_of_stream = false;

  do {
    $buf = socket_read($this->_socket, BUFFER_LENGTH, PHP_NORMAL_READ);

    if ($buf === "") $end_of_stream = true;
    elseif ($buf === false) $this->handleSocketError("Could not read from socket");
    else {
      if ($buf === "OK\n") $end_of_stream = true;
      else{
        $response .= $buf;
        $this->nrOfBytesRead += strlen($buf);
      }
    }
  } while (!$end_of_stream);

  return $response;
}//readResponse()
Pianoman
  • 327
  • 2
  • 10