Connection.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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 = 727;
  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)
  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 DLL_EXPORT CSerializer
  226. {
  227. public:
  228. typedef std::map<const std::type_info *, boost::any, TypeComparer> TTypeVecMap;
  229. TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
  230. bool smartVectorMembersSerialization;
  231. CSerializer();
  232. ~CSerializer();
  233. virtual void reportState(CLogger &out){};
  234. template <typename T>
  235. void registerVectoredType(const std::vector<T*> *Vector, const si32 T::*IdPtr)
  236. {
  237. vectors[&typeid(T)] = VectorisedObjectInfo<T>(Vector, IdPtr);
  238. }
  239. template <typename T>
  240. void registerVectoredType(const std::vector<ConstTransitivePtr<T> > *Vector, const si32 T::*IdPtr)
  241. {
  242. vectors[&typeid(T)] = VectorisedObjectInfo<T>(Vector, IdPtr);
  243. }
  244. template <typename T>
  245. const VectorisedObjectInfo<T> *getVectorisedTypeInfo()
  246. {
  247. const std::type_info *myType = NULL;
  248. //
  249. // 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..
  250. // myType = &typeid(CGObjectInstance);
  251. // else
  252. myType = &typeid(T);
  253. TTypeVecMap::iterator i = vectors.find(myType);
  254. if(i == vectors.end())
  255. return NULL;
  256. else
  257. {
  258. assert(!i->second.empty());
  259. assert(i->second.type() == typeid(VectorisedObjectInfo<T>));
  260. VectorisedObjectInfo<T> *ret = &(boost::any_cast<VectorisedObjectInfo<T>&>(i->second));
  261. return ret;
  262. }
  263. }
  264. template <typename T>
  265. T* getVectorItemFromId(const VectorisedObjectInfo<T> &oInfo, ui32 id) const
  266. {
  267. /* if(id < 0)
  268. return NULL;*/
  269. assert(oInfo.vector);
  270. assert(oInfo.vector->size() > id);
  271. return const_cast<T*>((*oInfo.vector)[id].get());
  272. }
  273. template <typename T>
  274. si32 getIdFromVectorItem(const VectorisedObjectInfo<T> &oInfo, const T* obj) const
  275. {
  276. if(!obj)
  277. return -1;
  278. return obj->*oInfo.idPtr;
  279. }
  280. void addStdVecItems(CGameState *gs, LibClasses *lib = VLC);
  281. };
  282. class DLL_EXPORT CSaverBase : public virtual CSerializer
  283. {
  284. };
  285. class CBasicPointerSaver
  286. {
  287. public:
  288. virtual void savePtr(CSaverBase &ar, const void *data) const =0;
  289. ~CBasicPointerSaver(){}
  290. };
  291. template <typename Serializer, typename T> class CPointerSaver : public CBasicPointerSaver
  292. {
  293. public:
  294. void savePtr(CSaverBase &ar, const void *data) const
  295. {
  296. Serializer &s = static_cast<Serializer&>(ar);
  297. const T *ptr = static_cast<const T*>(data);
  298. //T is most derived known type, it's time to call actual serialize
  299. const_cast<T&>(*ptr).serialize(s,version);
  300. }
  301. };
  302. template <typename T> //metafunction returning CGObjectInstance if T is its derivate or T elsewise
  303. struct VectorisedTypeFor
  304. {
  305. typedef typename
  306. //if
  307. mpl::eval_if<boost::is_base_of<CGObjectInstance,T>,
  308. mpl::identity<CGObjectInstance>,
  309. //else
  310. mpl::identity<T>
  311. >::type type;
  312. };
  313. template <typename Serializer> class DLL_EXPORT COSer : public CSaverBase
  314. {
  315. public:
  316. bool saving;
  317. std::map<ui16,CBasicPointerSaver*> savers; // typeID => CPointerSaver<serializer,type>
  318. std::map<const void*, ui32> savedPointers;
  319. bool smartPointerSerialization;
  320. COSer()
  321. {
  322. saving=true;
  323. smartPointerSerialization = true;
  324. }
  325. ~COSer()
  326. {
  327. std::map<ui16,CBasicPointerSaver*>::iterator iter;
  328. for(iter = savers.begin(); iter != savers.end(); iter++)
  329. delete iter->second;
  330. }
  331. template<typename T> void registerType(const T * t=NULL)
  332. {
  333. ui16 ID = typeList.registerType(t);
  334. savers[ID] = new CPointerSaver<COSer<Serializer>,T>;
  335. }
  336. Serializer * This()
  337. {
  338. return static_cast<Serializer*>(this);
  339. }
  340. template<class T>
  341. Serializer & operator<<(const T &t)
  342. {
  343. this->This()->save(t);
  344. return * this->This();
  345. }
  346. template<class T>
  347. COSer & operator&(const T & t)
  348. {
  349. return * this->This() << t;
  350. }
  351. int write(const void * data, unsigned size);
  352. template <typename T>
  353. void savePrimitive(const T &data)
  354. {
  355. this->This()->write(&data,sizeof(data));
  356. }
  357. template <typename T>
  358. void savePointer(const T &data)
  359. {
  360. //write if pointer is not NULL
  361. ui8 hlp = (data!=NULL);
  362. *this << hlp;
  363. //if pointer is NULL then we don't need anything more...
  364. if(!hlp)
  365. return;
  366. if(smartVectorMembersSerialization)
  367. {
  368. typedef typename boost::remove_const<typename boost::remove_pointer<T>::type>::type TObjectType;
  369. typedef typename VectorisedTypeFor<TObjectType>::type VType;
  370. if(const VectorisedObjectInfo<VType> *info = getVectorisedTypeInfo<VType>())
  371. {
  372. si32 id = getIdFromVectorItem<VType>(*info, data);
  373. *this << id;
  374. if(id != -1) //vector id is enough
  375. return;
  376. }
  377. }
  378. if(smartPointerSerialization)
  379. {
  380. std::map<const void*,ui32>::iterator i = savedPointers.find(data);
  381. if(i != savedPointers.end())
  382. {
  383. //this pointer has been already serialized - write only it's id
  384. *this << i->second;
  385. return;
  386. }
  387. //give id to this pointer
  388. ui32 pid = (ui32)savedPointers.size();
  389. savedPointers[data] = pid;
  390. *this << pid;
  391. }
  392. //write type identifier
  393. ui16 tid = typeList.getTypeID(data);
  394. *this << tid;
  395. This()->savePointerHlp(tid, data);
  396. }
  397. //that part of ptr serialization was extracted to allow customization of its behavior in derived classes
  398. template <typename T>
  399. void savePointerHlp(ui16 tid, const T &data)
  400. {
  401. if(!tid)
  402. *this << *data; //if type is unregistered simply write all data in a standard way
  403. else
  404. savers[tid]->savePtr(*this,data); //call serializer specific for our real type
  405. }
  406. template <typename T>
  407. void saveArray(const T &data)
  408. {
  409. ui32 size = ARRAY_COUNT(data);
  410. for(ui32 i=0; i < size; i++)
  411. *this << data[i];
  412. }
  413. template <typename T>
  414. void save(const T &data)
  415. {
  416. typedef
  417. //if
  418. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  419. mpl::identity<SavePrimitive<Serializer,T> >,
  420. //else if
  421. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  422. mpl::identity<SavePointer<Serializer,T> >,
  423. //else if
  424. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Array> >,
  425. mpl::identity<SaveArray<Serializer,T> >,
  426. //else if
  427. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  428. mpl::identity<SaveSerializable<Serializer,T> >,
  429. //else
  430. mpl::identity<SaveWrong<Serializer,T> >
  431. >
  432. >
  433. >
  434. >::type typex;
  435. typex::invoke(* this->This(), data);
  436. }
  437. template <typename T>
  438. void saveSerializable(const T &data)
  439. {
  440. const_cast<T&>(data).serialize(*this,version);
  441. }
  442. template <typename T>
  443. void saveSerializable(const boost::shared_ptr<T> &data)
  444. {
  445. T *internalPtr = data.get();
  446. *this << internalPtr;
  447. }
  448. template <typename T>
  449. void saveSerializable(const std::vector<T> &data)
  450. {
  451. boost::uint32_t length = data.size();
  452. *this << length;
  453. for(ui32 i=0;i<length;i++)
  454. *this << data[i];
  455. }
  456. template <typename T>
  457. void saveSerializable(const std::set<T> &data)
  458. {
  459. std::set<T> &d = const_cast<std::set<T> &>(data);
  460. boost::uint32_t length = d.size();
  461. *this << length;
  462. for(typename std::set<T>::iterator i=d.begin();i!=d.end();i++)
  463. *this << *i;
  464. }
  465. template <typename T, typename U>
  466. void saveSerializable(const boost::unordered_set<T, U> &data)
  467. {
  468. boost::unordered_set<T, U> &d = const_cast<boost::unordered_set<T, U> &>(data);
  469. boost::uint32_t length = d.size();
  470. *this << length;
  471. for(typename boost::unordered_set<T, U>::iterator i=d.begin();i!=d.end();i++)
  472. *this << *i;
  473. }
  474. template <typename T>
  475. void saveSerializable(const std::list<T> &data)
  476. {
  477. std::list<T> &d = const_cast<std::list<T> &>(data);
  478. boost::uint32_t length = d.size();
  479. *this << length;
  480. for(typename std::list<T>::iterator i=d.begin();i!=d.end();i++)
  481. *this << *i;
  482. }
  483. void saveSerializable(const std::string &data)
  484. {
  485. *this << ui32(data.length());
  486. this->This()->write(data.c_str(),data.size());
  487. }
  488. template <typename T1, typename T2>
  489. void saveSerializable(const std::pair<T1,T2> &data)
  490. {
  491. *this << data.first << data.second;
  492. }
  493. template <typename T1, typename T2>
  494. void saveSerializable(const std::map<T1,T2> &data)
  495. {
  496. *this << ui32(data.size());
  497. for(typename std::map<T1,T2>::const_iterator i=data.begin();i!=data.end();i++)
  498. *this << i->first << i->second;
  499. }
  500. };
  501. class DLL_EXPORT CLoaderBase : public virtual CSerializer
  502. {};
  503. class CBasicPointerLoader
  504. {
  505. public:
  506. virtual void loadPtr(CLoaderBase &ar, void *data, ui32 pid) const =0; //data is pointer to the ACTUAL POINTER
  507. virtual ~CBasicPointerLoader(){}
  508. };
  509. template <typename Serializer, typename T> class CPointerLoader : public CBasicPointerLoader
  510. {
  511. public:
  512. void loadPtr(CLoaderBase &ar, void *data, ui32 pid) const //data is pointer to the ACTUAL POINTER
  513. {
  514. Serializer &s = static_cast<Serializer&>(ar);
  515. T *&ptr = *static_cast<T**>(data);
  516. //create new object under pointer
  517. typedef typename boost::remove_pointer<T>::type npT;
  518. ptr = new npT;
  519. s.ptrAllocated(ptr, pid);
  520. //T is most derived known type, it's time to call actual serialize
  521. ptr->serialize(s,version);
  522. }
  523. };
  524. template <typename Serializer> class DLL_EXPORT CISer : public CLoaderBase
  525. {
  526. public:
  527. bool saving;
  528. std::map<ui16,CBasicPointerLoader*> loaders; // typeID => CPointerSaver<serializer,type>
  529. ui32 myVersion;
  530. std::map<ui32, void*> loadedPointers;
  531. bool smartPointerSerialization;
  532. CISer()
  533. {
  534. saving = false;
  535. myVersion = version;
  536. smartPointerSerialization = true;
  537. }
  538. ~CISer()
  539. {
  540. std::map<ui16,CBasicPointerLoader*>::iterator iter;
  541. for(iter = loaders.begin(); iter != loaders.end(); iter++)
  542. delete iter->second;
  543. }
  544. template<typename T> void registerType(const T * t=NULL)
  545. {
  546. ui16 ID = typeList.registerType(t);
  547. loaders[ID] = new CPointerLoader<CISer<Serializer>,T>;
  548. }
  549. Serializer * This()
  550. {
  551. return static_cast<Serializer*>(this);
  552. }
  553. template<class T>
  554. Serializer & operator>>(T &t)
  555. {
  556. this->This()->load(t);
  557. return * this->This();
  558. }
  559. template<class T>
  560. CISer & operator&(T & t)
  561. {
  562. return * this->This() >> t;
  563. }
  564. int write(const void * data, unsigned size);
  565. template <typename T>
  566. void load(T &data)
  567. {
  568. typedef
  569. //if
  570. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  571. mpl::identity<LoadPrimitive<Serializer,T> >,
  572. //else if
  573. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  574. mpl::identity<LoadPointer<Serializer,T> >,
  575. //else if
  576. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Array> >,
  577. mpl::identity<LoadArray<Serializer,T> >,
  578. //else if
  579. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  580. mpl::identity<LoadSerializable<Serializer,T> >,
  581. //else
  582. mpl::identity<LoadWrong<Serializer,T> >
  583. >
  584. >
  585. >
  586. >::type typex;
  587. typex::invoke(* this->This(), data);
  588. }
  589. template <typename T>
  590. void loadPrimitive(T &data)
  591. {
  592. this->This()->read(&data,sizeof(data));
  593. }
  594. template <typename T>
  595. void loadSerializable(T &data)
  596. {
  597. ////that const cast is evil because it allows to implicitly overwrite const objects when deserializing
  598. typedef typename boost::remove_const<T>::type nonConstT;
  599. nonConstT &hlp = const_cast<nonConstT&>(data);
  600. hlp.serialize(*this,myVersion);
  601. //data.serialize(*this,myVersion);
  602. }
  603. template <typename T>
  604. void loadArray(T &data)
  605. {
  606. ui32 size = ARRAY_COUNT(data);
  607. for(ui32 i=0; i < size; i++)
  608. *this >> data[i];
  609. }
  610. template <typename T>
  611. void loadPointer(T &data)
  612. {
  613. ui8 hlp;
  614. *this >> hlp;
  615. if(!hlp)
  616. {
  617. data = NULL;
  618. return;
  619. }
  620. if(smartVectorMembersSerialization)
  621. {
  622. typedef typename boost::remove_const<typename boost::remove_pointer<T>::type>::type TObjectType; //eg: const CGHeroInstance * => CGHeroInstance
  623. typedef typename VectorisedTypeFor<TObjectType>::type VType; //eg: CGHeroInstance -> CGobjectInstance
  624. if(const VectorisedObjectInfo<VType> *info = getVectorisedTypeInfo<VType>())
  625. {
  626. si32 id;
  627. *this >> id;
  628. if(id != -1)
  629. {
  630. data = static_cast<T>(getVectorItemFromId(*info, id));
  631. return;
  632. }
  633. }
  634. }
  635. ui32 pid = 0xffffffff; //pointer id (or maybe rather pointee id)
  636. if(smartPointerSerialization)
  637. {
  638. *this >> pid; //get the id
  639. std::map<ui32, void*>::iterator i = loadedPointers.find(pid); //lookup
  640. if(i != loadedPointers.end())
  641. {
  642. //we already got this pointer
  643. data = static_cast<T>(i->second);
  644. return;
  645. }
  646. }
  647. //get type id
  648. ui16 tid;
  649. *this >> tid;
  650. This()->loadPointerHlp(tid, data, pid);
  651. }
  652. //that part of ptr deserialization was extracted to allow customization of its behavior in derived classes
  653. template <typename T>
  654. void loadPointerHlp( ui16 tid, T & data, ui32 pid )
  655. {
  656. if(!tid)
  657. {
  658. typedef typename boost::remove_pointer<T>::type npT;
  659. typedef typename boost::remove_const<npT>::type ncpT;
  660. data = new ncpT;
  661. ptrAllocated(data, pid);
  662. *this >> *data;
  663. }
  664. else
  665. {
  666. loaders[tid]->loadPtr(*this,&data, pid);
  667. }
  668. }
  669. template <typename T>
  670. void ptrAllocated(const T *ptr, ui32 pid)
  671. {
  672. if(smartPointerSerialization && pid != 0xffffffff)
  673. loadedPointers[pid] = (void*)ptr; //add loaded pointer to our lookup map; cast is to avoid errors with const T* pt
  674. }
  675. #define READ_CHECK_U32(x) \
  676. boost::uint32_t length; \
  677. *this >> length; \
  678. if(length > 50000) \
  679. { \
  680. tlog2 << "Warning: very big length: " << length << "\n" ;\
  681. reportState(tlog2); \
  682. };
  683. template <typename T>
  684. void loadSerializable(boost::shared_ptr<T> &data)
  685. {
  686. T *internalPtr;
  687. *this >> internalPtr;
  688. data.reset(internalPtr);
  689. }
  690. template <typename T>
  691. void loadSerializable(std::vector<T> &data)
  692. {
  693. READ_CHECK_U32(length);
  694. data.resize(length);
  695. for(ui32 i=0;i<length;i++)
  696. *this >> data[i];
  697. }
  698. template <typename T>
  699. void loadSerializable(std::set<T> &data)
  700. {
  701. READ_CHECK_U32(length);
  702. T ins;
  703. for(ui32 i=0;i<length;i++)
  704. {
  705. *this >> ins;
  706. data.insert(ins);
  707. }
  708. }
  709. template <typename T, typename U>
  710. void loadSerializable(boost::unordered_set<T, U> &data)
  711. {
  712. READ_CHECK_U32(length);
  713. T ins;
  714. for(ui32 i=0;i<length;i++)
  715. {
  716. *this >> ins;
  717. data.insert(ins);
  718. }
  719. }
  720. template <typename T>
  721. void loadSerializable(std::list<T> &data)
  722. {
  723. READ_CHECK_U32(length);
  724. T ins;
  725. for(ui32 i=0;i<length;i++)
  726. {
  727. *this >> ins;
  728. data.push_back(ins);
  729. }
  730. }
  731. template <typename T1, typename T2>
  732. void loadSerializable(std::pair<T1,T2> &data)
  733. {
  734. *this >> data.first >> data.second;
  735. }
  736. template <typename T1, typename T2>
  737. void loadSerializable(std::map<T1,T2> &data)
  738. {
  739. READ_CHECK_U32(length);
  740. T1 t;
  741. for(ui32 i=0;i<length;i++)
  742. {
  743. *this >> t;
  744. *this >> data[t];
  745. }
  746. }
  747. void loadSerializable(std::string &data)
  748. {
  749. READ_CHECK_U32(length);
  750. data.resize(length);
  751. this->This()->read((void*)data.c_str(),length);
  752. }
  753. };
  754. class DLL_EXPORT CSaveFile
  755. : public COSer<CSaveFile>
  756. {
  757. void dummyMagicFunction()
  758. {
  759. *this << std::string("This function makes stuff working.");
  760. }
  761. public:
  762. std::string fName;
  763. std::ofstream *sfile;
  764. CSaveFile(const std::string &fname);
  765. ~CSaveFile();
  766. int write(const void * data, unsigned size);
  767. void close();
  768. void openNextFile(const std::string &fname);
  769. void reportState(CLogger &out);
  770. };
  771. class DLL_EXPORT CLoadFile
  772. : public CISer<CLoadFile>
  773. {
  774. void dummyMagicFunction()
  775. {
  776. std::string dummy = "This function makes stuff working.";
  777. *this >> dummy;
  778. }
  779. public:
  780. std::string fName;
  781. std::ifstream *sfile;
  782. CLoadFile(const std::string &fname, int minimalVersion = version);
  783. ~CLoadFile();
  784. int read(const void * data, unsigned size);
  785. void close();
  786. void openNextFile(const std::string &fname, int minimalVersion);
  787. void reportState(CLogger &out);
  788. };
  789. typedef boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > TSocket;
  790. typedef boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > TAcceptor;
  791. class DLL_EXPORT CConnection
  792. :public CISer<CConnection>, public COSer<CConnection>
  793. {
  794. //CGameState *gs;
  795. CConnection(void);
  796. void init();
  797. void reportState(CLogger &out);
  798. public:
  799. boost::mutex *rmx, *wmx; // read/write mutexes
  800. TSocket * socket;
  801. bool logging;
  802. bool connected;
  803. bool myEndianess, contactEndianess; //true if little endian, if ednianess is different we'll have to revert recieved multi-byte vars
  804. boost::asio::io_service *io_service;
  805. std::string name; //who uses this connection
  806. int connectionID;
  807. CConnection *c;
  808. boost::thread *handler;
  809. bool receivedStop, sendStop;
  810. CConnection(std::string host, std::string port, std::string Name);
  811. CConnection(TAcceptor * acceptor, boost::asio::io_service *Io_service, std::string Name);
  812. CConnection(TSocket * Socket, std::string Name); //use immediately after accepting connection into socket
  813. int write(const void * data, unsigned size);
  814. int read(void * data, unsigned size);
  815. void close();
  816. bool isOpen() const;
  817. template<class T>
  818. CConnection &operator&(const T&);
  819. ~CConnection(void);
  820. CPack *retreivePack(); //gets from server next pack (allocates it with new)
  821. };
  822. DLL_EXPORT std::ostream &operator<<(std::ostream &str, const CConnection &cpc);
  823. template<typename T>
  824. class CApplier
  825. {
  826. public:
  827. std::map<ui16,T*> apps;
  828. ~CApplier()
  829. {
  830. typename std::map<ui16, T*>::iterator iter;
  831. for(iter = apps.begin(); iter != apps.end(); iter++)
  832. delete iter->second;
  833. }
  834. template<typename U> void registerType(const U * t=NULL)
  835. {
  836. ui16 ID = typeList.registerType(t);
  837. apps[ID] = T::getApplier(t);
  838. }
  839. };
  840. #endif // __CONNECTION_H__