David Garcia Quintas | b523c73 | 2016-01-25 18:22:28 -0800 | [diff] [blame] | 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> |
| 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 | |
| 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. |
| 90 | class 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 Kuchibhotla | b0d1567 | 2016-03-07 10:51:02 -0800 | [diff] [blame] | 106 | // 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 Quintas | b523c73 | 2016-01-25 18:22:28 -0800 | [diff] [blame] | 109 | bool IsCancelled() const; |
| 110 | |
Sree Kuchibhotla | f25c6ba | 2016-01-28 18:04:03 -0800 | [diff] [blame] | 111 | // Cancel the Call from the server. This is a best-effort API and depending on |
Sree Kuchibhotla | 3075c81 | 2016-01-29 18:25:22 -0800 | [diff] [blame] | 112 | // 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 Kuchibhotla | f25c6ba | 2016-01-28 18:04:03 -0800 | [diff] [blame] | 116 | // |
Sree Kuchibhotla | 3075c81 | 2016-01-29 18:25:22 -0800 | [diff] [blame] | 117 | // 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 Kuchibhotla | 7fa9d6f | 2016-01-28 17:32:48 -0800 | [diff] [blame] | 122 | void TryCancel() const; |
| 123 | |
David Garcia Quintas | b523c73 | 2016-01-25 18:22:28 -0800 | [diff] [blame] | 124 | 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 |