2008-07-03 18:03:32 +03:00
|
|
|
#define VCMI_DLL
|
2008-07-09 20:22:28 +03:00
|
|
|
#pragma warning(disable:4355)
|
2008-07-03 18:03:32 +03:00
|
|
|
#include "Connection.h"
|
|
|
|
#include <boost/asio.hpp>
|
2008-07-26 16:57:32 +03:00
|
|
|
#include <boost/thread.hpp>
|
2008-11-16 03:06:15 +02:00
|
|
|
#include <fstream>
|
2008-07-03 18:03:32 +03:00
|
|
|
using namespace boost;
|
|
|
|
using namespace boost::asio::ip;
|
|
|
|
|
|
|
|
#define LOG(a) \
|
|
|
|
if(logging)\
|
|
|
|
out << a
|
|
|
|
#if defined(__hppa__) || \
|
|
|
|
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
|
|
|
|
(defined(__MIPS__) && defined(__MISPEB__)) || \
|
|
|
|
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
|
|
|
|
defined(__sparc__)
|
|
|
|
#define BIG_ENDIAN
|
|
|
|
#else
|
|
|
|
#define LIL_ENDIAN
|
|
|
|
#endif
|
|
|
|
|
2008-07-09 20:22:28 +03:00
|
|
|
void CConnection::init()
|
2008-07-03 18:03:32 +03:00
|
|
|
{
|
|
|
|
#ifdef LIL_ENDIAN
|
|
|
|
myEndianess = true;
|
|
|
|
#else
|
|
|
|
myEndianess = false;
|
|
|
|
#endif
|
2008-07-09 20:22:28 +03:00
|
|
|
connected = true;
|
|
|
|
std::string pom;
|
|
|
|
//we got connection
|
|
|
|
(*this) << std::string("Aiya!\n") << name << myEndianess; //identify ourselves
|
|
|
|
(*this) >> pom >> pom >> contactEndianess;
|
|
|
|
out << "Established connection with "<<pom<<std::endl;
|
2008-07-26 16:57:32 +03:00
|
|
|
wmx = new boost::mutex;
|
|
|
|
rmx = new boost::mutex;
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CConnection::CConnection(std::string host, std::string port, std::string Name, std::ostream & Out)
|
2008-07-25 20:28:28 +03:00
|
|
|
:io_service(new asio::io_service), name(Name), out(Out)//, send(this), rec(this)
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-09-10 00:10:24 +03:00
|
|
|
int i;
|
2008-08-04 12:05:52 +03:00
|
|
|
boost::system::error_code error = asio::error::host_not_found;
|
2008-07-03 18:03:32 +03:00
|
|
|
socket = new tcp::socket(*io_service);
|
|
|
|
tcp::resolver resolver(*io_service);
|
2008-09-07 06:38:37 +03:00
|
|
|
tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host,port),error);
|
|
|
|
if(error)
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "Problem with resolving: " << std::endl << error <<std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
goto connerror1;
|
|
|
|
}
|
|
|
|
pom = endpoint_iterator;
|
|
|
|
if(pom != end)
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog0<<"Found endpoints:" << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
else
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "Critical problem: No endpoints found!" << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
goto connerror1;
|
|
|
|
}
|
2008-09-10 00:10:24 +03:00
|
|
|
i=0;
|
2008-09-07 06:38:37 +03:00
|
|
|
while(pom != end)
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog0 << "\t" << i << ": " << (boost::asio::ip::tcp::endpoint&)*pom << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
pom++;
|
|
|
|
}
|
2008-09-09 10:05:02 +03:00
|
|
|
i=0;
|
2008-09-07 06:38:37 +03:00
|
|
|
while(endpoint_iterator != end)
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog0 << "Trying connection to " << (boost::asio::ip::tcp::endpoint&)*endpoint_iterator << " (" << i++ << ")" << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
socket->connect(*endpoint_iterator, error);
|
|
|
|
if(!error)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "Problem with connecting: " << std::endl << error << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
}
|
2008-09-09 10:05:02 +03:00
|
|
|
endpoint_iterator++;
|
2008-09-07 06:38:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//we shouldn't be here - error handling
|
|
|
|
connerror1:
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "Something went wrong... checking for error info" << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
if(error)
|
2008-09-19 15:09:15 +03:00
|
|
|
tlog1 << error <<std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
else
|
2008-09-19 11:16:19 +03:00
|
|
|
tlog1 << "No error info. " << std::endl;
|
2008-09-07 06:38:37 +03:00
|
|
|
delete io_service;
|
2008-09-09 10:05:02 +03:00
|
|
|
//delete socket;
|
2008-09-07 06:38:37 +03:00
|
|
|
throw std::string("Can't establish connection :(");
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
|
|
|
CConnection::CConnection(
|
|
|
|
boost::asio::basic_stream_socket<boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
|
|
|
|
std::string Name,
|
|
|
|
std::ostream & Out )
|
2008-07-25 20:28:28 +03:00
|
|
|
:socket(Socket),io_service(&Socket->io_service()), out(Out), name(Name)//, send(this), rec(this)
|
2008-07-03 18:03:32 +03:00
|
|
|
{
|
2008-07-09 20:22:28 +03:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
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)
|
2008-07-25 20:28:28 +03:00
|
|
|
: out(Out), name(Name)//, send(this), rec(this)
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-08-04 12:05:52 +03:00
|
|
|
boost::system::error_code error = asio::error::host_not_found;
|
2008-07-09 20:22:28 +03:00
|
|
|
socket = new tcp::socket(*io_service);
|
|
|
|
acceptor->accept(*socket,error);
|
2008-09-09 10:05:02 +03:00
|
|
|
if (error)
|
|
|
|
{
|
2008-09-19 15:09:15 +03:00
|
|
|
tlog1 << "Error on accepting: " << std::endl << error << std::endl;
|
2008-09-09 10:05:02 +03:00
|
|
|
delete socket;
|
|
|
|
throw "Can't establish connection :(";
|
|
|
|
}
|
2008-07-09 20:22:28 +03:00
|
|
|
init();
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
|
|
|
int CConnection::write(const void * data, unsigned size)
|
|
|
|
{
|
2008-09-17 13:18:22 +03:00
|
|
|
//LOG("Sending " << size << " byte(s) of data" <<std::endl);
|
2008-07-03 18:03:32 +03:00
|
|
|
int ret;
|
|
|
|
ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
int CConnection::read(void * data, unsigned size)
|
|
|
|
{
|
2008-09-17 13:18:22 +03:00
|
|
|
//LOG("Receiving " << size << " byte(s) of data" <<std::endl);
|
2008-07-03 18:03:32 +03:00
|
|
|
int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CConnection::~CConnection(void)
|
|
|
|
{
|
2008-08-25 13:25:16 +03:00
|
|
|
close();
|
2008-07-25 20:28:28 +03:00
|
|
|
delete io_service;
|
2008-07-26 16:57:32 +03:00
|
|
|
delete wmx;
|
|
|
|
delete rmx;
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
2008-08-04 12:05:52 +03:00
|
|
|
|
2008-08-25 13:25:16 +03:00
|
|
|
void CConnection::close()
|
|
|
|
{
|
|
|
|
if(socket)
|
|
|
|
{
|
|
|
|
socket->close();
|
|
|
|
delete socket;
|
|
|
|
socket = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-11-16 03:06:15 +02:00
|
|
|
|
|
|
|
CSaveFile::CSaveFile( const std::string &fname )
|
|
|
|
:sfile(new std::ofstream(fname.c_str()))
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
2008-11-16 03:06:15 +02:00
|
|
|
if(!(*sfile))
|
|
|
|
{
|
|
|
|
tlog1 << "Error: cannot open to write " << fname << std::endl;
|
|
|
|
sfile = NULL;
|
|
|
|
}
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
|
|
|
|
2008-11-16 03:06:15 +02:00
|
|
|
CSaveFile::~CSaveFile()
|
2008-08-04 12:05:52 +03:00
|
|
|
{
|
2008-11-16 03:06:15 +02:00
|
|
|
delete sfile;
|
2008-08-04 12:05:52 +03:00
|
|
|
}
|
2008-11-16 03:06:15 +02:00
|
|
|
|
|
|
|
int CSaveFile::write( const void * data, unsigned size )
|
|
|
|
{
|
|
|
|
sfile->write((char *)data,size);
|
|
|
|
return size;
|
|
|
|
}
|