18 #ifndef INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP 19 #define INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP 23 #include <type_traits> 25 #include <RCF/ByteBuffer.hpp> 26 #include <RCF/MemStream.hpp> 28 #if RCF_FEATURE_PROTOBUF==1 29 #include <RCF/GoogleProtobufs.hpp> 34 #include <RCF/Config.hpp> 37 #include <RCF/SerializationProtocol_SF.hpp> 40 #if RCF_FEATURE_BOOST_SERIALIZATION==1 41 #include <RCF/SerializationProtocol_BS.hpp> 46 RCF_EXPORT
bool isSerializationProtocolSupported(
int protocol);
48 RCF_EXPORT std::string getSerializationProtocolName(
int protocol);
50 class MethodInvocationRequest;
51 class MethodInvocationResponse;
53 class RCF_EXPORT SerializationProtocolIn
56 SerializationProtocolIn();
57 ~SerializationProtocolIn();
59 MemIstream& getIstream() {
return mIs; }
61 void setSerializationProtocol(
int protocol);
62 int getSerializationProtocol()
const;
65 const ByteBuffer &data,
69 bool enableSfPointerTracking);
71 void clearByteBuffer();
73 void extractSlice(ByteBuffer &byteBuffer, std::size_t len);
74 std::size_t getArchiveLength();
75 std::size_t getRemainingArchiveLength();
78 void read(
const T *pt)
90 case 1: mInProtocol1 >> t;
break;
91 case 2: mInProtocol2 >> t;
break;
92 case 3: mInProtocol3 >> t;
break;
93 case 4: mInProtocol4 >> t;
break;
94 case 5: mInProtocol5 >> t;
break;
96 default: RCF_ASSERT_ALWAYS(
"");
101 RCF_UNUSED_VARIABLE(e);
104 catch(
const std::exception &e)
120 void unbindProtocol();
122 friend class ClientStub;
123 friend class RcfSession;
126 ByteBuffer mByteBuffer;
129 Protocol< Int<1> >::In mInProtocol1;
130 Protocol< Int<2> >::In mInProtocol2;
131 Protocol< Int<3> >::In mInProtocol3;
132 Protocol< Int<4> >::In mInProtocol4;
133 Protocol< Int<5> >::In mInProtocol5;
139 class RCF_EXPORT SerializationProtocolOut
142 SerializationProtocolOut();
144 MemOstream& getMemOstream() {
return *mOsPtr; }
146 void setSerializationProtocol(
int protocol);
147 int getSerializationProtocol()
const;
152 ByteBuffer byteBuffer,
155 bool enableSfPointerTracking);
158 void write(
const T &t)
164 case 1: mOutProtocol1 << t;
break;
165 case 2: mOutProtocol2 << t;
break;
166 case 3: mOutProtocol3 << t;
break;
167 case 4: mOutProtocol4 << t;
break;
168 case 5: mOutProtocol5 << t;
break;
170 default: RCF_ASSERT_ALWAYS(
"");
173 catch(
const std::exception &e)
184 void insert(
const ByteBuffer &byteBuffer);
185 void extractByteBuffers();
186 void extractByteBuffers(std::vector<ByteBuffer> &byteBuffers);
193 void unbindProtocol();
196 friend class ClientStub;
197 friend class RcfSession;
201 std::shared_ptr<MemOstream> mOsPtr;
202 std::vector<std::pair<std::size_t, ByteBuffer> > mByteBuffers;
205 Protocol< Int<1> >::Out mOutProtocol1;
206 Protocol< Int<2> >::Out mOutProtocol2;
207 Protocol< Int<3> >::Out mOutProtocol3;
208 Protocol< Int<4> >::Out mOutProtocol4;
209 Protocol< Int<5> >::Out mOutProtocol5;
215 inline void serialize(
216 SerializationProtocolOut &,
221 inline void serialize(
222 SerializationProtocolOut &,
227 inline void deserialize(
228 SerializationProtocolIn &,
233 inline void deserialize(
234 SerializationProtocolIn &,
241 SerializationProtocolOut &out,
249 void deserializeImpl(
250 SerializationProtocolIn &in,
257 #if RCF_FEATURE_PROTOBUF==1 263 void serializeProtobufOrNot(
264 SerializationProtocolOut &out,
268 serializeImpl(out, t, 0);
272 void deserializeProtobufOrNot(
273 SerializationProtocolIn &in,
277 deserializeImpl(in, t, 0);
281 void serializeProtobufOrNot(
282 SerializationProtocolOut &out,
286 MemOstream & os = out.getMemOstream();
288 std::streamoff beginPos = os.tellp();
290 if (!t.IsInitialized())
292 RCF_THROW(Exception(RcfError_ProtobufWriteInit,
typeid(t).name()));
297 std::size_t byteSize = t.ByteSizeLong();
298 for (std::size_t i=0; i<byteSize; ++i)
302 std::streamoff endPos = os.tellp();
305 char * pch = os.str();
306 bool ok = t.SerializeToArray(pch + beginPos, static_cast<int>(endPos - beginPos));
309 Exception e(RcfError_ProtobufWrite,
typeid(t).name());
314 std::uint32_t len =
static_cast<std::uint32_t
>(endPos - beginPos);
315 char buffer[4] = {0};
316 memcpy(buffer, &len, 4);
317 RCF::machineToNetworkOrder(buffer, 4, 1);
319 os.rdbuf()->pubseekoff(beginPos-4, std::ios::beg, std::ios::out);
321 os.rdbuf()->pubseekoff(endPos, std::ios::beg, std::ios::out);
325 void serializeProtobufOrNot(
326 SerializationProtocolOut &out,
330 serializeProtobufOrNot(out, *pt, (RCF::TrueType *) NULL);
334 void serializeProtobufOrNot(
335 SerializationProtocolOut &out,
339 serializeProtobufOrNot(out, *pt, (RCF::TrueType *) NULL);
343 void deserializeProtobufOrNot(
344 SerializationProtocolIn &in,
348 MemIstream & is = in.getIstream();
352 std::uint32_t len = 0;
353 memcpy( &len, buffer, 4);
354 RCF::networkToMachineOrder(&len, 4, 1);
356 ByteBuffer byteBuffer;
357 in.extractSlice(byteBuffer, len);
358 bool ok = t.ParseFromArray(byteBuffer.getPtr(),
static_cast<int>(byteBuffer.getLength()));
361 Exception e(RcfError_ProtobufRead,
typeid(t).name());
367 void deserializeProtobufOrNot(
368 SerializationProtocolIn &in,
377 deserializeProtobufOrNot(in, *pt, (RCF::TrueType *) NULL);
382 SerializationProtocolOut &out,
385 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
386 serializeProtobufOrNot(out, pt, (type *) NULL);
391 SerializationProtocolOut &out,
394 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
395 serializeProtobufOrNot(out, pt, (type *) NULL);
400 SerializationProtocolOut &out,
403 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
404 serializeProtobufOrNot(out, t, (type *) NULL);
409 SerializationProtocolIn &in,
412 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
413 deserializeProtobufOrNot(in, pt, (type *) NULL);
418 SerializationProtocolIn &in,
421 typedef typename std::is_base_of<google::protobuf::Message, T>::type type;
422 deserializeProtobufOrNot(in, t, (type *) NULL);
434 SerializationProtocolOut &out,
437 serializeImpl(out, t, 0);
442 SerializationProtocolIn &in,
445 deserializeImpl(in, t, 0);
452 #endif // ! INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP Base class for all RCF exceptions.
Definition: Exception.hpp:67
RCF_EXPORT std::uint32_t getRuntimeVersion()
Gets the RCF runtime version number.
Definition: AmiIoHandler.hpp:23