1

多年来,我一直是 Linux 上的 MPD 和 GMPC 的粉丝。最近承担了建立一个与 GMPC 外观和感觉相似的网站的任务。作为一名教师,我需要一个很好的 Angular 网站示例,这是一个很好的“宠物项目”。

一切都很顺利,直到我使用命令 list Artist列出所有艺术家。如果我像这样使用 MPC 命令行工具:

mpc list Artist

正如预期的那样,我得到了很多行。如果我计算行数,我会得到例如 1500 位艺术家。但是,当使用 PHP 和套接字 (fsockopen) 时,最多只能接收 16384 个。这导致大约 600-650 位艺术家被列入名单。我检测到 EOF(使用了 feof 函数)并停止阅读。重新打开套接字没有帮助。

我尝试了很多,甚至在我的开发机器上从源代码安装了最后一个 MPD 版本(0.21)(哇!)。更改 MPD 配置max_output_buffer_size无济于事。检查新版本是否真的启动(从/usr/local/bin/mpd)并指定正确的配置文件(/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

我从高级 PHP 函数切换到低级套接字。这是我的代码:

<?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

像这样调用:

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

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

没有错误。在 16384 (16Kb?) 之后,数据就停止了。如果我继续阅读,我会收到一个套接字错误 104(对等方重置连接)。

那么这里有什么问题呢?

问候马丁

4

1 回答 1

1

在 MPD 论坛(非常感谢 Max)的提示之后,我得到了它的工作。命令中有第二个 \n ,破坏了协议。新的 readResponse 函数如下。请注意使用该 socket_read()函数而不是socket_recv.There 有待改进,但为了在这里回答这个问题,它是:

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()
于 2019-08-22T07:45:38.750 回答