Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: third_party/grpc/src/cpp/server/server_context.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #include <grpc++/server_context.h>
35
36 #include <grpc++/completion_queue.h>
37 #include <grpc++/impl/call.h>
38 #include <grpc++/impl/sync.h>
39 #include <grpc++/support/time.h>
40 #include <grpc/compression.h>
41 #include <grpc/grpc.h>
42 #include <grpc/support/alloc.h>
43 #include <grpc/support/log.h>
44
45 #include "src/core/channel/compress_filter.h"
46 #include "src/cpp/common/create_auth_context.h"
47
48 namespace grpc {
49
50 // CompletionOp
51
52 class ServerContext::CompletionOp GRPC_FINAL : public CallOpSetInterface {
53 public:
54 // initial refs: one in the server context, one in the cq
55 CompletionOp()
56 : has_tag_(false),
57 tag_(nullptr),
58 refs_(2),
59 finalized_(false),
60 cancelled_(0) {}
61
62 void FillOps(grpc_op* ops, size_t* nops) GRPC_OVERRIDE;
63 bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE;
64
65 bool CheckCancelled(CompletionQueue* cq);
66
67 void set_tag(void* tag) {
68 has_tag_ = true;
69 tag_ = tag;
70 }
71
72 void Unref();
73
74 private:
75 bool has_tag_;
76 void* tag_;
77 grpc::mutex mu_;
78 int refs_;
79 bool finalized_;
80 int cancelled_;
81 };
82
83 void ServerContext::CompletionOp::Unref() {
84 grpc::unique_lock<grpc::mutex> lock(mu_);
85 if (--refs_ == 0) {
86 lock.unlock();
87 delete this;
88 }
89 }
90
91 bool ServerContext::CompletionOp::CheckCancelled(CompletionQueue* cq) {
92 cq->TryPluck(this);
93 grpc::lock_guard<grpc::mutex> g(mu_);
94 return finalized_ ? cancelled_ != 0 : false;
95 }
96
97 void ServerContext::CompletionOp::FillOps(grpc_op* ops, size_t* nops) {
98 ops->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
99 ops->data.recv_close_on_server.cancelled = &cancelled_;
100 ops->flags = 0;
101 ops->reserved = NULL;
102 *nops = 1;
103 }
104
105 bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) {
106 grpc::unique_lock<grpc::mutex> lock(mu_);
107 finalized_ = true;
108 bool ret = false;
109 if (has_tag_) {
110 *tag = tag_;
111 ret = true;
112 }
113 if (!*status) cancelled_ = 1;
114 if (--refs_ == 0) {
115 lock.unlock();
116 delete this;
117 }
118 return ret;
119 }
120
121 // ServerContext body
122
123 ServerContext::ServerContext()
124 : completion_op_(nullptr),
125 has_notify_when_done_tag_(false),
126 async_notify_when_done_tag_(nullptr),
127 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
128 call_(nullptr),
129 cq_(nullptr),
130 sent_initial_metadata_(false) {}
131
132 ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata* metadata,
133 size_t metadata_count)
134 : completion_op_(nullptr),
135 has_notify_when_done_tag_(false),
136 async_notify_when_done_tag_(nullptr),
137 deadline_(deadline),
138 call_(nullptr),
139 cq_(nullptr),
140 sent_initial_metadata_(false) {
141 for (size_t i = 0; i < metadata_count; i++) {
142 client_metadata_.insert(std::pair<grpc::string_ref, grpc::string_ref>(
143 metadata[i].key,
144 grpc::string_ref(metadata[i].value, metadata[i].value_length)));
145 }
146 }
147
148 ServerContext::~ServerContext() {
149 if (call_) {
150 grpc_call_destroy(call_);
151 }
152 if (completion_op_) {
153 completion_op_->Unref();
154 }
155 }
156
157 void ServerContext::BeginCompletionOp(Call* call) {
158 GPR_ASSERT(!completion_op_);
159 completion_op_ = new CompletionOp();
160 if (has_notify_when_done_tag_) {
161 completion_op_->set_tag(async_notify_when_done_tag_);
162 }
163 call->PerformOps(completion_op_);
164 }
165
166 void ServerContext::AddInitialMetadata(const grpc::string& key,
167 const grpc::string& value) {
168 initial_metadata_.insert(std::make_pair(key, value));
169 }
170
171 void ServerContext::AddTrailingMetadata(const grpc::string& key,
172 const grpc::string& value) {
173 trailing_metadata_.insert(std::make_pair(key, value));
174 }
175
176 void ServerContext::TryCancel() const {
177 grpc_call_error err = grpc_call_cancel_with_status(
178 call_, GRPC_STATUS_CANCELLED, "Cancelled on the server side", NULL);
179 if (err != GRPC_CALL_OK) {
180 gpr_log(GPR_ERROR, "TryCancel failed with: %d", err);
181 }
182 }
183
184 bool ServerContext::IsCancelled() const {
185 return completion_op_ && completion_op_->CheckCancelled(cq_);
186 }
187
188 void ServerContext::set_compression_level(grpc_compression_level level) {
189 const grpc_compression_algorithm algorithm_for_level =
190 grpc_compression_algorithm_for_level(level);
191 set_compression_algorithm(algorithm_for_level);
192 }
193
194 void ServerContext::set_compression_algorithm(
195 grpc_compression_algorithm algorithm) {
196 char* algorithm_name = NULL;
197 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
198 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
199 algorithm);
200 abort();
201 }
202 GPR_ASSERT(algorithm_name != NULL);
203 AddInitialMetadata(GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, algorithm_name);
204 }
205
206 void ServerContext::set_call(grpc_call* call) {
207 call_ = call;
208 auth_context_ = CreateAuthContext(call);
209 }
210
211 std::shared_ptr<const AuthContext> ServerContext::auth_context() const {
212 if (auth_context_.get() == nullptr) {
213 auth_context_ = CreateAuthContext(call_);
214 }
215 return auth_context_;
216 }
217
218 grpc::string ServerContext::peer() const {
219 grpc::string peer;
220 if (call_) {
221 char* c_peer = grpc_call_get_peer(call_);
222 peer = c_peer;
223 gpr_free(c_peer);
224 }
225 return peer;
226 }
227
228 const struct census_context* ServerContext::census_context() const {
229 return grpc_census_call_get_context(call_);
230 }
231
232 } // namespace grpc
OLDNEW
« no previous file with comments | « third_party/grpc/src/cpp/server/server_builder.cc ('k') | third_party/grpc/src/cpp/server/server_credentials.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698