Connection.cpp 7.4 KB

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