blob: 628822a338749b2644a822d9cc246bf77c9a5ce0 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Gaoee7ab3d2015-03-04 09:08:28 -080039#include <grpc++/config.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc++/status.h>
41
42namespace grpc {
43
44AsyncServerContext::AsyncServerContext(
Yang Gao6baa9b62015-03-17 10:49:39 -070045 grpc_call* call, const grpc::string& method, const grpc::string& host,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046 system_clock::time_point absolute_deadline)
47 : method_(method),
48 host_(host),
49 absolute_deadline_(absolute_deadline),
50 request_(nullptr),
Craig Tillerb5dcec52015-01-13 11:13:42 -080051 call_(call) {}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
53AsyncServerContext::~AsyncServerContext() { grpc_call_destroy(call_); }
54
Yang Gao6baa9b62015-03-17 10:49:39 -070055void AsyncServerContext::Accept(grpc_completion_queue* cq) {
Craig Tillerc4f0ebe2015-02-02 10:16:30 -080056 GPR_ASSERT(grpc_call_server_accept_old(call_, cq, this) == GRPC_CALL_OK);
Craig Tiller573523f2015-02-17 07:38:26 -080057 GPR_ASSERT(grpc_call_server_end_initial_metadata_old(
58 call_, GRPC_WRITE_BUFFER_HINT) == GRPC_CALL_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059}
60
Yang Gao6baa9b62015-03-17 10:49:39 -070061bool AsyncServerContext::StartRead(grpc::protobuf::Message* request) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062 GPR_ASSERT(request);
63 request_ = request;
Craig Tillerc4f0ebe2015-02-02 10:16:30 -080064 grpc_call_error err = grpc_call_start_read_old(call_, this);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065 return err == GRPC_CALL_OK;
66}
67
Yang Gao6baa9b62015-03-17 10:49:39 -070068bool AsyncServerContext::StartWrite(const grpc::protobuf::Message& response,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069 int flags) {
Yang Gao6baa9b62015-03-17 10:49:39 -070070 grpc_byte_buffer* buffer = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071 if (!SerializeProto(response, &buffer)) {
72 return false;
73 }
Craig Tillerc4f0ebe2015-02-02 10:16:30 -080074 grpc_call_error err = grpc_call_start_write_old(call_, buffer, this, flags);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 grpc_byte_buffer_destroy(buffer);
76 return err == GRPC_CALL_OK;
77}
78
Yang Gao6baa9b62015-03-17 10:49:39 -070079bool AsyncServerContext::StartWriteStatus(const Status& status) {
Craig Tillerc4f0ebe2015-02-02 10:16:30 -080080 grpc_call_error err = grpc_call_start_write_status_old(
ctiller2845cad2014-12-15 15:14:12 -080081 call_, static_cast<grpc_status_code>(status.code()),
82 status.details().empty() ? nullptr
Yang Gao6baa9b62015-03-17 10:49:39 -070083 : const_cast<char*>(status.details().c_str()),
ctiller2845cad2014-12-15 15:14:12 -080084 this);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085 return err == GRPC_CALL_OK;
86}
87
Yang Gao6baa9b62015-03-17 10:49:39 -070088bool AsyncServerContext::ParseRead(grpc_byte_buffer* read_buffer) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089 GPR_ASSERT(request_);
90 bool success = DeserializeProto(read_buffer, request_);
91 request_ = nullptr;
92 return success;
93}
94
Craig Tiller190d3602015-02-18 09:23:38 -080095} // namespace grpc