RTI Routing Service  Version 6.0.1
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
BodyManagedReply.hpp
1 /*
2  * $Id$
3  *
4  * (c) Copyright, Real-Time Innovations, 2016-.
5  * All rights reserved.
6  * No duplications, whole or partial, manual or electronic, may be made
7  * without express written permission. Any such copies, or
8  * revisions thereof, must display this notice unaltered.
9  * This code contains trade secrets of Real-Time Innovations, Inc.
10  */
11 
12 #ifndef HPP_SERVICE_BODY_MANAGED_REPLY_HPP_
13 #define HPP_SERVICE_BODY_MANAGED_REPLY_HPP_
14 
15 
16 // IMPORTANT: macros.hpp must be the first RTI header included in every header
17 // file so that symbols are exported correctly on Windows
18 #include <dds/core/macros.hpp>
19 
20 #include "osapi/osapi_heap.h"
21 #include "routingservice/routingservice_log.h"
22 #include "routingservice/routingservice_remote_config.h"
23 
24 #include <rtiboost/assert.hpp>
25 #include <dds/core/Value.hpp>
26 #include <dds/core/types.hpp>
27 #include <rti/service/ServiceException.hpp>
28 #include <rti/service/DynamicBuffer.hpp>
29 
30 namespace rti { namespace service { namespace admin {
31 
32 class BodyManagedReply
33 {
34 public:
35 
36  typedef RTI_Service_Admin_CommandReply reply_type;
37 
38  BodyManagedReply()
39  : string_body_(default_property()), octet_body_(default_property())
40  {
41  initialize_native_reply();
42  }
43 
44  BodyManagedReply(const DynamicBufferProperty &property)
45  : string_body_(property), octet_body_(property)
46  {
47  initialize_native_reply();
48  }
49 
50  ~BodyManagedReply()
51  {
52  // CODEGENII-779: clear pointers to avoid invalid deletion by finalizer
53  reply_.string_body = NULL;
54  if (!DDS_OctetSeq_has_ownership(&reply_.octet_body)) {
55  DDS_OctetSeq_unloan(&reply_.octet_body);
56  }
57 
58  RTI_Service_Admin_CommandReply_finalize_ex(
59  &reply_,
60  RTI_FALSE /* delete_pointers */);
61  }
62 
63  void release_string_body(char *&buffer, int32_t& length)
64  {
65  string_body_.release(buffer, length);
66  consolidate_string_body();
67  }
68 
69  void own_string_body(char *buffer, int32_t length)
70  {
71  string_body_.own(buffer, length);
72  consolidate_string_body();
73  }
74 
75  char* string_body()
76  {
77  return reply_.string_body;
78  }
79 
80  DDS_OctetSeq& octet_body()
81  {
82  return reply_.octet_body;
83  }
84 
85  char* string_body(int32_t length)
86  {
87  string_body_.ensure_length(length + 1);
88  consolidate_string_body();
89  return string_body();
90  }
91 
92  DDS_OctetSeq& octet_body(int32_t length)
93  {
94  octet_body_.ensure_length(length + 1);
95  consolidate_octet_body();
96  return octet_body();
97  }
98 
99  void retcode(RTI_Service_Admin_CommandReplyRetcode retcode)
100  {
101  reply_.retcode = retcode;
102  }
103 
104  void native_retcode(int32_t native_retcode)
105  {
106  reply_.native_retcode = native_retcode;
107  }
108 
109  void clear()
110  {
111  string_body_.trim_to_size();
112  *string_body_.buffer() = '\0';
113  octet_body_.trim_to_size();
114  DDS_OctetSeq_set_length(&reply_.octet_body, 0);
115  retcode(RTI_SERVICE_COMMAND_REPLY_OK);
116  }
117 
118  const reply_type& reply() const
119  {
120  return reply_;
121  }
122 
123  static DynamicBufferProperty default_property()
124  {
125  return DynamicBufferProperty(32 * 1024, false);
126  }
127 
128 private:
129  void consolidate_string_body()
130  {
131  reply_.string_body = string_body_.buffer();
132  }
133 
134  void consolidate_octet_body()
135  {
136  // re-loan
137  if (!DDS_OctetSeq_has_ownership(&reply_.octet_body)) {
138  DDS_OctetSeq_unloan(&reply_.octet_body);
139  }
140  DDS_OctetSeq_loan_contiguous(
141  &reply_.octet_body,
142  (DDS_Octet *) octet_body_.buffer(),
143  0,
144  octet_body_.length());
145  }
146 
147  void initialize_native_reply()
148  {
149  RTIOsapiMemory_zero(&reply_, sizeof(reply_type));
150  if (!RTI_Service_Admin_CommandReply_initialize_ex(
151  &reply_,
152  RTI_FALSE, /*allocate_pointers */
153  RTI_FALSE /*allocate_memory*/)) {
154  RTI_THROW_SERVICE_EXCEPTION(
155  &RTI_LOG_INIT_FAILURE_s,
156  "reply buffer");
157  }
158  RTIBOOST_ASSERT(reply_.string_body == NULL);
159  RTIBOOST_ASSERT(DDS_OctetSeq_get_length(&reply_.octet_body) == 0);
160  consolidate_string_body();
161  consolidate_octet_body();
162  }
163 
164 private:
165  reply_type reply_;
166  DynamicBuffer string_body_;
167  DynamicBuffer octet_body_;
168 
169 };
170 
171 } } } // rti::service::admin
172 #endif /* HPP_SERVICE_BODY_MANAGED_REPLY_HPP_ */

RTI Routing Service Version 6.0.1 Copyright © Sun Nov 17 2019 Real-Time Innovations, Inc