blob: d3086aef0a0a776a5c6792b02c328936a4c482bf [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
106 bool IsCancelled() const;
107
Sree Kuchibhotlaf25c6ba2016-01-28 18:04:03 -0800108 // Cancel the Call from the server. This is a best-effort API and depending on
109 // when this is called, the Call may still appear successful to the client.
110 // For example, if called on a separate thread, it might race with the
111 // server handler which might return success to the client before TryCancel()
112 // was called.
113 //
114 // It is the caller's responsibility to prevent such races and ensure that the
115 // serverhandler returns Status::CANCELLED if TryCancel() is called (unless
116 // the serverhandler is already returning an error code)
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800117 void TryCancel() const;
118
David Garcia Quintasb523c732016-01-25 18:22:28 -0800119 const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata() {
120 return client_metadata_;
121 }
122
123 grpc_compression_level compression_level() const {
124 return compression_level_;
125 }
126 void set_compression_level(grpc_compression_level level);
127
128 grpc_compression_algorithm compression_algorithm() const {
129 return compression_algorithm_;
130 }
131 void set_compression_algorithm(grpc_compression_algorithm algorithm);
132
133 std::shared_ptr<const AuthContext> auth_context() const;
134
135 // Return the peer uri in a string.
136 // WARNING: this value is never authenticated or subject to any security
137 // related code. It must not be used for any authentication related
138 // functionality. Instead, use auth_context.
139 grpc::string peer() const;
140
141 const struct census_context* census_context() const;
142
143 // Async only. Has to be called before the rpc starts.
144 // Returns the tag in completion queue when the rpc finishes.
145 // IsCancelled() can then be called to check whether the rpc was cancelled.
146 void AsyncNotifyWhenDone(void* tag) {
147 has_notify_when_done_tag_ = true;
148 async_notify_when_done_tag_ = tag;
149 }
150
151 private:
152 friend class ::grpc::testing::InteropServerContextInspector;
153 friend class ::grpc::ServerInterface;
154 friend class ::grpc::Server;
155 template <class W, class R>
156 friend class ::grpc::ServerAsyncReader;
157 template <class W>
158 friend class ::grpc::ServerAsyncWriter;
159 template <class W>
160 friend class ::grpc::ServerAsyncResponseWriter;
161 template <class W, class R>
162 friend class ::grpc::ServerAsyncReaderWriter;
163 template <class R>
164 friend class ::grpc::ServerReader;
165 template <class W>
166 friend class ::grpc::ServerWriter;
167 template <class W, class R>
168 friend class ::grpc::ServerReaderWriter;
169 template <class ServiceType, class RequestType, class ResponseType>
170 friend class RpcMethodHandler;
171 template <class ServiceType, class RequestType, class ResponseType>
172 friend class ClientStreamingHandler;
173 template <class ServiceType, class RequestType, class ResponseType>
174 friend class ServerStreamingHandler;
175 template <class ServiceType, class RequestType, class ResponseType>
176 friend class BidiStreamingHandler;
177 friend class UnknownMethodHandler;
178 friend class ::grpc::ClientContext;
179
180 // Prevent copying.
181 ServerContext(const ServerContext&);
182 ServerContext& operator=(const ServerContext&);
183
184 class CompletionOp;
185
186 void BeginCompletionOp(Call* call);
187
188 ServerContext(gpr_timespec deadline, grpc_metadata* metadata,
189 size_t metadata_count);
190
191 void set_call(grpc_call* call);
192
193 CompletionOp* completion_op_;
194 bool has_notify_when_done_tag_;
195 void* async_notify_when_done_tag_;
196
197 gpr_timespec deadline_;
198 grpc_call* call_;
199 CompletionQueue* cq_;
200 bool sent_initial_metadata_;
201 mutable std::shared_ptr<const AuthContext> auth_context_;
202 std::multimap<grpc::string_ref, grpc::string_ref> client_metadata_;
203 std::multimap<grpc::string, grpc::string> initial_metadata_;
204 std::multimap<grpc::string, grpc::string> trailing_metadata_;
205
206 grpc_compression_level compression_level_;
207 grpc_compression_algorithm compression_algorithm_;
208};
209
210} // namespace grpc
211
212#endif // GRPCXX_IMPL_CODEGEN_SERVER_CONTEXT_H