server_callback.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/grpc.h>
  19. #include <grpc/support/alloc.h>
  20. #include <grpcpp/security/server_credentials.h>
  21. #include <grpcpp/server.h>
  22. #include <grpcpp/server_context.h>
  23. #include "src/core/lib/gprpp/host_port.h"
  24. #include "src/proto/grpc/testing/benchmark_service.grpc.pb.h"
  25. #include "test/cpp/qps/qps_server_builder.h"
  26. #include "test/cpp/qps/server.h"
  27. #include "test/cpp/qps/usage_timer.h"
  28. namespace grpc {
  29. namespace testing {
  30. class BenchmarkCallbackServiceImpl final
  31. : public BenchmarkService::CallbackService {
  32. public:
  33. grpc::ServerUnaryReactor* UnaryCall(grpc::CallbackServerContext* context,
  34. const SimpleRequest* request,
  35. SimpleResponse* response) override {
  36. auto* reactor = context->DefaultReactor();
  37. reactor->Finish(SetResponse(request, response));
  38. return reactor;
  39. }
  40. grpc::ServerBidiReactor<grpc::testing::SimpleRequest,
  41. grpc::testing::SimpleResponse>*
  42. StreamingCall(grpc::CallbackServerContext*) override {
  43. class Reactor
  44. : public grpc::ServerBidiReactor<grpc::testing::SimpleRequest,
  45. grpc::testing::SimpleResponse> {
  46. public:
  47. Reactor() { StartRead(&request_); }
  48. void OnReadDone(bool ok) override {
  49. if (!ok) {
  50. Finish(grpc::Status::OK);
  51. return;
  52. }
  53. auto s = SetResponse(&request_, &response_);
  54. if (!s.ok()) {
  55. Finish(s);
  56. return;
  57. }
  58. StartWrite(&response_);
  59. }
  60. void OnWriteDone(bool ok) override {
  61. if (!ok) {
  62. Finish(grpc::Status::OK);
  63. return;
  64. }
  65. StartRead(&request_);
  66. }
  67. void OnDone() override { delete (this); }
  68. private:
  69. SimpleRequest request_;
  70. SimpleResponse response_;
  71. };
  72. return new Reactor;
  73. }
  74. private:
  75. static Status SetResponse(const SimpleRequest* request,
  76. SimpleResponse* response) {
  77. if (request->response_size() > 0) {
  78. if (!Server::SetPayload(request->response_type(),
  79. request->response_size(),
  80. response->mutable_payload())) {
  81. return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
  82. }
  83. }
  84. return Status::OK;
  85. }
  86. };
  87. class CallbackServer final : public grpc::testing::Server {
  88. public:
  89. explicit CallbackServer(const ServerConfig& config) : Server(config) {
  90. std::unique_ptr<ServerBuilder> builder = CreateQpsServerBuilder();
  91. auto port_num = port();
  92. // Negative port number means inproc server, so no listen port needed
  93. if (port_num >= 0) {
  94. std::string server_address = grpc_core::JoinHostPort("::", port_num);
  95. builder->AddListeningPort(server_address.c_str(),
  96. Server::CreateServerCredentials(config),
  97. &port_num);
  98. }
  99. ApplyConfigToBuilder(config, builder.get());
  100. builder->RegisterService(&service_);
  101. impl_ = builder->BuildAndStart();
  102. if (impl_ == nullptr) {
  103. gpr_log(GPR_ERROR, "Server: Fail to BuildAndStart(port=%d)", port_num);
  104. } else {
  105. gpr_log(GPR_INFO, "Server: BuildAndStart(port=%d)", port_num);
  106. }
  107. }
  108. std::shared_ptr<Channel> InProcessChannel(
  109. const ChannelArguments& args) override {
  110. return impl_->InProcessChannel(args);
  111. }
  112. private:
  113. BenchmarkCallbackServiceImpl service_;
  114. std::unique_ptr<grpc::Server> impl_;
  115. };
  116. std::unique_ptr<grpc::testing::Server> CreateCallbackServer(
  117. const ServerConfig& config) {
  118. return std::unique_ptr<Server>(new CallbackServer(config));
  119. }
  120. } // namespace testing
  121. } // namespace grpc