Connection.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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>
  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 = 703;
  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 type_info *a, const type_info *b)
  51. {
  52. return a->before(*b);
  53. }
  54. };
  55. class DLL_EXPORT CTypeList
  56. {
  57. typedef std::multimap<const type_info *,ui16,TypeComparer> TTypeMap;
  58. TTypeMap types;
  59. public:
  60. CTypeList();
  61. ui16 registerType(const type_info *type);
  62. template <typename T> ui16 registerType(const T * t)
  63. {
  64. return registerType(getTypeInfo(t));
  65. }
  66. ui16 getTypeID(const type_info *type);
  67. template <typename T> ui16 getTypeID(const T * t)
  68. {
  69. return getTypeID(getTypeInfo(t));
  70. }
  71. template <typename T> const 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. bool saving;
  197. };
  198. class DLL_EXPORT CSaverBase : public virtual CSerializerBase
  199. {
  200. };
  201. class CBasicPointerSaver
  202. {
  203. public:
  204. virtual void savePtr(CSaverBase &ar, const void *data) const =0;
  205. };
  206. template <typename Serializer, typename T> class CPointerSaver : public CBasicPointerSaver
  207. {
  208. public:
  209. void savePtr(CSaverBase &ar, const void *data) const
  210. {
  211. Serializer &s = static_cast<Serializer&>(ar);
  212. const T *ptr = static_cast<const T*>(data);
  213. //T is most derived known type, it's time to call actual serialize
  214. const_cast<T&>(*ptr).serialize(s,version);
  215. }
  216. };
  217. template <typename Serializer> class DLL_EXPORT COSer : public CSaverBase
  218. {
  219. public:
  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. std::map<ui16,CBasicPointerLoader*> loaders; // typeID => CPointerSaver<serializer,type>
  373. CISer()
  374. {
  375. saving = false;
  376. }
  377. template<typename T> void registerType(const T * t=NULL)
  378. {
  379. ui16 ID = typeList.registerType(t);
  380. loaders[ID] = new CPointerLoader<CISer<Serializer>,T>;
  381. }
  382. Serializer * This()
  383. {
  384. return static_cast<Serializer*>(this);
  385. }
  386. template<class T>
  387. Serializer & operator>>(T &t)
  388. {
  389. this->This()->load(t);
  390. return * this->This();
  391. }
  392. template<class T>
  393. CISer & operator&(T & t)
  394. {
  395. return * this->This() >> t;
  396. }
  397. int write(const void * data, unsigned size);
  398. template <typename T>
  399. void load(T &data)
  400. {
  401. typedef
  402. //if
  403. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  404. mpl::identity<LoadPrimitive<Serializer,T> >,
  405. //else if
  406. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  407. mpl::identity<LoadPointer<Serializer,T> >,
  408. //else if
  409. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Array> >,
  410. mpl::identity<LoadArray<Serializer,T> >,
  411. //else if
  412. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  413. mpl::identity<LoadSerializable<Serializer,T> >,
  414. //else
  415. mpl::identity<LoadWrong<Serializer,T> >
  416. >
  417. >
  418. >
  419. >::type typex;
  420. typex::invoke(* this->This(), data);
  421. }
  422. template <typename T>
  423. void loadPrimitive(T &data)
  424. {
  425. this->This()->read(&data,sizeof(data));
  426. }
  427. template <typename T>
  428. void loadSerializable(T &data)
  429. {
  430. data.serialize(*this,version);
  431. }
  432. template <typename T>
  433. void loadArray(T &data)
  434. {
  435. ui32 size = ARRAY_COUNT(data);
  436. for(ui32 i=0; i < size; i++)
  437. *this >> data[i];
  438. }
  439. template <typename T>
  440. void loadPointer(T &data)
  441. {
  442. ui8 hlp;
  443. *this >> hlp;
  444. if(!hlp)
  445. {
  446. data = NULL;
  447. return;
  448. }
  449. //get type id
  450. ui16 tid;
  451. *this >> tid;
  452. if(!tid)
  453. {
  454. typedef typename boost::remove_pointer<T>::type npT;
  455. data = new npT;
  456. *this >> *data;
  457. }
  458. else
  459. {
  460. loaders[tid]->loadPtr(*this,&data);
  461. }
  462. }
  463. template <typename T>
  464. void loadSerializable(std::vector<T> &data)
  465. {
  466. boost::uint32_t length;
  467. *this >> length;
  468. data.resize(length);
  469. for(ui32 i=0;i<length;i++)
  470. *this >> data[i];
  471. }
  472. template <typename T>
  473. void loadSerializable(std::set<T> &data)
  474. {
  475. boost::uint32_t length;
  476. *this >> length;
  477. T ins;
  478. for(ui32 i=0;i<length;i++)
  479. {
  480. *this >> ins;
  481. data.insert(ins);
  482. }
  483. }
  484. template <typename T>
  485. void loadSerializable(std::list<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.push_back(ins);
  494. }
  495. }
  496. template <typename T1, typename T2>
  497. void loadSerializable(std::pair<T1,T2> &data)
  498. {
  499. *this >> data.first >> data.second;
  500. }
  501. template <typename T1, typename T2>
  502. void loadSerializable(std::map<T1,T2> &data)
  503. {
  504. ui32 length;
  505. *this >> length;
  506. T1 t;
  507. for(int i=0;i<length;i++)
  508. {
  509. *this >> t;
  510. *this >> data[t];
  511. }
  512. }
  513. void loadSerializable(std::string &data)
  514. {
  515. ui32 length;
  516. *this >> length;
  517. data.resize(length);
  518. this->This()->read((void*)data.c_str(),length);
  519. }
  520. };
  521. class DLL_EXPORT CSaveFile
  522. : public COSer<CSaveFile>
  523. {
  524. void dummyMagicFunction()
  525. {
  526. *this << std::string("This function makes stuff working.");
  527. }
  528. public:
  529. std::ofstream *sfile;
  530. CSaveFile(const std::string &fname);
  531. ~CSaveFile();
  532. int write(const void * data, unsigned size);
  533. };
  534. class DLL_EXPORT CLoadFile
  535. : public CISer<CLoadFile>
  536. {
  537. void dummyMagicFunction()
  538. {
  539. std::string dummy = "This function makes stuff working.";
  540. *this >> dummy;
  541. }
  542. public:
  543. std::ifstream *sfile;
  544. CLoadFile(const std::string &fname);
  545. ~CLoadFile();
  546. int read(const void * data, unsigned size);
  547. };
  548. class DLL_EXPORT CConnection
  549. :public CISer<CConnection>, public COSer<CConnection>
  550. {
  551. CConnection(void);
  552. void init();
  553. public:
  554. boost::mutex *rmx, *wmx; // read/write mutexes
  555. boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * socket;
  556. bool logging;
  557. bool connected;
  558. bool myEndianess, contactEndianess; //true if little endian, if ednianess is different we'll have to revert recieved multi-byte vars
  559. boost::asio::io_service *io_service;
  560. std::string name; //who uses this connection
  561. CConnection
  562. (std::string host, std::string port, std::string Name);
  563. CConnection
  564. (boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > * acceptor,
  565. boost::asio::io_service *Io_service, std::string Name);
  566. CConnection
  567. (boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  568. std::string Name); //use immediately after accepting connection into socket
  569. int write(const void * data, unsigned size);
  570. int read(void * data, unsigned size);
  571. int readLine(void * data, unsigned maxSize);
  572. void close();
  573. ~CConnection(void);
  574. };
  575. #endif // __CONNECTION_H__