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