GRPC C++  1.2.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
server_context.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, 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 
34 #ifndef GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H
35 #define GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H
36 
37 #include <map>
38 #include <memory>
39 #include <vector>
40 
42 #include <grpc/load_reporting.h>
43 
50 
51 struct grpc_metadata;
52 struct grpc_call;
53 struct census_context;
54 
55 namespace grpc {
56 class ClientContext;
57 template <class W, class R>
58 class ServerAsyncReader;
59 template <class W>
60 class ServerAsyncWriter;
61 template <class W>
62 class ServerAsyncResponseWriter;
63 template <class W, class R>
64 class ServerAsyncReaderWriter;
65 template <class R>
66 class ServerReader;
67 template <class W>
68 class ServerWriter;
69 namespace internal {
70 template <class W, class R>
71 class ServerReaderWriterBody;
72 }
73 template <class ServiceType, class RequestType, class ResponseType>
74 class RpcMethodHandler;
75 template <class ServiceType, class RequestType, class ResponseType>
76 class ClientStreamingHandler;
77 template <class ServiceType, class RequestType, class ResponseType>
78 class ServerStreamingHandler;
79 template <class ServiceType, class RequestType, class ResponseType>
80 class BidiStreamingHandler;
81 class UnknownMethodHandler;
82 
83 class Call;
84 class CallOpBuffer;
85 class CompletionQueue;
86 class Server;
87 class ServerInterface;
88 
89 namespace testing {
90 class InteropServerContextInspector;
91 class ServerContextTestSpouse;
92 } // namespace testing
93 
94 // Interface of server side rpc context.
96  public:
97  ServerContext(); // for async calls
99 
100  std::chrono::system_clock::time_point deadline() const {
101  return Timespec2Timepoint(deadline_);
102  }
103 
104  gpr_timespec raw_deadline() const { return deadline_; }
105 
106  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
107  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
108 
109  // IsCancelled is always safe to call when using sync API
110  // When using async API, it is only safe to call IsCancelled after
111  // the AsyncNotifyWhenDone tag has been delivered
112  bool IsCancelled() const;
113 
114  // Cancel the Call from the server. This is a best-effort API and depending on
115  // when it is called, the RPC may still appear successful to the client.
116  // For example, if TryCancel() is called on a separate thread, it might race
117  // with the server handler which might return success to the client before
118  // TryCancel() was even started by the thread.
119  //
120  // It is the caller's responsibility to prevent such races and ensure that if
121  // TryCancel() is called, the serverhandler must return Status::CANCELLED. The
122  // only exception is that if the serverhandler is already returning an error
123  // status code, it is ok to not return Status::CANCELLED even if TryCancel()
124  // was called.
125  void TryCancel() const;
126 
127  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata()
128  const {
129  return *client_metadata_.map();
130  }
131 
133  return compression_level_;
134  }
135 
137  compression_level_set_ = true;
138  compression_level_ = level;
139  }
140 
141  bool compression_level_set() const { return compression_level_set_; }
142 
144  return compression_algorithm_;
145  }
147 
148  // Set the load reporting costs in \a cost_data for the call.
149  void SetLoadReportingCosts(const std::vector<grpc::string>& cost_data);
150 
151  std::shared_ptr<const AuthContext> auth_context() const {
152  if (auth_context_.get() == nullptr) {
153  auth_context_ = CreateAuthContext(call_);
154  }
155  return auth_context_;
156  }
157 
158  // Return the peer uri in a string.
159  // WARNING: this value is never authenticated or subject to any security
160  // related code. It must not be used for any authentication related
161  // functionality. Instead, use auth_context.
162  grpc::string peer() const;
163 
164  const struct census_context* census_context() const;
165 
166  // Async only. Has to be called before the rpc starts.
167  // Returns the tag in completion queue when the rpc finishes.
168  // IsCancelled() can then be called to check whether the rpc was cancelled.
169  void AsyncNotifyWhenDone(void* tag) {
170  has_notify_when_done_tag_ = true;
171  async_notify_when_done_tag_ = tag;
172  }
173 
174  // Should be used for framework-level extensions only.
175  // Applications never need to call this method.
176  grpc_call* c_call() { return call_; }
177 
178  private:
179  friend class ::grpc::testing::InteropServerContextInspector;
180  friend class ::grpc::testing::ServerContextTestSpouse;
181  friend class ::grpc::ServerInterface;
182  friend class ::grpc::Server;
183  template <class W, class R>
184  friend class ::grpc::ServerAsyncReader;
185  template <class W>
186  friend class ::grpc::ServerAsyncWriter;
187  template <class W>
188  friend class ::grpc::ServerAsyncResponseWriter;
189  template <class W, class R>
190  friend class ::grpc::ServerAsyncReaderWriter;
191  template <class R>
192  friend class ::grpc::ServerReader;
193  template <class W>
194  friend class ::grpc::ServerWriter;
195  template <class W, class R>
196  friend class ::grpc::internal::ServerReaderWriterBody;
197  template <class ServiceType, class RequestType, class ResponseType>
198  friend class RpcMethodHandler;
199  template <class ServiceType, class RequestType, class ResponseType>
201  template <class ServiceType, class RequestType, class ResponseType>
203  template <class Streamer, bool WriteNeeded>
205  friend class UnknownMethodHandler;
206  friend class ::grpc::ClientContext;
207 
208  // Prevent copying.
210  ServerContext& operator=(const ServerContext&);
211 
212  class CompletionOp;
213 
214  void BeginCompletionOp(Call* call);
215 
217 
218  void set_call(grpc_call* call) { call_ = call; }
219 
220  uint32_t initial_metadata_flags() const { return 0; }
221 
222  CompletionOp* completion_op_;
223  bool has_notify_when_done_tag_;
224  void* async_notify_when_done_tag_;
225 
226  gpr_timespec deadline_;
227  grpc_call* call_;
228  CompletionQueue* cq_;
229  bool sent_initial_metadata_;
230  mutable std::shared_ptr<const AuthContext> auth_context_;
231  MetadataMap client_metadata_;
232  std::multimap<grpc::string, grpc::string> initial_metadata_;
233  std::multimap<grpc::string, grpc::string> trailing_metadata_;
234 
235  bool compression_level_set_;
236  grpc_compression_level compression_level_;
237  grpc_compression_algorithm compression_algorithm_;
238 };
239 
240 } // namespace grpc
241 
242 #endif // GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H
grpc_compression_algorithm compression_algorithm() const
Definition: server_context.h:143
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:85
std::shared_ptr< const AuthContext > CreateAuthContext(grpc_call *call)
void SetLoadReportingCosts(const std::vector< grpc::string > &cost_data)
void set_compression_algorithm(grpc_compression_algorithm algorithm)
grpc_compression_level compression_level() const
Definition: server_context.h:132
std::string string
Definition: config.h:50
gpr_timespec raw_deadline() const
Definition: server_context.h:104
void set_compression_level(grpc_compression_level level)
Definition: server_context.h:136
void AsyncNotifyWhenDone(void *tag)
Definition: server_context.h:169
Definition: grpc_types.h:363
void TryCancel() const
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:84
void AddInitialMetadata(const grpc::string &key, const grpc::string &value)
Definition: method_handler_impl.h:253
const struct census_context * census_context() const
Definition: method_handler_impl.h:177
A single metadata element.
Definition: grpc_types.h:323
grpc_compression_algorithm
Definition: compression_types.h:72
Definition: call.h:617
Definition: completion_queue.h:76
bool IsCancelled() const
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:58
Definition: server_context.h:95
grpc::string peer() const
const std::multimap< grpc::string_ref, grpc::string_ref > & client_metadata() const
Definition: server_context.h:127
Definition: completion_queue.h:78
Definition: completion_queue.h:80
Definition: gpr_types.h:63
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
grpc_call * c_call()
Definition: server_context.h:176
void AddTrailingMetadata(const grpc::string &key, const grpc::string &value)
std::shared_ptr< const AuthContext > auth_context() const
Definition: server_context.h:151
bool compression_level_set() const
Definition: server_context.h:141
std::chrono::system_clock::time_point deadline() const
Definition: server_context.h:100