nshead_service.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_SERVICE_H
  18. #define BRPC_NSHEAD_SERVICE_H
  19. #include "brpc/controller.h" // Controller
  20. #include "brpc/nshead_message.h" // NsheadMessage
  21. #include "brpc/describable.h"
  22. namespace brpc {
  23. class Server;
  24. class MethodStatus;
  25. class StatusService;
  26. namespace policy {
  27. void ProcessNsheadRequest(InputMessageBase* msg_base);
  28. }
  29. // The continuation of request processing. Namely send response back to client.
  30. // NOTE: you DON'T need to inherit this class or create instance of this class.
  31. class NsheadClosure : public google::protobuf::Closure {
  32. public:
  33. explicit NsheadClosure(void* additional_space);
  34. // [Required] Call this to send response back to the client.
  35. void Run();
  36. // [Optional] Set the full method name. If unset, use name of the service.
  37. void SetMethodName(const std::string& full_method_name);
  38. // The space required by subclass at NsheadServiceOptions. subclass may
  39. // utilizes this feature to save the cost of allocating closure separately.
  40. // If subclass does not require space, this return value is NULL.
  41. void* additional_space() { return _additional_space; }
  42. int64_t received_us() const { return _received_us; }
  43. // Don't send response back, used by MIMO.
  44. void DoNotRespond();
  45. private:
  46. friend void policy::ProcessNsheadRequest(InputMessageBase* msg_base);
  47. friend class DeleteNsheadClosure;
  48. // Only callable by Run().
  49. ~NsheadClosure();
  50. const Server* _server;
  51. int64_t _received_us;
  52. NsheadMessage _request;
  53. NsheadMessage _response;
  54. bool _do_respond;
  55. void* _additional_space;
  56. Controller _controller;
  57. };
  58. struct NsheadServiceOptions {
  59. NsheadServiceOptions() : generate_status(true), additional_space(0) {}
  60. NsheadServiceOptions(bool generate_status2, size_t additional_space2)
  61. : generate_status(generate_status2)
  62. , additional_space(additional_space2) {}
  63. bool generate_status;
  64. size_t additional_space;
  65. };
  66. // Inherit this class to let brpc server understands nshead requests.
  67. class NsheadService : public Describable {
  68. public:
  69. NsheadService();
  70. NsheadService(const NsheadServiceOptions&);
  71. virtual ~NsheadService();
  72. // Implement this method to handle nshead requests. Notice that this
  73. // method can be called with a failed Controller(something wrong with the
  74. // request before calling this method), in which case the implemenetation
  75. // shall send specific response with error information back to client.
  76. // Parameters:
  77. // server The server receiving the request.
  78. // controller per-rpc settings.
  79. // request The nshead request received.
  80. // response The nshead response that you should fill in.
  81. // done You must call done->Run() to end the processing.
  82. virtual void ProcessNsheadRequest(const Server& server,
  83. Controller* controller,
  84. const NsheadMessage& request,
  85. NsheadMessage* response,
  86. NsheadClosure* done) = 0;
  87. // Put descriptions into the stream.
  88. void Describe(std::ostream &os, const DescribeOptions&) const;
  89. private:
  90. DISALLOW_COPY_AND_ASSIGN(NsheadService);
  91. friend class NsheadClosure;
  92. friend void policy::ProcessNsheadRequest(InputMessageBase* msg_base);
  93. friend class StatusService;
  94. friend class Server;
  95. private:
  96. void Expose(const butil::StringPiece& prefix);
  97. // Tracking status of non NsheadPbService
  98. MethodStatus* _status;
  99. size_t _additional_space;
  100. std::string _cached_name;
  101. };
  102. } // namespace brpc
  103. #endif // BRPC_NSHEAD_SERVICE_H