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

143 lines
2.9 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"
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/serialization/split_member.hpp>
#include "../StartInfo.h"
#include "../map.h"
#include "../hch/CLodHandler.h"
#include "../lib/VCMI_Lib.h"
#include "CGameHandler.h"
std::string NAME = NAME_VER + std::string(" (server)");
2008-07-02 11:39:56 +03:00
using boost::asio::ip::tcp;
using namespace boost;
using namespace boost::asio;
2008-07-03 18:03:32 +03:00
using namespace boost::asio::ip;
bool end2 = false;
CVCMIServer::CVCMIServer()
: io(new io_service()), acceptor(new tcp::acceptor(*io, tcp::endpoint(tcp::v4(), 3030)))
2008-07-02 11:39:56 +03:00
{
}
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());
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
{
std::cout<<"Cannot establish connection - retrying..." << std::endl;
i--;
continue;
2008-07-09 20:22:28 +03:00
}
cc = new CConnection(s,NAME,std::cout);
}
gh.conns.insert(cc);
2008-07-03 18:03:32 +03:00
}
gh.run();
}
void CVCMIServer::start()
{
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());
acceptor->accept(*s,error);
if (error)
2008-07-03 18:03:32 +03:00
{
std::cout<<"Got connection but there is an error " << std::endl;
return;
}
CConnection *connection = new CConnection(s,NAME,std::cout);
std::cout<<"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;
2008-07-09 20:22:28 +03:00
}
2008-07-02 11:39:56 +03:00
}
}
2008-07-02 11:39:56 +03:00
#ifndef __GNUC__
2008-07-09 20:22:28 +03:00
int _tmain(int argc, _TCHAR* argv[])
#else
int main(int argc, _TCHAR* argv[])
#endif
{
CLodHandler h3bmp;
2008-08-17 22:30:00 +03:00
h3bmp.init("Data" PATHSEPARATOR "H3bitmap.lod","Data");
initDLL(&h3bmp);
srand ( (unsigned int)time(NULL) );
try
{
io_service io_service;
CVCMIServer server;
while(!end2)
server.start();
io_service.run();
} HANDLE_EXCEPTION
return 0;
2008-07-02 11:39:56 +03:00
}