2
0

Connection.cpp 5.3 KB

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