19 #ifndef INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP
20 #define INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP
25 #include <boost/mpl/bool_fwd.hpp>
27 #include <RCF/ByteBuffer.hpp>
28 #include <RCF/ByteOrdering.hpp>
29 #include <RCF/MemStream.hpp>
31 #if RCF_FEATURE_PROTOBUF==1
32 #include <boost/type_traits/is_base_and_derived.hpp>
33 #include <RCF/GoogleProtobufs.hpp>
38 #include <RCF/Config.hpp>
39 #include <RCF/SerializationProtocol_Base.hpp>
42 #include <RCF/SerializationProtocol_SF.hpp>
45 #if RCF_FEATURE_BOOST_SERIALIZATION==1
46 #include <RCF/SerializationProtocol_BS.hpp>
51 RCF_EXPORT
bool isSerializationProtocolSupported(
int protocol);
53 RCF_EXPORT std::string getSerializationProtocolName(
int protocol);
56 class MethodInvocationRequest;
57 class MethodInvocationResponse;
59 class RCF_EXPORT SerializationProtocolIn
62 SerializationProtocolIn();
63 ~SerializationProtocolIn();
65 MemIstream& getIstream() {
return mIs; }
67 void setSerializationProtocol(
int protocol);
68 int getSerializationProtocol()
const;
71 const ByteBuffer &data,
75 bool enableSfPointerTracking);
77 void clearByteBuffer();
79 void extractSlice(ByteBuffer &byteBuffer, std::size_t len);
80 std::size_t getArchiveLength();
81 std::size_t getRemainingArchiveLength();
84 void read(
const T *pt)
96 case 1: mInProtocol1 >> t;
break;
97 case 2: mInProtocol2 >> t;
break;
98 case 3: mInProtocol3 >> t;
break;
99 case 4: mInProtocol4 >> t;
break;
100 case 5: mInProtocol5 >> t;
break;
102 default: RCF_ASSERT(0)(mProtocol);
105 catch(
const RCF::Exception &e)
107 RCF_UNUSED_VARIABLE(e);
110 catch(
const std::exception &e)
112 RCF::SerializationException se( _RcfError_Deserialization(
121 int getRuntimeVersion();
126 void unbindProtocol();
128 friend class ClientStub;
129 friend class RcfSession;
132 ByteBuffer mByteBuffer;
135 Protocol< boost::mpl::int_<1> >::In mInProtocol1;
136 Protocol< boost::mpl::int_<2> >::In mInProtocol2;
137 Protocol< boost::mpl::int_<3> >::In mInProtocol3;
138 Protocol< boost::mpl::int_<4> >::In mInProtocol4;
139 Protocol< boost::mpl::int_<5> >::In mInProtocol5;
145 class RCF_EXPORT SerializationProtocolOut
148 SerializationProtocolOut();
150 MemOstream& getMemOstream() {
return *mOsPtr; }
152 void setSerializationProtocol(
int protocol);
153 int getSerializationProtocol()
const;
158 ByteBuffer byteBuffer,
161 bool enableSfPointerTracking);
164 void write(
const T &t)
170 case 1: mOutProtocol1 << t;
break;
171 case 2: mOutProtocol2 << t;
break;
172 case 3: mOutProtocol3 << t;
break;
173 case 4: mOutProtocol4 << t;
break;
174 case 5: mOutProtocol5 << t;
break;
176 default: RCF_ASSERT(0)(mProtocol);
179 catch(
const std::exception &e)
181 RCF::SerializationException se( _RcfError_Serialization(
190 void insert(
const ByteBuffer &byteBuffer);
191 void extractByteBuffers();
192 void extractByteBuffers(std::vector<ByteBuffer> &byteBuffers);
194 int getRuntimeVersion();
199 void unbindProtocol();
202 friend class ClientStub;
203 friend class RcfSession;
207 boost::shared_ptr<MemOstream> mOsPtr;
208 std::vector<std::pair<std::size_t, ByteBuffer> > mByteBuffers;
211 Protocol< boost::mpl::int_<1> >::Out mOutProtocol1;
212 Protocol< boost::mpl::int_<2> >::Out mOutProtocol2;
213 Protocol< boost::mpl::int_<3> >::Out mOutProtocol3;
214 Protocol< boost::mpl::int_<4> >::Out mOutProtocol4;
215 Protocol< boost::mpl::int_<5> >::Out mOutProtocol5;
221 inline void serialize(
222 SerializationProtocolOut &,
227 inline void serialize(
228 SerializationProtocolOut &,
233 inline void deserialize(
234 SerializationProtocolIn &,
239 inline void deserialize(
240 SerializationProtocolIn &,
247 SerializationProtocolOut &out,
255 void deserializeImpl(
256 SerializationProtocolIn &in,
263 #if RCF_FEATURE_PROTOBUF==1
269 void serializeProtobufOrNot(
270 SerializationProtocolOut &out,
274 serializeImpl(out, t, 0);
278 void deserializeProtobufOrNot(
279 SerializationProtocolIn &in,
283 deserializeImpl(in, t, 0);
287 void serializeProtobufOrNot(
288 SerializationProtocolOut &out,
292 MemOstream & os = out.getMemOstream();
294 std::streamoff beginPos = os.tellp();
296 if (!t.IsInitialized())
298 RCF_THROW(Exception(_RcfError_ProtobufWriteInit(
typeid(t).name())));
303 int byteSize = t.ByteSize();
304 RCF_ASSERT_GTEQ(byteSize , 0);
305 for (
int i=0; i<byteSize; ++i)
309 std::streamoff endPos = os.tellp();
312 char * pch = os.str();
313 bool ok = t.SerializeToArray(pch + beginPos, static_cast<int>(endPos - beginPos));
314 RCF_VERIFY(ok, Exception(_RcfError_ProtobufWrite(
typeid(t).name())))(
typeid(t));
317 boost::uint32_t len =
static_cast<boost::uint32_t
>(endPos - beginPos);
318 char buffer[4] = {0};
319 memcpy(buffer, &len, 4);
320 RCF::machineToNetworkOrder(buffer, 4, 1);
322 os.rdbuf()->pubseekoff(beginPos-4, std::ios::beg, std::ios::out);
324 os.rdbuf()->pubseekoff(endPos, std::ios::beg, std::ios::out);
328 void serializeProtobufOrNot(
329 SerializationProtocolOut &out,
333 serializeProtobufOrNot(out, *pt, (RCF::TrueType *) NULL);
337 void serializeProtobufOrNot(
338 SerializationProtocolOut &out,
342 serializeProtobufOrNot(out, *pt, (RCF::TrueType *) NULL);
346 void deserializeProtobufOrNot(
347 SerializationProtocolIn &in,
351 MemIstream & is = in.getIstream();
355 boost::uint32_t len = 0;
356 memcpy( &len, buffer, 4);
357 RCF::networkToMachineOrder(&len, 4, 1);
359 ByteBuffer byteBuffer;
360 in.extractSlice(byteBuffer, len);
361 bool ok = t.ParseFromArray(byteBuffer.getPtr(),
static_cast<int>(byteBuffer.getLength()));
362 RCF_VERIFY(ok, Exception(_RcfError_ProtobufRead(
typeid(t).name())))(
typeid(t));
366 void deserializeProtobufOrNot(
367 SerializationProtocolIn &in,
376 deserializeProtobufOrNot(in, *pt, (RCF::TrueType *) NULL);
381 SerializationProtocolOut &out,
384 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
385 serializeProtobufOrNot(out, pt, (type *) NULL);
390 SerializationProtocolOut &out,
393 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
394 serializeProtobufOrNot(out, pt, (type *) NULL);
399 SerializationProtocolOut &out,
402 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
403 serializeProtobufOrNot(out, t, (type *) NULL);
408 SerializationProtocolIn &in,
411 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
412 deserializeProtobufOrNot(in, pt, (type *) NULL);
417 SerializationProtocolIn &in,
420 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
421 deserializeProtobufOrNot(in, t, (type *) NULL);
433 SerializationProtocolOut &out,
436 serializeImpl(out, t, 0);
441 SerializationProtocolIn &in,
444 deserializeImpl(in, t, 0);
451 #endif // ! INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP