Connection.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. CTypeList typeList;
  10. #define LOG(a) \
  11. if(logging)\
  12. out << a
  13. #if defined(__hppa__) || \
  14. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  15. (defined(__MIPS__) && defined(__MISPEB__)) || \
  16. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  17. defined(__sparc__)
  18. #define BIG_ENDIAN
  19. #else
  20. #define LIL_ENDIAN
  21. #endif
  22. void CConnection::init()
  23. {
  24. #ifdef LIL_ENDIAN
  25. myEndianess = true;
  26. #else
  27. myEndianess = false;
  28. #endif
  29. connected = true;
  30. std::string pom;
  31. //we got connection
  32. (*this) << std::string("Aiya!\n") << name << myEndianess; //identify ourselves
  33. (*this) >> pom >> pom >> contactEndianess;
  34. tlog0 << "Established connection with "<<pom<<std::endl;
  35. wmx = new boost::mutex;
  36. rmx = new boost::mutex;
  37. }
  38. CConnection::CConnection(std::string host, std::string port, std::string Name)
  39. :io_service(new asio::io_service), name(Name)
  40. {
  41. int i;
  42. boost::system::error_code error = asio::error::host_not_found;
  43. socket = new tcp::socket(*io_service);
  44. tcp::resolver resolver(*io_service);
  45. tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host,port),error);
  46. if(error)
  47. {
  48. tlog1 << "Problem with resolving: " << std::endl << error <<std::endl;
  49. goto connerror1;
  50. }
  51. pom = endpoint_iterator;
  52. if(pom != end)
  53. tlog0<<"Found endpoints:" << std::endl;
  54. else
  55. {
  56. tlog1 << "Critical problem: No endpoints found!" << std::endl;
  57. goto connerror1;
  58. }
  59. i=0;
  60. while(pom != end)
  61. {
  62. tlog0 << "\t" << i << ": " << (boost::asio::ip::tcp::endpoint&)*pom << std::endl;
  63. pom++;
  64. }
  65. i=0;
  66. while(endpoint_iterator != end)
  67. {
  68. tlog0 << "Trying connection to " << (boost::asio::ip::tcp::endpoint&)*endpoint_iterator << " (" << i++ << ")" << std::endl;
  69. socket->connect(*endpoint_iterator, error);
  70. if(!error)
  71. {
  72. init();
  73. return;
  74. }
  75. else
  76. {
  77. tlog1 << "Problem with connecting: " << std::endl << error << std::endl;
  78. }
  79. endpoint_iterator++;
  80. }
  81. //we shouldn't be here - error handling
  82. connerror1:
  83. tlog1 << "Something went wrong... checking for error info" << std::endl;
  84. if(error)
  85. tlog1 << error <<std::endl;
  86. else
  87. tlog1 << "No error info. " << std::endl;
  88. delete io_service;
  89. //delete socket;
  90. throw std::string("Can't establish connection :(");
  91. }
  92. CConnection::CConnection(
  93. boost::asio::basic_stream_socket<boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  94. std::string Name )
  95. :socket(Socket),io_service(&Socket->io_service()), 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,
  100. boost::asio::io_service *Io_service, std::string Name)
  101. : name(Name)//, send(this), rec(this)
  102. {
  103. boost::system::error_code error = asio::error::host_not_found;
  104. socket = new tcp::socket(*io_service);
  105. acceptor->accept(*socket,error);
  106. if (error)
  107. {
  108. tlog1 << "Error on accepting: " << std::endl << error << std::endl;
  109. delete socket;
  110. throw "Can't establish connection :(";
  111. }
  112. init();
  113. }
  114. int CConnection::write(const void * data, unsigned size)
  115. {
  116. //LOG("Sending " << size << " byte(s) of data" <<std::endl);
  117. int ret;
  118. ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
  119. return ret;
  120. }
  121. int CConnection::read(void * data, unsigned size)
  122. {
  123. //LOG("Receiving " << size << " byte(s) of data" <<std::endl);
  124. int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
  125. return ret;
  126. }
  127. CConnection::~CConnection(void)
  128. {
  129. close();
  130. delete io_service;
  131. delete wmx;
  132. delete rmx;
  133. }
  134. void CConnection::close()
  135. {
  136. if(socket)
  137. {
  138. socket->close();
  139. delete socket;
  140. socket = NULL;
  141. }
  142. }
  143. CSaveFile::CSaveFile( const std::string &fname )
  144. :sfile(new std::ofstream(fname.c_str(),std::ios::binary))
  145. {
  146. registerTypes(*this);
  147. if(!(*sfile))
  148. {
  149. tlog1 << "Error: cannot open to write " << fname << std::endl;
  150. sfile = NULL;
  151. }
  152. }
  153. CSaveFile::~CSaveFile()
  154. {
  155. delete sfile;
  156. }
  157. int CSaveFile::write( const void * data, unsigned size )
  158. {
  159. sfile->write((char *)data,size);
  160. return size;
  161. }
  162. CLoadFile::CLoadFile( const std::string &fname )
  163. :sfile(new std::ifstream(fname.c_str(),std::ios::binary))
  164. {
  165. registerTypes(*this);
  166. if(!(*sfile))
  167. {
  168. tlog1 << "Error: cannot open to read " << fname << std::endl;
  169. sfile = NULL;
  170. }
  171. }
  172. CLoadFile::~CLoadFile()
  173. {
  174. delete sfile;
  175. }
  176. int CLoadFile::read( const void * data, unsigned size )
  177. {
  178. sfile->read((char *)data,size);
  179. return size;
  180. }
  181. CTypeList::CTypeList()
  182. {
  183. }
  184. ui16 CTypeList::registerType( const type_info *type )
  185. {
  186. std::map<const type_info *,ui16>::const_iterator i = types.find(type);
  187. if(i != types.end())
  188. return i->second;
  189. ui16 id = types.size() + 1;
  190. types[type] = id;
  191. return id;
  192. }
  193. ui16 CTypeList::getTypeID( const type_info *type )
  194. {
  195. if(vstd::contains(types,type))
  196. return types[type];
  197. else
  198. return 0;
  199. }