mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 1 | <?php |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 2 | /* |
| 3 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 4 | * Copyright 2015 gRPC authors. |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 5 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 9 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 11 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Craig Tiller | 2e498aa | 2015-02-16 12:09:31 -0800 | [diff] [blame] | 17 | * |
| 18 | */ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 19 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 20 | namespace Grpc; |
| 21 | |
| 22 | /** |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 23 | * Represents an active call that allows for sending and recieving messages |
| 24 | * in streams in any order. |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 25 | */ |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 26 | class BidiStreamingCall extends AbstractCall |
| 27 | { |
| 28 | /** |
| 29 | * Start the call. |
| 30 | * |
| 31 | * @param array $metadata Metadata to send with the call, if applicable |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 32 | * (optional) |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 33 | */ |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 34 | public function start(array $metadata = []) |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 35 | { |
| 36 | $this->call->startBatch([ |
| 37 | OP_SEND_INITIAL_METADATA => $metadata, |
| 38 | ]); |
murgatroid99 | d8cc6b8 | 2015-04-01 11:14:16 -0700 | [diff] [blame] | 39 | } |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Reads the next value from the server. |
| 43 | * |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 44 | * @return mixed The next value from the server, or null if there is none |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 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 | |
thinkerou | 8772a36 | 2017-01-20 18:20:47 +0800 | [diff] [blame] | 57 | return $this->_deserializeResponse($read_event->message); |
murgatroid99 | d8cc6b8 | 2015-04-01 11:14:16 -0700 | [diff] [blame] | 58 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 59 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 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 |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 65 | * @param array $options An array of options, possible keys: |
| 66 | * 'flags' => a number (optional) |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 67 | */ |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 68 | public function write($data, array $options = []) |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 69 | { |
thinkerou | 8772a36 | 2017-01-20 18:20:47 +0800 | [diff] [blame] | 70 | $message_array = ['message' => $this->_serializeMessage($data)]; |
Stanislav Pavlovichev | d58199b | 2016-08-23 23:20:34 +0700 | [diff] [blame] | 71 | if (array_key_exists('flags', $options)) { |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 72 | $message_array['flags'] = $options['flags']; |
| 73 | } |
| 74 | $this->call->startBatch([ |
| 75 | OP_SEND_MESSAGE => $message_array, |
| 76 | ]); |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 77 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 78 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 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 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 88 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Wait for the server to send the status, and return it. |
| 91 | * |
thinkerou | 0c3e8db | 2016-12-15 00:27:03 +0800 | [diff] [blame] | 92 | * @return \stdClass The status object, with integer $code, string |
| 93 | * $details, and array $metadata members |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 94 | */ |
| 95 | public function getStatus() |
| 96 | { |
| 97 | $status_event = $this->call->startBatch([ |
| 98 | OP_RECV_STATUS_ON_CLIENT => true, |
| 99 | ]); |
| 100 | |
Stanley Cheung | 6668d51 | 2016-05-18 14:05:09 -0700 | [diff] [blame] | 101 | $this->trailing_metadata = $status_event->status->metadata; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 102 | |
Stanley Cheung | d5b2056 | 2015-10-27 13:27:05 -0700 | [diff] [blame] | 103 | return $status_event->status; |
| 104 | } |
| 105 | } |