2008-07-01 11:01:02 +03:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include "../global.h"
|
2008-07-03 18:03:32 +03:00
|
|
|
#include "../lib/Connection.h"
|
2008-07-09 20:22:28 +03:00
|
|
|
#include "zlib.h"
|
2008-08-04 12:05:52 +03:00
|
|
|
#ifndef __GNUC__
|
2008-07-25 20:28:28 +03:00
|
|
|
#include <tchar.h>
|
2008-08-04 12:05:52 +03:00
|
|
|
#endif
|
2008-07-25 20:28:28 +03:00
|
|
|
#include "CVCMIServer.h"
|
2008-07-09 20:22:28 +03:00
|
|
|
#include <boost/crc.hpp>
|
2008-09-12 11:51:46 +03:00
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
|
|
|
#include <boost/interprocess/shared_memory_object.hpp>
|
2008-07-09 20:22:28 +03:00
|
|
|
#include "../StartInfo.h"
|
2008-07-25 20:28:28 +03:00
|
|
|
#include "../map.h"
|
|
|
|
#include "../hch/CLodHandler.h"
|
2008-09-12 11:51:46 +03:00
|
|
|
#include "../lib/Interprocess.h"
|
2008-07-25 20:28:28 +03:00
|
|
|
#include "../lib/VCMI_Lib.h"
|
|
|
|
#include "CGameHandler.h"
|
2008-07-01 11:01:02 +03:00
|
|
|
std::string NAME = NAME_VER + std::string(" (server)");
|
2008-07-02 11:39:56 +03:00
|
|
|
using namespace boost;
|
|
|
|
using namespace boost::asio;
|
2008-07-03 18:03:32 +03:00
|
|
|
using namespace boost::asio::ip;
|
2008-09-12 11:51:46 +03:00
|
|
|
namespace intpr = boost::interprocess;
|
2008-08-04 12:05:52 +03:00
|
|
|
bool end2 = false;
|
2008-09-09 04:40:43 +03:00
|
|
|
int port = 3030;
|
2008-07-25 20:28:28 +03:00
|
|
|
|
2008-09-12 11:51:46 +03:00
|
|
|
void vaccept(tcp::acceptor *ac, tcp::socket *s, boost::system::error_code *error)
|
|
|
|
{
|
|
|
|
ac->accept(*s,*error);
|
|
|
|
}
|
|
|
|
|
2008-07-25 20:28:28 +03:00
|
|
|
CVCMIServer::CVCMIServer()
|
2008-09-09 04:40:43 +03:00
|
|
|
: io(new io_service()), acceptor(new tcp::acceptor(*io, tcp::endpoint(tcp::v4(), port)))
|
2008-07-02 11:39:56 +03:00
|
|
|
{
|
2008-09-07 06:38:37 +03:00
|
|
|
std::cout << "CVCMIServer created!" <<std::endl;
|
2008-07-25 20:28:28 +03:00
|
|
|
}
|
|
|
|
CVCMIServer::~CVCMIServer()
|
|
|
|
{
|
2008-07-27 20:07:37 +03:00
|
|
|
//delete io;
|
|
|
|
//delete acceptor;
|
2008-07-25 20:28:28 +03:00
|
|
|
}
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
void CVCMIServer::newGame(CConnection *c)
|
2008-07-25 20:28:28 +03:00
|
|
|
{
|
|
|
|
CGameHandler gh;
|
|
|
|
boost::system::error_code error;
|
|
|
|
StartInfo *si = new StartInfo;
|
|
|
|
ui8 clients;
|
2008-08-04 12:05:52 +03:00
|
|
|
*c >> clients; //how many clients should be connected - TODO: support more than one
|
|
|
|
*c >> *si; //get start options
|
2008-07-25 20:28:28 +03:00
|
|
|
int problem;
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
FILE *f;
|
|
|
|
problem = fopen_s(&f,si->mapname.c_str(),"r");
|
|
|
|
#else
|
|
|
|
FILE * f = fopen(si->mapname.c_str(),"r");
|
|
|
|
problem = !f;
|
|
|
|
#endif
|
|
|
|
if(problem)
|
2008-07-02 11:39:56 +03:00
|
|
|
{
|
2008-08-04 12:05:52 +03:00
|
|
|
*c << ui8(problem); //WRONG!
|
2008-07-25 20:28:28 +03:00
|
|
|
return;
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
else
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
fclose(f);
|
2008-08-04 12:05:52 +03:00
|
|
|
*c << ui8(0); //OK!
|
2008-07-02 11:39:56 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
gh.init(si,rand());
|
|
|
|
|
|
|
|
CConnection* cc; //tcp::socket * ss;
|
|
|
|
for(int i=0; i<clients; i++)
|
2008-07-02 11:39:56 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
if(!i)
|
2008-07-02 11:39:56 +03:00
|
|
|
{
|
2008-08-04 12:05:52 +03:00
|
|
|
cc=c;
|
2008-07-02 11:39:56 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
else
|
2008-07-02 11:39:56 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
tcp::socket * s = new tcp::socket(acceptor->io_service());
|
|
|
|
acceptor->accept(*s,error);
|
|
|
|
if(error) //retry
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
std::cout<<"Cannot establish connection - retrying..." << std::endl;
|
|
|
|
i--;
|
|
|
|
continue;
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
cc = new CConnection(s,NAME,std::cout);
|
|
|
|
}
|
|
|
|
gh.conns.insert(cc);
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
gh.run();
|
|
|
|
}
|
|
|
|
void CVCMIServer::start()
|
|
|
|
{
|
2008-09-12 11:51:46 +03:00
|
|
|
ServerReady *sr = NULL;
|
|
|
|
intpr::mapped_region *mr;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
intpr::shared_memory_object smo(intpr::open_only,"vcmi_memory",intpr::read_write);
|
|
|
|
mr = new intpr::mapped_region(smo,intpr::read_write);
|
|
|
|
sr = (ServerReady*)mr->get_address();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
intpr::shared_memory_object smo(intpr::create_only,"vcmi_memory",intpr::read_write);
|
|
|
|
smo.truncate(sizeof(ServerReady));
|
|
|
|
mr = new intpr::mapped_region(smo,intpr::read_write);
|
|
|
|
sr = new(mr->get_address())ServerReady();
|
|
|
|
}
|
|
|
|
|
2008-07-25 20:28:28 +03:00
|
|
|
boost::system::error_code error;
|
|
|
|
std::cout<<"Listening for connections at port " << acceptor->local_endpoint().port() << std::endl;
|
|
|
|
tcp::socket * s = new tcp::socket(acceptor->io_service());
|
2008-09-12 11:51:46 +03:00
|
|
|
boost::thread acc(boost::bind(vaccept,acceptor,s,&error));
|
|
|
|
sr->setToTrueAndNotify();
|
|
|
|
delete mr;
|
|
|
|
acc.join();
|
2008-07-25 20:28:28 +03:00
|
|
|
if (error)
|
2008-07-03 18:03:32 +03:00
|
|
|
{
|
2008-09-07 06:38:37 +03:00
|
|
|
std::cout<<"Got connection but there is an error " << std::endl << error;
|
2008-07-25 20:28:28 +03:00
|
|
|
return;
|
|
|
|
}
|
2008-09-07 06:38:37 +03:00
|
|
|
std::cout<<"We've accepted someone... " << std::endl;
|
2008-08-04 12:05:52 +03:00
|
|
|
CConnection *connection = new CConnection(s,NAME,std::cout);
|
2008-07-25 20:28:28 +03:00
|
|
|
std::cout<<"Got connection!" << std::endl;
|
2008-08-04 12:05:52 +03:00
|
|
|
while(!end2)
|
2008-07-25 20:28:28 +03:00
|
|
|
{
|
|
|
|
uint8_t mode;
|
2008-08-04 12:05:52 +03:00
|
|
|
*connection >> mode;
|
2008-07-25 20:28:28 +03:00
|
|
|
switch (mode)
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
case 0:
|
2008-08-04 12:05:52 +03:00
|
|
|
connection->socket->close();
|
2008-07-25 20:28:28 +03:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 1:
|
2008-08-04 12:05:52 +03:00
|
|
|
connection->socket->close();
|
2008-07-09 20:22:28 +03:00
|
|
|
return;
|
2008-07-25 20:28:28 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
newGame(connection);
|
|
|
|
break;
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
2008-07-02 11:39:56 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
}
|
2008-07-02 11:39:56 +03:00
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
#ifndef __GNUC__
|
2008-07-09 20:22:28 +03:00
|
|
|
int _tmain(int argc, _TCHAR* argv[])
|
2008-08-04 12:05:52 +03:00
|
|
|
#else
|
2008-09-10 00:10:24 +03:00
|
|
|
int main(int argc, char** argv)
|
2008-08-04 12:05:52 +03:00
|
|
|
#endif
|
2008-07-01 11:01:02 +03:00
|
|
|
{
|
2008-09-17 13:18:22 +03:00
|
|
|
logfile = new std::ofstream("VCMI_Server_log.txt");
|
|
|
|
console = new CConsoleHandler;
|
|
|
|
boost::thread t(boost::bind(&CConsoleHandler::run,::console));
|
2008-09-09 04:40:43 +03:00
|
|
|
if(argc > 1)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
port = _tstoi(argv[1]);
|
|
|
|
#else
|
|
|
|
port = _ttoi(argv[1]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
std::cout << "Port " << port << " will be used." << std::endl;
|
2008-07-25 20:28:28 +03:00
|
|
|
CLodHandler h3bmp;
|
2008-08-17 22:30:00 +03:00
|
|
|
h3bmp.init("Data" PATHSEPARATOR "H3bitmap.lod","Data");
|
2008-09-17 13:18:22 +03:00
|
|
|
initDLL(&h3bmp,console,logfile);
|
2008-07-25 20:28:28 +03:00
|
|
|
srand ( (unsigned int)time(NULL) );
|
|
|
|
try
|
|
|
|
{
|
|
|
|
io_service io_service;
|
|
|
|
CVCMIServer server;
|
2008-08-04 12:05:52 +03:00
|
|
|
while(!end2)
|
2008-09-12 11:51:46 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
server.start();
|
2008-09-12 11:51:46 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
io_service.run();
|
2008-07-27 20:07:37 +03:00
|
|
|
} HANDLE_EXCEPTION
|
2008-07-01 11:01:02 +03:00
|
|
|
return 0;
|
2008-07-02 11:39:56 +03:00
|
|
|
}
|