Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 | |
| 34 | #include <grpc++/async_server_context.h> |
| 35 | |
| 36 | #include <grpc/grpc.h> |
| 37 | #include <grpc/support/log.h> |
| 38 | #include "src/cpp/proto/proto_utils.h" |
Yang Gao | ee7ab3d | 2015-03-04 09:08:28 -0800 | [diff] [blame] | 39 | #include <grpc++/config.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc++/status.h> |
| 41 | |
| 42 | namespace grpc { |
| 43 | |
| 44 | AsyncServerContext::AsyncServerContext( |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 45 | grpc_call* call, const grpc::string& method, const grpc::string& host, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | system_clock::time_point absolute_deadline) |
| 47 | : method_(method), |
| 48 | host_(host), |
| 49 | absolute_deadline_(absolute_deadline), |
| 50 | request_(nullptr), |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 51 | call_(call) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | |
| 53 | AsyncServerContext::~AsyncServerContext() { grpc_call_destroy(call_); } |
| 54 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 55 | void AsyncServerContext::Accept(grpc_completion_queue* cq) { |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 56 | GPR_ASSERT(grpc_call_server_accept_old(call_, cq, this) == GRPC_CALL_OK); |
Craig Tiller | 573523f | 2015-02-17 07:38:26 -0800 | [diff] [blame] | 57 | GPR_ASSERT(grpc_call_server_end_initial_metadata_old( |
| 58 | call_, GRPC_WRITE_BUFFER_HINT) == GRPC_CALL_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 61 | bool AsyncServerContext::StartRead(grpc::protobuf::Message* request) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | GPR_ASSERT(request); |
| 63 | request_ = request; |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 64 | grpc_call_error err = grpc_call_start_read_old(call_, this); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | return err == GRPC_CALL_OK; |
| 66 | } |
| 67 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 68 | bool AsyncServerContext::StartWrite(const grpc::protobuf::Message& response, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | int flags) { |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 70 | grpc_byte_buffer* buffer = nullptr; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | if (!SerializeProto(response, &buffer)) { |
| 72 | return false; |
| 73 | } |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 74 | grpc_call_error err = grpc_call_start_write_old(call_, buffer, this, flags); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | grpc_byte_buffer_destroy(buffer); |
| 76 | return err == GRPC_CALL_OK; |
| 77 | } |
| 78 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 79 | bool AsyncServerContext::StartWriteStatus(const Status& status) { |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 80 | grpc_call_error err = grpc_call_start_write_status_old( |
ctiller | 2845cad | 2014-12-15 15:14:12 -0800 | [diff] [blame] | 81 | call_, static_cast<grpc_status_code>(status.code()), |
| 82 | status.details().empty() ? nullptr |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 83 | : const_cast<char*>(status.details().c_str()), |
ctiller | 2845cad | 2014-12-15 15:14:12 -0800 | [diff] [blame] | 84 | this); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 85 | return err == GRPC_CALL_OK; |
| 86 | } |
| 87 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 88 | bool AsyncServerContext::ParseRead(grpc_byte_buffer* read_buffer) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | GPR_ASSERT(request_); |
| 90 | bool success = DeserializeProto(read_buffer, request_); |
| 91 | request_ = nullptr; |
| 92 | return success; |
| 93 | } |
| 94 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 95 | } // namespace grpc |