Skip to content

Commit

Permalink
feat: TCP supports custom header packing and unpacking format
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 17, 2019
1 parent c2c4b93 commit a6fbe93
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/tcp/src/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ class Protocol
*/
private $openLengthCheck = false;

/**
* Header pack format
*
* @var string
*/
private $headerPackFormat = self::HEADER_PACK_FORMAT;

/**
* Header unpack format
*
* @var string
*/
private $headerUnpackFormat = self::HEADER_UNPACK_FORMAT;

/**
* @link https://wiki.swoole.com/wiki/page/463.html
* @link https://www.php.net/manual/en/function.pack.php
Expand Down Expand Up @@ -239,7 +253,7 @@ public function packBody(string $body): string
}

// Use length check
$format = self::HEADER_PACK_FORMAT;
$format = $this->headerPackFormat;

// TODO
// Args sort please see self::HEADER_UNPACK_FORMAT
Expand All @@ -263,7 +277,7 @@ public function unpackData(string $data): array
}

// Use length check
$format = self::HEADER_UNPACK_FORMAT;
$format = $this->headerUnpackFormat;
$headLen = $this->packageBodyOffset;

// Like: ['type' => 'json', 'len' => 254, ]
Expand Down Expand Up @@ -541,4 +555,36 @@ public function setPackageMaxLength(int $packageMaxLength): void
{
$this->packageMaxLength = $packageMaxLength;
}

/**
* @return string
*/
public function getHeaderPackFormat(): string
{
return $this->headerPackFormat;
}

/**
* @param string $headerPackFormat
*/
public function setHeaderPackFormat(string $headerPackFormat): void
{
$this->headerPackFormat = $headerPackFormat;
}

/**
* @return string
*/
public function getHeaderUnpackFormat(): string
{
return $this->headerUnpackFormat;
}

/**
* @param string $headerUnpackFormat
*/
public function setHeaderUnpackFormat(string $headerUnpackFormat): void
{
$this->headerUnpackFormat = $headerUnpackFormat;
}
}

0 comments on commit a6fbe93

Please sign in to comment.