Connection.cpp 7.2 KB

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