Connection.h 23 KB

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