blob: 2c0aa9f8d9e6b1b2a4bb7dda0d9b6184fe0aa043 [file] [log] [blame]
mlumishb892a272014-12-09 16:28:23 -08001<?php
Craig Tiller2e498aa2015-02-16 12:09:31 -08002/*
3 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004 * Copyright 2015 gRPC authors.
Craig Tiller2e498aa2015-02-16 12:09:31 -08005 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02006 * 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 Tiller2e498aa2015-02-16 12:09:31 -08009 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller2e498aa2015-02-16 12:09:31 -080011 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020012 * 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 Tiller2e498aa2015-02-16 12:09:31 -080017 *
18 */
Stanley Cheungd5b20562015-10-27 13:27:05 -070019
mlumishb892a272014-12-09 16:28:23 -080020namespace Grpc;
21
22/**
thinkerou0c3e8db2016-12-15 00:27:03 +080023 * Represents an active call that allows for sending and recieving messages
24 * in streams in any order.
mlumishb892a272014-12-09 16:28:23 -080025 */
Stanley Cheungd5b20562015-10-27 13:27:05 -070026class BidiStreamingCall extends AbstractCall
27{
28 /**
29 * Start the call.
30 *
31 * @param array $metadata Metadata to send with the call, if applicable
thinkerou0c3e8db2016-12-15 00:27:03 +080032 * (optional)
Stanley Cheungd5b20562015-10-27 13:27:05 -070033 */
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070034 public function start(array $metadata = [])
Stanley Cheungd5b20562015-10-27 13:27:05 -070035 {
36 $this->call->startBatch([
37 OP_SEND_INITIAL_METADATA => $metadata,
38 ]);
murgatroid99d8cc6b82015-04-01 11:14:16 -070039 }
Stanley Cheungd5b20562015-10-27 13:27:05 -070040
41 /**
42 * Reads the next value from the server.
43 *
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070044 * @return mixed The next value from the server, or null if there is none
Stanley Cheungd5b20562015-10-27 13:27:05 -070045 */
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
thinkerou8772a362017-01-20 18:20:47 +080057 return $this->_deserializeResponse($read_event->message);
murgatroid99d8cc6b82015-04-01 11:14:16 -070058 }
mlumishb892a272014-12-09 16:28:23 -080059
Stanley Cheungd5b20562015-10-27 13:27:05 -070060 /**
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
thinkerou0c3e8db2016-12-15 00:27:03 +080065 * @param array $options An array of options, possible keys:
66 * 'flags' => a number (optional)
Stanley Cheungd5b20562015-10-27 13:27:05 -070067 */
thinkerou0c3e8db2016-12-15 00:27:03 +080068 public function write($data, array $options = [])
Stanley Cheungd5b20562015-10-27 13:27:05 -070069 {
thinkerou8772a362017-01-20 18:20:47 +080070 $message_array = ['message' => $this->_serializeMessage($data)];
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070071 if (array_key_exists('flags', $options)) {
Stanley Cheungd5b20562015-10-27 13:27:05 -070072 $message_array['flags'] = $options['flags'];
73 }
74 $this->call->startBatch([
75 OP_SEND_MESSAGE => $message_array,
76 ]);
Stanley Cheung3ab8e792015-08-24 16:58:42 -070077 }
mlumishb892a272014-12-09 16:28:23 -080078
Stanley Cheungd5b20562015-10-27 13:27:05 -070079 /**
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 }
mlumishb892a272014-12-09 16:28:23 -080088
Stanley Cheungd5b20562015-10-27 13:27:05 -070089 /**
90 * Wait for the server to send the status, and return it.
91 *
thinkerou0c3e8db2016-12-15 00:27:03 +080092 * @return \stdClass The status object, with integer $code, string
93 * $details, and array $metadata members
Stanley Cheungd5b20562015-10-27 13:27:05 -070094 */
95 public function getStatus()
96 {
97 $status_event = $this->call->startBatch([
98 OP_RECV_STATUS_ON_CLIENT => true,
99 ]);
100
Stanley Cheung6668d512016-05-18 14:05:09 -0700101 $this->trailing_metadata = $status_event->status->metadata;
thinkeroua3730b72016-07-20 16:59:54 +0800102
Stanley Cheungd5b20562015-10-27 13:27:05 -0700103 return $status_event->status;
104 }
105}