| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* |
| 5 |
* Copyright 2020 gRPC authors. |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
* you may not use this file except in compliance with the License. |
| 9 |
* You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
* See the License for the specific language governing permissions and |
| 17 |
* limitations under the License. |
| 18 |
* |
| 19 |
*/ |
| 20 |
namespace Dudlewebs\WPMCS\GCP\Grpc; |
| 21 |
|
| 22 |
/** |
| 23 |
* This is an experimental and incomplete implementation of gRPC server |
| 24 |
* for PHP. APIs are _definitely_ going to be changed. |
| 25 |
* |
| 26 |
* DO NOT USE in production. |
| 27 |
*/ |
| 28 |
/** |
| 29 |
* Class RpcServer |
| 30 |
* @package Grpc |
| 31 |
*/ |
| 32 |
class RpcServer extends Server |
| 33 |
{ |
| 34 |
// [ <String method_full_path> => MethodDescriptor ] |
| 35 |
private $paths_map = []; |
| 36 |
private function waitForNextEvent() |
| 37 |
{ |
| 38 |
return $this->requestCall(); |
| 39 |
} |
| 40 |
/** |
| 41 |
* Add a service to this server |
| 42 |
* |
| 43 |
* @param Object $service The service to be added |
| 44 |
*/ |
| 45 |
public function handle($service) |
| 46 |
{ |
| 47 |
$methodDescriptors = $service->getMethodDescriptors(); |
| 48 |
$exist_methods = \array_intersect_key($this->paths_map, $methodDescriptors); |
| 49 |
if (!empty($exist_methods)) { |
| 50 |
\fwrite(\STDERR, "WARNING: " . 'override already registered methods: ' . \implode(', ', \array_keys($exist_methods)) . \PHP_EOL); |
| 51 |
} |
| 52 |
$this->paths_map = \array_merge($this->paths_map, $methodDescriptors); |
| 53 |
return $this->paths_map; |
| 54 |
} |
| 55 |
public function run() |
| 56 |
{ |
| 57 |
$this->start(); |
| 58 |
while (\true) { |
| 59 |
try { |
| 60 |
// This blocks until the server receives a request |
| 61 |
$event = $this->waitForNextEvent(); |
| 62 |
$full_path = $event->method; |
| 63 |
$context = new ServerContext($event); |
| 64 |
$server_writer = new ServerCallWriter($event->call, $context); |
| 65 |
if (!\array_key_exists($full_path, $this->paths_map)) { |
| 66 |
$context->setStatus(Status::unimplemented()); |
| 67 |
$server_writer->finish(); |
| 68 |
continue; |
| 69 |
} |
| 70 |
$method_desc = $this->paths_map[$full_path]; |
| 71 |
$server_reader = new ServerCallReader($event->call, $method_desc->request_type); |
| 72 |
try { |
| 73 |
$this->processCall($method_desc, $server_reader, $server_writer, $context); |
| 74 |
} catch (\Exception $e) { |
| 75 |
$context->setStatus(Status::status(STATUS_INTERNAL, $e->getMessage())); |
| 76 |
$server_writer->finish(); |
| 77 |
} |
| 78 |
} catch (\Exception $e) { |
| 79 |
\fwrite(\STDERR, "ERROR: " . $e->getMessage() . \PHP_EOL); |
| 80 |
exit(1); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
private function processCall(MethodDescriptor $method_desc, ServerCallReader $server_reader, ServerCallWriter $server_writer, ServerContext $context) |
| 85 |
{ |
| 86 |
// Dispatch to actual server logic |
| 87 |
switch ($method_desc->call_type) { |
| 88 |
case MethodDescriptor::UNARY_CALL: |
| 89 |
$request = $server_reader->read(); |
| 90 |
$response = \call_user_func(array($method_desc->service, $method_desc->method_name), $request ?? new $method_desc->request_type(), $context); |
| 91 |
$server_writer->finish($response); |
| 92 |
break; |
| 93 |
case MethodDescriptor::SERVER_STREAMING_CALL: |
| 94 |
$request = $server_reader->read(); |
| 95 |
\call_user_func(array($method_desc->service, $method_desc->method_name), $request ?? new $method_desc->request_type(), $server_writer, $context); |
| 96 |
break; |
| 97 |
case MethodDescriptor::CLIENT_STREAMING_CALL: |
| 98 |
$response = \call_user_func(array($method_desc->service, $method_desc->method_name), $server_reader, $context); |
| 99 |
$server_writer->finish($response); |
| 100 |
break; |
| 101 |
case MethodDescriptor::BIDI_STREAMING_CALL: |
| 102 |
\call_user_func(array($method_desc->service, $method_desc->method_name), $server_reader, $server_writer, $context); |
| 103 |
break; |
| 104 |
default: |
| 105 |
throw new \Exception(); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|