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"
|
2009-01-11 00:08:18 +02:00
|
|
|
#include "../hch/CArtHandler.h"
|
|
|
|
#include "../hch/CDefObjInfoHandler.h"
|
|
|
|
#include "../hch/CGeneralTextHandler.h"
|
|
|
|
#include "../hch/CHeroHandler.h"
|
|
|
|
#include "../hch/CTownHandler.h"
|
|
|
|
#include "../hch/CObjectHandler.h"
|
|
|
|
#include "../hch/CBuildingHandler.h"
|
|
|
|
#include "../hch/CSpellHandler.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"
|
2009-05-20 13:08:56 +03:00
|
|
|
#include "../lib/map.h"
|
2008-07-25 20:28:28 +03:00
|
|
|
#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"
|
2009-07-31 23:10:22 +03:00
|
|
|
std::string NAME_AFFIX = "server";
|
|
|
|
std::string NAME = NAME_VER + std::string(" (") + NAME_AFFIX + ')'; //application name
|
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
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CVCMIServer.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-05-24 01:57:39 +03:00
|
|
|
static void vaccept(tcp::acceptor *ac, tcp::socket *s, boost::system::error_code *error)
|
2008-09-12 11:51:46 +03:00
|
|
|
{
|
|
|
|
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-19 15:09:15 +03:00
|
|
|
tlog4 << "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());
|
2009-10-06 03:32:33 +03:00
|
|
|
c->setGS(gh.gs);
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
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-09-19 15:09:15 +03:00
|
|
|
tlog3<<"Cannot establish connection - retrying..." << std::endl;
|
2008-07-25 20:28:28 +03:00
|
|
|
i--;
|
|
|
|
continue;
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
2009-01-11 00:08:18 +02:00
|
|
|
cc = new CConnection(s,NAME);
|
2009-10-06 03:32:33 +03:00
|
|
|
cc->setGS(gh.gs);
|
2008-07-25 20:28:28 +03:00
|
|
|
}
|
|
|
|
gh.conns.insert(cc);
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
|
2009-01-11 00:08:18 +02:00
|
|
|
gh.run(false);
|
2008-07-25 20:28:28 +03:00
|
|
|
}
|
|
|
|
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);
|
2009-01-31 00:23:13 +02:00
|
|
|
smo.truncate(sizeof(ServerReady));
|
2008-09-12 11:51:46 +03:00
|
|
|
mr = new intpr::mapped_region(smo,intpr::read_write);
|
2009-05-01 17:37:25 +03:00
|
|
|
sr = reinterpret_cast<ServerReady*>(mr->get_address());
|
2008-09-12 11:51:46 +03:00
|
|
|
}
|
|
|
|
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;
|
2008-09-19 15:09:15 +03:00
|
|
|
tlog0<<"Listening for connections at port " << acceptor->local_endpoint().port() << std::endl;
|
2008-07-25 20:28:28 +03:00
|
|
|
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-19 15:09:15 +03:00
|
|
|
tlog2<<"Got connection but there is an error " << std::endl << error;
|
2008-07-25 20:28:28 +03:00
|
|
|
return;
|
|
|
|
}
|
2008-09-19 15:09:15 +03:00
|
|
|
tlog0<<"We've accepted someone... " << std::endl;
|
2009-01-11 00:08:18 +02:00
|
|
|
CConnection *connection = new CConnection(s,NAME);
|
2008-09-19 15:09:15 +03:00
|
|
|
tlog0<<"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;
|
2009-01-11 00:08:18 +02:00
|
|
|
case 3:
|
|
|
|
loadGame(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
|
|
|
|
2009-01-11 00:08:18 +02:00
|
|
|
void CVCMIServer::loadGame( CConnection *c )
|
|
|
|
{
|
|
|
|
std::string fname;
|
|
|
|
CGameHandler gh;
|
|
|
|
boost::system::error_code error;
|
|
|
|
ui8 clients;
|
|
|
|
*c >> clients >> fname; //how many clients should be connected - TODO: support more than one
|
|
|
|
|
|
|
|
{
|
2009-02-01 16:11:41 +02:00
|
|
|
ui32 ver;
|
2009-01-11 00:08:18 +02:00
|
|
|
char sig[8];
|
|
|
|
CMapHeader dum;
|
|
|
|
|
|
|
|
CLoadFile lf(fname + ".vlgm1");
|
2009-06-02 01:31:11 +03:00
|
|
|
lf >> sig >> dum >> *sig;
|
2009-01-11 00:08:18 +02:00
|
|
|
tlog0 <<"Reading save signature"<<std::endl;
|
|
|
|
|
|
|
|
lf >> *VLC;
|
|
|
|
tlog0 <<"Reading handlers"<<std::endl;
|
|
|
|
|
|
|
|
lf >> (gh.gs);
|
2009-10-06 03:32:33 +03:00
|
|
|
c->setGS(gh.gs);
|
2009-01-11 00:08:18 +02:00
|
|
|
tlog0 <<"Reading gamestate"<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
CLoadFile lf(fname + ".vsgm1");
|
|
|
|
lf >> gh;
|
|
|
|
}
|
|
|
|
|
|
|
|
*c << ui8(0);
|
|
|
|
|
|
|
|
CConnection* cc; //tcp::socket * ss;
|
|
|
|
for(int i=0; i<clients; i++)
|
|
|
|
{
|
|
|
|
if(!i)
|
|
|
|
{
|
|
|
|
cc=c;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tcp::socket * s = new tcp::socket(acceptor->io_service());
|
|
|
|
acceptor->accept(*s,error);
|
|
|
|
if(error) //retry
|
|
|
|
{
|
|
|
|
tlog3<<"Cannot establish connection - retrying..." << std::endl;
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cc = new CConnection(s,NAME);
|
2009-10-06 03:32:33 +03:00
|
|
|
cc->setGS(gh.gs);
|
2009-01-11 00:08:18 +02:00
|
|
|
}
|
|
|
|
gh.conns.insert(cc);
|
|
|
|
}
|
|
|
|
|
|
|
|
gh.run(true);
|
|
|
|
}
|
|
|
|
|
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;
|
2008-10-19 02:20:48 +03:00
|
|
|
//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
|
|
|
|
}
|
2008-09-19 15:09:15 +03:00
|
|
|
tlog0 << "Port " << port << " will be used." << std::endl;
|
2008-07-25 20:28:28 +03:00
|
|
|
CLodHandler h3bmp;
|
2009-10-04 05:02:45 +03:00
|
|
|
h3bmp.init(DATA_DIR "/Data/H3bitmap.lod", DATA_DIR "/Data");
|
2009-05-09 19:18:27 +03:00
|
|
|
initDLL(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();
|
2009-09-20 15:47:40 +03:00
|
|
|
}
|
|
|
|
catch(boost::system::system_error &e) //for boost errors just log, not crash - probably client shut down connection
|
|
|
|
{
|
|
|
|
tlog1 << e.what() << std::endl;
|
|
|
|
end2 = true;
|
|
|
|
}HANDLE_EXCEPTION
|
2008-07-01 11:01:02 +03:00
|
|
|
return 0;
|
2008-07-02 11:39:56 +03:00
|
|
|
}
|