-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from spiral/feature/protobuf
Add protobuf support
- Loading branch information
Showing
10 changed files
with
190 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace PHPSTORM_META { | ||
|
||
// | ||
// Relay | ||
// | ||
|
||
registerArgumentsSet('goridge_relay_socket_type', | ||
\Spiral\Goridge\SocketRelay::SOCK_TCP, | ||
\Spiral\Goridge\SocketRelay::SOCK_UNIX, | ||
); | ||
|
||
expectedArguments(\Spiral\Goridge\SocketRelay::__construct(), 2, 'goridge_relay_socket_type'); | ||
|
||
// | ||
// RPC | ||
// | ||
|
||
registerArgumentsSet('goridge_rpc_options_json', | ||
\JSON_BIGINT_AS_STRING, | ||
\JSON_INVALID_UTF8_IGNORE, | ||
\JSON_INVALID_UTF8_SUBSTITUTE, | ||
\JSON_OBJECT_AS_ARRAY, | ||
\JSON_THROW_ON_ERROR, | ||
); | ||
|
||
registerArgumentsSet('goridge_rpc_options_msgpack', | ||
\MessagePack\UnpackOptions::BIGINT_AS_DEC, | ||
\MessagePack\UnpackOptions::BIGINT_AS_GMP, | ||
\MessagePack\UnpackOptions::BIGINT_AS_STR, | ||
); | ||
|
||
expectedArguments(\Spiral\Goridge\RPC\RPCInterface::call(), 2, 'goridge_rpc_options_json'); | ||
expectedArguments(\Spiral\Goridge\RPC\RPC::call(), 2, 'goridge_rpc_options_json'); | ||
expectedArguments(\Spiral\Goridge\RPC\RPCInterface::call(), 2, 'goridge_rpc_options_msgpack'); | ||
expectedArguments(\Spiral\Goridge\RPC\RPC::call(), 2, 'goridge_rpc_options_msgpack'); | ||
|
||
override(\Spiral\Goridge\RPC\RPCInterface::call(), map(['' => '@'])); | ||
override(\Spiral\Goridge\RPC\RPC::call(), map(['' => '@'])); | ||
|
||
// | ||
// RPC Methods | ||
// | ||
|
||
registerArgumentsSet('goridge_rpc_methods_informer', | ||
'informer.Workers', | ||
'informer.List', | ||
); | ||
|
||
expectedArguments(\Spiral\Goridge\RPC\RPCInterface::call(), 0, 'goridge_rpc_methods_informer'); | ||
expectedArguments(\Spiral\Goridge\RPC\RPC::call(), 0, 'goridge_rpc_methods_informer'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/** | ||
* Dead simple, high performance, drop-in bridge to Golang RPC with zero dependencies | ||
* | ||
* @author Wolfy-J | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Goridge\RPC\Codec; | ||
|
||
use Spiral\Goridge\Frame; | ||
use Google\Protobuf\Internal\Message; | ||
use Spiral\Goridge\RPC\CodecInterface; | ||
|
||
final class ProtobufCodec implements CodecInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const ERROR_DEPENDENCY = | ||
'Could not initialize protobuf codec. ' . | ||
'Please add "ext-protobuf" PECL extension or ' . | ||
'install "google/protobuf" Composer dependency.'; | ||
|
||
public function __construct() | ||
{ | ||
$this->assertAvailable(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
private function assertAvailable(): void | ||
{ | ||
if (!\class_exists(Message::class)) { | ||
throw new \LogicException(self::ERROR_DEPENDENCY); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getIndex(): int | ||
{ | ||
return Frame::CODEC_PROTO; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @psalm-suppress MixedInferredReturnType | ||
* @psalm-suppress MixedReturnStatement | ||
*/ | ||
public function encode($payload): string | ||
{ | ||
if ($payload instanceof Message) { | ||
return $payload->serializeToString(); | ||
} | ||
|
||
return $payload; | ||
} | ||
|
||
/** | ||
* @psalm-suppress UnsafeInstantiation | ||
* | ||
* @param class-string<Message> $class | ||
* @return Message | ||
*/ | ||
protected function create(string $class): Message | ||
{ | ||
return new $class(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function decode(string $payload, $options = null) | ||
{ | ||
if (\is_string($options) && \is_subclass_of($options, Message::class, true)) { | ||
$options = $this->create($options); | ||
} | ||
|
||
if ($options instanceof Message) { | ||
$options->mergeFromString($payload); | ||
|
||
return $options; | ||
} | ||
|
||
return $payload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters