1<?php 2/* 3 * 4 * Copyright 2015 gRPC authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 */ 19 20namespace Grpc; 21 22/** 23 * Represents an active call that allows for sending and recieving messages 24 * in streams in any order. 25 */ 26class BidiStreamingCall extends AbstractCall 27{ 28 /** 29 * Start the call. 30 * 31 * @param array $metadata Metadata to send with the call, if applicable 32 * (optional) 33 */ 34 public function start(array $metadata = []) 35 { 36 $this->call->startBatch([ 37 OP_SEND_INITIAL_METADATA => $metadata, 38 ]); 39 } 40 41 /** 42 * Reads the next value from the server. 43 * 44 * @return mixed The next value from the server, or null if there is none 45 */ 46 public function read() 47 { 48 $batch = [OP_RECV_MESSAGE => true]; 49 if ($this->metadata === null) { 50 $batch[OP_RECV_INITIAL_METADATA] = true; 51 } 52 $read_event = $this->call->startBatch($batch); 53 if ($this->metadata === null) { 54 $this->metadata = $read_event->metadata; 55 } 56 57 return $this->_deserializeResponse($read_event->message); 58 } 59 60 /** 61 * Write a single message to the server. This cannot be called after 62 * writesDone is called. 63 * 64 * @param ByteBuffer $data The data to write 65 * @param array $options An array of options, possible keys: 66 * 'flags' => a number (optional) 67 */ 68 public function write($data, array $options = []) 69 { 70 $message_array = ['message' => $this->_serializeMessage($data)]; 71 if (array_key_exists('flags', $options)) { 72 $message_array['flags'] = $options['flags']; 73 } 74 $this->call->startBatch([ 75 OP_SEND_MESSAGE => $message_array, 76 ]); 77 } 78 79 /** 80 * Indicate that no more writes will be sent. 81 */ 82 public function writesDone() 83 { 84 $this->call->startBatch([ 85 OP_SEND_CLOSE_FROM_CLIENT => true, 86 ]); 87 } 88 89 /** 90 * Wait for the server to send the status, and return it. 91 * 92 * @return \stdClass The status object, with integer $code, string 93 * $details, and array $metadata members 94 */ 95 public function getStatus() 96 { 97 $status_event = $this->call->startBatch([ 98 OP_RECV_STATUS_ON_CLIENT => true, 99 ]); 100 101 $this->trailing_metadata = $status_event->status->metadata; 102 103 return $status_event->status; 104 } 105} 106