Connection.h 14 KB

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