Connection.h 20 KB

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