gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
call.h
1 /*
2  *
3  * Copyright 2015, 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_CALL_H
35 #define GRPCXX_IMPL_CALL_H
36 
37 #include <grpc/grpc.h>
38 #include <grpc++/completion_queue.h>
39 #include <grpc++/config.h>
40 #include <grpc++/status.h>
41 
42 #include <memory>
43 #include <map>
44 
45 struct grpc_call;
46 struct grpc_op;
47 
48 namespace grpc {
49 
50 class ByteBuffer;
51 class Call;
52 
54  public:
55  CallOpBuffer();
56  ~CallOpBuffer();
57 
58  void Reset(void* next_return_tag);
59 
60  // Does not take ownership.
61  void AddSendInitialMetadata(
62  std::multimap<grpc::string, grpc::string>* metadata);
63  void AddSendInitialMetadata(ClientContext* ctx);
64  void AddRecvInitialMetadata(ClientContext* ctx);
65  void AddSendMessage(const grpc::protobuf::Message& message);
66  void AddSendMessage(const ByteBuffer& message);
67  void AddRecvMessage(grpc::protobuf::Message* message);
68  void AddRecvMessage(ByteBuffer* message);
69  void AddClientSendClose();
70  void AddClientRecvStatus(ClientContext* ctx, Status* status);
71  void AddServerSendStatus(std::multimap<grpc::string, grpc::string>* metadata,
72  const Status& status);
73  void AddServerRecvClose(bool* cancelled);
74 
75  // INTERNAL API:
76 
77  // Convert to an array of grpc_op elements
78  void FillOps(grpc_op* ops, size_t* nops);
79 
80  // Called by completion queue just prior to returning from Next() or Pluck()
81  bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE;
82 
83  void set_max_message_size(int max_message_size) {
84  max_message_size_ = max_message_size;
85  }
86 
87  bool got_message;
88 
89  private:
90  void* return_tag_;
91  // Send initial metadata
92  bool send_initial_metadata_;
93  size_t initial_metadata_count_;
94  grpc_metadata* initial_metadata_;
95  // Recv initial metadta
96  std::multimap<grpc::string, grpc::string>* recv_initial_metadata_;
97  grpc_metadata_array recv_initial_metadata_arr_;
98  // Send message
99  const grpc::protobuf::Message* send_message_;
100  const ByteBuffer* send_message_buffer_;
101  grpc_byte_buffer* send_buf_;
102  // Recv message
103  grpc::protobuf::Message* recv_message_;
104  ByteBuffer* recv_message_buffer_;
105  grpc_byte_buffer* recv_buf_;
106  int max_message_size_;
107  // Client send close
108  bool client_send_close_;
109  // Client recv status
110  std::multimap<grpc::string, grpc::string>* recv_trailing_metadata_;
111  Status* recv_status_;
112  grpc_metadata_array recv_trailing_metadata_arr_;
113  grpc_status_code status_code_;
114  char* status_details_;
115  size_t status_details_capacity_;
116  // Server send status
117  bool send_status_available_;
118  grpc_status_code send_status_code_;
119  grpc::string send_status_details_;
120  size_t trailing_metadata_count_;
121  grpc_metadata* trailing_metadata_;
122  int cancelled_buf_;
123  bool* recv_closed_;
124 };
125 
126 // SneakyCallOpBuffer does not post completions to the completion queue
127 class SneakyCallOpBuffer GRPC_FINAL : public CallOpBuffer {
128  public:
129  bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
130  return CallOpBuffer::FinalizeResult(tag, status) && false;
131  }
132 };
133 
134 // Channel and Server implement this to allow them to hook performing ops
135 class CallHook {
136  public:
137  virtual ~CallHook() {}
138  virtual void PerformOpsOnCall(CallOpBuffer* ops, Call* call) = 0;
139 };
140 
141 // Straightforward wrapping of the C call object
142 class Call GRPC_FINAL {
143  public:
144  /* call is owned by the caller */
145  Call(grpc_call* call, CallHook* call_hook_, CompletionQueue* cq);
146  Call(grpc_call* call, CallHook* call_hook_, CompletionQueue* cq,
147  int max_message_size);
148 
149  void PerformOps(CallOpBuffer* buffer);
150 
151  grpc_call* call() { return call_; }
152  CompletionQueue* cq() { return cq_; }
153 
154  int max_message_size() { return max_message_size_; }
155 
156  private:
157  CallHook* call_hook_;
158  CompletionQueue* cq_;
159  grpc_call* call_;
160  int max_message_size_;
161 };
162 
163 } // namespace grpc
164 
165 #endif // GRPCXX_IMPL_CALL_H
Definition: completion_queue.h:76
Definition: _completion_queue.h:40
Definition: status.h:42
Definition: byte_buffer.h:43
Definition: grpc.h:207
Definition: completion_queue.h:64
Definition: channel.h:53
Definition: client_context.h:72
Definition: grpc.h:182
Definition: grpc.h:258
Definition: call.h:135
Definition: _call.h:44
Definition: call.h:53
Definition: call.c:128