Connection.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 <boost/type_traits/is_fundamental.hpp>
  10. #include <boost/type_traits/is_enum.hpp>
  11. #include <boost/type_traits/is_pointer.hpp>
  12. #include <boost/type_traits/is_class.hpp>
  13. #include <boost/type_traits/remove_pointer.hpp>
  14. #include <boost/mpl/eval_if.hpp>
  15. #include <boost/mpl/equal_to.hpp>
  16. #include <boost/mpl/int.hpp>
  17. #include <boost/mpl/identity.hpp>
  18. #include <boost/type_traits/is_array.hpp>
  19. const ui32 version = 710;
  20. class CConnection;
  21. namespace mpl = boost::mpl;
  22. /*
  23. * Connection.h, part of VCMI engine
  24. *
  25. * Authors: listed in file AUTHORS in main folder
  26. *
  27. * License: GNU General Public License v2.0 or later
  28. * Full text of license available in license.txt file, in main folder
  29. *
  30. */
  31. namespace boost
  32. {
  33. namespace asio
  34. {
  35. namespace ip
  36. {
  37. class tcp;
  38. }
  39. class io_service;
  40. template <typename Protocol> class stream_socket_service;
  41. template <typename Protocol,typename StreamSocketService>
  42. class basic_stream_socket;
  43. template <typename Protocol> class socket_acceptor_service;
  44. template <typename Protocol,typename SocketAcceptorService>
  45. class basic_socket_acceptor;
  46. }
  47. class mutex;
  48. };
  49. enum SerializationLvl
  50. {
  51. Wrong=0,
  52. Primitive,
  53. Array,
  54. Pointer,
  55. Serializable
  56. };
  57. struct TypeComparer
  58. {
  59. bool operator()(const std::type_info *a, const std::type_info *b) const
  60. {
  61. return a->before(*b);
  62. }
  63. };
  64. class DLL_EXPORT CTypeList
  65. {
  66. typedef std::multimap<const std::type_info *,ui16,TypeComparer> TTypeMap;
  67. TTypeMap types;
  68. public:
  69. CTypeList();
  70. ui16 registerType(const std::type_info *type);
  71. template <typename T> ui16 registerType(const T * t = NULL)
  72. {
  73. return registerType(getTypeInfo(t));
  74. }
  75. ui16 getTypeID(const std::type_info *type);
  76. template <typename T> ui16 getTypeID(const T * t)
  77. {
  78. return getTypeID(getTypeInfo(t));
  79. }
  80. template <typename T> const std::type_info * getTypeInfo(const T * t = NULL)
  81. {
  82. if(t)
  83. return &typeid(*t);
  84. else
  85. return &typeid(T);
  86. }
  87. };
  88. extern DLL_EXPORT CTypeList typeList;
  89. template<typename Ser,typename T>
  90. struct SavePrimitive
  91. {
  92. static void invoke(Ser &s, const T &data)
  93. {
  94. s.savePrimitive(data);
  95. }
  96. };
  97. template<typename Ser,typename T>
  98. struct SaveSerializable
  99. {
  100. static void invoke(Ser &s, const T &data)
  101. {
  102. s.saveSerializable(data);
  103. }
  104. };
  105. template<typename Ser,typename T>
  106. struct LoadPrimitive
  107. {
  108. static void invoke(Ser &s, T &data)
  109. {
  110. s.loadPrimitive(data);
  111. }
  112. };
  113. template<typename Ser,typename T>
  114. struct SavePointer
  115. {
  116. static void invoke(Ser &s, const T &data)
  117. {
  118. s.savePointer(data);
  119. }
  120. };
  121. template<typename Ser,typename T>
  122. struct LoadPointer
  123. {
  124. static void invoke(Ser &s, T &data)
  125. {
  126. s.loadPointer(data);
  127. }
  128. };
  129. template<typename Ser,typename T>
  130. struct SaveArray
  131. {
  132. static void invoke(Ser &s, const T &data)
  133. {
  134. s.saveArray(data);
  135. }
  136. };
  137. template<typename Ser,typename T>
  138. struct LoadArray
  139. {
  140. static void invoke(Ser &s, T &data)
  141. {
  142. s.loadArray(data);
  143. }
  144. };
  145. template<typename Ser,typename T>
  146. struct LoadSerializable
  147. {
  148. static void invoke(Ser &s, T &data)
  149. {
  150. s.loadSerializable(data);
  151. }
  152. };
  153. template<typename Ser,typename T>
  154. struct SaveWrong
  155. {
  156. static void invoke(Ser &s, const T &data)
  157. {
  158. throw std::string("Wrong save serialization call!");
  159. }
  160. };
  161. template<typename Ser,typename T>
  162. struct LoadWrong
  163. {
  164. static void invoke(Ser &s, const T &data)
  165. {
  166. throw std::string("Wrong load serialization call!");
  167. }
  168. };
  169. template<typename T>
  170. struct SerializationLevel
  171. {
  172. typedef mpl::integral_c_tag tag;
  173. typedef
  174. typename mpl::eval_if<
  175. boost::is_fundamental<T>,
  176. mpl::int_<Primitive>,
  177. //else
  178. typename mpl::eval_if<
  179. boost::is_class<T>,
  180. mpl::int_<Serializable>,
  181. //else
  182. typename mpl::eval_if<
  183. boost::is_array<T>,
  184. mpl::int_<Array>,
  185. //else
  186. typename mpl::eval_if<
  187. boost::is_pointer<T>,
  188. mpl::int_<Pointer>,
  189. //else
  190. typename mpl::eval_if<
  191. boost::is_enum<T>,
  192. mpl::int_<Primitive>,
  193. //else
  194. mpl::int_<Wrong>
  195. >
  196. >
  197. >
  198. >
  199. >::type type;
  200. static const int value = SerializationLevel::type::value;
  201. };
  202. class DLL_EXPORT CSerializerBase
  203. {
  204. public:
  205. };
  206. class DLL_EXPORT CSaverBase : public virtual CSerializerBase
  207. {
  208. };
  209. class CBasicPointerSaver
  210. {
  211. public:
  212. virtual void savePtr(CSaverBase &ar, const void *data) const =0;
  213. };
  214. template <typename Serializer, typename T> class CPointerSaver : public CBasicPointerSaver
  215. {
  216. public:
  217. void savePtr(CSaverBase &ar, const void *data) const
  218. {
  219. Serializer &s = static_cast<Serializer&>(ar);
  220. const T *ptr = static_cast<const T*>(data);
  221. //T is most derived known type, it's time to call actual serialize
  222. const_cast<T&>(*ptr).serialize(s,version);
  223. }
  224. };
  225. template <typename Serializer> class DLL_EXPORT COSer : public CSaverBase
  226. {
  227. public:
  228. bool saving;
  229. std::map<ui16,CBasicPointerSaver*> savers; // typeID => CPointerSaver<serializer,type>
  230. COSer()
  231. {
  232. saving=true;
  233. }
  234. template<typename T> void registerType(const T * t=NULL)
  235. {
  236. ui16 ID = typeList.registerType(t);
  237. savers[ID] = new CPointerSaver<COSer<Serializer>,T>;
  238. }
  239. Serializer * This()
  240. {
  241. return static_cast<Serializer*>(this);
  242. }
  243. template<class T>
  244. Serializer & operator<<(const T &t)
  245. {
  246. this->This()->save(t);
  247. return * this->This();
  248. }
  249. template<class T>
  250. COSer & operator&(const T & t)
  251. {
  252. return * this->This() << t;
  253. }
  254. int write(const void * data, unsigned size);
  255. template <typename T>
  256. void savePrimitive(const T &data)
  257. {
  258. this->This()->write(&data,sizeof(data));
  259. }
  260. template <typename T>
  261. void savePointer(const T &data)
  262. {
  263. //write if pointer is not NULL
  264. ui8 hlp = (data!=NULL);
  265. *this << hlp;
  266. //if pointer is NULL then we don't need anything more...
  267. if(!hlp)
  268. return;
  269. //write type identifier
  270. ui16 tid = typeList.getTypeID(data);
  271. *this << tid;
  272. if(!tid)
  273. *this << *data; //if type is unregistered simply write all data in a standard way
  274. else
  275. savers[tid]->savePtr(*this,data); //call serializer specific for our real type
  276. }
  277. template <typename T>
  278. void saveArray(const T &data)
  279. {
  280. ui32 size = ARRAY_COUNT(data);
  281. for(ui32 i=0; i < size; i++)
  282. *this << data[i];
  283. }
  284. template <typename T>
  285. void save(const T &data)
  286. {
  287. typedef
  288. //if
  289. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  290. mpl::identity<SavePrimitive<Serializer,T> >,
  291. //else if
  292. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  293. mpl::identity<SavePointer<Serializer,T> >,
  294. //else if
  295. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Array> >,
  296. mpl::identity<SaveArray<Serializer,T> >,
  297. //else if
  298. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  299. mpl::identity<SaveSerializable<Serializer,T> >,
  300. //else
  301. mpl::identity<SaveWrong<Serializer,T> >
  302. >
  303. >
  304. >
  305. >::type typex;
  306. typex::invoke(* this->This(), data);
  307. }
  308. template <typename T>
  309. void saveSerializable(const T &data)
  310. {
  311. const_cast<T&>(data).serialize(*this,version);
  312. }
  313. template <typename T>
  314. void saveSerializable(const std::vector<T> &data)
  315. {
  316. boost::uint32_t length = data.size();
  317. *this << length;
  318. for(ui32 i=0;i<length;i++)
  319. *this << data[i];
  320. }
  321. template <typename T>
  322. void saveSerializable(const std::set<T> &data)
  323. {
  324. std::set<T> &d = const_cast<std::set<T> &>(data);
  325. boost::uint32_t length = d.size();
  326. *this << length;
  327. for(typename std::set<T>::iterator i=d.begin();i!=d.end();i++)
  328. *this << *i;
  329. }
  330. template <typename T>
  331. void saveSerializable(const std::list<T> &data)
  332. {
  333. std::list<T> &d = const_cast<std::list<T> &>(data);
  334. boost::uint32_t length = d.size();
  335. *this << length;
  336. for(typename std::list<T>::iterator i=d.begin();i!=d.end();i++)
  337. *this << *i;
  338. }
  339. void saveSerializable(const std::string &data)
  340. {
  341. *this << ui32(data.length());
  342. this->This()->write(data.c_str(),data.size());
  343. }
  344. template <typename T1, typename T2>
  345. void saveSerializable(const std::pair<T1,T2> &data)
  346. {
  347. *this << data.first << data.second;
  348. }
  349. template <typename T1, typename T2>
  350. void saveSerializable(const std::map<T1,T2> &data)
  351. {
  352. *this << ui32(data.size());
  353. for(typename std::map<T1,T2>::const_iterator i=data.begin();i!=data.end();i++)
  354. *this << i->first << i->second;
  355. }
  356. };
  357. class DLL_EXPORT CLoaderBase : public virtual CSerializerBase
  358. {};
  359. class CBasicPointerLoader
  360. {
  361. public:
  362. virtual void loadPtr(CLoaderBase &ar, void *data) const =0; //data is pointer to the ACTUAL POINTER
  363. };
  364. template <typename Serializer, typename T> class CPointerLoader : public CBasicPointerLoader
  365. {
  366. public:
  367. void loadPtr(CLoaderBase &ar, void *data) const //data is pointer to the ACTUAL POINTER
  368. {
  369. Serializer &s = static_cast<Serializer&>(ar);
  370. T *&ptr = *static_cast<T**>(data);
  371. //create new object under pointer
  372. typedef typename boost::remove_pointer<T>::type npT;
  373. ptr = new npT;
  374. //T is most derived known type, it's time to call actual serialize
  375. ptr->serialize(s,version);
  376. }
  377. };
  378. template <typename Serializer> class DLL_EXPORT CISer : public CLoaderBase
  379. {
  380. public:
  381. bool saving;
  382. std::map<ui16,CBasicPointerLoader*> loaders; // typeID => CPointerSaver<serializer,type>
  383. ui32 myVersion;
  384. CISer()
  385. {
  386. saving = false;
  387. myVersion = version;
  388. }
  389. template<typename T> void registerType(const T * t=NULL)
  390. {
  391. ui16 ID = typeList.registerType(t);
  392. loaders[ID] = new CPointerLoader<CISer<Serializer>,T>;
  393. }
  394. Serializer * This()
  395. {
  396. return static_cast<Serializer*>(this);
  397. }
  398. template<class T>
  399. Serializer & operator>>(T &t)
  400. {
  401. this->This()->load(t);
  402. return * this->This();
  403. }
  404. template<class T>
  405. CISer & operator&(T & t)
  406. {
  407. return * this->This() >> t;
  408. }
  409. int write(const void * data, unsigned size);
  410. template <typename T>
  411. void load(T &data)
  412. {
  413. typedef
  414. //if
  415. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  416. mpl::identity<LoadPrimitive<Serializer,T> >,
  417. //else if
  418. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  419. mpl::identity<LoadPointer<Serializer,T> >,
  420. //else if
  421. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Array> >,
  422. mpl::identity<LoadArray<Serializer,T> >,
  423. //else if
  424. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  425. mpl::identity<LoadSerializable<Serializer,T> >,
  426. //else
  427. mpl::identity<LoadWrong<Serializer,T> >
  428. >
  429. >
  430. >
  431. >::type typex;
  432. typex::invoke(* this->This(), data);
  433. }
  434. template <typename T>
  435. void loadPrimitive(T &data)
  436. {
  437. this->This()->read(&data,sizeof(data));
  438. }
  439. template <typename T>
  440. void loadSerializable(T &data)
  441. {
  442. data.serialize(*this,myVersion);
  443. }
  444. template <typename T>
  445. void loadArray(T &data)
  446. {
  447. ui32 size = ARRAY_COUNT(data);
  448. for(ui32 i=0; i < size; i++)
  449. *this >> data[i];
  450. }
  451. template <typename T>
  452. void loadPointer(T &data)
  453. {
  454. ui8 hlp;
  455. *this >> hlp;
  456. if(!hlp)
  457. {
  458. data = NULL;
  459. return;
  460. }
  461. //get type id
  462. ui16 tid;
  463. *this >> tid;
  464. if(!tid)
  465. {
  466. typedef typename boost::remove_pointer<T>::type npT;
  467. data = new npT;
  468. *this >> *data;
  469. }
  470. else
  471. {
  472. loaders[tid]->loadPtr(*this,&data);
  473. }
  474. }
  475. template <typename T>
  476. void loadSerializable(std::vector<T> &data)
  477. {
  478. boost::uint32_t length;
  479. *this >> length;
  480. data.resize(length);
  481. for(ui32 i=0;i<length;i++)
  482. *this >> data[i];
  483. }
  484. template <typename T>
  485. void loadSerializable(std::set<T> &data)
  486. {
  487. boost::uint32_t length;
  488. *this >> length;
  489. T ins;
  490. for(ui32 i=0;i<length;i++)
  491. {
  492. *this >> ins;
  493. data.insert(ins);
  494. }
  495. }
  496. template <typename T>
  497. void loadSerializable(std::list<T> &data)
  498. {
  499. boost::uint32_t length;
  500. *this >> length;
  501. T ins;
  502. for(ui32 i=0;i<length;i++)
  503. {
  504. *this >> ins;
  505. data.push_back(ins);
  506. }
  507. }
  508. template <typename T1, typename T2>
  509. void loadSerializable(std::pair<T1,T2> &data)
  510. {
  511. *this >> data.first >> data.second;
  512. }
  513. template <typename T1, typename T2>
  514. void loadSerializable(std::map<T1,T2> &data)
  515. {
  516. ui32 length;
  517. *this >> length;
  518. T1 t;
  519. for(ui32 i=0;i<length;i++)
  520. {
  521. *this >> t;
  522. *this >> data[t];
  523. }
  524. }
  525. void loadSerializable(std::string &data)
  526. {
  527. ui32 length;
  528. *this >> length;
  529. data.resize(length);
  530. this->This()->read((void*)data.c_str(),length);
  531. }
  532. };
  533. class DLL_EXPORT CSaveFile
  534. : public COSer<CSaveFile>
  535. {
  536. void dummyMagicFunction()
  537. {
  538. *this << std::string("This function makes stuff working.");
  539. }
  540. public:
  541. std::ofstream *sfile;
  542. CSaveFile(const std::string &fname);
  543. ~CSaveFile();
  544. int write(const void * data, unsigned size);
  545. };
  546. class DLL_EXPORT CLoadFile
  547. : public CISer<CLoadFile>
  548. {
  549. void dummyMagicFunction()
  550. {
  551. std::string dummy = "This function makes stuff working.";
  552. *this >> dummy;
  553. }
  554. public:
  555. std::ifstream *sfile;
  556. CLoadFile(const std::string &fname);
  557. ~CLoadFile();
  558. int read(const void * data, unsigned size);
  559. };
  560. class DLL_EXPORT CConnection
  561. :public CISer<CConnection>, public COSer<CConnection>
  562. {
  563. CConnection(void);
  564. void init();
  565. public:
  566. boost::mutex *rmx, *wmx; // read/write mutexes
  567. boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * socket;
  568. bool logging;
  569. bool connected;
  570. bool myEndianess, contactEndianess; //true if little endian, if ednianess is different we'll have to revert recieved multi-byte vars
  571. boost::asio::io_service *io_service;
  572. std::string name; //who uses this connection
  573. CConnection
  574. (std::string host, std::string port, std::string Name);
  575. CConnection
  576. (boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > * acceptor,
  577. boost::asio::io_service *Io_service, std::string Name);
  578. CConnection
  579. (boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  580. std::string Name); //use immediately after accepting connection into socket
  581. int write(const void * data, unsigned size);
  582. int read(void * data, unsigned size);
  583. int readLine(void * data, unsigned maxSize);
  584. void close();
  585. template<class T>
  586. CConnection &operator&(const T&);
  587. ~CConnection(void);
  588. };
  589. #endif // __CONNECTION_H__