1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Experimantal parts of netcode

This commit is contained in:
Michał W. Urbańczyk 2008-07-03 15:03:32 +00:00
parent 1b73678b7c
commit 9422b0d9ca
10 changed files with 196 additions and 72 deletions

View File

@ -262,7 +262,6 @@ void CTerrainRect::deactivate()
};
void CTerrainRect::clickLeft(tribool down)
{
LOGE("Left mouse button down2");
if ((down==false) || indeterminate(down))
return;
if (LOCPLINT->adventureInt->selection.type != HEROI_TYPE)

32
CMT.cpp
View File

@ -42,11 +42,10 @@
#include "CLua.h"
#include "CAdvmapInterface.h"
#include "client/Graphics.h"
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include "lib/Connection.h"
std::string NAME = NAME_VER + std::string(" (client)");
DLL_EXPORT void initDLL(CLodHandler *b);
using boost::asio::ip::tcp;
SDL_Surface * screen, * screen2;
extern SDL_Surface * CSDL_Ext::std32bppSurface;
TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
@ -61,30 +60,13 @@ void handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * sc
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread servthr(boost::bind(system,"VCMI_server.exe"));
/*
boost::asio::io_service io_service;
boost::system::error_code error = boost::asio::error::host_not_found;
tcp::socket socket(io_service);
tcp::resolver resolver(io_service);
tcp::resolver::query query("127.0.0.1", "3030");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(tcp::resolver::query("127.0.0.1", "3030"));
socket.connect(*endpoint_iterator, error);
boost::array<char, 128> buf;
size_t len = socket.read_some(boost::asio::buffer(buf), error);
if (error == boost::asio::error::eof)
; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
std::cout.write(buf.data(), len);
len = socket.read_some(boost::asio::buffer(buf), error);
std::cout.write(buf.data(), len);*/
//boost::thread servthr(boost::bind(system,"VCMI_server.exe")); //runs server executable;
//TODO: add versions for other platforms
CConnection c("localhost","3030",NAME,std::cout);
int r;
c >> r;
std::cout << NAME << std::endl;
srand ( time(NULL) );
CPG=NULL;
atexit(SDL_Quit);

View File

@ -1756,7 +1756,6 @@ void CPlayerInterface::handleEvent(SDL_Event *sEvent)
else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
{
LOGE("Left mouse button down1");
for(int i=0; i<lclickable.size();i++)
{
if (isItIn(&lclickable[i]->pos,sEvent->motion.x,sEvent->motion.y))

View File

@ -17,7 +17,7 @@
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
OutputDirectory="$(SolutionDir)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"

View File

@ -7,9 +7,10 @@
#include <iostream>
#include "int3.h"
#define THC
#else
#define THC //
#endif
#define NAME_VER ("VCMI \"Altanatse\" 0.7")
enum Ecolor {RED, BLUE, TAN, GREEN, ORANGE, PURPLE, TEAL, PINK}; //player's colors
enum EterrainType {border=-1, dirt, sand, grass, snow, swamp, rough, subterranean, lava, water, rock};
enum Eriver {noRiver=0, clearRiver, icyRiver, muddyRiver, lavaRiver};
@ -31,18 +32,6 @@ extern CGameInfo* CGI;
#define HEROI_TYPE (0)
#define TOWNI_TYPE (1)
//#define LOGUJ
#ifdef LOGUJ
#define LOG(x) std::cout<<x;
#define LOGE(x) std::cout<<x<<std::endl;
#else
#define LOG(x) ;
#define LOGE(x) ;
#endif
#define NAME_VER ("VCMI \"Altanatse\" 0.7")
const int F_NUMBER = 9; //factions (town types) quantity
const int PLAYER_LIMIT = 8; //player limit per map
const int HEROES_PER_TYPE=8; //amount of heroes of each type

72
lib/Connection.cpp Normal file
View File

@ -0,0 +1,72 @@
#define VCMI_DLL
#include "Connection.h"
#include <boost/asio.hpp>
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
CConnection::CConnection(std::string host, std::string port, std::string Name, std::ostream & Out)
:io_service(new asio::io_service), name(Name), out(Out)
{
#ifdef LIL_ENDIAN
myEndianess = true;
#else
myEndianess = false;
#endif
system::error_code error = asio::error::host_not_found;
socket = new tcp::socket(*io_service);
tcp::resolver resolver(*io_service);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(tcp::resolver::query(host,port));
socket->connect(*endpoint_iterator, error);
if(error)
{
connected = false;
return;
}
std::string pom;
//we got connection
(*this) << std::string("Aiya!\n") << name << myEndianess; //identify ourselves
(*this) >> pom >> pom >> contactEndianess;
}
CConnection::CConnection(
boost::asio::basic_stream_socket<boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
boost::asio::io_service *Io_service,
std::string Name,
std::ostream & Out )
:socket(Socket),io_service(Io_service), out(Out), name(Name)
{
std::string pom;
//we start with just connected socket
(*this) << std::string("Aiya!\n") << name << myEndianess; //identify ourselves
(*this) >> pom >> pom >> contactEndianess;
}
int CConnection::write(const void * data, unsigned size)
{
LOG("wysylam dane o rozmiarze " << size << std::endl);
int ret;
ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
return ret;
}
int CConnection::read(void * data, unsigned size)
{
int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
return ret;
}
CConnection::~CConnection(void)
{
delete io_service;
delete socket;
}

84
lib/Connection.h Normal file
View File

@ -0,0 +1,84 @@
#pragma once
#include "../global.h"
#include <boost/cstdint.hpp>
#include <string>
namespace boost
{
namespace asio
{
namespace ip
{
class tcp;
}
class io_service;
template <typename Protocol> class stream_socket_service;
template <typename Protocol,
typename StreamSocketService>
class basic_stream_socket;
}
};
class DLL_EXPORT CConnection
{
std::ostream &out;
CConnection(void);
public:
boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * socket;
bool logging;
bool connected;
bool myEndianess, contactEndianess; //true if little endian, if ednianess is different we'll have to revert recieved multi-byte vars
boost::asio::io_service *io_service;
std::string name; //who uses this connection
CConnection(std::string host, std::string port, std::string Name, std::ostream & Out);
CConnection(boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket, boost::asio::io_service *Io_service, std::string Name, std::ostream & Out); //use immediately after accepting connection into socket
int write(const void * data, unsigned size);
int read(void * data, unsigned size);
int readLine(void * data, unsigned maxSize);
~CConnection(void);
};
template <typename T> CConnection & operator<<(CConnection &c, const T &data);
template <typename T> CConnection & operator>>(CConnection &c, T &data);
CConnection & operator<<(CConnection &c, const std::string &data)
{
boost::uint32_t length = data.size();
c << length;
c.write(data.c_str(),length);
return c;
}
CConnection & operator>>(CConnection &c, std::string &data)
{
boost::uint32_t length;
c >> length;
data.reserve(length);
c.read((void*)data.c_str(),length);
return c;
}
CConnection & operator<<(CConnection &c, const char * &data)
{
boost::uint32_t length = strlen(data);
c << length;
c.write(data,length);
return c;
}
CConnection & operator>>(CConnection &c, char * &data)
{
boost::uint32_t length;
c >> length;
data = new char[length];
c.read(data,length);
return c;
}
template <typename T> CConnection & operator<<(CConnection &c, const T &data)
{
c.write(&data,sizeof(data));
return c;
}
template <typename T> CConnection & operator>>(CConnection &c, T &data)
{
c.read(&data,sizeof(data));
return c;
}

View File

@ -17,7 +17,7 @@
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
OutputDirectory="$(SolutionDir)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="2"
CharacterSet="2"
@ -363,6 +363,10 @@
RelativePath="..\hch\CObjectHandler.cpp"
>
</File>
<File
RelativePath=".\Connection.cpp"
>
</File>
<File
RelativePath="..\hch\CTownHandler.cpp"
>
@ -421,6 +425,10 @@
RelativePath="..\hch\CObjectHandler.h"
>
</File>
<File
RelativePath=".\Connection.h"
>
</File>
<File
RelativePath="..\hch\CTownHandler.h"
>

View File

@ -57,6 +57,8 @@
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="VCMI_lib.lib"
AdditionalLibraryDirectories="G:\vcmt\repa\libs"
GenerateDebugInformation="true"
TargetMachine="1"
/>
@ -125,6 +127,8 @@
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="VCMI_lib.lib"
AdditionalLibraryDirectories="G:\vcmt\repa\libs"
GenerateDebugInformation="true"
OptimizeReferences="2"
EnableCOMDATFolding="2"

View File

@ -3,27 +3,12 @@
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include "../global.h"
#include "../lib/Connection.h"
std::string NAME = NAME_VER + std::string(" (server)");
using boost::asio::ip::tcp;
using namespace boost;
using namespace boost::asio;
class CConnection
{
public:
int ID;
tcp::socket socket;
void witaj()
{
char message[50]; strcpy(message,NAME.c_str());message[NAME.size()]='\n';
write(socket,buffer("Aiya!\n"));
write(socket,buffer(message,NAME.size()+1));
}
CConnection(io_service& io_service, int id=-1)
: socket(io_service), ID(id)
{
}
};
using namespace boost::asio::ip;
class CVCMIServer
{
@ -38,27 +23,29 @@ public:
private:
void start_accept()
{
boost::system::error_code error;
std::cout<<"Listening for connections at port " << acceptor.local_endpoint().port() << std::endl;
CConnection * new_connection = new CConnection(acceptor.io_service());
acceptor.accept(new_connection->socket);
new_connection->witaj();
acceptor.async_accept(new_connection->socket,
boost::bind(&CVCMIServer::gotConnection, this, new_connection,
placeholders::error));
}
void gotConnection(CConnection * connection,const boost::system::error_code& error)
{
tcp::socket s(acceptor.io_service());
acceptor.accept(s,error);
if (!error)
{
CConnection *connection = new CConnection(&s,&s.io_service(),NAME,std::cout);
std::cout<<"Got connection!" << std::endl;
connection->witaj();
start_accept();
}
else
{
std::cout<<"Got connection but there is an error " << std::endl;
}
//asio::write(s,asio::buffer("570"));
//new_connection->witaj();
//acceptor.async_accept(s,
// boost::bind(&CVCMIServer::gotConnection, this, &s,
// placeholders::error));
}
void gotConnection(tcp::socket *s,const boost::system::error_code& error)
{
}
};