yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 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 | |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 34 | #include <grpc++/server_context.h> |
Craig Tiller | c7625b0 | 2015-02-18 15:18:58 -0800 | [diff] [blame] | 35 | |
| 36 | #include <mutex> |
| 37 | |
Craig Tiller | 0156752 | 2015-02-11 21:08:49 -0800 | [diff] [blame] | 38 | #include <grpc++/impl/call.h> |
Craig Tiller | 854a30c | 2015-02-11 11:44:10 -0800 | [diff] [blame] | 39 | #include <grpc/grpc.h> |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/log.h> |
Craig Tiller | 854a30c | 2015-02-11 11:44:10 -0800 | [diff] [blame] | 41 | #include "src/cpp/util/time.h" |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 42 | |
Craig Tiller | 854a30c | 2015-02-11 11:44:10 -0800 | [diff] [blame] | 43 | namespace grpc { |
| 44 | |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 45 | // CompletionOp |
| 46 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 47 | class ServerContext::CompletionOp GRPC_FINAL : public CallOpBuffer { |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 48 | public: |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 49 | // initial refs: one in the server context, one in the cq |
| 50 | CompletionOp() : refs_(2), finalized_(false), cancelled_(false) { |
| 51 | AddServerRecvClose(&cancelled_); |
| 52 | } |
| 53 | bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE; |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 54 | |
| 55 | bool CheckCancelled(CompletionQueue* cq); |
| 56 | |
| 57 | void Unref(); |
| 58 | |
| 59 | private: |
| 60 | std::mutex mu_; |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 61 | int refs_; |
| 62 | bool finalized_; |
| 63 | bool cancelled_; |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 66 | void ServerContext::CompletionOp::Unref() { |
| 67 | std::unique_lock<std::mutex> lock(mu_); |
| 68 | if (--refs_ == 0) { |
| 69 | lock.unlock(); |
| 70 | delete this; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | bool ServerContext::CompletionOp::CheckCancelled(CompletionQueue* cq) { |
Craig Tiller | d24d13d | 2015-02-18 15:35:32 -0800 | [diff] [blame] | 75 | cq->TryPluck(this); |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 76 | std::lock_guard<std::mutex> g(mu_); |
| 77 | return finalized_ ? cancelled_ : false; |
| 78 | } |
| 79 | |
| 80 | bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { |
| 81 | GPR_ASSERT(CallOpBuffer::FinalizeResult(tag, status)); |
| 82 | std::unique_lock<std::mutex> lock(mu_); |
| 83 | finalized_ = true; |
| 84 | if (!*status) cancelled_ = true; |
| 85 | if (--refs_ == 0) { |
| 86 | lock.unlock(); |
| 87 | delete this; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // ServerContext body |
| 93 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 94 | ServerContext::ServerContext() |
| 95 | : completion_op_(nullptr), |
| 96 | call_(nullptr), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 97 | sent_initial_metadata_(false) {} |
Craig Tiller | c645306 | 2015-02-12 17:32:57 -0800 | [diff] [blame] | 98 | |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 99 | ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata* metadata, |
Craig Tiller | 9dcb0f8 | 2015-02-11 15:36:31 -0800 | [diff] [blame] | 100 | size_t metadata_count) |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 101 | : completion_op_(nullptr), |
| 102 | deadline_(Timespec2Timepoint(deadline)), |
| 103 | call_(nullptr), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 104 | sent_initial_metadata_(false) { |
Craig Tiller | 854a30c | 2015-02-11 11:44:10 -0800 | [diff] [blame] | 105 | for (size_t i = 0; i < metadata_count; i++) { |
Craig Tiller | 9dcb0f8 | 2015-02-11 15:36:31 -0800 | [diff] [blame] | 106 | client_metadata_.insert(std::make_pair( |
| 107 | grpc::string(metadata[i].key), |
| 108 | grpc::string(metadata[i].value, |
| 109 | metadata[i].value + metadata[i].value_length))); |
Craig Tiller | 854a30c | 2015-02-11 11:44:10 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 113 | ServerContext::~ServerContext() { |
| 114 | if (call_) { |
| 115 | grpc_call_destroy(call_); |
| 116 | } |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 117 | if (completion_op_) { |
| 118 | completion_op_->Unref(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void ServerContext::BeginCompletionOp(Call* call) { |
| 123 | GPR_ASSERT(!completion_op_); |
| 124 | completion_op_ = new CompletionOp(); |
| 125 | call->PerformOps(completion_op_); |
Craig Tiller | 3d6ceb6 | 2015-02-12 14:33:54 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 128 | void ServerContext::AddInitialMetadata(const grpc::string& key, |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 129 | const grpc::string& value) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 130 | initial_metadata_.insert(std::make_pair(key, value)); |
| 131 | } |
| 132 | |
| 133 | void ServerContext::AddTrailingMetadata(const grpc::string& key, |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 134 | const grpc::string& value) { |
Yang Gao | 2b7f537 | 2015-02-18 00:45:53 -0800 | [diff] [blame] | 135 | trailing_metadata_.insert(std::make_pair(key, value)); |
| 136 | } |
| 137 | |
Craig Tiller | 854a30c | 2015-02-11 11:44:10 -0800 | [diff] [blame] | 138 | } // namespace grpc |