Connection.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #define VCMI_DLL
  2. #pragma warning(disable:4355)
  3. #include "Connection.h"
  4. #include <boost/asio.hpp>
  5. #include <boost/thread.hpp>
  6. using namespace boost;
  7. using namespace boost::asio::ip;
  8. #define LOG(a) \
  9. if(logging)\
  10. out << a
  11. #if defined(__hppa__) || \
  12. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  13. (defined(__MIPS__) && defined(__MISPEB__)) || \
  14. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  15. defined(__sparc__)
  16. #define BIG_ENDIAN
  17. #else
  18. #define LIL_ENDIAN
  19. #endif
  20. void CConnection::init()
  21. {
  22. #ifdef LIL_ENDIAN
  23. myEndianess = true;
  24. #else
  25. myEndianess = false;
  26. #endif
  27. connected = true;
  28. std::string pom;
  29. //we got connection
  30. (*this) << std::string("Aiya!\n") << name << myEndianess; //identify ourselves
  31. (*this) >> pom >> pom >> contactEndianess;
  32. out << "Established connection with "<<pom<<std::endl;
  33. wmx = new boost::mutex;
  34. rmx = new boost::mutex;
  35. }
  36. CConnection::CConnection(std::string host, std::string port, std::string Name, std::ostream & Out)
  37. :io_service(new asio::io_service), name(Name), out(Out)//, send(this), rec(this)
  38. {
  39. boost::system::error_code error = asio::error::host_not_found;
  40. socket = new tcp::socket(*io_service);
  41. tcp::resolver resolver(*io_service);
  42. tcp::resolver::iterator endpoint_iterator = resolver.resolve(tcp::resolver::query(host,port));
  43. socket->connect(*endpoint_iterator, error);
  44. if (error){ delete socket; throw "Can't establish connection :("; }
  45. init();
  46. }
  47. CConnection::CConnection(
  48. boost::asio::basic_stream_socket<boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
  49. std::string Name,
  50. std::ostream & Out )
  51. :socket(Socket),io_service(&Socket->io_service()), out(Out), name(Name)//, send(this), rec(this)
  52. {
  53. init();
  54. }
  55. CConnection::CConnection(boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > * acceptor, boost::asio::io_service *Io_service, std::string Name, std::ostream & Out)
  56. : out(Out), name(Name)//, send(this), rec(this)
  57. {
  58. boost::system::error_code error = asio::error::host_not_found;
  59. socket = new tcp::socket(*io_service);
  60. acceptor->accept(*socket,error);
  61. if (error){ delete socket; throw "Can't establish connection :("; }
  62. init();
  63. }
  64. int CConnection::write(const void * data, unsigned size)
  65. {
  66. LOG("Sending " << size << " byte(s) of data" <<std::endl);
  67. int ret;
  68. ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
  69. return ret;
  70. }
  71. int CConnection::read(void * data, unsigned size)
  72. {
  73. LOG("Receiving " << size << " byte(s) of data" <<std::endl);
  74. int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
  75. return ret;
  76. }
  77. CConnection::~CConnection(void)
  78. {
  79. close();
  80. delete io_service;
  81. delete wmx;
  82. delete rmx;
  83. }
  84. void CConnection::close()
  85. {
  86. if(socket)
  87. {
  88. socket->close();
  89. delete socket;
  90. socket = NULL;
  91. }
  92. }
  93. template <>
  94. void CConnection::saveSerializable<std::string>(const std::string &data)
  95. {
  96. *this << ui32(data.size());
  97. write(data.c_str(),data.size());
  98. }
  99. template <>
  100. void CConnection::loadSerializable<std::string>(std::string &data)
  101. {
  102. ui32 l;
  103. *this >> l;
  104. data.resize(l);
  105. read((void*)data.c_str(),l);
  106. }