1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/server/CVCMIServer.cpp

273 lines
6.1 KiB
C++
Raw Normal View History

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include "../global.h"
2008-07-03 18:03:32 +03:00
#include "../lib/Connection.h"
#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"
#ifndef __GNUC__
#include <tchar.h>
#endif
#include "CVCMIServer.h"
2008-07-09 20:22:28 +03:00
#include <boost/crc.hpp>
#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"
#include "../hch/CLodHandler.h"
#include "../lib/Interprocess.h"
#include "../lib/VCMI_Lib.h"
#include "CGameHandler.h"
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;
namespace intpr = boost::interprocess;
bool end2 = false;
int port = 3030;
/*
* 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
*
*/
static void vaccept(tcp::acceptor *ac, tcp::socket *s, boost::system::error_code *error)
{
ac->accept(*s,*error);
}
CVCMIServer::CVCMIServer()
: io(new io_service()), acceptor(new tcp::acceptor(*io, tcp::endpoint(tcp::v4(), port)))
2008-07-02 11:39:56 +03:00
{
tlog4 << "CVCMIServer created!" <<std::endl;
}
CVCMIServer::~CVCMIServer()
{
//delete io;
//delete acceptor;
}
void CVCMIServer::newGame(CConnection *c)
{
CGameHandler gh;
boost::system::error_code error;
StartInfo *si = new StartInfo;
ui8 clients;
*c >> clients; //how many clients should be connected - TODO: support more than one
*c >> *si; //get start options
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
{
*c << ui8(problem); //WRONG!
return;
2008-07-09 20:22:28 +03:00
}
else
2008-07-09 20:22:28 +03:00
{
fclose(f);
*c << ui8(0); //OK!
2008-07-02 11:39:56 +03:00
}
gh.init(si,rand());
c->setGS(gh.gs);
CConnection* cc; //tcp::socket * ss;
for(int i=0; i<clients; i++)
2008-07-02 11:39:56 +03:00
{
if(!i)
2008-07-02 11:39:56 +03:00
{
cc=c;
2008-07-02 11:39:56 +03:00
}
else
2008-07-02 11:39:56 +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
{
tlog3<<"Cannot establish connection - retrying..." << std::endl;
i--;
continue;
2008-07-09 20:22:28 +03:00
}
cc = new CConnection(s,NAME);
cc->setGS(gh.gs);
}
gh.conns.insert(cc);
2008-07-03 18:03:32 +03:00
}
gh.run(false);
}
void CVCMIServer::start()
{
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));
mr = new intpr::mapped_region(smo,intpr::read_write);
2009-05-01 17:37:25 +03:00
sr = reinterpret_cast<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();
}
boost::system::error_code error;
tlog0<<"Listening for connections at port " << acceptor->local_endpoint().port() << std::endl;
tcp::socket * s = new tcp::socket(acceptor->io_service());
boost::thread acc(boost::bind(vaccept,acceptor,s,&error));
sr->setToTrueAndNotify();
delete mr;
acc.join();
if (error)
2008-07-03 18:03:32 +03:00
{
tlog2<<"Got connection but there is an error " << std::endl << error;
return;
}
tlog0<<"We've accepted someone... " << std::endl;
CConnection *connection = new CConnection(s,NAME);
tlog0<<"Got connection!" << std::endl;
while(!end2)
{
uint8_t mode;
*connection >> mode;
switch (mode)
2008-07-09 20:22:28 +03:00
{
case 0:
connection->socket->close();
exit(0);
break;
case 1:
connection->socket->close();
2008-07-09 20:22:28 +03:00
return;
break;
case 2:
newGame(connection);
break;
case 3:
loadGame(connection);
break;
2008-07-09 20:22:28 +03:00
}
2008-07-02 11:39:56 +03:00
}
}
2008-07-02 11:39:56 +03: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
{
ui32 ver;
char sig[8];
CMapHeader dum;
CLoadFile lf(fname + ".vlgm1");
lf >> sig >> dum >> *sig;
tlog0 <<"Reading save signature"<<std::endl;
lf >> *VLC;
tlog0 <<"Reading handlers"<<std::endl;
lf >> (gh.gs);
c->setGS(gh.gs);
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);
cc->setGS(gh.gs);
}
gh.conns.insert(cc);
}
gh.run(true);
}
#ifndef __GNUC__
2008-07-09 20:22:28 +03:00
int _tmain(int argc, _TCHAR* argv[])
#else
2008-09-10 00:10:24 +03:00
int main(int argc, char** argv)
#endif
{
logfile = new std::ofstream("VCMI_Server_log.txt");
console = new CConsoleHandler;
//boost::thread t(boost::bind(&CConsoleHandler::run,::console));
if(argc > 1)
{
#ifdef _MSC_VER
port = _tstoi(argv[1]);
#else
port = _ttoi(argv[1]);
#endif
}
tlog0 << "Port " << port << " will be used." << std::endl;
CLodHandler h3bmp;
h3bmp.init(DATA_DIR "/Data/H3bitmap.lod", DATA_DIR "/Data");
initDLL(console,logfile);
srand ( (unsigned int)time(NULL) );
try
{
io_service io_service;
CVCMIServer server;
while(!end2)
{
server.start();
}
io_service.run();
}
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
return 0;
2008-07-02 11:39:56 +03:00
}