Connection.h 25 KB

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