Connection.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. int i=0;
  57. while(pom != end)
  58. {
  59. std::cout << "\t" << i << ": " << (boost::asio::ip::tcp::endpoint&)*pom << std::endl;
  60. pom++;
  61. }
  62. i=0;
  63. while(endpoint_iterator != end)
  64. {
  65. std::cout << "Trying connection to " << (boost::asio::ip::tcp::endpoint&)*endpoint_iterator << " (" << i++ << ")" << std::endl;
  66. socket->connect(*endpoint_iterator, error);
  67. if(!error)
  68. {
  69. init();
  70. return;
  71. }
  72. else
  73. {
  74. std::cout << "Problem with connecting: " << std::endl << error << std::endl;
  75. }
  76. endpoint_iterator++;
  77. }
  78. //we shouldn't be here - error handling
  79. connerror1:
  80. std::cout << "Something went wrong... checking for error info" << std::endl;
  81. if(error)
  82. std::cout << error <<std::endl;
  83. else
  84. std::cout << "No error info. " << std::endl;
  85. delete io_service;
  86. //delete socket;
  87. throw std::string("Can't establish connection :(");
  88. }
  89. CConnection::CConnection(
  90. boost::asio::basic_stream_socket<boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  91. std::string Name,
  92. std::ostream & Out )
  93. :socket(Socket),io_service(&Socket->io_service()), out(Out), name(Name)//, send(this), rec(this)
  94. {
  95. init();
  96. }
  97. 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)
  98. : out(Out), name(Name)//, send(this), rec(this)
  99. {
  100. boost::system::error_code error = asio::error::host_not_found;
  101. socket = new tcp::socket(*io_service);
  102. acceptor->accept(*socket,error);
  103. if (error)
  104. {
  105. std::cout << "Error on accepting: " << std::endl << error << std::endl;
  106. delete socket;
  107. throw "Can't establish connection :(";
  108. }
  109. init();
  110. }
  111. int CConnection::write(const void * data, unsigned size)
  112. {
  113. LOG("Sending " << size << " byte(s) of data" <<std::endl);
  114. int ret;
  115. ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
  116. return ret;
  117. }
  118. int CConnection::read(void * data, unsigned size)
  119. {
  120. LOG("Receiving " << size << " byte(s) of data" <<std::endl);
  121. int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
  122. return ret;
  123. }
  124. CConnection::~CConnection(void)
  125. {
  126. close();
  127. delete io_service;
  128. delete wmx;
  129. delete rmx;
  130. }
  131. void CConnection::close()
  132. {
  133. if(socket)
  134. {
  135. socket->close();
  136. delete socket;
  137. socket = NULL;
  138. }
  139. }
  140. template <>
  141. void CConnection::saveSerializable<std::string>(const std::string &data)
  142. {
  143. *this << ui32(data.size());
  144. write(data.c_str(),data.size());
  145. }
  146. template <>
  147. void CConnection::loadSerializable<std::string>(std::string &data)
  148. {
  149. ui32 l;
  150. *this >> l;
  151. data.resize(l);
  152. read((void*)data.c_str(),l);
  153. }