stream_context.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "src/cpp/stream/stream_context.h"
  34. #include <grpc/support/log.h>
  35. #include "src/cpp/rpc_method.h"
  36. #include "src/cpp/proto/proto_utils.h"
  37. #include "src/cpp/util/time.h"
  38. #include <grpc++/client_context.h>
  39. #include <grpc++/config.h>
  40. #include <google/protobuf/message.h>
  41. namespace grpc {
  42. // Client only ctor
  43. StreamContext::StreamContext(const RpcMethod& method, ClientContext* context,
  44. const google::protobuf::Message* request,
  45. google::protobuf::Message* result)
  46. : is_client_(true),
  47. method_(&method),
  48. call_(context->call()),
  49. cq_(context->cq()),
  50. request_(const_cast<google::protobuf::Message*>(request)),
  51. result_(result),
  52. peer_halfclosed_(false),
  53. self_halfclosed_(false) {
  54. GPR_ASSERT(method_->method_type() != RpcMethod::RpcType::NORMAL_RPC);
  55. }
  56. // Server only ctor
  57. StreamContext::StreamContext(const RpcMethod& method, grpc_call* call,
  58. grpc_completion_queue* cq,
  59. google::protobuf::Message* request, google::protobuf::Message* result)
  60. : is_client_(false),
  61. method_(&method),
  62. call_(call),
  63. cq_(cq),
  64. request_(request),
  65. result_(result),
  66. peer_halfclosed_(false),
  67. self_halfclosed_(false) {
  68. GPR_ASSERT(method_->method_type() != RpcMethod::RpcType::NORMAL_RPC);
  69. }
  70. StreamContext::~StreamContext() {}
  71. void StreamContext::Start(bool buffered) {
  72. if (is_client_) {
  73. // TODO(yangg) handle metadata send path
  74. int flag = buffered ? GRPC_WRITE_BUFFER_HINT : 0;
  75. grpc_call_error error = grpc_call_start_invoke(call(), cq(), invoke_tag(),
  76. client_metadata_read_tag(),
  77. finished_tag(), flag);
  78. GPR_ASSERT(GRPC_CALL_OK == error);
  79. grpc_event* invoke_ev =
  80. grpc_completion_queue_pluck(cq(), invoke_tag(), gpr_inf_future);
  81. if (invoke_ev->data.invoke_accepted != GRPC_OP_OK) {
  82. peer_halfclosed_ = true;
  83. self_halfclosed_ = true;
  84. }
  85. grpc_event_finish(invoke_ev);
  86. } else {
  87. // TODO(yangg) metadata needs to be added before accept
  88. // TODO(yangg) correctly set flag to accept
  89. GPR_ASSERT(grpc_call_server_accept(call(), cq(), finished_tag()) ==
  90. GRPC_CALL_OK);
  91. GPR_ASSERT(grpc_call_server_end_initial_metadata(call(), 0) ==
  92. GRPC_CALL_OK);
  93. }
  94. }
  95. bool StreamContext::Read(google::protobuf::Message* msg) {
  96. // TODO(yangg) check peer_halfclosed_ here for possible early return.
  97. grpc_call_error err = grpc_call_start_read(call(), read_tag());
  98. GPR_ASSERT(err == GRPC_CALL_OK);
  99. grpc_event* read_ev =
  100. grpc_completion_queue_pluck(cq(), read_tag(), gpr_inf_future);
  101. GPR_ASSERT(read_ev->type == GRPC_READ);
  102. bool ret = true;
  103. if (read_ev->data.read) {
  104. if (!DeserializeProto(read_ev->data.read, msg)) {
  105. ret = false;
  106. FinishStream(
  107. Status(StatusCode::DATA_LOSS, "Failed to parse incoming proto"),
  108. true);
  109. }
  110. } else {
  111. ret = false;
  112. peer_halfclosed_ = true;
  113. }
  114. grpc_event_finish(read_ev);
  115. return ret;
  116. }
  117. bool StreamContext::Write(const google::protobuf::Message* msg, bool is_last) {
  118. // TODO(yangg) check self_halfclosed_ for possible early return.
  119. bool ret = true;
  120. grpc_event* ev = nullptr;
  121. if (msg) {
  122. grpc_byte_buffer* out_buf = nullptr;
  123. if (!SerializeProto(*msg, &out_buf)) {
  124. FinishStream(Status(StatusCode::INVALID_ARGUMENT,
  125. "Failed to serialize outgoing proto"),
  126. true);
  127. return false;
  128. }
  129. int flag = is_last ? GRPC_WRITE_BUFFER_HINT : 0;
  130. grpc_call_error err =
  131. grpc_call_start_write(call(), out_buf, write_tag(), flag);
  132. grpc_byte_buffer_destroy(out_buf);
  133. GPR_ASSERT(err == GRPC_CALL_OK);
  134. ev = grpc_completion_queue_pluck(cq(), write_tag(), gpr_inf_future);
  135. GPR_ASSERT(ev->type == GRPC_WRITE_ACCEPTED);
  136. ret = ev->data.write_accepted == GRPC_OP_OK;
  137. grpc_event_finish(ev);
  138. }
  139. if (ret && is_last) {
  140. grpc_call_error err = grpc_call_writes_done(call(), halfclose_tag());
  141. GPR_ASSERT(err == GRPC_CALL_OK);
  142. ev = grpc_completion_queue_pluck(cq(), halfclose_tag(), gpr_inf_future);
  143. GPR_ASSERT(ev->type == GRPC_FINISH_ACCEPTED);
  144. grpc_event_finish(ev);
  145. self_halfclosed_ = true;
  146. } else if (!ret) { // Stream broken
  147. self_halfclosed_ = true;
  148. peer_halfclosed_ = true;
  149. }
  150. return ret;
  151. }
  152. const Status& StreamContext::Wait() {
  153. // TODO(yangg) properly support metadata
  154. grpc_event* metadata_ev = grpc_completion_queue_pluck(
  155. cq(), client_metadata_read_tag(), gpr_inf_future);
  156. grpc_event_finish(metadata_ev);
  157. // TODO(yangg) protect states by a mutex, including other places.
  158. if (!self_halfclosed_ || !peer_halfclosed_) {
  159. FinishStream(Status::Cancelled, true);
  160. } else {
  161. grpc_event* finish_ev =
  162. grpc_completion_queue_pluck(cq(), finished_tag(), gpr_inf_future);
  163. GPR_ASSERT(finish_ev->type == GRPC_FINISHED);
  164. final_status_ = Status(
  165. static_cast<StatusCode>(finish_ev->data.finished.status),
  166. finish_ev->data.finished.details ? finish_ev->data.finished.details
  167. : "");
  168. grpc_event_finish(finish_ev);
  169. }
  170. return final_status_;
  171. }
  172. void StreamContext::FinishStream(const Status& status, bool send) {
  173. if (send) {
  174. grpc_call_cancel(call());
  175. }
  176. grpc_event* finish_ev =
  177. grpc_completion_queue_pluck(cq(), finished_tag(), gpr_inf_future);
  178. GPR_ASSERT(finish_ev->type == GRPC_FINISHED);
  179. grpc_event_finish(finish_ev);
  180. final_status_ = status;
  181. }
  182. } // namespace grpc