19 #ifndef INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP 20 #define INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP 24 #include <type_traits> 26 #include <RCF/ByteBuffer.hpp> 27 #include <RCF/MemStream.hpp> 29 #if RCF_FEATURE_PROTOBUF==1 30 #include <RCF/GoogleProtobufs.hpp> 35 #include <RCF/Config.hpp> 38 #include <RCF/SerializationProtocol_SF.hpp> 41 #if RCF_FEATURE_BOOST_SERIALIZATION==1 42 #include <RCF/SerializationProtocol_BS.hpp> 47 RCF_EXPORT
bool isSerializationProtocolSupported(
int protocol);
49 RCF_EXPORT std::string getSerializationProtocolName(
int protocol);
51 class MethodInvocationRequest;
52 class MethodInvocationResponse;
54 class RCF_EXPORT SerializationProtocolIn
57 SerializationProtocolIn();
58 ~SerializationProtocolIn();
60 MemIstream& getIstream() {
return mIs; }
62 void setSerializationProtocol(
int protocol);
63 int getSerializationProtocol()
const;
66 const ByteBuffer &data,
70 bool enableSfPointerTracking);
72 void clearByteBuffer();
74 void extractSlice(ByteBuffer &byteBuffer, std::size_t len);
75 std::size_t getArchiveLength();
76 std::size_t getRemainingArchiveLength();
79 void read(
const T *pt)
91 case 1: mInProtocol1 >> t;
break;
92 case 2: mInProtocol2 >> t;
break;
93 case 3: mInProtocol3 >> t;
break;
94 case 4: mInProtocol4 >> t;
break;
95 case 5: mInProtocol5 >> t;
break;
97 default: RCF_ASSERT_ALWAYS(
"");
102 RCF_UNUSED_VARIABLE(e);
105 catch(
const std::exception &e)
121 void unbindProtocol();
123 friend class ClientStub;
124 friend class RcfSession;
127 ByteBuffer mByteBuffer;
130 Protocol< Int<1> >::In mInProtocol1;
131 Protocol< Int<2> >::In mInProtocol2;
132 Protocol< Int<3> >::In mInProtocol3;
133 Protocol< Int<4> >::In mInProtocol4;
134 Protocol< Int<5> >::In mInProtocol5;
140 class RCF_EXPORT SerializationProtocolOut
143 SerializationProtocolOut();
145 MemOstream& getMemOstream() {
return *mOsPtr; }
147 void setSerializationProtocol(
int protocol);
148 int getSerializationProtocol()
const;
153 ByteBuffer byteBuffer,
156 bool enableSfPointerTracking);
159 void write(
const T &t)
165 case 1: mOutProtocol1 << t;
break;
166 case 2: mOutProtocol2 << t;
break;
167 case 3: mOutProtocol3 << t;
break;
168 case 4: mOutProtocol4 << t;
break;
169 case 5: mOutProtocol5 << t;
break;
171 default: RCF_ASSERT_ALWAYS(
"");
174 catch(
const std::exception &e)
185 void insert(
const ByteBuffer &byteBuffer);
186 void extractByteBuffers();
187 void extractByteBuffers(std::vector<ByteBuffer> &byteBuffers);
194 void unbindProtocol();
197 friend class ClientStub;
198 friend class RcfSession;
202 std::shared_ptr<MemOstream> mOsPtr;
203 std::vector<std::pair<std::size_t, ByteBuffer> > mByteBuffers;
206 Protocol< Int<1> >::Out mOutProtocol1;
207 Protocol< Int<2> >::Out mOutProtocol2;
208 Protocol< Int<3> >::Out mOutProtocol3;
209 Protocol< Int<4> >::Out mOutProtocol4;
210 Protocol< Int<5> >::Out mOutProtocol5;
216 inline void serialize(
217 SerializationProtocolOut &,
222 inline void serialize(
223 SerializationProtocolOut &,
228 inline void deserialize(
229 SerializationProtocolIn &,
234 inline void deserialize(
235 SerializationProtocolIn &,
242 SerializationProtocolOut &out,
250 void deserializeImpl(
251 SerializationProtocolIn &in,
258 #if RCF_FEATURE_PROTOBUF==1 264 void serializeProtobufOrNot(
265 SerializationProtocolOut &out,
269 serializeImpl(out, t, 0);
273 void deserializeProtobufOrNot(
274 SerializationProtocolIn &in,
278 deserializeImpl(in, t, 0);
282 void serializeProtobufOrNot(
283 SerializationProtocolOut &out,
287 MemOstream & os = out.getMemOstream();
289 std::streamoff beginPos = os.tellp();
291 if (!t.IsInitialized())
293 RCF_THROW(Exception(RcfError_ProtobufWriteInit,
typeid(t).name()));
298 int byteSize = t.ByteSize();
299 RCF_ASSERT(byteSize >= 0);
300 for (
int i=0; i<byteSize; ++i)
304 std::streamoff endPos = os.tellp();
307 char * pch = os.str();
308 bool ok = t.SerializeToArray(pch + beginPos, static_cast<int>(endPos - beginPos));
311 Exception e(RcfError_ProtobufWrite,
typeid(t).name());
316 std::uint32_t len =
static_cast<std::uint32_t
>(endPos - beginPos);
317 char buffer[4] = {0};
318 memcpy(buffer, &len, 4);
319 RCF::machineToNetworkOrder(buffer, 4, 1);
321 os.rdbuf()->pubseekoff(beginPos-4, std::ios::beg, std::ios::out);
323 os.rdbuf()->pubseekoff(endPos, std::ios::beg, std::ios::out);
327 void serializeProtobufOrNot(
328 SerializationProtocolOut &out,
332 serializeProtobufOrNot(out, *pt, (RCF::TrueType *) NULL);
336 void serializeProtobufOrNot(
337 SerializationProtocolOut &out,
341 serializeProtobufOrNot(out, *pt, (RCF::TrueType *) NULL);
345 void deserializeProtobufOrNot(
346 SerializationProtocolIn &in,
350 MemIstream & is = in.getIstream();
354 std::uint32_t len = 0;
355 memcpy( &len, buffer, 4);
356 RCF::networkToMachineOrder(&len, 4, 1);
358 ByteBuffer byteBuffer;
359 in.extractSlice(byteBuffer, len);
360 bool ok = t.ParseFromArray(byteBuffer.getPtr(),
static_cast<int>(byteBuffer.getLength()));
363 Exception e(RcfError_ProtobufRead,
typeid(t).name());
369 void deserializeProtobufOrNot(
370 SerializationProtocolIn &in,
379 deserializeProtobufOrNot(in, *pt, (RCF::TrueType *) NULL);
384 SerializationProtocolOut &out,
387 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
388 serializeProtobufOrNot(out, pt, (type *) NULL);
393 SerializationProtocolOut &out,
396 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
397 serializeProtobufOrNot(out, pt, (type *) NULL);
402 SerializationProtocolOut &out,
405 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
406 serializeProtobufOrNot(out, t, (type *) NULL);
411 SerializationProtocolIn &in,
414 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
415 deserializeProtobufOrNot(in, pt, (type *) NULL);
420 SerializationProtocolIn &in,
423 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
424 deserializeProtobufOrNot(in, t, (type *) NULL);
436 SerializationProtocolOut &out,
439 serializeImpl(out, t, 0);
444 SerializationProtocolIn &in,
447 deserializeImpl(in, t, 0);
454 #endif // ! INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP Base class for all RCF exceptions.
Definition: Exception.hpp:64
RCF_EXPORT std::uint32_t getRuntimeVersion()
Gets the RCF runtime version number.
Definition: AmiIoHandler.hpp:24