Connection.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. #ifndef __CONNECTION_H__
  2. #define __CONNECTION_H__
  3. #include "../global.h"
  4. #include <string>
  5. #include <vector>
  6. #include <set>
  7. #include <list>
  8. #include <typeinfo> //XXX this is in namespace std if you want w/o use typeinfo.h?
  9. #include <assert.h>
  10. #include <boost/type_traits/is_fundamental.hpp>
  11. #include <boost/type_traits/is_enum.hpp>
  12. #include <boost/type_traits/is_pointer.hpp>
  13. #include <boost/type_traits/is_class.hpp>
  14. #include <boost/type_traits/is_base_of.hpp>
  15. #include <boost/type_traits/is_array.hpp>
  16. #include <boost/type_traits/remove_pointer.hpp>
  17. #include <boost/type_traits/remove_const.hpp>
  18. #include <boost/mpl/eval_if.hpp>
  19. #include <boost/mpl/equal_to.hpp>
  20. #include <boost/mpl/int.hpp>
  21. #include <boost/mpl/identity.hpp>
  22. #include <boost/any.hpp>
  23. const ui32 version = 726;
  24. class CConnection;
  25. class CGObjectInstance;
  26. class CGameState;
  27. class CCreature;
  28. class LibClasses;
  29. class CHero;
  30. extern DLL_EXPORT LibClasses * VLC;
  31. namespace mpl = boost::mpl;
  32. /*
  33. * Connection.h, part of VCMI engine
  34. *
  35. * Authors: listed in file AUTHORS in main folder
  36. *
  37. * License: GNU General Public License v2.0 or later
  38. * Full text of license available in license.txt file, in main folder
  39. *
  40. */
  41. namespace boost
  42. {
  43. namespace asio
  44. {
  45. namespace ip
  46. {
  47. class tcp;
  48. }
  49. class io_service;
  50. template <typename Protocol> class stream_socket_service;
  51. template <typename Protocol,typename StreamSocketService>
  52. class basic_stream_socket;
  53. template <typename Protocol> class socket_acceptor_service;
  54. template <typename Protocol,typename SocketAcceptorService>
  55. class basic_socket_acceptor;
  56. }
  57. class mutex;
  58. };
  59. enum SerializationLvl
  60. {
  61. Wrong=0,
  62. Primitive,
  63. Array,
  64. Pointer,
  65. Serializable
  66. };
  67. struct TypeComparer
  68. {
  69. bool operator()(const std::type_info *a, const std::type_info *b) const
  70. {
  71. return a->before(*b);
  72. }
  73. };
  74. class DLL_EXPORT CTypeList
  75. {
  76. typedef std::multimap<const std::type_info *,ui16,TypeComparer> TTypeMap;
  77. TTypeMap types;
  78. public:
  79. CTypeList();
  80. ui16 registerType(const std::type_info *type);
  81. template <typename T> ui16 registerType(const T * t = NULL)
  82. {
  83. return registerType(getTypeInfo(t));
  84. }
  85. ui16 getTypeID(const std::type_info *type);
  86. template <typename T> ui16 getTypeID(const T * t)
  87. {
  88. return getTypeID(getTypeInfo(t));
  89. }
  90. template <typename T> const std::type_info * getTypeInfo(const T * t = NULL)
  91. {
  92. if(t)
  93. return &typeid(*t);
  94. else
  95. return &typeid(T);
  96. }
  97. };
  98. extern DLL_EXPORT CTypeList typeList;
  99. template<typename Ser,typename T>
  100. struct SavePrimitive
  101. {
  102. static void invoke(Ser &s, const T &data)
  103. {
  104. s.savePrimitive(data);
  105. }
  106. };
  107. template<typename Ser,typename T>
  108. struct SaveSerializable
  109. {
  110. static void invoke(Ser &s, const T &data)
  111. {
  112. s.saveSerializable(data);
  113. }
  114. };
  115. template<typename Ser,typename T>
  116. struct LoadPrimitive
  117. {
  118. static void invoke(Ser &s, T &data)
  119. {
  120. s.loadPrimitive(data);
  121. }
  122. };
  123. template<typename Ser,typename T>
  124. struct SavePointer
  125. {
  126. static void invoke(Ser &s, const T &data)
  127. {
  128. s.savePointer(data);
  129. }
  130. };
  131. template<typename Ser,typename T>
  132. struct LoadPointer
  133. {
  134. static void invoke(Ser &s, T &data)
  135. {
  136. s.loadPointer(data);
  137. }
  138. };
  139. template<typename Ser,typename T>
  140. struct SaveArray
  141. {
  142. static void invoke(Ser &s, const T &data)
  143. {
  144. s.saveArray(data);
  145. }
  146. };
  147. template<typename Ser,typename T>
  148. struct LoadArray
  149. {
  150. static void invoke(Ser &s, T &data)
  151. {
  152. s.loadArray(data);
  153. }
  154. };
  155. template<typename Ser,typename T>
  156. struct LoadSerializable
  157. {
  158. static void invoke(Ser &s, T &data)
  159. {
  160. s.loadSerializable(data);
  161. }
  162. };
  163. template<typename Ser,typename T>
  164. struct SaveWrong
  165. {
  166. static void invoke(Ser &s, const T &data)
  167. {
  168. throw std::string("Wrong save serialization call!");
  169. }
  170. };
  171. template<typename Ser,typename T>
  172. struct LoadWrong
  173. {
  174. static void invoke(Ser &s, const T &data)
  175. {
  176. throw std::string("Wrong load serialization call!");
  177. }
  178. };
  179. template<typename T>
  180. struct SerializationLevel
  181. {
  182. typedef mpl::integral_c_tag tag;
  183. typedef
  184. typename mpl::eval_if<
  185. boost::is_fundamental<T>,
  186. mpl::int_<Primitive>,
  187. //else
  188. typename mpl::eval_if<
  189. boost::is_class<T>,
  190. mpl::int_<Serializable>,
  191. //else
  192. typename mpl::eval_if<
  193. boost::is_array<T>,
  194. mpl::int_<Array>,
  195. //else
  196. typename mpl::eval_if<
  197. boost::is_pointer<T>,
  198. mpl::int_<Pointer>,
  199. //else
  200. typename mpl::eval_if<
  201. boost::is_enum<T>,
  202. mpl::int_<Primitive>,
  203. //else
  204. mpl::int_<Wrong>
  205. >
  206. >
  207. >
  208. >
  209. >::type type;
  210. static const int value = SerializationLevel::type::value;
  211. };
  212. template <typename T>
  213. struct VectorisedObjectInfo
  214. {
  215. const std::vector<T*> *vector; //pointer to the appropriate vector
  216. const si32 T::*idPtr; //pointer to the field representing the position in the vector
  217. VectorisedObjectInfo(const std::vector<T*> *Vector, const si32 T::*IdPtr)
  218. :vector(Vector), idPtr(IdPtr)
  219. {
  220. }
  221. };
  222. class DLL_EXPORT CSerializer
  223. {
  224. public:
  225. typedef std::map<const std::type_info *, boost::any, TypeComparer> TTypeVecMap;
  226. TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
  227. bool smartVectorMembersSerialization;
  228. CSerializer();
  229. ~CSerializer();
  230. virtual void reportState(CLogger &out){};
  231. template <typename T>
  232. void registerVectoredType(const std::vector<T*> *Vector, const si32 T::*IdPtr)
  233. {
  234. vectors[&typeid(T)] = VectorisedObjectInfo<T>(Vector, IdPtr);
  235. }
  236. template <typename T>
  237. const VectorisedObjectInfo<T> *getVectorisedTypeInfo()
  238. {
  239. const std::type_info *myType = NULL;
  240. //
  241. // if(boost::is_base_of<CGObjectInstance, T>::value) //ugly workaround to support also types derived from CGObjectInstance -> if we encounter one, treat it aas CGObj..
  242. // myType = &typeid(CGObjectInstance);
  243. // else
  244. myType = &typeid(T);
  245. TTypeVecMap::iterator i = vectors.find(myType);
  246. if(i == vectors.end())
  247. return NULL;
  248. else
  249. {
  250. assert(!i->second.empty());
  251. assert(i->second.type() == typeid(VectorisedObjectInfo<T>));
  252. VectorisedObjectInfo<T> *ret = &(boost::any_cast<VectorisedObjectInfo<T>&>(i->second));
  253. return ret;
  254. }
  255. }
  256. template <typename T>
  257. T* getVectorItemFromId(const VectorisedObjectInfo<T> &oInfo, si32 id) const
  258. {
  259. if(id < 0)
  260. return NULL;
  261. assert(oInfo.vector);
  262. assert(oInfo.vector->size() > id);
  263. return (*oInfo.vector)[id];
  264. }
  265. template <typename T>
  266. si32 getIdFromVectorItem(const VectorisedObjectInfo<T> &oInfo, const T* obj) const
  267. {
  268. if(!obj)
  269. return -1;
  270. return obj->*oInfo.idPtr;
  271. }
  272. void addStdVecItems(CGameState *gs, LibClasses *lib = VLC);
  273. };
  274. class DLL_EXPORT CSaverBase : public virtual CSerializer
  275. {
  276. };
  277. class CBasicPointerSaver
  278. {
  279. public:
  280. virtual void savePtr(CSaverBase &ar, const void *data) const =0;
  281. ~CBasicPointerSaver(){}
  282. };
  283. template <typename Serializer, typename T> class CPointerSaver : public CBasicPointerSaver
  284. {
  285. public:
  286. void savePtr(CSaverBase &ar, const void *data) const
  287. {
  288. Serializer &s = static_cast<Serializer&>(ar);
  289. const T *ptr = static_cast<const T*>(data);
  290. //T is most derived known type, it's time to call actual serialize
  291. const_cast<T&>(*ptr).serialize(s,version);
  292. }
  293. };
  294. template <typename T> //metafunction returning CGObjectInstance if T is its derivate or T elsewise
  295. struct VectorisedTypeFor
  296. {
  297. typedef typename
  298. //if
  299. mpl::eval_if<boost::is_base_of<CGObjectInstance,T>,
  300. mpl::identity<CGObjectInstance>,
  301. //else
  302. mpl::identity<T>
  303. >::type type;
  304. };
  305. template <typename Serializer> class DLL_EXPORT COSer : public CSaverBase
  306. {
  307. public:
  308. bool saving;
  309. std::map<ui16,CBasicPointerSaver*> savers; // typeID => CPointerSaver<serializer,type>
  310. std::map<const void*, ui32> savedPointers;
  311. bool smartPointerSerialization;
  312. COSer()
  313. {
  314. saving=true;
  315. smartPointerSerialization = true;
  316. }
  317. ~COSer()
  318. {
  319. std::map<ui16,CBasicPointerSaver*>::iterator iter;
  320. for(iter = savers.begin(); iter != savers.end(); iter++)
  321. delete iter->second;
  322. }
  323. template<typename T> void registerType(const T * t=NULL)
  324. {
  325. ui16 ID = typeList.registerType(t);
  326. savers[ID] = new CPointerSaver<COSer<Serializer>,T>;
  327. }
  328. Serializer * This()
  329. {
  330. return static_cast<Serializer*>(this);
  331. }
  332. template<class T>
  333. Serializer & operator<<(const T &t)
  334. {
  335. this->This()->save(t);
  336. return * this->This();
  337. }
  338. template<class T>
  339. COSer & operator&(const T & t)
  340. {
  341. return * this->This() << t;
  342. }
  343. int write(const void * data, unsigned size);
  344. template <typename T>
  345. void savePrimitive(const T &data)
  346. {
  347. this->This()->write(&data,sizeof(data));
  348. }
  349. template <typename T>
  350. void savePointer(const T &data)
  351. {
  352. //write if pointer is not NULL
  353. ui8 hlp = (data!=NULL);
  354. *this << hlp;
  355. //if pointer is NULL then we don't need anything more...
  356. if(!hlp)
  357. return;
  358. if(smartVectorMembersSerialization)
  359. {
  360. typedef typename boost::remove_const<typename boost::remove_pointer<T>::type>::type TObjectType;
  361. typedef typename VectorisedTypeFor<TObjectType>::type VType;
  362. if(const VectorisedObjectInfo<VType> *info = getVectorisedTypeInfo<VType>())
  363. {
  364. *this << getIdFromVectorItem<VType>(*info, data);
  365. return;
  366. }
  367. }
  368. if(smartPointerSerialization)
  369. {
  370. std::map<const void*,ui32>::iterator i = savedPointers.find(data);
  371. if(i != savedPointers.end())
  372. {
  373. //this pointer has been already serialized - write only it's id
  374. *this << i->second;
  375. return;
  376. }
  377. //give id to this pointer
  378. ui32 pid = (ui32)savedPointers.size();
  379. savedPointers[data] = pid;
  380. *this << pid;
  381. }
  382. //write type identifier
  383. ui16 tid = typeList.getTypeID(data);
  384. *this << tid;
  385. This()->savePointerHlp(tid, data);
  386. }
  387. //that part of ptr serialization was extracted to allow customization of its behavior in derived classes
  388. template <typename T>
  389. void savePointerHlp(ui16 tid, const T &data)
  390. {
  391. if(!tid)
  392. *this << *data; //if type is unregistered simply write all data in a standard way
  393. else
  394. savers[tid]->savePtr(*this,data); //call serializer specific for our real type
  395. }
  396. template <typename T>
  397. void saveArray(const T &data)
  398. {
  399. ui32 size = ARRAY_COUNT(data);
  400. for(ui32 i=0; i < size; i++)
  401. *this << data[i];
  402. }
  403. template <typename T>
  404. void save(const T &data)
  405. {
  406. typedef
  407. //if
  408. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  409. mpl::identity<SavePrimitive<Serializer,T> >,
  410. //else if
  411. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  412. mpl::identity<SavePointer<Serializer,T> >,
  413. //else if
  414. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Array> >,
  415. mpl::identity<SaveArray<Serializer,T> >,
  416. //else if
  417. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  418. mpl::identity<SaveSerializable<Serializer,T> >,
  419. //else
  420. mpl::identity<SaveWrong<Serializer,T> >
  421. >
  422. >
  423. >
  424. >::type typex;
  425. typex::invoke(* this->This(), data);
  426. }
  427. template <typename T>
  428. void saveSerializable(const T &data)
  429. {
  430. const_cast<T&>(data).serialize(*this,version);
  431. }
  432. template <typename T>
  433. void saveSerializable(const std::vector<T> &data)
  434. {
  435. boost::uint32_t length = data.size();
  436. *this << length;
  437. for(ui32 i=0;i<length;i++)
  438. *this << data[i];
  439. }
  440. template <typename T>
  441. void saveSerializable(const std::set<T> &data)
  442. {
  443. std::set<T> &d = const_cast<std::set<T> &>(data);
  444. boost::uint32_t length = d.size();
  445. *this << length;
  446. for(typename std::set<T>::iterator i=d.begin();i!=d.end();i++)
  447. *this << *i;
  448. }
  449. template <typename T>
  450. void saveSerializable(const std::list<T> &data)
  451. {
  452. std::list<T> &d = const_cast<std::list<T> &>(data);
  453. boost::uint32_t length = d.size();
  454. *this << length;
  455. for(typename std::list<T>::iterator i=d.begin();i!=d.end();i++)
  456. *this << *i;
  457. }
  458. void saveSerializable(const std::string &data)
  459. {
  460. *this << ui32(data.length());
  461. this->This()->write(data.c_str(),data.size());
  462. }
  463. template <typename T1, typename T2>
  464. void saveSerializable(const std::pair<T1,T2> &data)
  465. {
  466. *this << data.first << data.second;
  467. }
  468. template <typename T1, typename T2>
  469. void saveSerializable(const std::map<T1,T2> &data)
  470. {
  471. *this << ui32(data.size());
  472. for(typename std::map<T1,T2>::const_iterator i=data.begin();i!=data.end();i++)
  473. *this << i->first << i->second;
  474. }
  475. };
  476. class DLL_EXPORT CLoaderBase : public virtual CSerializer
  477. {};
  478. class CBasicPointerLoader
  479. {
  480. public:
  481. virtual void loadPtr(CLoaderBase &ar, void *data, ui32 pid) const =0; //data is pointer to the ACTUAL POINTER
  482. virtual ~CBasicPointerLoader(){}
  483. };
  484. template <typename Serializer, typename T> class CPointerLoader : public CBasicPointerLoader
  485. {
  486. public:
  487. void loadPtr(CLoaderBase &ar, void *data, ui32 pid) const //data is pointer to the ACTUAL POINTER
  488. {
  489. Serializer &s = static_cast<Serializer&>(ar);
  490. T *&ptr = *static_cast<T**>(data);
  491. //create new object under pointer
  492. typedef typename boost::remove_pointer<T>::type npT;
  493. ptr = new npT;
  494. s.ptrAllocated(ptr, pid);
  495. //T is most derived known type, it's time to call actual serialize
  496. ptr->serialize(s,version);
  497. }
  498. };
  499. template <typename Serializer> class DLL_EXPORT CISer : public CLoaderBase
  500. {
  501. public:
  502. bool saving;
  503. std::map<ui16,CBasicPointerLoader*> loaders; // typeID => CPointerSaver<serializer,type>
  504. ui32 myVersion;
  505. std::map<ui32, void*> loadedPointers;
  506. bool smartPointerSerialization;
  507. CISer()
  508. {
  509. saving = false;
  510. myVersion = version;
  511. smartPointerSerialization = true;
  512. }
  513. ~CISer()
  514. {
  515. std::map<ui16,CBasicPointerLoader*>::iterator iter;
  516. for(iter = loaders.begin(); iter != loaders.end(); iter++)
  517. delete iter->second;
  518. }
  519. template<typename T> void registerType(const T * t=NULL)
  520. {
  521. ui16 ID = typeList.registerType(t);
  522. loaders[ID] = new CPointerLoader<CISer<Serializer>,T>;
  523. }
  524. Serializer * This()
  525. {
  526. return static_cast<Serializer*>(this);
  527. }
  528. template<class T>
  529. Serializer & operator>>(T &t)
  530. {
  531. this->This()->load(t);
  532. return * this->This();
  533. }
  534. template<class T>
  535. CISer & operator&(T & t)
  536. {
  537. return * this->This() >> t;
  538. }
  539. int write(const void * data, unsigned size);
  540. template <typename T>
  541. void load(T &data)
  542. {
  543. typedef
  544. //if
  545. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  546. mpl::identity<LoadPrimitive<Serializer,T> >,
  547. //else if
  548. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  549. mpl::identity<LoadPointer<Serializer,T> >,
  550. //else if
  551. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Array> >,
  552. mpl::identity<LoadArray<Serializer,T> >,
  553. //else if
  554. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  555. mpl::identity<LoadSerializable<Serializer,T> >,
  556. //else
  557. mpl::identity<LoadWrong<Serializer,T> >
  558. >
  559. >
  560. >
  561. >::type typex;
  562. typex::invoke(* this->This(), data);
  563. }
  564. template <typename T>
  565. void loadPrimitive(T &data)
  566. {
  567. this->This()->read(&data,sizeof(data));
  568. }
  569. template <typename T>
  570. void loadSerializable(T &data)
  571. {
  572. ////that const cast is evil because it allows to implicitly overwrite const objects when deserializing
  573. typedef typename boost::remove_const<T>::type nonConstT;
  574. nonConstT &hlp = const_cast<nonConstT&>(data);
  575. hlp.serialize(*this,myVersion);
  576. //data.serialize(*this,myVersion);
  577. }
  578. template <typename T>
  579. void loadArray(T &data)
  580. {
  581. ui32 size = ARRAY_COUNT(data);
  582. for(ui32 i=0; i < size; i++)
  583. *this >> data[i];
  584. }
  585. template <typename T>
  586. void loadPointer(T &data)
  587. {
  588. ui8 hlp;
  589. *this >> hlp;
  590. if(!hlp)
  591. {
  592. data = NULL;
  593. return;
  594. }
  595. if(smartVectorMembersSerialization)
  596. {
  597. typedef typename boost::remove_const<typename boost::remove_pointer<T>::type>::type TObjectType; //eg: const CGHeroInstance * => CGHeroInstance
  598. typedef typename VectorisedTypeFor<TObjectType>::type VType; //eg: CGHeroInstance -> CGobjectInstance
  599. if(const VectorisedObjectInfo<VType> *info = getVectorisedTypeInfo<VType>())
  600. {
  601. si32 id;
  602. *this >> id;
  603. data = static_cast<T>(getVectorItemFromId(*info, id));
  604. return;
  605. }
  606. }
  607. ui32 pid = -1; //pointer id (or maybe rather pointee id)
  608. if(smartPointerSerialization)
  609. {
  610. *this >> pid; //get the id
  611. std::map<ui32, void*>::iterator i = loadedPointers.find(pid); //lookup
  612. if(i != loadedPointers.end())
  613. {
  614. //we already got this pointer
  615. data = static_cast<T>(i->second);
  616. return;
  617. }
  618. }
  619. //get type id
  620. ui16 tid;
  621. *this >> tid;
  622. This()->loadPointerHlp(tid, data, pid);
  623. }
  624. //that part of ptr deserialization was extracted to allow customization of its behavior in derived classes
  625. template <typename T>
  626. void loadPointerHlp( ui16 tid, T & data, ui32 pid )
  627. {
  628. if(!tid)
  629. {
  630. typedef typename boost::remove_pointer<T>::type npT;
  631. typedef typename boost::remove_const<npT>::type ncpT;
  632. data = new ncpT;
  633. ptrAllocated(data, pid);
  634. *this >> *data;
  635. }
  636. else
  637. {
  638. loaders[tid]->loadPtr(*this,&data, pid);
  639. }
  640. }
  641. template <typename T>
  642. void ptrAllocated(const T *ptr, ui32 pid)
  643. {
  644. if(smartPointerSerialization && pid != -1)
  645. loadedPointers[pid] = (void*)ptr; //add loaded pointer to our lookup map; cast is to avoid errors with const T* pt
  646. }
  647. #define READ_CHECK_U32(x) \
  648. boost::uint32_t length; \
  649. *this >> length; \
  650. if(length > 50000) \
  651. { \
  652. tlog2 << "Warning: very big length: " << length << "\n" ;\
  653. reportState(tlog2); \
  654. };
  655. template <typename T>
  656. void loadSerializable(std::vector<T> &data)
  657. {
  658. READ_CHECK_U32(length);
  659. data.resize(length);
  660. for(ui32 i=0;i<length;i++)
  661. *this >> data[i];
  662. }
  663. template <typename T>
  664. void loadSerializable(std::set<T> &data)
  665. {
  666. READ_CHECK_U32(length);
  667. T ins;
  668. for(ui32 i=0;i<length;i++)
  669. {
  670. *this >> ins;
  671. data.insert(ins);
  672. }
  673. }
  674. template <typename T>
  675. void loadSerializable(std::list<T> &data)
  676. {
  677. READ_CHECK_U32(length);
  678. T ins;
  679. for(ui32 i=0;i<length;i++)
  680. {
  681. *this >> ins;
  682. data.push_back(ins);
  683. }
  684. }
  685. template <typename T1, typename T2>
  686. void loadSerializable(std::pair<T1,T2> &data)
  687. {
  688. *this >> data.first >> data.second;
  689. }
  690. template <typename T1, typename T2>
  691. void loadSerializable(std::map<T1,T2> &data)
  692. {
  693. READ_CHECK_U32(length);
  694. T1 t;
  695. for(ui32 i=0;i<length;i++)
  696. {
  697. *this >> t;
  698. *this >> data[t];
  699. }
  700. }
  701. void loadSerializable(std::string &data)
  702. {
  703. READ_CHECK_U32(length);
  704. data.resize(length);
  705. this->This()->read((void*)data.c_str(),length);
  706. }
  707. };
  708. class DLL_EXPORT CSaveFile
  709. : public COSer<CSaveFile>
  710. {
  711. void dummyMagicFunction()
  712. {
  713. *this << std::string("This function makes stuff working.");
  714. }
  715. public:
  716. std::string fName;
  717. std::ofstream *sfile;
  718. CSaveFile(const std::string &fname);
  719. ~CSaveFile();
  720. int write(const void * data, unsigned size);
  721. void close();
  722. void openNextFile(const std::string &fname);
  723. void reportState(CLogger &out);
  724. };
  725. class DLL_EXPORT CLoadFile
  726. : public CISer<CLoadFile>
  727. {
  728. void dummyMagicFunction()
  729. {
  730. std::string dummy = "This function makes stuff working.";
  731. *this >> dummy;
  732. }
  733. public:
  734. std::string fName;
  735. std::ifstream *sfile;
  736. CLoadFile(const std::string &fname, bool requireLatest = true);
  737. ~CLoadFile();
  738. int read(const void * data, unsigned size);
  739. void close();
  740. void openNextFile(const std::string &fname, bool requireLatest);
  741. void reportState(CLogger &out);
  742. };
  743. typedef boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > TSocket;
  744. typedef boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > TAcceptor;
  745. class DLL_EXPORT CConnection
  746. :public CISer<CConnection>, public COSer<CConnection>
  747. {
  748. //CGameState *gs;
  749. CConnection(void);
  750. void init();
  751. void reportState(CLogger &out);
  752. public:
  753. boost::mutex *rmx, *wmx; // read/write mutexes
  754. TSocket * socket;
  755. bool logging;
  756. bool connected;
  757. bool myEndianess, contactEndianess; //true if little endian, if ednianess is different we'll have to revert recieved multi-byte vars
  758. boost::asio::io_service *io_service;
  759. std::string name; //who uses this connection
  760. CConnection(std::string host, std::string port, std::string Name);
  761. CConnection(TAcceptor * acceptor, boost::asio::io_service *Io_service, std::string Name);
  762. CConnection(TSocket * Socket, std::string Name); //use immediately after accepting connection into socket
  763. int write(const void * data, unsigned size);
  764. int read(void * data, unsigned size);
  765. void close();
  766. bool isOpen() const;
  767. template<class T>
  768. CConnection &operator&(const T&);
  769. ~CConnection(void);
  770. };
  771. #endif // __CONNECTION_H__