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 sends a stream of messages and then gets 24 * a single response. 25 */ 26class ClientStreamingCall 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 * Write a single message to the server. This cannot be called after 43 * wait is called. 44 * 45 * @param ByteBuffer $data The data to write 46 * @param array $options An array of options, possible keys: 47 * 'flags' => a number (optional) 48 */ 49 public function write($data, array $options = []) 50 { 51 $message_array = ['message' => $this->_serializeMessage($data)]; 52 if (array_key_exists('flags', $options)) { 53 $message_array['flags'] = $options['flags']; 54 } 55 $this->call->startBatch([ 56 OP_SEND_MESSAGE => $message_array, 57 ]); 58 } 59 60 /** 61 * Wait for the server to respond with data and a status. 62 * 63 * @return array [response data, status] 64 */ 65 public function wait() 66 { 67 $event = $this->call->startBatch([ 68 OP_SEND_CLOSE_FROM_CLIENT => true, 69 OP_RECV_INITIAL_METADATA => true, 70 OP_RECV_MESSAGE => true, 71 OP_RECV_STATUS_ON_CLIENT => true, 72 ]); 73 $this->metadata = $event->metadata; 74 75 $status = $event->status; 76 $this->trailing_metadata = $status->metadata; 77 78 return [$this->_deserializeResponse($event->message), $status]; 79 } 80} 81