Connection.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 = 704;
  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. CISer()
  384. {
  385. saving = false;
  386. }
  387. template<typename T> void registerType(const T * t=NULL)
  388. {
  389. ui16 ID = typeList.registerType(t);
  390. loaders[ID] = new CPointerLoader<CISer<Serializer>,T>;
  391. }
  392. Serializer * This()
  393. {
  394. return static_cast<Serializer*>(this);
  395. }
  396. template<class T>
  397. Serializer & operator>>(T &t)
  398. {
  399. this->This()->load(t);
  400. return * this->This();
  401. }
  402. template<class T>
  403. CISer & operator&(T & t)
  404. {
  405. return * this->This() >> t;
  406. }
  407. int write(const void * data, unsigned size);
  408. template <typename T>
  409. void load(T &data)
  410. {
  411. typedef
  412. //if
  413. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  414. mpl::identity<LoadPrimitive<Serializer,T> >,
  415. //else if
  416. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  417. mpl::identity<LoadPointer<Serializer,T> >,
  418. //else if
  419. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Array> >,
  420. mpl::identity<LoadArray<Serializer,T> >,
  421. //else if
  422. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  423. mpl::identity<LoadSerializable<Serializer,T> >,
  424. //else
  425. mpl::identity<LoadWrong<Serializer,T> >
  426. >
  427. >
  428. >
  429. >::type typex;
  430. typex::invoke(* this->This(), data);
  431. }
  432. template <typename T>
  433. void loadPrimitive(T &data)
  434. {
  435. this->This()->read(&data,sizeof(data));
  436. }
  437. template <typename T>
  438. void loadSerializable(T &data)
  439. {
  440. data.serialize(*this,version);
  441. }
  442. template <typename T>
  443. void loadArray(T &data)
  444. {
  445. ui32 size = ARRAY_COUNT(data);
  446. for(ui32 i=0; i < size; i++)
  447. *this >> data[i];
  448. }
  449. template <typename T>
  450. void loadPointer(T &data)
  451. {
  452. ui8 hlp;
  453. *this >> hlp;
  454. if(!hlp)
  455. {
  456. data = NULL;
  457. return;
  458. }
  459. //get type id
  460. ui16 tid;
  461. *this >> tid;
  462. if(!tid)
  463. {
  464. typedef typename boost::remove_pointer<T>::type npT;
  465. data = new npT;
  466. *this >> *data;
  467. }
  468. else
  469. {
  470. loaders[tid]->loadPtr(*this,&data);
  471. }
  472. }
  473. template <typename T>
  474. void loadSerializable(std::vector<T> &data)
  475. {
  476. boost::uint32_t length;
  477. *this >> length;
  478. data.resize(length);
  479. for(ui32 i=0;i<length;i++)
  480. *this >> data[i];
  481. }
  482. template <typename T>
  483. void loadSerializable(std::set<T> &data)
  484. {
  485. boost::uint32_t length;
  486. *this >> length;
  487. T ins;
  488. for(ui32 i=0;i<length;i++)
  489. {
  490. *this >> ins;
  491. data.insert(ins);
  492. }
  493. }
  494. template <typename T>
  495. void loadSerializable(std::list<T> &data)
  496. {
  497. boost::uint32_t length;
  498. *this >> length;
  499. T ins;
  500. for(ui32 i=0;i<length;i++)
  501. {
  502. *this >> ins;
  503. data.push_back(ins);
  504. }
  505. }
  506. template <typename T1, typename T2>
  507. void loadSerializable(std::pair<T1,T2> &data)
  508. {
  509. *this >> data.first >> data.second;
  510. }
  511. template <typename T1, typename T2>
  512. void loadSerializable(std::map<T1,T2> &data)
  513. {
  514. ui32 length;
  515. *this >> length;
  516. T1 t;
  517. for(ui32 i=0;i<length;i++)
  518. {
  519. *this >> t;
  520. *this >> data[t];
  521. }
  522. }
  523. void loadSerializable(std::string &data)
  524. {
  525. ui32 length;
  526. *this >> length;
  527. data.resize(length);
  528. this->This()->read((void*)data.c_str(),length);
  529. }
  530. };
  531. class DLL_EXPORT CSaveFile
  532. : public COSer<CSaveFile>
  533. {
  534. void dummyMagicFunction()
  535. {
  536. *this << std::string("This function makes stuff working.");
  537. }
  538. public:
  539. std::ofstream *sfile;
  540. CSaveFile(const std::string &fname);
  541. ~CSaveFile();
  542. int write(const void * data, unsigned size);
  543. };
  544. class DLL_EXPORT CLoadFile
  545. : public CISer<CLoadFile>
  546. {
  547. void dummyMagicFunction()
  548. {
  549. std::string dummy = "This function makes stuff working.";
  550. *this >> dummy;
  551. }
  552. public:
  553. std::ifstream *sfile;
  554. CLoadFile(const std::string &fname);
  555. ~CLoadFile();
  556. int read(const void * data, unsigned size);
  557. };
  558. class DLL_EXPORT CConnection
  559. :public CISer<CConnection>, public COSer<CConnection>
  560. {
  561. CConnection(void);
  562. void init();
  563. public:
  564. boost::mutex *rmx, *wmx; // read/write mutexes
  565. boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * socket;
  566. bool logging;
  567. bool connected;
  568. bool myEndianess, contactEndianess; //true if little endian, if ednianess is different we'll have to revert recieved multi-byte vars
  569. boost::asio::io_service *io_service;
  570. std::string name; //who uses this connection
  571. CConnection
  572. (std::string host, std::string port, std::string Name);
  573. CConnection
  574. (boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > * acceptor,
  575. boost::asio::io_service *Io_service, std::string Name);
  576. CConnection
  577. (boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  578. std::string Name); //use immediately after accepting connection into socket
  579. int write(const void * data, unsigned size);
  580. int read(void * data, unsigned size);
  581. int readLine(void * data, unsigned maxSize);
  582. void close();
  583. template<class T>
  584. CConnection &operator&(const T&);
  585. ~CConnection(void);
  586. };
  587. #endif // __CONNECTION_H__