中间件底层,websocket

Stream.hpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //******************************************************************************
  2. // RCF - Remote Call Framework
  3. //
  4. // Copyright (c) 2005 - 2020, Delta V Software. All rights reserved.
  5. // http://www.deltavsoft.com
  6. //
  7. // RCF is distributed under dual licenses - closed source or GPL.
  8. // Consult your particular license for conditions of use.
  9. //
  10. // If you have not purchased a commercial license, you are using RCF
  11. // under GPL terms.
  12. //
  13. // Version: 3.2
  14. // Contact: support <at> deltavsoft.com
  15. //
  16. //******************************************************************************
  17. #ifndef INCLUDE_SF_STREAM_HPP
  18. #define INCLUDE_SF_STREAM_HPP
  19. #include <map>
  20. #include <string>
  21. #include <RCF/Export.hpp>
  22. #include <SF/DataPtr.hpp>
  23. #include <SF/Encoding.hpp>
  24. #include <SF/I_Stream.hpp>
  25. #include <iosfwd>
  26. namespace RCF {
  27. class SerializationProtocolIn;
  28. class SerializationProtocolOut;
  29. class MemIstream;
  30. class MemOstream;
  31. }
  32. namespace SF {
  33. //**************************************************
  34. // Encoding of object data
  35. template<typename E>
  36. class Encoding : public I_Encoding
  37. {
  38. public:
  39. virtual UInt32 getCount(
  40. DataPtr & data,
  41. const std::type_info & type)
  42. {
  43. return countElements( (E *) 0, data, type);
  44. }
  45. virtual void toData(
  46. DataPtr & data,
  47. void * pvObject,
  48. const std::type_info & type,
  49. int nCount)
  50. {
  51. encodeElements( (E *) 0, data, pvObject, type, nCount);
  52. }
  53. virtual void toObject(
  54. DataPtr & data,
  55. void * pvObject,
  56. const std::type_info & type,
  57. int nCount)
  58. {
  59. decodeElements( (E *) 0, data, pvObject, type, nCount);
  60. }
  61. };
  62. //**************************************************
  63. // Context handling
  64. class RCF_EXPORT ContextRead
  65. {
  66. public:
  67. ContextRead();
  68. ~ContextRead();
  69. void add(SF::UInt32 nid, const ObjectId &id);
  70. void add(void *ptr, const std::type_info &objType, void *pObj);
  71. bool query(SF::UInt32 nid, ObjectId &id);
  72. bool query(void *ptr, const std::type_info &objType, void *&pObj);
  73. void clear();
  74. void setEnabled(bool enabled);
  75. bool getEnabled() const;
  76. private:
  77. bool mEnabled;
  78. std::unique_ptr<std::map<UInt32, ObjectId> > mNidToIdMap;
  79. std::unique_ptr<std::map<std::string, std::map< void *, void * > > > mTypeToObjMap;
  80. };
  81. class RCF_EXPORT ContextWrite
  82. {
  83. public:
  84. ContextWrite();
  85. ~ContextWrite();
  86. void setEnabled(bool enabled);
  87. bool getEnabled() const;
  88. void add(const ObjectId &id, UInt32 &nid);
  89. bool query(const ObjectId &id, UInt32 &nid);
  90. void clear();
  91. private:
  92. bool mEnabled;
  93. UInt32 mCurrentId;
  94. std::unique_ptr<std::map<ObjectId, UInt32> > mIdToNidMap;
  95. };
  96. //**************************************************
  97. // Stream local storage
  98. class RCF_EXPORT LocalStorage : Noncopyable
  99. {
  100. public:
  101. LocalStorage();
  102. ~LocalStorage();
  103. void setNode(Node *);
  104. Node *getNode();
  105. private:
  106. Node * mpNode;
  107. };
  108. //****************************************************
  109. // Base stream classes
  110. class Node;
  111. class SerializerBase;
  112. /// Base class for input streams using SF serialization. Use operator >>() to deserialize objects from the stream.
  113. class RCF_EXPORT IStream : Noncopyable
  114. {
  115. public:
  116. IStream();
  117. IStream(
  118. RCF::MemIstream & is,
  119. std::size_t archiveSize = 0,
  120. int runtimeVersion = 0,
  121. int archiveVersion = 0);
  122. /// Constructs an IStream from a std::istream. Serialized data will be read from the std::istream.
  123. IStream(
  124. std::istream & is,
  125. std::size_t archiveSize = 0,
  126. int runtimeVersion = 0,
  127. int archiveVersion = 0);
  128. virtual ~IStream();
  129. void setIs(
  130. std::istream & is,
  131. std::size_t archiveSize = 0,
  132. int runtimeVersion = 0,
  133. int archiveVersion = 0);
  134. void clearState();
  135. UInt32 read(Byte8 *pBytes, UInt32 nLength);
  136. bool verifyAgainstArchiveSize(std::size_t bytesToRead);
  137. bool begin(Node &node);
  138. bool get(DataPtr &value);
  139. void end();
  140. UInt32 read_int(UInt32 &n);
  141. UInt32 read_byte(Byte8 &byte);
  142. void putback_byte(Byte8 byte);
  143. std::size_t tell() const;
  144. void seek(std::size_t newPos);
  145. virtual I_Encoding &
  146. getEncoding() = 0;
  147. /// Gets the RCF runtime version associated with this stream.
  148. int getRuntimeVersion();
  149. /// Gets the archive version associated with this stream.
  150. int getArchiveVersion();
  151. /// Sets the archive version associated with this stream.
  152. void setArchiveVersion(int archiveVersion);
  153. /// Sets the RCF runtime version associated with this stream.
  154. void setRuntimeVersion(int runtimeVersion);
  155. void ignoreVersionStamp(bool ignore = true);
  156. void setRemoteCallContext(
  157. RCF::SerializationProtocolIn * pSerializationProtocolIn);
  158. RCF::SerializationProtocolIn *
  159. getRemoteCallContext();
  160. ContextRead & getTrackingContext();
  161. const ContextRead & getTrackingContext() const;
  162. LocalStorage & getLocalStorage();
  163. void setEnablePointerTracking(bool enable);
  164. bool getEnablePointerTracking() const;
  165. // Streaming operators.
  166. /// Deserialize an object from the stream.
  167. template<typename T>
  168. IStream & operator>>(T &t);
  169. /// Deserialize an object from the stream.
  170. template<typename T>
  171. IStream & operator>>(const T &t);
  172. private:
  173. ContextRead mContextRead;
  174. LocalStorage mLocalStorage;
  175. std::istream * mpIs;
  176. std::size_t mArchiveSize;
  177. int mRuntimeVersion;
  178. int mArchiveVersion;
  179. bool mIgnoreVersionStamp;
  180. RCF::SerializationProtocolIn * mpSerializationProtocolIn;
  181. };
  182. /// Base class for output streams using SF serialization. Use operator <<() to serialize objects into the stream.
  183. class RCF_EXPORT OStream : Noncopyable
  184. {
  185. public:
  186. OStream();
  187. OStream(
  188. RCF::MemOstream & os,
  189. int runtimeVersion = 0,
  190. int archiveVersion = 0);
  191. /// Constructs an OStream from a std::ostream. Serialized data will be written to the std::ostream.
  192. OStream(
  193. std::ostream & os,
  194. int runtimeVersion = 0,
  195. int archiveVersion = 0);
  196. virtual ~OStream();
  197. void setOs(
  198. std::ostream & os,
  199. int runtimeVersion = 0,
  200. int archiveVersion = 0);
  201. void clearState();
  202. UInt32 writeRaw(const Byte8 *pBytes, UInt32 nLength);
  203. void begin(const Node &node);
  204. void put(const DataPtr &value);
  205. void end();
  206. UInt32 write_int(UInt32 n);
  207. UInt32 write_byte(Byte8 byte);
  208. UInt32 write(const Byte8 *pBytes, UInt32 nLength);
  209. virtual I_Encoding &
  210. getEncoding() = 0;
  211. /// Gets the RCF runtime version associated with this stream.
  212. int getRuntimeVersion();
  213. /// Gets the archive version associated with this stream.
  214. int getArchiveVersion();
  215. /// Sets the archive version associated with this stream.
  216. void setArchiveVersion(int archiveVersion);
  217. /// Sets the RCF runtime version associated with this stream.
  218. void setRuntimeVersion(int runtimeVersion);
  219. void suppressArchiveMetadata(bool suppress = true);
  220. void setRemoteCallContext(
  221. RCF::SerializationProtocolOut * pSerializationProtocolOut);
  222. RCF::SerializationProtocolOut *
  223. getRemoteCallContext();
  224. ContextWrite & getTrackingContext();
  225. const ContextWrite & getTrackingContext() const;
  226. LocalStorage & getLocalStorage();
  227. void setEnablePointerTracking(bool enable);
  228. bool getEnablePointerTracking() const;
  229. // Streaming operator.
  230. /// Serialize an object to the stream.
  231. template<typename T>
  232. OStream & operator<<(const T &t);
  233. private:
  234. void writeArchiveMetadata();
  235. ContextWrite mContextWrite;
  236. LocalStorage mLocalStorage;
  237. std::ostream * mpOs;
  238. int mRuntimeVersion;
  239. int mArchiveVersion;
  240. bool mSuppressArchiveMetadata;
  241. bool mArchiveMetadataWritten;
  242. RCF::SerializationProtocolOut * mpSerializationProtocolOut;
  243. };
  244. } // namespace SF
  245. #include <SF/Archive.hpp>
  246. #include <SF/Serializer.hpp>
  247. namespace SF {
  248. template<typename T>
  249. IStream & IStream::operator>>(T &t)
  250. {
  251. Archive ar(Archive::READ, this);
  252. ar & t;
  253. return *this;
  254. }
  255. template<typename T>
  256. IStream & IStream::operator>>(const T &t)
  257. {
  258. Archive ar(Archive::READ, this);
  259. ar & t;
  260. return *this;
  261. }
  262. template<typename T>
  263. OStream & OStream::operator<<(const T &t)
  264. {
  265. Archive ar(Archive::WRITE, this);
  266. ar & t;
  267. return *this;
  268. }
  269. } // namespace SF
  270. #endif // !INCLUDE_SF_STREAM_HPP