diff --git a/src/php/lib/Grpc/AbstractCall.php b/src/php/lib/Grpc/AbstractCall.php index d81df970671..413d5966e13 100644 --- a/src/php/lib/Grpc/AbstractCall.php +++ b/src/php/lib/Grpc/AbstractCall.php @@ -47,6 +47,7 @@ abstract class AbstractCall { public function __construct(Channel $channel, $method, $deserialize) { $this->call = new Call($channel, $method, Timeval::inf_future()); $this->deserialize = $deserialize; + $this->metadata = null; } /** diff --git a/src/php/lib/Grpc/BidiStreamingCall.php b/src/php/lib/Grpc/BidiStreamingCall.php index 454f7621ae4..2afceafce9e 100644 --- a/src/php/lib/Grpc/BidiStreamingCall.php +++ b/src/php/lib/Grpc/BidiStreamingCall.php @@ -43,10 +43,7 @@ class BidiStreamingCall extends AbstractCall { * @param array $metadata Metadata to send with the call, if applicable */ public function start($metadata) { - $event = $this->call->start_batch([ - OP_SEND_INITIAL_METADATA => $metadata, - OP_RECV_INITIAL_METADATA => true]); - $this->metadata = $event->metadata; + $this->call->start_batch([OP_SEND_INITIAL_METADATA => $metadata]); } /** @@ -54,7 +51,14 @@ class BidiStreamingCall extends AbstractCall { * @return The next value from the server, or null if there is none */ public function read() { - $read_event = $this->call->start_batch([OP_RECV_MESSAGE => true]); + $batch = [OP_RECV_MESSAGE => true]; + if ($this->metadata === null) { + $batch[OP_RECV_INITIAL_METADATA] = true; + } + $read_event = $this->call->start_batch($batch); + if ($this->metadata === null) { + $this->metadata = $read_event->metadata; + } return $this->deserializeResponse($read_event->message); } diff --git a/src/php/lib/Grpc/ClientStreamingCall.php b/src/php/lib/Grpc/ClientStreamingCall.php index fa29037b428..ec585da9856 100644 --- a/src/php/lib/Grpc/ClientStreamingCall.php +++ b/src/php/lib/Grpc/ClientStreamingCall.php @@ -44,10 +44,7 @@ class ClientStreamingCall extends AbstractCall { * @param array $metadata Metadata to send with the call, if applicable */ public function start($arg_iter, $metadata = array()) { - $event = $this->call->start_batch([ - OP_SEND_INITIAL_METADATA => $metadata, - OP_RECV_INITIAL_METADATA => true]); - $this->metadata = $event->metadata; + $event = $this->call->start_batch([OP_SEND_INITIAL_METADATA => $metadata]); foreach($arg_iter as $arg) { $this->call->start_batch([OP_SEND_MESSAGE => $arg->serialize()]); } @@ -60,8 +57,10 @@ class ClientStreamingCall extends AbstractCall { */ public function wait() { $event = $this->call->start_batch([ + OP_RECV_INITIAL_METADATA => true, OP_RECV_MESSAGE => true, OP_RECV_STATUS_ON_CLIENT => true]); + $this->metadata = $event->metadata; return array($this->deserializeResponse($event->message), $event->status); } } \ No newline at end of file