Connection.h 7.2 KB

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