Connection.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma once
  2. #include "../global.h"
  3. #include <string>
  4. #include <vector>
  5. #include <set>
  6. const int version = 63;
  7. class CConnection;
  8. namespace boost
  9. {
  10. namespace asio
  11. {
  12. namespace ip
  13. {
  14. class tcp;
  15. }
  16. class io_service;
  17. template <typename Protocol> class stream_socket_service;
  18. template <typename Protocol,typename StreamSocketService>
  19. class basic_stream_socket;
  20. template <typename Protocol> class socket_acceptor_service;
  21. template <typename Protocol,typename SocketAcceptorService>
  22. class basic_socket_acceptor;
  23. }
  24. };
  25. class CSender
  26. {
  27. public:
  28. CConnection* c;
  29. CSender(CConnection* C):c(C){};
  30. template <typename T> CSender & operator&&(T &data) //send built-in type
  31. {
  32. *c << data;
  33. return *this;
  34. }
  35. template <typename T> CSender & operator&(T &data) //send serializable type
  36. {
  37. *c < data;
  38. return *this;
  39. }
  40. };
  41. class CReceiver
  42. {
  43. public:
  44. CConnection *c;
  45. CReceiver(CConnection* C):c(C){};
  46. template <typename T> CReceiver & operator&&(T &data) //get built-in type
  47. {
  48. *c >> data;
  49. return *this;
  50. }
  51. template <typename T> CReceiver & operator&(T &data) //get serializable type
  52. {
  53. *c > data;
  54. return *this;
  55. }
  56. };
  57. class DLL_EXPORT CConnection
  58. {
  59. std::ostream &out;
  60. CConnection(void);
  61. void init();
  62. public:
  63. CSender send;
  64. CReceiver rec;
  65. boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * socket;
  66. bool logging;
  67. bool connected;
  68. bool myEndianess, contactEndianess; //true if little endian, if ednianess is different we'll have to revert recieved multi-byte vars
  69. boost::asio::io_service *io_service;
  70. std::string name; //who uses this connection
  71. CConnection
  72. (std::string host, std::string port, std::string Name, std::ostream & Out);
  73. CConnection
  74. (boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > * acceptor,
  75. boost::asio::io_service *Io_service, std::string Name, std::ostream & Out);
  76. CConnection
  77. (boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  78. std::string Name, std::ostream & Out); //use immediately after accepting connection into socket
  79. int write(const void * data, unsigned size);
  80. int read(void * data, unsigned size);
  81. int readLine(void * data, unsigned maxSize);
  82. ~CConnection(void);
  83. };
  84. template <typename T> CConnection & operator<<(CConnection &c, const T &data);
  85. template <typename T> CConnection & operator>>(CConnection &c, T &data);
  86. CConnection & operator<<(CConnection &c, const std::string &data)
  87. {
  88. boost::uint32_t length = data.size();
  89. c << length;
  90. c.write(data.c_str(),length);
  91. return c;
  92. }
  93. CConnection & operator>>(CConnection &c, std::string &data)
  94. {
  95. boost::uint32_t length;
  96. c >> length;
  97. data.resize(length);
  98. c.read((void*)data.c_str(),length);
  99. return c;
  100. }
  101. CConnection & operator<<(CConnection &c, const char * &data)
  102. {
  103. boost::uint32_t length = strlen(data);
  104. c << length;
  105. c.write(data,length);
  106. return c;
  107. }
  108. CConnection & operator>>(CConnection &c, char * &data)
  109. {
  110. boost::uint32_t length;
  111. c >> length;
  112. std::cout <<"Alokujemy " <<length << " bajtow."<<std::endl;
  113. data = new char[length];
  114. c.read(data,length);
  115. return c;
  116. }
  117. template <typename T> CConnection & operator<<(CConnection &c, std::vector<T> &data)
  118. {
  119. boost::uint32_t length = data.size();
  120. c << length;
  121. for(ui32 i=0;i<length;i++)
  122. c << data[i];
  123. return c;
  124. }
  125. template <typename T> CConnection & operator>>(CConnection &c, std::vector<T> &data)
  126. {
  127. boost::uint32_t length;
  128. c >> length;
  129. data.resize(length);
  130. for(ui32 i=0;i<length;i++)
  131. c >> data[i];
  132. return c;
  133. }
  134. //template <typename T> CConnection & operator<<(CConnection &c, std::set<T> &data)
  135. //{
  136. // boost::uint32_t length = data.size();
  137. // c << length;
  138. // for(std::set<T>::iterator i=data.begin();i!=data.end();i++)
  139. // c << *i;
  140. // return c;
  141. //}
  142. //template <typename T> CConnection & operator>>(CConnection &c, std::set<T> &data)
  143. //{
  144. // boost::uint32_t length;
  145. // c >> length;
  146. // data.resize(length);
  147. // T pom;
  148. // for(int i=0;i<length;i++)
  149. // {
  150. // c >> pom;
  151. // data.insert(pom);
  152. // }
  153. // return c;
  154. //}
  155. template <typename T> CConnection & operator<<(CConnection &c, const T &data)
  156. {
  157. c.write(&data,sizeof(data));
  158. return c;
  159. }
  160. template <typename T> CConnection & operator>>(CConnection &c, T &data)
  161. {
  162. c.read(&data,sizeof(data));
  163. return c;
  164. }
  165. template <typename T> CConnection & operator<(CConnection &c, std::vector<T> &data)
  166. {
  167. boost::uint32_t length = data.size();
  168. c << length;
  169. for(ui32 i=0;i<length;i++)
  170. data[i].serialize(c.send,version);
  171. return c;
  172. }
  173. template <typename T> CConnection & operator>(CConnection &c, std::vector<T> &data)
  174. {
  175. boost::uint32_t length;
  176. c >> length;
  177. data.resize(length);
  178. for(ui32 i=0;i<length;i++)
  179. data[i].serialize(c.rec,version);
  180. return c;
  181. }
  182. template <typename T> CConnection & operator<(CConnection &c, T &data)
  183. {
  184. data.serialize(c.send,version);
  185. return c;
  186. }
  187. template <typename T> CConnection & operator>(CConnection &c, T &data)
  188. {
  189. data.serialize(c.rec,version);
  190. return c;
  191. }