Connection.cpp 8.1 KB

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