Ejemplo n.º 1
0
 /**
  * Try to read a frame from the server.
  *
  * @return Frame|false when no frame to read
  * @throws ConnectionException
  * @throws ErrorFrameException
  */
 public function readFrame()
 {
     if ($this->parser->hasBufferedFrames()) {
         return $this->parser->getFrame();
     }
     if (!$this->hasDataToRead()) {
         return false;
     }
     // See if there are newlines waiting to be processed (some brokers send empty lines as heartbeat)
     $this->gobbleNewLines();
     do {
         $read = @stream_get_line($this->connection, 8192, Parser::FRAME_END);
         if ($read === false || $read === '') {
             throw new ConnectionException('Was not possible to read data from stream.', $this->activeHost);
         }
         $this->parser->addData($read);
         // Include zero-byte back at the end
         if (strlen($read) != 8192) {
             $this->parser->addData(Parser::FRAME_END);
         }
     } while (!$this->parser->parse());
     // See if there are newlines after the \0
     $this->gobbleNewLines();
     $frame = $this->parser->getFrame();
     if ($frame->isErrorFrame()) {
         throw new ErrorFrameException($frame);
     }
     return $frame;
 }
Ejemplo n.º 2
0
 /**
  * Try to read a frame from the server.
  *
  * @return Frame|false when no frame to read
  * @throws ConnectionException
  * @throws ErrorFrameException
  */
 public function readFrame()
 {
     if ($this->parser->hasBufferedFrames()) {
         return $this->parser->getFrame();
     }
     if (!$this->hasDataToRead()) {
         return false;
     }
     do {
         $read = @fread($this->connection, 1024);
         if ($read === false || $read === '') {
             throw new ConnectionException('Was not possible to read data from stream.', $this->activeHost);
         }
         $this->parser->addData($read);
     } while (!$this->parser->parse());
     $frame = $this->parser->getFrame();
     if ($frame->isErrorFrame()) {
         throw new ErrorFrameException($frame);
     }
     return $frame;
 }