diff --git a/Salmon Engine.sln b/Salmon Engine.sln index 3f9b82a..7c8d2b5 100644 --- a/Salmon Engine.sln +++ b/Salmon Engine.sln @@ -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 diff --git a/Salmon Engine/Salmon Engine.vcxproj b/Salmon Engine/Salmon Engine.vcxproj index da451f6..456a260 100644 --- a/Salmon Engine/Salmon Engine.vcxproj +++ b/Salmon Engine/Salmon Engine.vcxproj @@ -52,6 +52,8 @@ + + @@ -92,6 +94,7 @@ + diff --git a/UtilsEngine/UtilsEngine.vcxproj b/UtilsEngine/UtilsEngine.vcxproj new file mode 100644 index 0000000..7647468 --- /dev/null +++ b/UtilsEngine/UtilsEngine.vcxproj @@ -0,0 +1,95 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {C29A7BA8-207A-46B8-AAAF-368AED22EC2A} + UtilsEngine + + + + StaticLibrary + true + NotSet + + + StaticLibrary + false + true + NotSet + + + + + + + + + + + + + + + Level3 + Disabled + $(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 + UTILS_ENGINE;TARGET_WIN32;WIN32_LEAN_AND_MEAN;_WIN32_WINNT=0x0501;DEBUG + + + true + + + + + Level3 + MaxSpeed + true + true + $(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 + UTILS_ENGINE;TARGET_WIN32;WIN32_LEAN_AND_MEAN;_WIN32_WINNT=0x0501 + + + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/include/Utils/Network/Network.h b/include/Utils/Network/Network.h new file mode 100644 index 0000000..c41dd41 --- /dev/null +++ b/include/Utils/Network/Network.h @@ -0,0 +1,137 @@ +#ifndef NETWORK_H_INCLUDED +#define NETWORK_H_INCLUDED + +#include +#include +#include +#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 +{ + boost::asio::ip::tcp::socket& Socket; + + int DataSize; + + std::vector 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 DataReadSignal; + boost::signal 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 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 AuthorizedSignal; + boost::signal SaveLoginPasswordSignal; + boost::signal ErrorSignal; +}; + +typedef boost::variant> TAuthorizationVariant; + +class TClientSocket : public boost::enable_shared_from_this +{ +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 ReadData; + + boost::shared_ptr ConnectionTimeoutTimer; + + bool Opened; + +public: + boost::signal OnAddressNotResolvedSignal; + boost::signal OnConnectedSignal; + boost::signal OnAutorizedSignal; + boost::signal 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 \ No newline at end of file diff --git a/include/Utils/Network/SignalSender.h b/include/Utils/Network/SignalSender.h new file mode 100644 index 0000000..7008912 --- /dev/null +++ b/include/Utils/Network/SignalSender.h @@ -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 +{ + boost::asio::ip::tcp::socket& Socket; + + int Length; + + std::string Data; + + boost::signal 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 diff --git a/include/Utils/Utils.h b/include/Utils/Utils.h index 4b2e011..9ee52b7 100644 --- a/include/Utils/Utils.h +++ b/include/Utils/Utils.h @@ -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" diff --git a/src/GUIManager/ButtonWidget.cpp b/src/GUIManager/ButtonWidget.cpp index dae5c85..bf3f30e 100644 --- a/src/GUIManager/ButtonWidget.cpp +++ b/src/GUIManager/ButtonWidget.cpp @@ -501,9 +501,11 @@ TEdit* TEdit::CreateEditWithFillers(vec2 posFrom, vec2 posTo, std::vectorTriangleListVector.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) diff --git a/src/SalmonEngineWindows.cpp b/src/SalmonEngineWindows.cpp index d3a1376..305054f 100644 --- a/src/SalmonEngineWindows.cpp +++ b/src/SalmonEngineWindows.cpp @@ -129,6 +129,10 @@ void TApplication::OnKeyPress(cardinal key) Console->ConsoleInput = ""; Console->ConsoleCursor = Console->ConsoleInput.size(); } + else + { + ResourceManager->GUIManager.KeyPressedSignal(static_cast(key)); + } } } diff --git a/src/Utils/Network/Network.cpp b/src/Utils/Network/Network.cpp new file mode 100644 index 0000000..67bf6a7 --- /dev/null +++ b/src/Utils/Network/Network.cpp @@ -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 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 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("OnHello.Login"); + Password = p.get("OnHello.Password"); + + SaveLoginPasswordSignal(Login, Password); + AuthorizedSignal(); + + return; + } + + ErrorSignal(); +} + +TClientSocket::TClientSocket() + : Socket(IoService) + , ReadDataLenLong(0) + , Opened(false) + , Authorization(std::shared_ptr(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(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 authorization = boost::get>(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 \ No newline at end of file