network stuff

This commit is contained in:
Vladislav Khorev 2013-02-16 21:05:10 +00:00
parent 770f0783ec
commit 9627247bdc
9 changed files with 555 additions and 5 deletions

View File

@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 11.00
# Visual C++ Express 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Salmon Engine", "Salmon Engine\Salmon Engine.vcxproj", "{48ADCE9F-9539-4D3A-BCFA-C2ABABAF0B20}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UtilsEngine", "UtilsEngine\UtilsEngine.vcxproj", "{C29A7BA8-207A-46B8-AAAF-368AED22EC2A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug_nosound|Win32 = Debug_nosound|Win32
@ -16,6 +18,12 @@ Global
{48ADCE9F-9539-4D3A-BCFA-C2ABABAF0B20}.Debug|Win32.Build.0 = Debug|Win32
{48ADCE9F-9539-4D3A-BCFA-C2ABABAF0B20}.Release|Win32.ActiveCfg = Release|Win32
{48ADCE9F-9539-4D3A-BCFA-C2ABABAF0B20}.Release|Win32.Build.0 = Release|Win32
{C29A7BA8-207A-46B8-AAAF-368AED22EC2A}.Debug_nosound|Win32.ActiveCfg = Debug|Win32
{C29A7BA8-207A-46B8-AAAF-368AED22EC2A}.Debug_nosound|Win32.Build.0 = Debug|Win32
{C29A7BA8-207A-46B8-AAAF-368AED22EC2A}.Debug|Win32.ActiveCfg = Debug|Win32
{C29A7BA8-207A-46B8-AAAF-368AED22EC2A}.Debug|Win32.Build.0 = Debug|Win32
{C29A7BA8-207A-46B8-AAAF-368AED22EC2A}.Release|Win32.ActiveCfg = Release|Win32
{C29A7BA8-207A-46B8-AAAF-368AED22EC2A}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -52,6 +52,8 @@
<ClInclude Include="..\include\Utils\DataTypes\NewDataTypes.h" />
<ClInclude Include="..\include\Utils\ErrorTypes\ErrorTypes.h" />
<ClInclude Include="..\include\Utils\FileUtils\FileUtils.h" />
<ClInclude Include="..\include\Utils\Network\Network.h" />
<ClInclude Include="..\include\Utils\Network\SignalSender.h" />
<ClInclude Include="..\include\Utils\PngHelper.h" />
<ClInclude Include="..\include\Utils\SerializeInterface\SerializeInterface.h" />
<ClInclude Include="..\include\Utils\ThreadUtils.h" />
@ -92,6 +94,7 @@
<ClCompile Include="..\src\Utils\DataTypes\DataTypes.cpp" />
<ClCompile Include="..\src\Utils\DataTypes\NewDataTypes.cpp" />
<ClCompile Include="..\src\Utils\FileUtils\FileUtils.cpp" />
<ClCompile Include="..\src\Utils\Network\Network.cpp" />
<ClCompile Include="..\src\Utils\PngHelper.cpp" />
<ClCompile Include="..\src\Utils\SerializeInterface\SerializeInterface.cpp" />
<ClCompile Include="..\src\Utils\SimpleTimer.cpp" />

View File

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{C29A7BA8-207A-46B8-AAAF-368AED22EC2A}</ProjectGuid>
<RootNamespace>UtilsEngine</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>$(SalmonEnginePath);$(LibsPath)\boost_1_52_0;$(LibsPath)\openal\OpenAL11_windows_sdk;$(LibsPath)\libogg-1.3.0\include;$(LibsPath)\libvorbis-1.3.2\include;$(LibsPath)\sqplus\sqplus;$(LibsPath)\sqplus\include;$(LibsPath)\DirectXsdk\Include;$(LibsPath)\lpng1510</AdditionalIncludeDirectories>
<PreprocessorDefinitions>UTILS_ENGINE;TARGET_WIN32;WIN32_LEAN_AND_MEAN;_WIN32_WINNT=0x0501;DEBUG</PreprocessorDefinitions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<AdditionalIncludeDirectories>$(SalmonEnginePath);$(LibsPath)\boost_1_52_0;$(LibsPath)\openal\OpenAL11_windows_sdk;$(LibsPath)\libogg-1.3.0\include;$(LibsPath)\libvorbis-1.3.2\include;$(LibsPath)\sqplus\sqplus;$(LibsPath)\sqplus\include;$(LibsPath)\DirectXsdk\Include;$(LibsPath)\lpng1510</AdditionalIncludeDirectories>
<PreprocessorDefinitions>UTILS_ENGINE;TARGET_WIN32;WIN32_LEAN_AND_MEAN;_WIN32_WINNT=0x0501</PreprocessorDefinitions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\src\Utils\Console\Console.cpp" />
<ClCompile Include="..\src\Utils\DataTypes\DataTypes.cpp" />
<ClCompile Include="..\src\Utils\DataTypes\NewDataTypes.cpp" />
<ClCompile Include="..\src\Utils\FileUtils\FileUtils.cpp" />
<ClCompile Include="..\src\Utils\Network\Network.cpp" />
<ClCompile Include="..\src\Utils\SerializeInterface\SerializeInterface.cpp" />
<ClCompile Include="..\src\Utils\SimpleTimer.cpp" />
<ClCompile Include="..\src\Utils\ThreadUtils.cpp" />
<ClCompile Include="..\src\Utils\WinApi\WinApi.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\Utils\BindableVar.h" />
<ClInclude Include="..\include\Utils\Console\console.h" />
<ClInclude Include="..\include\Utils\DataTypes\DataTypes.h" />
<ClInclude Include="..\include\Utils\DataTypes\NewDataTypes.h" />
<ClInclude Include="..\include\Utils\ErrorTypes\ErrorTypes.h" />
<ClInclude Include="..\include\Utils\FileUtils\FileUtils.h" />
<ClInclude Include="..\include\Utils\Network\Network.h" />
<ClInclude Include="..\include\Utils\Network\SignalSender.h" />
<ClInclude Include="..\include\Utils\SerializeInterface\SerializeInterface.h" />
<ClInclude Include="..\include\Utils\SimpleTimer.h" />
<ClInclude Include="..\include\Utils\ThreadUtils.h" />
<ClInclude Include="..\include\Utils\Utils.h" />
<ClInclude Include="..\include\Utils\WinApi\WinApi.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,137 @@
#ifndef NETWORK_H_INCLUDED
#define NETWORK_H_INCLUDED
#include <string>
#include <map>
#include <vector>
#include "boost/shared_array.hpp"
#include "boost/property_tree/ptree.hpp"
#include "boost/foreach.hpp"
#include "boost/asio.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/signal.hpp"
#include "boost/variant.hpp"
#include "include/Utils/Network/SignalSender.h"
namespace SE
{
const int CONST_CONNECTION_TIMEOUT_SECONDS = 300;
void SendPropertyTree(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket, boost::property_tree::ptree pTree);
//Must be stored in shared_ptr only
struct TDataReader : public boost::enable_shared_from_this<TDataReader>
{
boost::asio::ip::tcp::socket& Socket;
int DataSize;
std::vector<char> Data;
TDataReader(boost::asio::ip::tcp::socket& socket);
void StartRead();
void HandleReadDataSize(const boost::system::error_code& error);
void HandleReadData(const boost::system::error_code& error);
boost::signal<void(boost::property_tree::ptree)> DataReadSignal;
boost::signal<void()> ErrorSignal;
};
class TAuthorizationInterface
{
public:
virtual void Authorize() = 0;
};
class TSimpleAuthorization : public TAuthorizationInterface
{
public:
boost::asio::io_service& IoService;
boost::asio::ip::tcp::socket& Socket;
//std::shared_ptr<TDataReader> DataReader;
std::string Login;
std::string Password;
TSimpleAuthorization(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket);
virtual void Authorize();
void HandleGetData(boost::property_tree::ptree pTree);
boost::signal<void()> AuthorizedSignal;
boost::signal<void(std::string, std::string)> SaveLoginPasswordSignal;
boost::signal<void()> ErrorSignal;
};
typedef boost::variant<std::shared_ptr<TSimpleAuthorization>> TAuthorizationVariant;
class TClientSocket : public boost::enable_shared_from_this<TClientSocket>
{
protected:
boost::asio::io_service IoService; //IoService must be declared before Socket
boost::thread IoServiceThread;
boost::asio::ip::tcp::socket Socket;
int ReadDataLenLong;
std::vector<char> ReadData;
boost::shared_ptr<boost::asio::deadline_timer> ConnectionTimeoutTimer;
bool Opened;
public:
boost::signal<void()> OnAddressNotResolvedSignal;
boost::signal<void()> OnConnectedSignal;
boost::signal<void()> OnAutorizedSignal;
boost::signal<void()> OnDisconnectedSignal;
TAuthorizationVariant Authorization;
TClientSocket();
~TClientSocket();
void IoServiceRun();
void Open(const std::string address, const std::string& port);
void Close();
void HandleConnectTimeout(const boost::system::error_code& error);
void HandleConnect(const boost::system::error_code& error);
void HandleAuthorized();
void HandleAuthorizationError();
};
class TServerSocket
{
protected:
public:
};
} //namespace SE
#endif

View File

@ -0,0 +1,54 @@
#ifndef SIGNAL_SENDER_H_INCLUDED
#define SIGNAL_SENDER_H_INCLUDED
#include "boost/asio.hpp"
#include "boost/bind.hpp"
#include "boost/shared_ptr.hpp"
#include "boost/array.hpp"
#include "boost/enable_shared_from_this.hpp"
#include "boost/signal.hpp"
#include "boost/thread.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/xml_parser.hpp"
namespace SE
{
//Xperimental -- need to optimize this
struct TSignalSender
: public boost::enable_shared_from_this<TSignalSender>
{
boost::asio::ip::tcp::socket& Socket;
int Length;
std::string Data;
boost::signal<void()> ErrorSignal;
TSignalSender(boost::asio::ip::tcp::socket& socket, int length, std::string data)
: Socket(socket)
, Length(length)
, Data(data)
{
}
void Send()
{
boost::asio::async_write(Socket, boost::asio::buffer(&Length, 4), boost::bind(&TSignalSender::Handle, shared_from_this(), boost::asio::placeholders::error));
boost::asio::async_write(Socket, boost::asio::buffer(Data.c_str(), Data.size()), boost::bind(&TSignalSender::Handle, shared_from_this(), boost::asio::placeholders::error));
}
void Handle(const boost::system::error_code& error)
{
if (error)
{
ErrorSignal();
}
}
};
} //namespace SE
#endif

View File

@ -26,6 +26,7 @@ This code combines additional routines (such as console/log, exceptions, math ut
#include "include/Utils/BindableVar.h"
#include "include/Utils/SimpleTimer.h"
#include "include/Utils/ThreadUtils.h"
#include "include/Utils/Network/Network.h"
#ifdef TARGET_WIN32
#include "WinApi/WinApi.h"

View File

@ -501,9 +501,11 @@ TEdit* TEdit::CreateEditWithFillers(vec2 posFrom, vec2 posTo, std::vector<boost:
void TEdit::EditFiller(vec2 posFrom, vec2 posTo, const std::string& texName, TTextParams textParams, TEdit* edit)
{
TRenderParams renderParams = textParams.RenderParams;
TRenderParams renderParams;
renderParams.SamplerMap[CONST_STRING_TEXTURE_UNIFORM] = texName;
renderParams.FloatMap[CONST_STRING_TRANSPARENCY_UNIFORM] = 1.f;
TTriangleList triangleList = MakeTriangleList(posFrom, posTo);
@ -512,6 +514,8 @@ void TEdit::EditFiller(vec2 posFrom, vec2 posTo, const std::string& texName, TTe
edit->TriangleListVector.insert(edit->TriangleListVector.end(), TRenderPair(renderParams, triangleList));
renderParams = textParams.RenderParams;
if (textParams.FontName == "")
{
textParams.FontName = ResourceManager->FontManager.GetCurrentFontName();
@ -519,17 +523,19 @@ void TEdit::EditFiller(vec2 posFrom, vec2 posTo, const std::string& texName, TTe
ResourceManager->FontManager.PushFont(textParams.FontName);
triangleList = CreateTriangleListForText(posFrom, posTo, textParams);
TTriangleList textTriangleList = CreateTriangleListForText(posFrom, posTo, textParams);
textTriangleList.RefreshBuffer();
renderParams.SamplerMap[CONST_STRING_TEXTURE_UNIFORM] = ResourceManager->FontManager.GetCurrentFontTextureName();
edit->TextIterator = (edit->TriangleListVector.insert(edit->TriangleListVector.end(),TRenderPair(renderParams, triangleList)));
triangleList.RefreshBuffer();
edit->TextIterator = (edit->TriangleListVector.insert(edit->TriangleListVector.end(),TRenderPair(renderParams, textTriangleList)));
ResourceManager->FontManager.PopFont();
edit->TextParams = textParams;
}
void TEdit::SmartValueTextFiller(const std::string& dictionaryName, const std::string& smartValueName, TEdit* edit)

View File

@ -129,6 +129,10 @@ void TApplication::OnKeyPress(cardinal key)
Console->ConsoleInput = "";
Console->ConsoleCursor = Console->ConsoleInput.size();
}
else
{
ResourceManager->GUIManager.KeyPressedSignal(static_cast<int>(key));
}
}
}

View File

@ -0,0 +1,242 @@
#include "include/Utils/Utils.h"
namespace SE
{
void SendPropertyTree(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket, boost::property_tree::ptree pTree)
{
//Xperimental -- need to optimize this
std::stringstream o_stream;
boost::property_tree::write_xml(o_stream, pTree);
std::string data = o_stream.str();
int len = data.size();
boost::shared_ptr<TSignalSender> signalSender(new TSignalSender(socket, len, data));
ioService.post(boost::bind(&TSignalSender::Send, signalSender));
}
TDataReader::TDataReader(boost::asio::ip::tcp::socket& socket)
: Socket(socket)
{
}
void TDataReader::StartRead()
{
boost::asio::async_read(Socket, boost::asio::buffer(&DataSize, 4), boost::bind(&TDataReader::HandleReadDataSize, shared_from_this(), boost::asio::placeholders::error));
}
void TDataReader::HandleReadDataSize(const boost::system::error_code& error)
{
if (error)
{
ErrorSignal();
return;
}
if (DataSize > 65536 || DataSize <= 0)
{
// len>65536 is something unbelievable. Prevent this just in case;
ErrorSignal();
return;
}
Data.resize(DataSize);
boost::asio::async_read(Socket, boost::asio::buffer(Data), boost::bind(&TDataReader::HandleReadData, shared_from_this(), boost::asio::placeholders::error));
}
void TDataReader::HandleReadData(const boost::system::error_code& error)
{
if (error)
{
ErrorSignal();
return;
}
try
{
//Xperimental - Might be optimized a lot:
std::string xmlCode = std::string(&Data[0], &Data[0] + Data.size());
std::stringstream stream(xmlCode);
boost::property_tree::ptree propertyTree;
boost::property_tree::read_xml(stream, propertyTree);
DataReadSignal(propertyTree);
}
catch(boost::property_tree::ptree_error)
{
ErrorSignal();
}
}
TSimpleAuthorization::TSimpleAuthorization(boost::asio::io_service& ioService, boost::asio::ip::tcp::socket& socket)
: Socket(socket)
, IoService(ioService)
{
}
void TSimpleAuthorization::Authorize()
{
std::shared_ptr<TDataReader> dataReader(new TDataReader(Socket));
dataReader->DataReadSignal.connect(boost::bind(&TSimpleAuthorization::HandleGetData, this, _1));
dataReader->ErrorSignal.connect(ErrorSignal);
boost::property_tree::ptree pt;
pt.put("Hello", "");
SendPropertyTree(IoService, Socket, pt);
dataReader->StartRead();
}
void TSimpleAuthorization::HandleGetData(boost::property_tree::ptree pTree)
{
if (pTree.find("OnHello") != pTree.not_found())
{
Login = p.get<std::string>("OnHello.Login");
Password = p.get<std::string>("OnHello.Password");
SaveLoginPasswordSignal(Login, Password);
AuthorizedSignal();
return;
}
ErrorSignal();
}
TClientSocket::TClientSocket()
: Socket(IoService)
, ReadDataLenLong(0)
, Opened(false)
, Authorization(std::shared_ptr<TSimpleAuthorization>(new TSimpleAuthorization(IoService, Socket)))
{
}
TClientSocket::~TClientSocket()
{
}
void TClientSocket::IoServiceRun()
{
//Need try-catch here!
//To be run in separated thread
IoService.run();
IoService.reset();
}
void TClientSocket::Open(const std::string address, const std::string& port)
{
boost::asio::ip::tcp::resolver resolver(IoService);
boost::asio::ip::tcp::resolver::query query(address.c_str(), port.c_str());
boost::system::error_code ec;
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query, ec);
boost::asio::ip::tcp::resolver::iterator end;
if (ec)
{
OnAddressNotResolvedSignal();
return;
}
boost::asio::async_connect(Socket, iterator, boost::bind(&TClientSocket::HandleConnect, shared_from_this(), boost::asio::placeholders::error));
ConnectionTimeoutTimer = boost::shared_ptr<boost::asio::deadline_timer>(new boost::asio::deadline_timer(IoService, boost::posix_time::seconds(CONST_CONNECTION_TIMEOUT_SECONDS)));
ConnectionTimeoutTimer->async_wait(boost::bind(&TClientSocket::HandleConnectTimeout, this, boost::asio::placeholders::error));
IoServiceThread = boost::thread(boost::bind(&TClientSocket::IoServiceRun, this));
}
void TClientSocket::Close()
{
if (Opened)
{
Opened = false;
ConnectionTimeoutTimer->cancel();
Socket.close();
OnDisconnectedSignal();
}
}
void TClientSocket::HandleConnectTimeout(const boost::system::error_code& error)
{
if (!error)
{
Socket.cancel();
IoService.stop();
}
}
void TClientSocket::HandleConnect(const boost::system::error_code& error)
{
if (error)
{
Socket.close();
return;
}
Opened = true;
OnConnectedSignal();
std::shared_ptr<TSimpleAuthorization> authorization = boost::get<std::shared_ptr<TSimpleAuthorization>>(Authorization);
authorization->AuthorizedSignal.connect(boost::bind(&TClientSocket::HandleAuthorized, this));
authorization->ErrorSignal.connect(boost::bind(&TClientSocket::HandleAuthorizationError, this));
authorization->Authorize();
}
void TClientSocket::HandleAuthorized()
{
OnAutorizedSignal();
}
void TClientSocket::HandleAuthorizationError()
{
}
} //namspace SE