• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 single message and then gets a
24 * single response.
25 */
26class UnaryCall extends AbstractCall
27{
28    /**
29     * Start the call.
30     *
31     * @param mixed $data     The data to send
32     * @param array $metadata Metadata to send with the call, if applicable
33     *                        (optional)
34     * @param array $options  An array of options, possible keys:
35     *                        'flags' => a number (optional)
36     */
37    public function start($data, array $metadata = [], array $options = [])
38    {
39        $message_array = ['message' => $this->_serializeMessage($data)];
40        if (isset($options['flags'])) {
41            $message_array['flags'] = $options['flags'];
42        }
43        $this->call->startBatch([
44            OP_SEND_INITIAL_METADATA => $metadata,
45            OP_SEND_MESSAGE => $message_array,
46            OP_SEND_CLOSE_FROM_CLIENT => true,
47        ]);
48    }
49
50    /**
51     * Wait for the server to respond with data and a status.
52     *
53     * @return array [response data, status]
54     */
55    public function wait()
56    {
57        $batch = [
58            OP_RECV_MESSAGE => true,
59            OP_RECV_STATUS_ON_CLIENT => true,
60        ];
61        if ($this->metadata === null) {
62            $batch[OP_RECV_INITIAL_METADATA] = true;
63        }
64        $event = $this->call->startBatch($batch);
65        if ($this->metadata === null) {
66            $this->metadata = $event->metadata;
67        }
68        $status = $event->status;
69        $this->trailing_metadata = $status->metadata;
70
71        return [$this->_deserializeResponse($event->message), $status];
72    }
73
74    /**
75     * @return mixed The metadata sent by the server
76     */
77    public function getMetadata()
78    {
79        if ($this->metadata === null) {
80            $event = $this->call->startBatch([OP_RECV_INITIAL_METADATA => true]);
81            $this->metadata = $event->metadata;
82        }
83        return $this->metadata;
84    }
85}
86