Connection.cpp 6.2 KB

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