nshead_pb_service_adaptor.h 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. 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,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #ifndef BRPC_NSHEAD_PB_SERVICE_ADAPTOR_H
  18. #define BRPC_NSHEAD_PB_SERVICE_ADAPTOR_H
  19. #include "brpc/nshead_service.h" // NsheadService
  20. #include "brpc/nshead_meta.pb.h" // NsheadMeta
  21. namespace brpc {
  22. class NsheadPbServiceAdaptor;
  23. extern const size_t SendNsheadPbResponseSize;
  24. // Adapt nshead requests to use protobuf-based service.
  25. // What RPC does:
  26. // * Call ParseNsheadMeta() to understand the nshead header, user must
  27. // tell RPC which pb method to call in the callback.
  28. // * Call ParseRequestFromIOBuf() to convert the body after nshead header
  29. // to pb request, then call the pb method.
  30. // * When user calls server's done to end the RPC, SerializeResponseToIOBuf()
  31. // is called to convert pb response to binary data that will be appended
  32. // after nshead header and sent back to client.
  33. class NsheadPbServiceAdaptor : public NsheadService {
  34. public:
  35. NsheadPbServiceAdaptor() : NsheadService(
  36. NsheadServiceOptions(false, SendNsheadPbResponseSize)) {}
  37. virtual ~NsheadPbServiceAdaptor() {}
  38. // Fetch meta from `nshead_req' into `meta'.
  39. // Params:
  40. // server: where the RPC runs.
  41. // nshead_req: the nshead request that server received.
  42. // controller: If something goes wrong, call controller->SetFailed()
  43. // meta: Set meta information into this structure. `full_method_name'
  44. // must be set if controller is not SetFailed()-ed
  45. // FIXME: server is not needed anymore, controller->server() is same
  46. virtual void ParseNsheadMeta(const Server& server,
  47. const NsheadMessage& nshead_req,
  48. Controller* controller,
  49. NsheadMeta* meta) const = 0;
  50. // Transform `nshead_req' to `pb_req'.
  51. // Params:
  52. // meta: was set by ParseNsheadMeta()
  53. // nshead_req: the nshead request that server received.
  54. // controller: you can set attachment into the controller. If something
  55. // goes wrong, call controller->SetFailed()
  56. // pb_req: the pb request should be set by your implementation.
  57. virtual void ParseRequestFromIOBuf(const NsheadMeta& meta,
  58. const NsheadMessage& nshead_req,
  59. Controller* controller,
  60. google::protobuf::Message* pb_req) const = 0;
  61. // Transform `pb_res' (and controller) to `nshead_res'.
  62. // Params:
  63. // meta: was set by ParseNsheadMeta()
  64. // controller: If something goes wrong, call controller->SetFailed()
  65. // pb_res: the pb response that returned by pb method. [NOTE] `pb_res'
  66. // can be NULL or uninitialized when RPC failed (indicated by
  67. // Controller::Failed()), in which case you may put error
  68. // information into `nshead_res'.
  69. // nshead_res: the nshead response that will be sent back to client.
  70. virtual void SerializeResponseToIOBuf(const NsheadMeta& meta,
  71. Controller* controller,
  72. const google::protobuf::Message* pb_res,
  73. NsheadMessage* nshead_res) const = 0;
  74. private:
  75. void ProcessNsheadRequest(
  76. const Server& server, Controller* controller,
  77. const NsheadMessage& request, NsheadMessage* response,
  78. NsheadClosure* done);
  79. };
  80. } // namespace brpc
  81. #endif // BRPC_NSHEAD_PB_SERVICE_ADAPTOR_H