GRPC C++  0.13.1-pre1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
server_context.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, 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 
40 #include <grpc/impl/codegen/compression_types.h>
41 #include <grpc/impl/codegen/time.h>
46 
47 struct gpr_timespec;
48 struct grpc_metadata;
49 struct grpc_call;
50 struct census_context;
51 
52 namespace grpc {
53 
54 class ClientContext;
55 template <class W, class R>
56 class ServerAsyncReader;
57 template <class W>
58 class ServerAsyncWriter;
59 template <class W>
60 class ServerAsyncResponseWriter;
61 template <class W, class R>
62 class ServerAsyncReaderWriter;
63 template <class R>
64 class ServerReader;
65 template <class W>
66 class ServerWriter;
67 template <class W, class R>
68 class ServerReaderWriter;
69 template <class ServiceType, class RequestType, class ResponseType>
70 class RpcMethodHandler;
71 template <class ServiceType, class RequestType, class ResponseType>
72 class ClientStreamingHandler;
73 template <class ServiceType, class RequestType, class ResponseType>
74 class ServerStreamingHandler;
75 template <class ServiceType, class RequestType, class ResponseType>
76 class BidiStreamingHandler;
77 class UnknownMethodHandler;
78 
79 class Call;
80 class CallOpBuffer;
81 class CompletionQueue;
82 class Server;
83 class ServerInterface;
84 
85 namespace testing {
86 class InteropServerContextInspector;
87 } // namespace testing
88 
89 // Interface of server side rpc context.
91  public:
92  ServerContext(); // for async calls
94 
95 #ifndef GRPC_CXX0X_NO_CHRONO
96  std::chrono::system_clock::time_point deadline() {
97  return Timespec2Timepoint(deadline_);
98  }
99 #endif // !GRPC_CXX0X_NO_CHRONO
100 
101  gpr_timespec raw_deadline() { return deadline_; }
102 
103  void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
104  void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
105 
106  bool IsCancelled() const;
107 
108  // Cancel the Call from the server. This is a best-effort API and depending on
109  // when it is called, the RPC may still appear successful to the client.
110  // For example, if TryCancel() is called on a separate thread, it might race
111  // with the server handler which might return success to the client before
112  // TryCancel() was even started by the thread.
113  //
114  // It is the caller's responsibility to prevent such races and ensure that if
115  // TryCancel() is called, the serverhandler must return Status::CANCELLED. The
116  // only exception is that if the serverhandler is already returning an error
117  // status code, it is ok to not return Status::CANCELLED even if TryCancel()
118  // was called.
119  void TryCancel() const;
120 
121  const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata() {
122  return client_metadata_;
123  }
124 
125  grpc_compression_level compression_level() const {
126  return compression_level_;
127  }
128  void set_compression_level(grpc_compression_level level);
129 
130  grpc_compression_algorithm compression_algorithm() const {
131  return compression_algorithm_;
132  }
133  void set_compression_algorithm(grpc_compression_algorithm algorithm);
134 
135  std::shared_ptr<const AuthContext> auth_context() const;
136 
137  // Return the peer uri in a string.
138  // WARNING: this value is never authenticated or subject to any security
139  // related code. It must not be used for any authentication related
140  // functionality. Instead, use auth_context.
141  grpc::string peer() const;
142 
143  const struct census_context* census_context() const;
144 
145  // Async only. Has to be called before the rpc starts.
146  // Returns the tag in completion queue when the rpc finishes.
147  // IsCancelled() can then be called to check whether the rpc was cancelled.
148  void AsyncNotifyWhenDone(void* tag) {
149  has_notify_when_done_tag_ = true;
150  async_notify_when_done_tag_ = tag;
151  }
152 
153  private:
154  friend class ::grpc::testing::InteropServerContextInspector;
155  friend class ::grpc::ServerInterface;
156  friend class ::grpc::Server;
157  template <class W, class R>
158  friend class ::grpc::ServerAsyncReader;
159  template <class W>
160  friend class ::grpc::ServerAsyncWriter;
161  template <class W>
162  friend class ::grpc::ServerAsyncResponseWriter;
163  template <class W, class R>
164  friend class ::grpc::ServerAsyncReaderWriter;
165  template <class R>
166  friend class ::grpc::ServerReader;
167  template <class W>
168  friend class ::grpc::ServerWriter;
169  template <class W, class R>
170  friend class ::grpc::ServerReaderWriter;
171  template <class ServiceType, class RequestType, class ResponseType>
172  friend class RpcMethodHandler;
173  template <class ServiceType, class RequestType, class ResponseType>
175  template <class ServiceType, class RequestType, class ResponseType>
177  template <class ServiceType, class RequestType, class ResponseType>
178  friend class BidiStreamingHandler;
179  friend class UnknownMethodHandler;
180  friend class ::grpc::ClientContext;
181 
182  // Prevent copying.
184  ServerContext& operator=(const ServerContext&);
185 
186  class CompletionOp;
187 
188  void BeginCompletionOp(Call* call);
189 
190  ServerContext(gpr_timespec deadline, grpc_metadata* metadata,
191  size_t metadata_count);
192 
193  void set_call(grpc_call* call);
194 
195  CompletionOp* completion_op_;
196  bool has_notify_when_done_tag_;
197  void* async_notify_when_done_tag_;
198 
199  gpr_timespec deadline_;
200  grpc_call* call_;
201  CompletionQueue* cq_;
202  bool sent_initial_metadata_;
203  mutable std::shared_ptr<const AuthContext> auth_context_;
204  std::multimap<grpc::string_ref, grpc::string_ref> client_metadata_;
205  std::multimap<grpc::string, grpc::string> initial_metadata_;
206  std::multimap<grpc::string, grpc::string> trailing_metadata_;
207 
208  grpc_compression_level compression_level_;
209  grpc_compression_algorithm compression_algorithm_;
210 };
211 
212 } // namespace grpc
213 
214 #endif // GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H
const std::multimap< grpc::string_ref, grpc::string_ref > & client_metadata()
Definition: server_context.h:121
grpc_compression_algorithm compression_algorithm() const
Definition: server_context.h:130
std::chrono::system_clock::time_point deadline()
Definition: server_context.h:96
void set_compression_algorithm(grpc_compression_algorithm algorithm)
grpc_compression_level compression_level() const
Definition: server_context.h:125
std::string string
Definition: config.h:112
void set_compression_level(grpc_compression_level level)
void AsyncNotifyWhenDone(void *tag)
Definition: server_context.h:148
void TryCancel() const
void AddInitialMetadata(const grpc::string &key, const grpc::string &value)
Definition: method_handler_impl.h:181
const struct census_context * census_context() const
Definition: call.h:576
Definition: completion_queue.h:60
bool IsCancelled() const
Definition: server_context.h:90
grpc::string peer() const
A thin wrapper around grpc_completion_queue (see / src/core/surface/completion_queue.h).
Definition: completion_queue.h:81
Definition: completion_queue.h:62
Definition: completion_queue.h:64
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
void AddTrailingMetadata(const grpc::string &key, const grpc::string &value)
gpr_timespec raw_deadline()
Definition: server_context.h:101
std::shared_ptr< const AuthContext > auth_context() const
Definition: completion_queue.h:66