Connection.h 8.7 KB

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