blob: a1e1ed176f6e1ae480a40e033c3b67a216ae4cb4 [file] [log] [blame]
David Garcia Quintasb523c732016-01-25 18:22:28 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
David Garcia Quintasb523c732016-01-25 18:22:28 -08004 * 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
David Garcia Quintasb523c732016-01-25 18:22:28 -080040#include <grpc++/impl/codegen/config.h>
Craig Tiller3ab2fe02016-04-11 20:11:18 -070041#include <grpc++/impl/codegen/create_auth_context.h>
Craig Tillerf40df232016-03-25 13:38:14 -070042#include <grpc++/impl/codegen/security/auth_context.h>
David Garcia Quintasb523c732016-01-25 18:22:28 -080043#include <grpc++/impl/codegen/string_ref.h>
44#include <grpc++/impl/codegen/time.h>
Craig Tillerf40df232016-03-25 13:38:14 -070045#include <grpc/impl/codegen/compression_types.h>
46#include <grpc/impl/codegen/time.h>
David Garcia Quintasb523c732016-01-25 18:22:28 -080047
48struct gpr_timespec;
49struct grpc_metadata;
50struct grpc_call;
51struct census_context;
52
53namespace grpc {
54
55class ClientContext;
56template <class W, class R>
57class ServerAsyncReader;
58template <class W>
59class ServerAsyncWriter;
60template <class W>
61class ServerAsyncResponseWriter;
62template <class W, class R>
63class ServerAsyncReaderWriter;
64template <class R>
65class ServerReader;
66template <class W>
67class ServerWriter;
68template <class W, class R>
69class ServerReaderWriter;
70template <class ServiceType, class RequestType, class ResponseType>
71class RpcMethodHandler;
72template <class ServiceType, class RequestType, class ResponseType>
73class ClientStreamingHandler;
74template <class ServiceType, class RequestType, class ResponseType>
75class ServerStreamingHandler;
76template <class ServiceType, class RequestType, class ResponseType>
77class BidiStreamingHandler;
78class UnknownMethodHandler;
79
80class Call;
81class CallOpBuffer;
82class CompletionQueue;
83class Server;
84class ServerInterface;
85
86namespace testing {
87class InteropServerContextInspector;
88} // namespace testing
89
90// Interface of server side rpc context.
91class ServerContext {
92 public:
93 ServerContext(); // for async calls
94 ~ServerContext();
95
96#ifndef GRPC_CXX0X_NO_CHRONO
97 std::chrono::system_clock::time_point deadline() {
98 return Timespec2Timepoint(deadline_);
99 }
100#endif // !GRPC_CXX0X_NO_CHRONO
101
102 gpr_timespec raw_deadline() { 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
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800107 // 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
David Garcia Quintasb523c732016-01-25 18:22:28 -0800110 bool IsCancelled() const;
111
Sree Kuchibhotlaf25c6ba2016-01-28 18:04:03 -0800112 // Cancel the Call from the server. This is a best-effort API and depending on
Sree Kuchibhotla3075c812016-01-29 18:25:22 -0800113 // 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.
Sree Kuchibhotlaf25c6ba2016-01-28 18:04:03 -0800117 //
Sree Kuchibhotla3075c812016-01-29 18:25:22 -0800118 // 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.
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800123 void TryCancel() const;
124
David Garcia Quintasb523c732016-01-25 18:22:28 -0800125 const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata() {
126 return client_metadata_;
127 }
128
129 grpc_compression_level compression_level() const {
130 return compression_level_;
131 }
132 void set_compression_level(grpc_compression_level level);
133
134 grpc_compression_algorithm compression_algorithm() const {
135 return compression_algorithm_;
136 }
137 void set_compression_algorithm(grpc_compression_algorithm algorithm);
138
Craig Tiller3ab2fe02016-04-11 20:11:18 -0700139 std::shared_ptr<const AuthContext> auth_context() const {
140 if (auth_context_.get() == nullptr) {
141 auth_context_ = CreateAuthContext(call_);
142 }
143 return auth_context_;
144 }
David Garcia Quintasb523c732016-01-25 18:22:28 -0800145
146 // Return the peer uri in a string.
147 // WARNING: this value is never authenticated or subject to any security
148 // related code. It must not be used for any authentication related
149 // functionality. Instead, use auth_context.
150 grpc::string peer() const;
151
152 const struct census_context* census_context() const;
153
154 // Async only. Has to be called before the rpc starts.
155 // Returns the tag in completion queue when the rpc finishes.
156 // IsCancelled() can then be called to check whether the rpc was cancelled.
157 void AsyncNotifyWhenDone(void* tag) {
158 has_notify_when_done_tag_ = true;
159 async_notify_when_done_tag_ = tag;
160 }
161
162 private:
163 friend class ::grpc::testing::InteropServerContextInspector;
164 friend class ::grpc::ServerInterface;
165 friend class ::grpc::Server;
166 template <class W, class R>
167 friend class ::grpc::ServerAsyncReader;
168 template <class W>
169 friend class ::grpc::ServerAsyncWriter;
170 template <class W>
171 friend class ::grpc::ServerAsyncResponseWriter;
172 template <class W, class R>
173 friend class ::grpc::ServerAsyncReaderWriter;
174 template <class R>
175 friend class ::grpc::ServerReader;
176 template <class W>
177 friend class ::grpc::ServerWriter;
178 template <class W, class R>
179 friend class ::grpc::ServerReaderWriter;
180 template <class ServiceType, class RequestType, class ResponseType>
181 friend class RpcMethodHandler;
182 template <class ServiceType, class RequestType, class ResponseType>
183 friend class ClientStreamingHandler;
184 template <class ServiceType, class RequestType, class ResponseType>
185 friend class ServerStreamingHandler;
186 template <class ServiceType, class RequestType, class ResponseType>
187 friend class BidiStreamingHandler;
188 friend class UnknownMethodHandler;
189 friend class ::grpc::ClientContext;
190
191 // Prevent copying.
192 ServerContext(const ServerContext&);
193 ServerContext& operator=(const ServerContext&);
194
195 class CompletionOp;
196
197 void BeginCompletionOp(Call* call);
198
199 ServerContext(gpr_timespec deadline, grpc_metadata* metadata,
200 size_t metadata_count);
201
Craig Tiller3ab2fe02016-04-11 20:11:18 -0700202 void set_call(grpc_call* call) { call_ = call; }
David Garcia Quintasb523c732016-01-25 18:22:28 -0800203
Craig Tiller399b3c42016-04-01 12:24:18 -0700204 uint32_t initial_metadata_flags() const { return 0; }
205
David Garcia Quintasb523c732016-01-25 18:22:28 -0800206 CompletionOp* completion_op_;
207 bool has_notify_when_done_tag_;
208 void* async_notify_when_done_tag_;
209
210 gpr_timespec deadline_;
211 grpc_call* call_;
212 CompletionQueue* cq_;
213 bool sent_initial_metadata_;
214 mutable std::shared_ptr<const AuthContext> auth_context_;
215 std::multimap<grpc::string_ref, grpc::string_ref> client_metadata_;
216 std::multimap<grpc::string, grpc::string> initial_metadata_;
217 std::multimap<grpc::string, grpc::string> trailing_metadata_;
218
219 grpc_compression_level compression_level_;
220 grpc_compression_algorithm compression_algorithm_;
221};
222
223} // namespace grpc
224
225#endif // GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H