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>
41 #if defined(RCF_USE_SF_SERIALIZATION)
42 #include <RCF/SerializationProtocol_SF.hpp>
45 #if defined(RCF_USE_BOOST_SERIALIZATION) || defined(RCF_USE_BOOST_XML_SERIALIZATION)
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 std::istream& 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;
101 #ifdef RCF_USE_BOOST_XML_SERIALIZATION
102 case 5: mInProtocol5 >> boost::serialization::make_nvp(
"Dummy", t);
break;
104 case 5: mInProtocol5 >> t;
break;
107 default: RCF_ASSERT(0)(mProtocol);
110 catch(
const RCF::Exception &e)
112 RCF_UNUSED_VARIABLE(e);
115 catch(
const std::exception &e)
117 RCF::SerializationException se( _RcfError_Deserialization(
126 int getRuntimeVersion();
131 void unbindProtocol();
133 friend class ClientStub;
134 friend class RcfSession;
137 ByteBuffer mByteBuffer;
140 Protocol< boost::mpl::int_<1> >::In mInProtocol1;
141 Protocol< boost::mpl::int_<2> >::In mInProtocol2;
142 Protocol< boost::mpl::int_<3> >::In mInProtocol3;
143 Protocol< boost::mpl::int_<4> >::In mInProtocol4;
144 Protocol< boost::mpl::int_<5> >::In mInProtocol5;
150 class RCF_EXPORT SerializationProtocolOut
153 SerializationProtocolOut();
155 MemOstream& getMemOstream() {
return *mOsPtr; }
157 void setSerializationProtocol(
int protocol);
158 int getSerializationProtocol()
const;
163 ByteBuffer byteBuffer,
166 bool enableSfPointerTracking);
169 void write(
const T &t)
175 case 1: mOutProtocol1 << t;
break;
176 case 2: mOutProtocol2 << t;
break;
177 case 3: mOutProtocol3 << t;
break;
178 case 4: mOutProtocol4 << t;
break;
180 #ifdef RCF_USE_BOOST_XML_SERIALIZATION
181 case 5: mOutProtocol5 << boost::serialization::make_nvp(
"Dummy", t);
break;
183 case 5: mOutProtocol5 << t;
break;
186 default: RCF_ASSERT(0)(mProtocol);
189 catch(
const std::exception &e)
191 RCF::SerializationException se( _RcfError_Serialization(
200 void insert(
const ByteBuffer &byteBuffer);
201 void extractByteBuffers();
202 void extractByteBuffers(std::vector<ByteBuffer> &byteBuffers);
204 int getRuntimeVersion();
209 void unbindProtocol();
212 friend class ClientStub;
213 friend class RcfSession;
217 boost::shared_ptr<MemOstream> mOsPtr;
218 std::vector<std::pair<std::size_t, ByteBuffer> > mByteBuffers;
221 Protocol< boost::mpl::int_<1> >::Out mOutProtocol1;
222 Protocol< boost::mpl::int_<2> >::Out mOutProtocol2;
223 Protocol< boost::mpl::int_<3> >::Out mOutProtocol3;
224 Protocol< boost::mpl::int_<4> >::Out mOutProtocol4;
225 Protocol< boost::mpl::int_<5> >::Out mOutProtocol5;
231 inline void serialize(
232 SerializationProtocolOut &,
237 inline void serialize(
238 SerializationProtocolOut &,
243 inline void deserialize(
244 SerializationProtocolIn &,
249 inline void deserialize(
250 SerializationProtocolIn &,
257 SerializationProtocolOut &out,
265 void deserializeImpl(
266 SerializationProtocolIn &in,
273 #if RCF_FEATURE_PROTOBUF==1
279 void serializeProtobufOrNot(
280 SerializationProtocolOut &out,
282 boost::mpl::false_ *)
284 serializeImpl(out, t, 0);
288 void deserializeProtobufOrNot(
289 SerializationProtocolIn &in,
291 boost::mpl::false_ *)
293 deserializeImpl(in, t, 0);
297 void serializeProtobufOrNot(
298 SerializationProtocolOut &out,
302 MemOstream & os = out.getMemOstream();
304 std::streamoff beginPos = os.tellp();
306 if (!t.IsInitialized())
308 RCF_THROW(Exception(_RcfError_ProtobufWriteInit(
typeid(t).name())));
313 int byteSize = t.ByteSize();
314 RCF_ASSERT_GTEQ(byteSize , 0);
315 for (
int i=0; i<byteSize; ++i)
319 std::streamoff endPos = os.tellp();
322 char * pch = os.str();
323 bool ok = t.SerializeToArray(pch + beginPos, static_cast<int>(endPos - beginPos));
324 RCF_VERIFY(ok, Exception(_RcfError_ProtobufWrite(
typeid(t).name())))(
typeid(t));
327 boost::uint32_t len =
static_cast<boost::uint32_t
>(endPos - beginPos);
328 char buffer[4] = {0};
329 memcpy(buffer, &len, 4);
330 RCF::machineToNetworkOrder(buffer, 4, 1);
332 os.rdbuf()->pubseekoff(beginPos-4, std::ios::beg, std::ios::out);
334 os.rdbuf()->pubseekoff(endPos, std::ios::beg, std::ios::out);
338 void serializeProtobufOrNot(
339 SerializationProtocolOut &out,
343 serializeProtobufOrNot(out, *pt, (boost::mpl::true_ *) NULL);
347 void serializeProtobufOrNot(
348 SerializationProtocolOut &out,
352 serializeProtobufOrNot(out, *pt, (boost::mpl::true_ *) NULL);
356 void deserializeProtobufOrNot(
357 SerializationProtocolIn &in,
361 std::istream & is = in.getIstream();
365 boost::uint32_t len = 0;
366 memcpy( &len, buffer, 4);
367 RCF::networkToMachineOrder(&len, 4, 1);
369 ByteBuffer byteBuffer;
370 in.extractSlice(byteBuffer, len);
371 bool ok = t.ParseFromArray(byteBuffer.getPtr(), byteBuffer.getLength());
372 RCF_VERIFY(ok, Exception(_RcfError_ProtobufRead(
typeid(t).name())))(
typeid(t));
376 void deserializeProtobufOrNot(
377 SerializationProtocolIn &in,
386 deserializeProtobufOrNot(in, *pt, (boost::mpl::true_ *) NULL);
391 SerializationProtocolOut &out,
394 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
395 serializeProtobufOrNot(out, pt, (type *) NULL);
400 SerializationProtocolOut &out,
403 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
404 serializeProtobufOrNot(out, pt, (type *) NULL);
409 SerializationProtocolOut &out,
412 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
413 serializeProtobufOrNot(out, t, (type *) NULL);
418 SerializationProtocolIn &in,
421 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
422 deserializeProtobufOrNot(in, pt, (type *) NULL);
427 SerializationProtocolIn &in,
430 typedef typename boost::is_base_and_derived<google::protobuf::Message, T>::type type;
431 deserializeProtobufOrNot(in, t, (type *) NULL);
443 SerializationProtocolOut &out,
446 serializeImpl(out, t, 0);
451 SerializationProtocolIn &in,
454 deserializeImpl(in, t, 0);
461 #endif // ! INCLUDE_RCF_SERIALIZATIONPROTOCOL_HPP