blob: 91ebe574b147a0b9b7fd0197515eb9cc9db2cf8d [file] [log] [blame]
David Garcia Quintasb523c732016-01-25 18:22:28 -08001/*
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>
42#include <grpc++/impl/codegen/security/auth_context.h>
43#include <grpc++/impl/codegen/config.h>
44#include <grpc++/impl/codegen/string_ref.h>
45#include <grpc++/impl/codegen/time.h>
46
47struct gpr_timespec;
48struct grpc_metadata;
49struct grpc_call;
50struct census_context;
51
52namespace grpc {
53
54class ClientContext;
55template <class W, class R>
56class ServerAsyncReader;
57template <class W>
58class ServerAsyncWriter;
59template <class W>
60class ServerAsyncResponseWriter;
61template <class W, class R>
62class ServerAsyncReaderWriter;
63template <class R>
64class ServerReader;
65template <class W>
66class ServerWriter;
67template <class W, class R>
68class ServerReaderWriter;
69template <class ServiceType, class RequestType, class ResponseType>
70class RpcMethodHandler;
71template <class ServiceType, class RequestType, class ResponseType>
72class ClientStreamingHandler;
73template <class ServiceType, class RequestType, class ResponseType>
74class ServerStreamingHandler;
75template <class ServiceType, class RequestType, class ResponseType>
76class BidiStreamingHandler;
77class UnknownMethodHandler;
78
79class Call;
80class CallOpBuffer;
81class CompletionQueue;
82class Server;
83class ServerInterface;
84
85namespace testing {
86class InteropServerContextInspector;
87} // namespace testing
88
89// Interface of server side rpc context.
90class ServerContext {
91 public:
92 ServerContext(); // for async calls
93 ~ServerContext();
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
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800106 // IsCancelled is always safe to call when using sync API
107 // When using async API, it is only safe to call IsCancelled after
108 // the AsyncNotifyWhenDone tag has been delivered
David Garcia Quintasb523c732016-01-25 18:22:28 -0800109 bool IsCancelled() const;
110
Sree Kuchibhotlaf25c6ba2016-01-28 18:04:03 -0800111 // Cancel the Call from the server. This is a best-effort API and depending on
Sree Kuchibhotla3075c812016-01-29 18:25:22 -0800112 // when it is called, the RPC may still appear successful to the client.
113 // For example, if TryCancel() is called on a separate thread, it might race
114 // with the server handler which might return success to the client before
115 // TryCancel() was even started by the thread.
Sree Kuchibhotlaf25c6ba2016-01-28 18:04:03 -0800116 //
Sree Kuchibhotla3075c812016-01-29 18:25:22 -0800117 // It is the caller's responsibility to prevent such races and ensure that if
118 // TryCancel() is called, the serverhandler must return Status::CANCELLED. The
119 // only exception is that if the serverhandler is already returning an error
120 // status code, it is ok to not return Status::CANCELLED even if TryCancel()
121 // was called.
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800122 void TryCancel() const;
123
David Garcia Quintasb523c732016-01-25 18:22:28 -0800124 const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata() {
125 return client_metadata_;
126 }
127
128 grpc_compression_level compression_level() const {
129 return compression_level_;
130 }
131 void set_compression_level(grpc_compression_level level);
132
133 grpc_compression_algorithm compression_algorithm() const {
134 return compression_algorithm_;
135 }
136 void set_compression_algorithm(grpc_compression_algorithm algorithm);
137
138 std::shared_ptr<const AuthContext> auth_context() const;
139
140 // Return the peer uri in a string.
141 // WARNING: this value is never authenticated or subject to any security
142 // related code. It must not be used for any authentication related
143 // functionality. Instead, use auth_context.
144 grpc::string peer() const;
145
146 const struct census_context* census_context() const;
147
148 // Async only. Has to be called before the rpc starts.
149 // Returns the tag in completion queue when the rpc finishes.
150 // IsCancelled() can then be called to check whether the rpc was cancelled.
151 void AsyncNotifyWhenDone(void* tag) {
152 has_notify_when_done_tag_ = true;
153 async_notify_when_done_tag_ = tag;
154 }
155
156 private:
157 friend class ::grpc::testing::InteropServerContextInspector;
158 friend class ::grpc::ServerInterface;
159 friend class ::grpc::Server;
160 template <class W, class R>
161 friend class ::grpc::ServerAsyncReader;
162 template <class W>
163 friend class ::grpc::ServerAsyncWriter;
164 template <class W>
165 friend class ::grpc::ServerAsyncResponseWriter;
166 template <class W, class R>
167 friend class ::grpc::ServerAsyncReaderWriter;
168 template <class R>
169 friend class ::grpc::ServerReader;
170 template <class W>
171 friend class ::grpc::ServerWriter;
172 template <class W, class R>
173 friend class ::grpc::ServerReaderWriter;
174 template <class ServiceType, class RequestType, class ResponseType>
175 friend class RpcMethodHandler;
176 template <class ServiceType, class RequestType, class ResponseType>
177 friend class ClientStreamingHandler;
178 template <class ServiceType, class RequestType, class ResponseType>
179 friend class ServerStreamingHandler;
180 template <class ServiceType, class RequestType, class ResponseType>
181 friend class BidiStreamingHandler;
182 friend class UnknownMethodHandler;
183 friend class ::grpc::ClientContext;
184
185 // Prevent copying.
186 ServerContext(const ServerContext&);
187 ServerContext& operator=(const ServerContext&);
188
189 class CompletionOp;
190
191 void BeginCompletionOp(Call* call);
192
193 ServerContext(gpr_timespec deadline, grpc_metadata* metadata,
194 size_t metadata_count);
195
196 void set_call(grpc_call* call);
197
198 CompletionOp* completion_op_;
199 bool has_notify_when_done_tag_;
200 void* async_notify_when_done_tag_;
201
202 gpr_timespec deadline_;
203 grpc_call* call_;
204 CompletionQueue* cq_;
205 bool sent_initial_metadata_;
206 mutable std::shared_ptr<const AuthContext> auth_context_;
207 std::multimap<grpc::string_ref, grpc::string_ref> client_metadata_;
208 std::multimap<grpc::string, grpc::string> initial_metadata_;
209 std::multimap<grpc::string, grpc::string> trailing_metadata_;
210
211 grpc_compression_level compression_level_;
212 grpc_compression_algorithm compression_algorithm_;
213};
214
215} // namespace grpc
216
217#endif // GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H