blob: 129134636354055e45161ef03df749719ce37d48 [file] [log] [blame]
mlumish156e67d2015-01-02 14:59:16 -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
mlumish156e67d2015-01-02 14:59:16 -080020namespace Grpc;
21
mlumish156e67d2015-01-02 14:59:16 -080022/**
thinkerou0c3e8db2016-12-15 00:27:03 +080023 * Represents an active call that sends a single message and then gets a
24 * single response.
mlumish156e67d2015-01-02 14:59:16 -080025 */
Stanley Cheungd5b20562015-10-27 13:27:05 -070026class UnaryCall extends AbstractCall
27{
28 /**
29 * Start the call.
30 *
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070031 * @param mixed $data The data to send
Stanley Cheungd5b20562015-10-27 13:27:05 -070032 * @param array $metadata Metadata to send with the call, if applicable
thinkerou0c3e8db2016-12-15 00:27:03 +080033 * (optional)
34 * @param array $options An array of options, possible keys:
35 * 'flags' => a number (optional)
Stanley Cheungd5b20562015-10-27 13:27:05 -070036 */
thinkerou0c3e8db2016-12-15 00:27:03 +080037 public function start($data, array $metadata = [], array $options = [])
Stanley Cheungd5b20562015-10-27 13:27:05 -070038 {
thinkerou8772a362017-01-20 18:20:47 +080039 $message_array = ['message' => $this->_serializeMessage($data)];
Stanley Cheungd5b20562015-10-27 13:27:05 -070040 if (isset($options['flags'])) {
41 $message_array['flags'] = $options['flags'];
42 }
43 $event = $this->call->startBatch([
44 OP_SEND_INITIAL_METADATA => $metadata,
Stanley Cheungd5b20562015-10-27 13:27:05 -070045 OP_SEND_MESSAGE => $message_array,
46 OP_SEND_CLOSE_FROM_CLIENT => true,
47 ]);
48 $this->metadata = $event->metadata;
Stanley Cheung3ab8e792015-08-24 16:58:42 -070049 }
mlumish156e67d2015-01-02 14:59:16 -080050
Stanley Cheungd5b20562015-10-27 13:27:05 -070051 /**
52 * Wait for the server to respond with data and a status.
53 *
Stanislav Pavlovichevd58199b2016-08-23 23:20:34 +070054 * @return array [response data, status]
Stanley Cheungd5b20562015-10-27 13:27:05 -070055 */
56 public function wait()
57 {
Michael Bausor81fd6292017-08-15 10:13:06 -070058 $batch = [
Stanley Cheungd5b20562015-10-27 13:27:05 -070059 OP_RECV_MESSAGE => true,
60 OP_RECV_STATUS_ON_CLIENT => true,
Michael Bausor81fd6292017-08-15 10:13:06 -070061 ];
62 if ($this->metadata === null) {
63 $batch[OP_RECV_INITIAL_METADATA] = true;
64 }
65 $event = $this->call->startBatch($batch);
66 if ($this->metadata === null) {
67 $this->metadata = $event->metadata;
68 }
Stanley Cheung6668d512016-05-18 14:05:09 -070069 $status = $event->status;
70 $this->trailing_metadata = $status->metadata;
thinkeroua3730b72016-07-20 16:59:54 +080071
thinkerou8772a362017-01-20 18:20:47 +080072 return [$this->_deserializeResponse($event->message), $status];
Stanley Cheungd5b20562015-10-27 13:27:05 -070073 }
Michael Bausor81fd6292017-08-15 10:13:06 -070074
75 /**
76 * @return mixed The metadata sent by the server
77 */
78 public function getMetadata()
79 {
80 if ($this->metadata === null) {
81 $event = $this->call->startBatch([OP_RECV_INITIAL_METADATA => true]);
82 $this->metadata = $event->metadata;
83 }
84 return $this->metadata;
85 }
Stanley Cheung3ab8e792015-08-24 16:58:42 -070086}