Skip to content

Commit

Permalink
Introduce Packer\PacketLength
Browse files Browse the repository at this point in the history
  • Loading branch information
rybakit committed May 16, 2019
1 parent 197b267 commit f1b1fc7
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 21 deletions.
7 changes: 3 additions & 4 deletions src/Connection/StreamConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
use Tarantool\Client\Exception\CommunicationFailed;
use Tarantool\Client\Exception\ConnectionFailed;
use Tarantool\Client\Greeting;
use Tarantool\Client\Packer\PackUtils;
use Tarantool\Client\Response;
use Tarantool\Client\Packer\PacketLength;

final class StreamConnection implements Connection
{
Expand Down Expand Up @@ -112,8 +111,8 @@ public function send(string $data) : string
throw new CommunicationFailed('Unable to write request.');
}

$length = $this->read(Response::LENGTH_SIZE_BYTES, 'Unable to read response length.');
$length = PackUtils::unpackLength($length);
$length = $this->read(PacketLength::SIZE_BYTES, 'Unable to read response length.');
$length = PacketLength::unpack($length);

return $this->read($length, 'Unable to read response.');
}
Expand Down
12 changes: 7 additions & 5 deletions src/Packer/PackUtils.php → src/Packer/PacketLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@

use Tarantool\Client\Exception\UnpackingFailed;

final class PackUtils
final class PacketLength
{
public const SIZE_BYTES = 5;

private function __construct()
{
}

public static function packLength(int $length) : string
public static function pack(int $length) : string
{
return \pack('CN', 0xce, $length);
}

public static function unpackLength(string $data) : int
public static function unpack(string $data) : int
{
if (false === $data = @\unpack('C_/Nlength', $data)) {
if (false === $data = @\unpack('C/N', $data)) {
throw new UnpackingFailed('Unable to unpack length value.');
}

return $data['length'];
return $data[1];
}
}
2 changes: 1 addition & 1 deletion src/Packer/PeclPacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function pack(Request $request, ?int $sync = null) : string
$this->packer->pack($sync ?: 0).
$this->packer->pack($request->getBody());

return PackUtils::packLength(\strlen($content)).$content;
return PacketLength::pack(\strlen($content)).$content;
}

public function unpack(string $data) : Response
Expand Down
2 changes: 1 addition & 1 deletion src/Packer/PurePacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function pack(Request $request, ?int $sync = null) : string
$this->packer->packInt($sync ?: 0).
$this->packer->packMap($request->getBody());

return PackUtils::packLength(\strlen($content)).$content;
return PacketLength::pack(\strlen($content)).$content;
}

public function unpack(string $data) : Response
Expand Down
1 change: 0 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

final class Response
{
public const LENGTH_SIZE_BYTES = 5;
public const TYPE_ERROR = 0x8000;

private $header;
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Connection/ReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Tarantool\Client\Tests\Integration\Connection;

use Tarantool\Client\Exception\CommunicationFailed;
use Tarantool\Client\Packer\PackUtils;
use Tarantool\Client\Packer\PacketLength;
use Tarantool\Client\Tests\GreetingDataProvider;
use Tarantool\Client\Tests\Integration\ClientBuilder;
use Tarantool\Client\Tests\Integration\FakeServer\FakeServerBuilder;
Expand Down Expand Up @@ -93,7 +93,7 @@ public function testUnableToReadResponse() : void

FakeServerBuilder::create(
new WriteHandler(GreetingDataProvider::generateGreeting()),
new WriteHandler(PackUtils::packLength(42)),
new WriteHandler(PacketLength::pack(42)),
new SleepHandler(1)
)
->setUri($clientBuilder->getUri())
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Connection/WriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Tarantool\Client\Tests\Integration\Connection;

use Tarantool\Client\Keys;
use Tarantool\Client\Packer\PackUtils;
use Tarantool\Client\Packer\PacketLength;
use Tarantool\Client\Tests\Integration\TestCase;

final class WriteTest extends TestCase
Expand All @@ -25,7 +25,7 @@ public function testSendMalformedRequest() : void
$conn = $handler->getConnection();

$data = 'malformed';
$data = PackUtils::packLength(strlen($data)).$data;
$data = PacketLength::pack(strlen($data)).$data;

$conn->open();
$data = $conn->send($data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@

use PHPUnit\Framework\TestCase;
use Tarantool\Client\Exception\UnpackingFailed;
use Tarantool\Client\Packer\PackUtils;
use Tarantool\Client\Packer\PacketLength;

final class PackUtilsTest extends TestCase
final class PacketLengthTest extends TestCase
{
public function testPackUnpackLength() : void
{
$packed = PackUtils::packLength(42);
$packed = PacketLength::pack(42);

self::assertIsString($packed);
self::assertSame(42, PackUtils::unpackLength($packed));
self::assertSame(42, PacketLength::unpack($packed));
}

public function testUnpackLengthFromMalformedData() : void
{
$this->expectException(UnpackingFailed::class);
$this->expectExceptionMessage('Unable to unpack length value.');

PackUtils::unpackLength('foo');
PacketLength::unpack('foo');
}
}

0 comments on commit f1b1fc7

Please sign in to comment.