rpc_context.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright (c) 2020 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __RPC_CONTEXT_H__
  14. #define __RPC_CONTEXT_H__
  15. #include <string>
  16. #include <functional>
  17. #include <workflow/Workflow.h>
  18. #include "rpc_basic.h"
  19. namespace srpc
  20. {
  21. struct RPCSyncContext
  22. {
  23. long long seqid;
  24. std::string errmsg;
  25. std::string remote_ip;
  26. int status_code;
  27. int error;
  28. bool success;
  29. int timeout_reason;
  30. };
  31. class RPCContext
  32. {
  33. public:
  34. virtual long long get_seqid() const = 0;
  35. virtual std::string get_remote_ip() const = 0;
  36. virtual int get_peer_addr(struct sockaddr *addr, socklen_t *addrlen) const = 0;
  37. virtual const std::string& get_service_name() const = 0;
  38. virtual const std::string& get_method_name() const = 0;
  39. virtual SeriesWork *get_series() const = 0;
  40. virtual bool get_http_header(const std::string& name,
  41. std::string& value) const = 0;
  42. public:
  43. // for client-done
  44. virtual bool success() const = 0;
  45. virtual int get_status_code() const = 0;
  46. virtual const char *get_errmsg() const = 0;
  47. virtual int get_error() const = 0;
  48. virtual void *get_user_data() const = 0;
  49. virtual int get_timeout_reason() const = 0;
  50. public:
  51. // for server-process
  52. virtual void set_data_type(RPCDataType type) = 0;//enum RPCDataType
  53. virtual void set_compress_type(RPCCompressType type) = 0;//enum RPCCompressType
  54. virtual void set_attachment_nocopy(const char *attachment, size_t len) = 0;
  55. virtual bool get_attachment(const char **attachment, size_t *len) const = 0;
  56. virtual void set_reply_callback(std::function<void (RPCContext *ctx)> cb) = 0;
  57. virtual void set_send_timeout(int timeout) = 0;
  58. virtual void set_keep_alive(int timeout) = 0;
  59. virtual bool set_http_code(int code) = 0;
  60. virtual bool set_http_header(const std::string& name, const std::string& value) = 0;
  61. virtual bool add_http_header(const std::string& name, const std::string& value) = 0;
  62. virtual bool log(const RPCLogVector& fields) = 0;
  63. // Refer to : https://opentelemetry.io/docs/reference/specification/baggage/api
  64. // corresponding to SetValue(), GetValue(), RemoveValue()
  65. virtual bool add_baggage(const std::string& key, const std::string& value) = 0;
  66. virtual bool get_baggage(const std::string& key, std::string& value) = 0;
  67. //virtual bool remove_baggage(const std::string& key) = 0;
  68. //virtual void noreply();
  69. //virtual WFConnection *get_connection();
  70. public:
  71. // for json format
  72. // Currently only support pb to json. Default : false
  73. // Whether to add spaces, line breaks and indentation to make the JSON
  74. // output easy to read.
  75. virtual void set_json_add_whitespace(bool on) = 0;
  76. // Whether to always print enums as ints.
  77. virtual void set_json_always_print_enums_as_ints(bool flag) = 0;
  78. // Whether to preserve proto field names.
  79. virtual void set_json_preserve_proto_field_names(bool flag) = 0;
  80. // Whether to always print primitive fields.
  81. // By default proto3 primitive fields with default values will be omitted
  82. // in JSON output. For example, an int32 field set to 0 will be omitted.
  83. // Set this flag to true will override the default behavior and print
  84. // primitive fields regardless of their values.
  85. virtual void set_json_always_print_primitive_fields(bool flag) = 0;
  86. public:
  87. virtual ~RPCContext() { }
  88. };
  89. } // namespace srpc
  90. ////////
  91. // inl
  92. #include "rpc_context.inl"
  93. #include "rpc_task.inl"
  94. #endif