Example #1
0
 public function testWrite()
 {
     $this->connection = new \Nsq\Connection\Connection("127.0.0.1", 4150);
     $this->encoder = new \Nsq\Encoding\Encoder();
     $this->assertTrue($this->connection->write($this->encoder->magic()));
     $this->assertTrue($this->connection->write($this->encoder->pub("phpnsq_1", "test")));
     $decoder = new \Nsq\Encoding\Decoder();
     $frame = $decoder->readFame($this->connection, true);
     $this->assertEquals("OK", $frame->getContent());
 }
Example #2
0
 /**
  * @param Connection $con
  * @param bool|true $block
  * @return bool|Frame
  * @throws EncodingException
  * @throws \Nsq\Exception\ConnectionException
  */
 public function readFame(Connection $con, $block = true)
 {
     $data = $size = $type = $content = NULL;
     if (!$this->buffer->isEmpty()) {
         $data = $this->buffer->get();
     }
     // read size
     if (strlen($data) < 4) {
         $ret = $con->read(4 - strlen($data), $block);
         if ($ret === false) {
             return false;
         }
         $data .= $ret;
         $this->buffer->append($ret);
     }
     $size = $this->readSize($data);
     // read type
     if (strlen($data) < 8) {
         $ret = $con->read(8 - strlen($data), $block);
         if ($ret === false) {
             return false;
         }
         $data .= $ret;
         $this->buffer->append($ret);
     }
     $type = $this->readType($data);
     // read content
     if (strlen($data) < 8 + $size) {
         $ret = $con->read(4 + $size - strlen($data), $block);
         if ($ret === false) {
             return false;
         }
         $data .= $ret;
         $this->buffer->append($ret);
     }
     $content = $this->readContent($data);
     $this->buffer->sub(4 + $size);
     return new Frame($size, $type, $content);
 }