2
0

Connection.cpp 4.4 KB

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