Added server + network stuff
This commit is contained in:
1 parent
01b58124ec
commit
5d13869324
14 files changed
+417
-40
No files matched your search
@@ -16,7 +16,7 @@ TLiteModelResource::~TLiteModelResource()
|
||||
*Console<<"ResourceManager::ModelManager::LiteModelResource deleting";
|
||||
}
|
||||
|
||||
bool TLiteModelResource::LoadModel(boost::shared_array<byte> filePointer, cardinal fileSize)
|
||||
bool TLiteModelResource::LoadModel(boost::shared_array<unsigned char> filePointer, cardinal fileSize)
|
||||
{
|
||||
|
||||
FreeModel();
|
||||
|
||||
@@ -29,9 +29,10 @@ void TResourceManager::Update(cardinal timer)
|
||||
|
||||
GUIManager.Update(timer);
|
||||
|
||||
ST::MainThreadIoService.run();
|
||||
//ST::MainThreadIoService.run();
|
||||
ST::MainThreadIoService.run_one();
|
||||
|
||||
ST::MainThreadIoService.reset();
|
||||
//ST::MainThreadIoService.reset();
|
||||
}
|
||||
|
||||
TResourceManager::~TResourceManager()
|
||||
|
||||
@@ -633,7 +633,7 @@ void TSoundManagerWindows::LoadSound(const std::string& soundFileName)
|
||||
|
||||
HRESULT result;
|
||||
|
||||
std::string fileName = ResourceManager->PathToResources + soundFileName;
|
||||
std::string fileName = ST::PathToResources + soundFileName;
|
||||
|
||||
|
||||
TWaveFile waveFile;
|
||||
@@ -748,7 +748,7 @@ void TSoundManagerWindows::LoadMusic(const std::string& musicFileName)
|
||||
|
||||
StreamMap[musicName] = ptr;
|
||||
|
||||
StreamMap[musicName]->Load(ResourceManager->PathToResources + musicFileName);
|
||||
StreamMap[musicName]->Load(ST::PathToResources + musicFileName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ const int CONST_ROW_WIDTH = 14;
|
||||
const std::string CONST_CONSOLE_TEX_NAME = "console_bkg.bmp";
|
||||
|
||||
#ifndef UTILS_ENGINE
|
||||
|
||||
void TSimpleConsole::Draw()
|
||||
{
|
||||
|
||||
|
||||
@@ -8,7 +8,12 @@
|
||||
|
||||
namespace SE
|
||||
{
|
||||
|
||||
|
||||
//Xperimental HACK!!!
|
||||
#ifdef UTILS_ENGINE
|
||||
TFileConsole* Console;
|
||||
#endif
|
||||
|
||||
namespace ST
|
||||
{
|
||||
|
||||
|
||||
@@ -18,17 +18,72 @@ void SendPropertyTree(boost::asio::io_service& ioService, boost::asio::ip::tcp::
|
||||
|
||||
boost::shared_ptr<TSignalSender> signalSender(new TSignalSender(socket, len, data));
|
||||
|
||||
ioService.post(boost::bind(&TSignalSender::Send, signalSender));
|
||||
signalSender->Send();
|
||||
//ioService.post(boost::bind(&TSignalSender::Send, signalSender));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void TDataReadSignalMap::AddSlot(const std::string& nodeName, boost::function<void(boost::property_tree::ptree)> f)
|
||||
{
|
||||
if (SignalMap.count(nodeName) == 0)
|
||||
{
|
||||
SignalMap[nodeName] = std::shared_ptr<boost::signal<void(boost::property_tree::ptree)>>(new boost::signal<void(boost::property_tree::ptree)>);
|
||||
SignalMap[nodeName]->connect(f);
|
||||
}
|
||||
else
|
||||
{
|
||||
SignalMap[nodeName]->connect(f);
|
||||
}
|
||||
}
|
||||
|
||||
bool TDataReadSignalMap::SignalExists(const std::string& signalName)
|
||||
{
|
||||
return SignalMap.count(signalName) != 0;
|
||||
}
|
||||
|
||||
void TDataReadSignalMap::EmitSignal(const std::string& signalName, const boost::property_tree::ptree& pt)
|
||||
{
|
||||
if (SignalMap.count(signalName) == 0)
|
||||
{
|
||||
throw ErrorToLog("Signal " + signalName + " does not exist!");
|
||||
}
|
||||
|
||||
(*SignalMap[signalName])(pt);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void TDataReadSignalMap::Clear()
|
||||
{
|
||||
SignalMap.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
TDataReader::TDataReader(boost::asio::ip::tcp::socket& socket)
|
||||
: Socket(socket)
|
||||
{
|
||||
}
|
||||
|
||||
void TDataReader::StartRead()
|
||||
void TDataReader::InnerStartRead()
|
||||
{
|
||||
boost::asio::async_read(Socket, boost::asio::buffer(&DataSize, 4), boost::bind(&TDataReader::HandleReadDataSize, shared_from_this(), boost::asio::placeholders::error));
|
||||
|
||||
}
|
||||
|
||||
void TDataReader::StartReadOnce()
|
||||
{
|
||||
InnerStartRead();
|
||||
Nonstop = false;
|
||||
}
|
||||
|
||||
void TDataReader::StartReadNonstop()
|
||||
{
|
||||
InnerStartRead();
|
||||
Nonstop = true;
|
||||
}
|
||||
|
||||
void TDataReader::HandleReadDataSize(const boost::system::error_code& error)
|
||||
@@ -73,7 +128,19 @@ void TDataReader::HandleReadData(const boost::system::error_code& error)
|
||||
|
||||
boost::property_tree::read_xml(stream, propertyTree);
|
||||
|
||||
DataReadSignal(propertyTree);
|
||||
BOOST_FOREACH(auto i, propertyTree)
|
||||
{
|
||||
if (DataReadSignalMap.SignalExists(i.first))
|
||||
{
|
||||
DataReadSignalMap.EmitSignal(i.first, i.second);
|
||||
}
|
||||
}
|
||||
|
||||
if (Nonstop)
|
||||
{
|
||||
InnerStartRead();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
catch(boost::property_tree::ptree_error)
|
||||
@@ -82,7 +149,6 @@ void TDataReader::HandleReadData(const boost::system::error_code& error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TSimpleAuthorization::TSimpleAuthorization(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket)
|
||||
: Socket(socket)
|
||||
, IoService(ioService)
|
||||
@@ -95,8 +161,8 @@ void TSimpleAuthorization::Authorize()
|
||||
{
|
||||
boost::shared_ptr<TDataReader> dataReader(new TDataReader(Socket));
|
||||
|
||||
dataReader->DataReadSignal.connect(boost::bind(&TSimpleAuthorization::HandleGetData, this, _1));
|
||||
|
||||
dataReader->DataReadSignalMap.AddSlot("OnHello", boost::bind(&TSimpleAuthorization::HandleGetData, this, _1));
|
||||
|
||||
dataReader->ErrorSignal.connect(ErrorSignal);
|
||||
|
||||
boost::property_tree::ptree pt;
|
||||
@@ -105,7 +171,7 @@ void TSimpleAuthorization::Authorize()
|
||||
|
||||
SendPropertyTree(IoService, Socket, pt);
|
||||
|
||||
dataReader->StartRead();
|
||||
dataReader->StartReadOnce();
|
||||
|
||||
|
||||
}
|
||||
@@ -113,18 +179,12 @@ void TSimpleAuthorization::Authorize()
|
||||
|
||||
void TSimpleAuthorization::HandleGetData(boost::property_tree::ptree pTree)
|
||||
{
|
||||
if (pTree.find("OnHello") != pTree.not_found())
|
||||
{
|
||||
Login = pTree.get<std::string>("OnHello.Login");
|
||||
Password = pTree.get<std::string>("OnHello.Password");
|
||||
Login = pTree.get<std::string>("Login");
|
||||
Password = pTree.get<std::string>("Password");
|
||||
|
||||
SaveLoginPasswordSignal(Login, Password);
|
||||
AuthorizedSignal();
|
||||
|
||||
return;
|
||||
}
|
||||
SaveLoginPasswordSignal(Login, Password);
|
||||
|
||||
ErrorSignal();
|
||||
AuthorizedSignal();
|
||||
}
|
||||
|
||||
TClientSocket::TClientSocket()
|
||||
@@ -192,6 +252,8 @@ void TClientSocket::Close()
|
||||
|
||||
Socket.close();
|
||||
|
||||
ClientDataReader->DataReadSignalMap.Clear();
|
||||
|
||||
OnDisconnectedSignal();
|
||||
}
|
||||
}
|
||||
@@ -231,7 +293,9 @@ void TClientSocket::HandleConnect(const boost::system::error_code& error)
|
||||
|
||||
void TClientSocket::HandleAuthorized()
|
||||
{
|
||||
ClientDataReader = boost::shared_ptr<TDataReader>(new TDataReader(Socket));
|
||||
OnAutorizedSignal();
|
||||
ClientDataReader->StartReadNonstop();
|
||||
}
|
||||
|
||||
void TClientSocket::HandleAuthorizationError()
|
||||
@@ -239,4 +303,9 @@ void TClientSocket::HandleAuthorizationError()
|
||||
|
||||
}
|
||||
|
||||
void TClientSocket::SendPropertyTree(boost::property_tree::ptree pTree)
|
||||
{
|
||||
SE::SendPropertyTree(IoService, Socket, pTree);
|
||||
}
|
||||
|
||||
} //namspace SE
|
||||
@@ -0,0 +1,144 @@
|
||||
#include "include/Utils/Utils.h"
|
||||
|
||||
|
||||
namespace SE
|
||||
{
|
||||
std::vector<std::string> TSimpleAutorizator::UserPasswords;
|
||||
|
||||
boost::mutex TSimpleAutorizator::UserPasswordsMutex;
|
||||
|
||||
|
||||
TSimpleAutorizator::TSimpleAutorizator(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket)
|
||||
: IoService(ioService)
|
||||
, Socket(socket)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void TSimpleAutorizator::StartListen()
|
||||
{
|
||||
boost::shared_ptr<TDataReader> dataReader(new TDataReader(Socket));
|
||||
|
||||
dataReader->DataReadSignalMap.AddSlot("Hello", boost::bind(&TSimpleAutorizator::HandleGetData, this, _1));
|
||||
|
||||
dataReader->ErrorSignal.connect(ErrorSignal);
|
||||
|
||||
dataReader->StartReadOnce();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void TSimpleAutorizator::HandleGetData(boost::property_tree::ptree pTree)
|
||||
{
|
||||
|
||||
std::string Login = boost::lexical_cast<std::string>(UserPasswords.size());
|
||||
std::string Password = "12345";
|
||||
|
||||
UserPasswordsMutex.lock();
|
||||
UserPasswords.push_back(Password);
|
||||
UserPasswordsMutex.unlock();
|
||||
|
||||
boost::property_tree::ptree pt;
|
||||
|
||||
pt.put("OnHello.Login", Login);
|
||||
pt.put("OnHello.Password", Password);
|
||||
|
||||
SendPropertyTree(IoService, Socket, pt);
|
||||
|
||||
AllowedSignal(Login);
|
||||
|
||||
}
|
||||
|
||||
|
||||
TConnectedUser::TConnectedUser(TServerSocket& server)
|
||||
: Server(server)
|
||||
, Socket(server.IoService)
|
||||
, Autorizator(std::shared_ptr<TSimpleAutorizator>(new TSimpleAutorizator(server.IoService, Socket)))
|
||||
{
|
||||
}
|
||||
|
||||
TConnectedUser::~TConnectedUser()
|
||||
{
|
||||
}
|
||||
|
||||
void TConnectedUser::HandleAllowed(std::string login)
|
||||
{
|
||||
UserDataReader = boost::shared_ptr<TDataReader>(new TDataReader(Socket));
|
||||
|
||||
Login = login;
|
||||
|
||||
Server.OnUserAuthorizedSignal(shared_from_this());
|
||||
|
||||
UserDataReader->StartReadNonstop();
|
||||
}
|
||||
|
||||
void TConnectedUser::SendPropertyTree(boost::property_tree::ptree pTree)
|
||||
{
|
||||
SE::SendPropertyTree(Server.IoService, Socket, pTree);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
TServerSocket::TServerSocket()
|
||||
{
|
||||
}
|
||||
|
||||
TServerSocket::~TServerSocket()
|
||||
{
|
||||
}
|
||||
|
||||
void TServerSocket::Open(int port)
|
||||
{
|
||||
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);
|
||||
|
||||
Acceptor = std::shared_ptr<boost::asio::ip::tcp::acceptor>(new boost::asio::ip::tcp::acceptor(IoService, endpoint));
|
||||
|
||||
StartAccept();
|
||||
|
||||
ServiceThread = boost::thread(boost::bind(&TServerSocket::UpdateInThread, this));
|
||||
|
||||
}
|
||||
|
||||
void TServerSocket::Close()
|
||||
{
|
||||
}
|
||||
|
||||
void TServerSocket::UpdateInThread()
|
||||
{
|
||||
IoService.run();
|
||||
IoService.reset();
|
||||
}
|
||||
|
||||
void TServerSocket::JoinServiceThread()
|
||||
{
|
||||
ServiceThread.join();
|
||||
}
|
||||
|
||||
void TServerSocket::StartAccept()
|
||||
{
|
||||
boost::shared_ptr<TConnectedUser> newUser(new TConnectedUser(*this));
|
||||
|
||||
Acceptor->async_accept(newUser->Socket, boost::bind(&TServerSocket::HandleAccept, this, newUser, boost::asio::placeholders::error));
|
||||
}
|
||||
|
||||
|
||||
void TServerSocket::HandleAccept(boost::shared_ptr<TConnectedUser> user, const boost::system::error_code& error)
|
||||
{
|
||||
if (!error)
|
||||
{
|
||||
|
||||
UserArr.push_back(user);
|
||||
|
||||
boost::get<std::shared_ptr<TSimpleAutorizator>>(user->Autorizator)->AllowedSignal.connect(boost::bind(&TConnectedUser::HandleAllowed, user, _1));
|
||||
|
||||
boost::get<std::shared_ptr<TSimpleAutorizator>>(user->Autorizator)->StartListen();
|
||||
|
||||
}
|
||||
|
||||
StartAccept();
|
||||
}
|
||||
|
||||
|
||||
} //namspace SE
|
||||
Reference in new issue
Block a user