Connection.cpp 4.4 KB

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