Connection.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #define VCMI_DLL
  2. #pragma warning(disable:4355)
  3. #include "Connection.h"
  4. #include <boost/asio.hpp>
  5. #include <boost/thread.hpp>
  6. using namespace boost;
  7. using namespace boost::asio::ip;
  8. #define LOG(a) \
  9. if(logging)\
  10. out << a
  11. #if defined(__hppa__) || \
  12. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  13. (defined(__MIPS__) && defined(__MISPEB__)) || \
  14. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  15. defined(__sparc__)
  16. #define BIG_ENDIAN
  17. #else
  18. #define LIL_ENDIAN
  19. #endif
  20. void CConnection::init()
  21. {
  22. #ifdef LIL_ENDIAN
  23. myEndianess = true;
  24. #else
  25. myEndianess = false;
  26. #endif
  27. connected = true;
  28. std::string pom;
  29. //we got connection
  30. (*this) << std::string("Aiya!\n") << name << myEndianess; //identify ourselves
  31. (*this) >> pom >> pom >> contactEndianess;
  32. out << "Established connection with "<<pom<<std::endl;
  33. wmx = new boost::mutex;
  34. rmx = new boost::mutex;
  35. }
  36. CConnection::CConnection(std::string host, std::string port, std::string Name, std::ostream & Out)
  37. :io_service(new asio::io_service), name(Name), out(Out)//, send(this), rec(this)
  38. {
  39. boost::system::error_code error = asio::error::host_not_found;
  40. socket = new tcp::socket(*io_service);
  41. tcp::resolver resolver(*io_service);
  42. tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host,port),error);
  43. if(error)
  44. {
  45. std::cout << "Problem with resolving. " << std::endl << error <<std::endl;
  46. goto connerror1;
  47. }
  48. pom = endpoint_iterator;
  49. if(pom != end)
  50. std::cout<<"Found endpoints:" << std::endl;
  51. else
  52. {
  53. std::cout<< "Critical problem: No endpoints found!" << std::endl;
  54. goto connerror1;
  55. }
  56. while(pom != end)
  57. {
  58. std::cout << (boost::asio::ip::tcp::endpoint&)*pom << std::endl;
  59. pom++;
  60. }
  61. while(endpoint_iterator != end)
  62. {
  63. socket->connect(*endpoint_iterator, error);
  64. if(!error)
  65. {
  66. init();
  67. return;
  68. }
  69. else
  70. {
  71. std::cout << "Problem with connecting. " << std::endl;
  72. }
  73. }
  74. //we shouldn't be here - error handling
  75. connerror1:
  76. if(error)
  77. std::cout << error <<std::endl;
  78. else
  79. std::cout << "No error info. " << std::endl;
  80. delete io_service;
  81. delete socket;
  82. throw std::string("Can't establish connection :(");
  83. }
  84. CConnection::CConnection(
  85. boost::asio::basic_stream_socket<boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  86. std::string Name,
  87. std::ostream & Out )
  88. :socket(Socket),io_service(&Socket->io_service()), out(Out), name(Name)//, send(this), rec(this)
  89. {
  90. init();
  91. }
  92. CConnection::CConnection(boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > * acceptor, boost::asio::io_service *Io_service, std::string Name, std::ostream & Out)
  93. : out(Out), name(Name)//, send(this), rec(this)
  94. {
  95. boost::system::error_code error = asio::error::host_not_found;
  96. socket = new tcp::socket(*io_service);
  97. acceptor->accept(*socket,error);
  98. if (error){ delete socket; throw "Can't establish connection :("; }
  99. init();
  100. }
  101. int CConnection::write(const void * data, unsigned size)
  102. {
  103. LOG("Sending " << size << " byte(s) of data" <<std::endl);
  104. int ret;
  105. ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
  106. return ret;
  107. }
  108. int CConnection::read(void * data, unsigned size)
  109. {
  110. LOG("Receiving " << size << " byte(s) of data" <<std::endl);
  111. int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
  112. return ret;
  113. }
  114. CConnection::~CConnection(void)
  115. {
  116. close();
  117. delete io_service;
  118. delete wmx;
  119. delete rmx;
  120. }
  121. void CConnection::close()
  122. {
  123. if(socket)
  124. {
  125. socket->close();
  126. delete socket;
  127. socket = NULL;
  128. }
  129. }
  130. template <>
  131. void CConnection::saveSerializable<std::string>(const std::string &data)
  132. {
  133. *this << ui32(data.size());
  134. write(data.c_str(),data.size());
  135. }
  136. template <>
  137. void CConnection::loadSerializable<std::string>(std::string &data)
  138. {
  139. ui32 l;
  140. *this >> l;
  141. data.resize(l);
  142. read((void*)data.c_str(),l);
  143. }