Connection.h 13 KB

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