Connection.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #ifndef __CONNECTION_H__
  2. #define __CONNECTION_H__
  3. #include "../global.h"
  4. #include <string>
  5. #include <vector>
  6. #include <set>
  7. #include <boost/type_traits/is_fundamental.hpp>
  8. #include <boost/type_traits/is_enum.hpp>
  9. #include <boost/type_traits/is_pointer.hpp>
  10. #include <boost/type_traits/is_class.hpp>
  11. #include <boost/type_traits/remove_pointer.hpp>
  12. #include <boost/mpl/eval_if.hpp>
  13. #include <boost/mpl/equal_to.hpp>
  14. #include <boost/mpl/int.hpp>
  15. #include <boost/mpl/identity.hpp>
  16. #include <boost/type_traits/is_array.hpp>
  17. const int version = 63;
  18. class CConnection;
  19. namespace mpl = boost::mpl;
  20. namespace boost
  21. {
  22. namespace asio
  23. {
  24. namespace ip
  25. {
  26. class tcp;
  27. }
  28. class io_service;
  29. template <typename Protocol> class stream_socket_service;
  30. template <typename Protocol,typename StreamSocketService>
  31. class basic_stream_socket;
  32. template <typename Protocol> class socket_acceptor_service;
  33. template <typename Protocol,typename SocketAcceptorService>
  34. class basic_socket_acceptor;
  35. }
  36. class mutex;
  37. };
  38. enum SerializationLvl
  39. {
  40. Wrong=0,
  41. Primitive,
  42. Pointer,
  43. Serializable
  44. };
  45. template<typename Ser,typename T>
  46. struct SavePrimitive
  47. {
  48. static void invoke(Ser &s, const T &data)
  49. {
  50. s.savePrimitive(data);
  51. }
  52. };
  53. template<typename Ser,typename T>
  54. struct SaveSerializable
  55. {
  56. static void invoke(Ser &s, const T &data)
  57. {
  58. s.saveSerializable(data);
  59. }
  60. };
  61. template<typename Ser,typename T>
  62. struct LoadPrimitive
  63. {
  64. static void invoke(Ser &s, T &data)
  65. {
  66. s.loadPrimitive(data);
  67. }
  68. };
  69. template<typename Ser,typename T>
  70. struct SavePointer
  71. {
  72. static void invoke(Ser &s, const T &data)
  73. {
  74. s.savePointer(data);
  75. }
  76. };
  77. template<typename Ser,typename T>
  78. struct LoadPointer
  79. {
  80. static void invoke(Ser &s, T &data)
  81. {
  82. s.loadPointer(data);
  83. }
  84. };
  85. template<typename Ser,typename T>
  86. struct LoadSerializable
  87. {
  88. static void invoke(Ser &s, T &data)
  89. {
  90. s.loadSerializable(data);
  91. }
  92. };
  93. template<typename Ser,typename T>
  94. struct SaveWrong
  95. {
  96. static void invoke(Ser &s, const T &data)
  97. {
  98. throw std::exception("Wrong save serialization call!");
  99. }
  100. };
  101. template<typename Ser,typename T>
  102. struct LoadWrong
  103. {
  104. static void invoke(Ser &s, const T &data)
  105. {
  106. throw std::exception("Wrong load serialization call!");
  107. }
  108. };
  109. template<typename T>
  110. struct SerializationLevel
  111. {
  112. typedef mpl::integral_c_tag tag;
  113. typedef
  114. typename mpl::eval_if<
  115. boost::is_fundamental<T>,
  116. mpl::int_<Primitive>,
  117. //else
  118. typename mpl::eval_if<
  119. boost::is_class<T>,
  120. mpl::int_<Serializable>,
  121. //else
  122. typename mpl::eval_if<
  123. boost::is_array<T>,
  124. mpl::int_<Primitive>,
  125. //else
  126. typename mpl::eval_if<
  127. boost::is_pointer<T>,
  128. mpl::int_<Pointer>,
  129. //else
  130. typename mpl::eval_if<
  131. boost::is_enum<T>,
  132. mpl::int_<Primitive>,
  133. //else
  134. mpl::int_<Wrong>
  135. >
  136. >
  137. >
  138. >
  139. >::type type;
  140. static const int value = SerializationLevel::type::value;
  141. };
  142. template <typename Serializer> class DLL_EXPORT COSer
  143. {
  144. public:
  145. bool saving;
  146. COSer(){saving=true;};
  147. Serializer * This()
  148. {
  149. return static_cast<Serializer*>(this);
  150. }
  151. template<class T>
  152. Serializer & operator<<(const T &t)
  153. {
  154. this->This()->save(t);
  155. return * this->This();
  156. }
  157. template<class T>
  158. COSer & operator&(T & t)
  159. {
  160. return * this->This() << t;
  161. }
  162. int write(const void * data, unsigned size);
  163. template <typename T>
  164. void savePrimitive(const T &data)
  165. {
  166. this->This()->write(&data,sizeof(data));
  167. }
  168. template <typename T>
  169. void savePointer(const T &data)
  170. {
  171. ui8 hlp = (data!=NULL);
  172. *this << hlp;
  173. if(hlp)
  174. *this << *data;
  175. }
  176. template <typename T>
  177. void save(const T &data)
  178. {
  179. typedef
  180. //if
  181. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  182. mpl::identity<SavePrimitive<Serializer,T> >,
  183. //else if
  184. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  185. mpl::identity<SavePointer<Serializer,T> >,
  186. //else if
  187. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  188. mpl::identity<SaveSerializable<Serializer,T> >,
  189. //else
  190. mpl::identity<SaveWrong<Serializer,T> >
  191. >
  192. >
  193. >::type typex;
  194. typex::invoke(* this->This(), data);
  195. }
  196. template <typename T>
  197. void saveSerializable(const T &data)
  198. {
  199. const_cast<T&>(data).serialize(*this,version);
  200. }
  201. template <typename T>
  202. void saveSerializable(const std::vector<T> &data)
  203. {
  204. boost::uint32_t length = data.size();
  205. *this << length;
  206. for(ui32 i=0;i<length;i++)
  207. *this << data[i];
  208. }
  209. template <typename T>
  210. void saveSerializable(const std::set<T> &data)
  211. {
  212. std::set<T> &d = const_cast<std::set<T> &>(data);
  213. boost::uint32_t length = d.size();
  214. *this << length;
  215. for(typename std::set<T>::iterator i=d.begin();i!=d.end();i++)
  216. *this << *i;
  217. }
  218. void saveSerializable(const std::string &data)
  219. {
  220. *this << ui32(data.length());
  221. this->This()->write(data.c_str(),data.size());
  222. }
  223. template <typename T1, typename T2>
  224. void saveSerializable(const std::pair<T1,T2> &data)
  225. {
  226. *this << data.first << data.second;
  227. }
  228. template <typename T1, typename T2>
  229. void saveSerializable(const std::map<T1,T2> &data)
  230. {
  231. *this << ui32(data.size());
  232. for(typename std::map<T1,T2>::const_iterator i=data.begin();i!=data.end();i++)
  233. *this << i->first << i->second;
  234. }
  235. };
  236. template <typename Serializer> class DLL_EXPORT CISer
  237. {
  238. public:
  239. bool saving;
  240. CISer(){saving = false;};
  241. Serializer * This()
  242. {
  243. return static_cast<Serializer*>(this);
  244. }
  245. template<class T>
  246. Serializer & operator>>(T &t)
  247. {
  248. this->This()->load(t);
  249. return * this->This();
  250. }
  251. template<class T>
  252. CISer & operator&(T & t)
  253. {
  254. return * this->This() >> t;
  255. }
  256. int write(const void * data, unsigned size);
  257. template <typename T>
  258. void load(T &data)
  259. {
  260. typedef
  261. //if
  262. typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
  263. mpl::identity<LoadPrimitive<Serializer,T> >,
  264. //else if
  265. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
  266. mpl::identity<LoadPointer<Serializer,T> >,
  267. //else if
  268. typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
  269. mpl::identity<LoadSerializable<Serializer,T> >,
  270. //else
  271. mpl::identity<LoadWrong<Serializer,T> >
  272. >
  273. >
  274. >::type typex;
  275. typex::invoke(* this->This(), data);
  276. }
  277. template <typename T>
  278. void loadPrimitive(T &data)
  279. {
  280. this->This()->read(&data,sizeof(data));
  281. }
  282. template <typename T>
  283. void loadSerializable(T &data)
  284. {
  285. data.serialize(*this,version);
  286. }
  287. template <typename T>
  288. void loadPointer(T &data)
  289. {
  290. ui8 hlp;
  291. *this >> hlp;
  292. if(!hlp)
  293. {
  294. data = NULL;
  295. return;
  296. }
  297. tlog5<<"Allocating memory for pointer!"<<std::endl;
  298. typedef typename boost::remove_pointer<T>::type npT;
  299. data = new npT;
  300. *this >> *data;
  301. }
  302. template <typename T>
  303. void loadSerializable(std::vector<T> &data)
  304. {
  305. boost::uint32_t length;
  306. *this >> length;
  307. data.resize(length);
  308. for(ui32 i=0;i<length;i++)
  309. *this >> data[i];
  310. }
  311. template <typename T>
  312. void loadSerializable(std::set<T> &data)
  313. {
  314. boost::uint32_t length;
  315. *this >> length;
  316. T ins;
  317. for(ui32 i=0;i<length;i++)
  318. {
  319. *this >> ins;
  320. data.insert(ins);
  321. }
  322. }
  323. template <typename T1, typename T2>
  324. void loadSerializable(std::pair<T1,T2> &data)
  325. {
  326. *this >> data.first >> data.second;
  327. }
  328. template <typename T1, typename T2>
  329. void loadSerializable(std::map<T1,T2> &data)
  330. {
  331. ui32 length;
  332. *this >> length;
  333. T1 t;
  334. for(int i=0;i<length;i++)
  335. {
  336. *this >> t;
  337. *this >> data[t];
  338. }
  339. }
  340. void loadSerializable(std::string &data)
  341. {
  342. ui32 length;
  343. *this >> length;
  344. data.resize(length);
  345. this->This()->read((void*)data.c_str(),length);
  346. }
  347. };
  348. class DLL_EXPORT CSaveFile
  349. : public COSer<CSaveFile>
  350. {
  351. void dummyMagicFunction()
  352. {
  353. *this << std::string("This function makes stuff working.");
  354. }
  355. public:
  356. std::ofstream *sfile;
  357. CSaveFile(const std::string &fname);
  358. ~CSaveFile();
  359. int write(const void * data, unsigned size);
  360. };
  361. class DLL_EXPORT CLoadFile
  362. : public CISer<CLoadFile>
  363. {
  364. void dummyMagicFunction()
  365. {
  366. *this >> std::string("This function makes stuff working.");
  367. }
  368. public:
  369. std::ifstream *sfile;
  370. CLoadFile(const std::string &fname);
  371. ~CLoadFile();
  372. int read(const void * data, unsigned size);
  373. };
  374. class DLL_EXPORT CConnection
  375. :public CISer<CConnection>, public COSer<CConnection>
  376. {
  377. std::ostream &out;
  378. CConnection(void);
  379. void init();
  380. public:
  381. boost::mutex *rmx, *wmx; // read/write mutexes
  382. boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * socket;
  383. bool logging;
  384. bool connected;
  385. bool myEndianess, contactEndianess; //true if little endian, if ednianess is different we'll have to revert recieved multi-byte vars
  386. boost::asio::io_service *io_service;
  387. std::string name; //who uses this connection
  388. CConnection
  389. (std::string host, std::string port, std::string Name, std::ostream & Out);
  390. CConnection
  391. (boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > * acceptor,
  392. boost::asio::io_service *Io_service, std::string Name, std::ostream & Out);
  393. CConnection
  394. (boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  395. std::string Name, std::ostream & Out); //use immediately after accepting connection into socket
  396. int write(const void * data, unsigned size);
  397. int read(void * data, unsigned size);
  398. int readLine(void * data, unsigned maxSize);
  399. void close();
  400. ~CConnection(void);
  401. };
  402. #endif // __CONNECTION_H__