GCPBidiStreamingCall.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. *
  4. * Copyright 2018 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. namespace Grpc\Gcp;
  20. /**
  21. * Represents an active call that allows for sending and recieving messages
  22. * in streams in any order.
  23. */
  24. class GCPBidiStreamingCall extends GcpBaseCall
  25. {
  26. private $response = null;
  27. protected function createRealCall($data = null)
  28. {
  29. $channel_ref = $this->_rpcPreProcess($data);
  30. $this->real_call = new \Grpc\BidiStreamingCall($channel_ref->getRealChannel(
  31. $this->gcp_channel->credentials), $this->method, $this->deserialize, $this->options);
  32. $this->real_call->start($this->metadata_rpc);
  33. return $this->real_call;
  34. }
  35. /**
  36. * Pick a channel and start the call.
  37. *
  38. * @param array $metadata Metadata to send with the call, if applicable
  39. * (optional)
  40. */
  41. public function start(array $metadata = [])
  42. {
  43. $this->metadata_rpc = $metadata;
  44. }
  45. /**
  46. * Reads the next value from the server.
  47. *
  48. * @return mixed The next value from the server, or null if there is none
  49. */
  50. public function read()
  51. {
  52. if (!$this->has_real_call) {
  53. $this->createRealCall();
  54. $this->has_real_call = true;
  55. }
  56. $response = $this->real_call->read();
  57. if ($response) {
  58. $this->response = $response;
  59. }
  60. return $response;
  61. }
  62. /**
  63. * Write a single message to the server. This cannot be called after
  64. * writesDone is called.
  65. *
  66. * @param ByteBuffer $data The data to write
  67. * @param array $options An array of options, possible keys:
  68. * 'flags' => a number (optional)
  69. */
  70. public function write($data, array $options = [])
  71. {
  72. if (!$this->has_real_call) {
  73. $this->createRealCall($data);
  74. $this->has_real_call = true;
  75. }
  76. $this->real_call->write($data, $options);
  77. }
  78. /**
  79. * Indicate that no more writes will be sent.
  80. */
  81. public function writesDone()
  82. {
  83. if (!$this->has_real_call) {
  84. $this->createRealCall();
  85. $this->has_real_call = true;
  86. }
  87. $this->real_call->writesDone();
  88. }
  89. /**
  90. * Wait for the server to send the status, and return it.
  91. *
  92. * @return \stdClass The status object, with integer $code, string
  93. * $details, and array $metadata members
  94. */
  95. public function getStatus()
  96. {
  97. $status = $this->real_call->getStatus();
  98. $this->_rpcPostProcess($status, $this->response);
  99. return $status;
  100. }
  101. }