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