Connection.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. registerTypes(static_cast<CISer<CConnection>&>(*this));
  26. registerTypes(static_cast<COSer<CConnection>&>(*this));
  27. #ifdef LIL_ENDIAN
  28. myEndianess = true;
  29. #else
  30. myEndianess = false;
  31. #endif
  32. connected = true;
  33. std::string pom;
  34. //we got connection
  35. (*this) << std::string("Aiya!\n") << name << myEndianess; //identify ourselves
  36. (*this) >> pom >> pom >> contactEndianess;
  37. tlog0 << "Established connection with "<<pom<<std::endl;
  38. wmx = new boost::mutex;
  39. rmx = new boost::mutex;
  40. }
  41. CConnection::CConnection(std::string host, std::string port, std::string Name)
  42. :io_service(new asio::io_service), name(Name)
  43. {
  44. int i;
  45. boost::system::error_code error = asio::error::host_not_found;
  46. socket = new tcp::socket(*io_service);
  47. tcp::resolver resolver(*io_service);
  48. tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host,port),error);
  49. if(error)
  50. {
  51. tlog1 << "Problem with resolving: " << std::endl << error <<std::endl;
  52. goto connerror1;
  53. }
  54. pom = endpoint_iterator;
  55. if(pom != end)
  56. tlog0<<"Found endpoints:" << std::endl;
  57. else
  58. {
  59. tlog1 << "Critical problem: No endpoints found!" << std::endl;
  60. goto connerror1;
  61. }
  62. i=0;
  63. while(pom != end)
  64. {
  65. tlog0 << "\t" << i << ": " << (boost::asio::ip::tcp::endpoint&)*pom << std::endl;
  66. pom++;
  67. }
  68. i=0;
  69. while(endpoint_iterator != end)
  70. {
  71. tlog0 << "Trying connection to " << (boost::asio::ip::tcp::endpoint&)*endpoint_iterator << " (" << i++ << ")" << std::endl;
  72. socket->connect(*endpoint_iterator, error);
  73. if(!error)
  74. {
  75. init();
  76. return;
  77. }
  78. else
  79. {
  80. tlog1 << "Problem with connecting: " << std::endl << error << std::endl;
  81. }
  82. endpoint_iterator++;
  83. }
  84. //we shouldn't be here - error handling
  85. connerror1:
  86. tlog1 << "Something went wrong... checking for error info" << std::endl;
  87. if(error)
  88. tlog1 << error <<std::endl;
  89. else
  90. tlog1 << "No error info. " << std::endl;
  91. delete io_service;
  92. //delete socket;
  93. throw std::string("Can't establish connection :(");
  94. }
  95. CConnection::CConnection(
  96. boost::asio::basic_stream_socket<boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  97. std::string Name )
  98. :socket(Socket),io_service(&Socket->io_service()), name(Name)//, send(this), rec(this)
  99. {
  100. init();
  101. }
  102. CConnection::CConnection(boost::asio::basic_socket_acceptor<boost::asio::ip::tcp,boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > * acceptor,
  103. boost::asio::io_service *Io_service, std::string Name)
  104. : name(Name)//, send(this), rec(this)
  105. {
  106. boost::system::error_code error = asio::error::host_not_found;
  107. socket = new tcp::socket(*io_service);
  108. acceptor->accept(*socket,error);
  109. if (error)
  110. {
  111. tlog1 << "Error on accepting: " << std::endl << error << std::endl;
  112. delete socket;
  113. throw "Can't establish connection :(";
  114. }
  115. init();
  116. }
  117. int CConnection::write(const void * data, unsigned size)
  118. {
  119. //LOG("Sending " << size << " byte(s) of data" <<std::endl);
  120. int ret;
  121. ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
  122. return ret;
  123. }
  124. int CConnection::read(void * data, unsigned size)
  125. {
  126. //LOG("Receiving " << size << " byte(s) of data" <<std::endl);
  127. int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
  128. return ret;
  129. }
  130. CConnection::~CConnection(void)
  131. {
  132. close();
  133. delete io_service;
  134. delete wmx;
  135. delete rmx;
  136. }
  137. template<class T>
  138. CConnection & CConnection::operator&(const T &t) {
  139. throw new std::exception();
  140. //XXX this is temporaly ? solution to fix gcc (4.3.3, other?) compilation
  141. // problem for more details contact [email protected] or [email protected]
  142. // do not remove this exception it shoudnt be called
  143. return *this;
  144. }
  145. void CConnection::close()
  146. {
  147. if(socket)
  148. {
  149. socket->close();
  150. delete socket;
  151. socket = NULL;
  152. }
  153. }
  154. CSaveFile::CSaveFile( const std::string &fname )
  155. :sfile(new std::ofstream(fname.c_str(),std::ios::binary))
  156. {
  157. registerTypes(*this);
  158. if(!(*sfile))
  159. {
  160. tlog1 << "Error: cannot open to write " << fname << std::endl;
  161. sfile = NULL;
  162. }
  163. }
  164. CSaveFile::~CSaveFile()
  165. {
  166. delete sfile;
  167. }
  168. int CSaveFile::write( const void * data, unsigned size )
  169. {
  170. sfile->write((char *)data,size);
  171. return size;
  172. }
  173. CLoadFile::CLoadFile( const std::string &fname )
  174. :sfile(new std::ifstream(fname.c_str(),std::ios::binary))
  175. {
  176. registerTypes(*this);
  177. if(!(*sfile))
  178. {
  179. tlog1 << "Error: cannot open to read " << fname << std::endl;
  180. sfile = NULL;
  181. }
  182. }
  183. CLoadFile::~CLoadFile()
  184. {
  185. delete sfile;
  186. }
  187. int CLoadFile::read( const void * data, unsigned size )
  188. {
  189. sfile->read((char *)data,size);
  190. return size;
  191. }
  192. CTypeList::CTypeList()
  193. {
  194. registerTypes(*this);
  195. }
  196. ui16 CTypeList::registerType( const std::type_info *type )
  197. {
  198. TTypeMap::const_iterator i = types.find(type);
  199. if(i != types.end())
  200. return i->second; //type found, return ID
  201. //type not found - add it to the list and return given ID
  202. ui16 id = types.size() + 1;
  203. types.insert(std::make_pair(type,id));
  204. return id;
  205. }
  206. ui16 CTypeList::getTypeID( const std::type_info *type )
  207. {
  208. TTypeMap::const_iterator i = types.find(type);
  209. if(i != types.end())
  210. return i->second;
  211. else
  212. return 0;
  213. }