public function send($data) { $count = socket_write($this->socket, $data, strlen($data)); $length = socket_read($this->socket, IProto::LENGTH_SIZE); $length = PackUtils::unpackLength($length); $data = socket_read($this->socket, $length); return $data; }
public function pack(Request $request, $sync = null) { $content = $this->packer->packMap([IProto::CODE => $request->getType(), IProto::SYNC => (int) $sync]); if (null !== ($body = $request->getBody())) { $content .= $this->packer->packMap($body); } return PackUtils::packLength(strlen($content)) . $content; }
/** * {@inheritdoc} */ public function pack(Request $request, $sync = null) { // @see https://github.com/msgpack/msgpack-php/issues/45 $content = pack('C*', 0x82, IProto::CODE, $request->getType(), IProto::SYNC); $content .= $this->packer->pack((int) $sync); if (null !== ($body = $request->getBody())) { $content .= $this->packer->pack($body); } return PackUtils::packLength(strlen($content)) . $content; }
/** * {@inheritdoc} */ public function unpack($data) { $headerSize = PackUtils::getHeaderSize($data); if (!($header = substr($data, 0, $headerSize))) { throw new Exception('Unable to unpack data.'); } $header = msgpack_unpack($header); if (!is_array($header)) { throw new Exception('Unable to unpack data.'); } $code = $header[IProto::CODE]; $body = substr($data, $headerSize); $body = msgpack_unpack($body); if ($code >= Request::TYPE_ERROR) { throw new Exception($body[IProto::ERROR], $code & Request::TYPE_ERROR - 1); } return new Response($header[IProto::SYNC], $body ? $body[IProto::DATA] : null); }
/** * @dataProvider provideHeaderData */ public function testGetHeaderSize($buffer, $expectedSize) { $this->assertSame($expectedSize, PackUtils::getHeaderSize($buffer)); }