From 46a5ca4d04167da86b4a8b3fec253a98eee9dceb Mon Sep 17 00:00:00 2001 From: Viacheslav Demydiuk Date: Sun, 16 Jun 2024 13:30:24 +0300 Subject: [PATCH] Refactored MTConnector with new architecure --- MTConnector/AssemblyInfo.cpp | 40 ---- MTConnector/MT4Handler.h | 20 +- MTConnector/MTConnector.cpp | 265 ++++-------------------- MTConnector/MTConnector.vcxproj | 56 +++-- MTConnector/MTConnector.vcxproj.filters | 3 - MTConnector/Stdafx.h | 2 +- 6 files changed, 83 insertions(+), 303 deletions(-) delete mode 100755 MTConnector/AssemblyInfo.cpp diff --git a/MTConnector/AssemblyInfo.cpp b/MTConnector/AssemblyInfo.cpp deleted file mode 100755 index 5f9bae2c..00000000 --- a/MTConnector/AssemblyInfo.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "stdafx.h" - -using namespace System; -using namespace System::Reflection; -using namespace System::Runtime::CompilerServices; -using namespace System::Runtime::InteropServices; -using namespace System::Security::Permissions; - -// -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -// -[assembly:AssemblyTitleAttribute("MTConnector")]; -[assembly:AssemblyDescriptionAttribute("")]; -[assembly:AssemblyConfigurationAttribute("")]; -[assembly:AssemblyCompanyAttribute("DW")]; -[assembly:AssemblyProductAttribute("MTConnector")]; -[assembly:AssemblyCopyrightAttribute("Copyright (c) DW 2011")]; -[assembly:AssemblyTrademarkAttribute("")]; -[assembly:AssemblyCultureAttribute("")]; - -// -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the value or you can default the Revision and Build Numbers -// by using the '*' as shown below: - -[assembly:AssemblyVersionAttribute("1.0.*")]; - -[assembly:ComVisible(false)]; - -[assembly:CLSCompliantAttribute(true)]; - -[assembly:SecurityPermission(SecurityAction::RequestMinimum, UnmanagedCode = true)]; diff --git a/MTConnector/MT4Handler.h b/MTConnector/MT4Handler.h index 2cfaba5a..19d95008 100755 --- a/MTConnector/MT4Handler.h +++ b/MTConnector/MT4Handler.h @@ -3,22 +3,28 @@ #pragma once #include +#include -using namespace MTApiService; - -ref class MT4Handler: IMetaTraderHandler +class MT4Handler: public MetaTraderHandler { public: - MT4Handler() + MT4Handler(int handle) + : handle_(handle) { - msgId = WM_TIMER; + msg_id_ = WM_TIMER; } virtual void SendTickToMetaTrader(int handle) { - PostMessage((HWND)handle, msgId, 0, 0); + PostMessage((HWND)handle, msg_id_, 0, 0); } private: - unsigned int msgId; + void SendTickToMetaTrader() override + { + PostMessage((HWND)handle_, msg_id_, 0, 0); + } + + int handle_; + unsigned int msg_id_; }; diff --git a/MTConnector/MTConnector.cpp b/MTConnector/MTConnector.cpp index 607248ea..0bf77e33 100755 --- a/MTConnector/MTConnector.cpp +++ b/MTConnector/MTConnector.cpp @@ -1,30 +1,28 @@ // This is the main DLL file. -#include "stdafx.h" +#include "Stdafx.h" #include "MT4Handler.h" +#include "MtService.h" #include "Windows.h" -#include #include +#include +#include -using namespace System; -using namespace MTApiService; -using namespace System::Runtime::InteropServices; -using namespace System::Reflection; -using namespace System::Security::Cryptography; -using namespace System::Security; -using namespace System::Text; -using namespace System::IO; -using namespace System::Collections::Generic; -using namespace System::Diagnostics; - -#define _DLLAPI extern "C" __declspec(dllexport) - -void convertSystemString(wchar_t* dest, String^ src) +static void convertSystemString(wchar_t* dest, const std::string& src) { - pin_ptr wch = PtrToStringChars(src); - memcpy(dest, wch, wcslen(wch) * sizeof(wchar_t)); - dest[wcslen(wch)] = '\0'; + using convert_typeX = std::codecvt_utf8; + std::wstring_convert converterX; + auto wstr = converterX.from_bytes(src); + memcpy(dest, wstr.c_str(), wcsnlen(wstr.c_str(), 1000) * sizeof(wchar_t)); +} + +static std::string convertWString(const wchar_t* src) +{ + std::wstring string_to_convert(src); + using convert_typeX = std::codecvt_utf8; + std::wstring_convert converterX; + return converterX.to_bytes(string_to_convert); } template T Execute(std::function func, wchar_t* err, T default_value) @@ -34,19 +32,19 @@ template T Execute(std::function func, wchar_t* err, T default { result = func(); } - catch (Exception^ e) + catch (std::exception& e) { - convertSystemString(err, e->Message); - MtAdapter::GetInstance()->LogError(e->Message); + convertSystemString(err, e.what()); + MtService::GetInstance().LogError(e.what()); } return result; } -_DLLAPI bool _stdcall initExpert(int expertHandle, int port, wchar_t* symbol, double bid, double ask, wchar_t* err) +_DLLAPI bool _stdcall initExpert(int expertHandle, int port, wchar_t* err) { - return Execute([&expertHandle, &port, symbol, &bid, &ask]() { - auto expert = gcnew MtExpert(expertHandle, gcnew String(symbol), bid, ask, gcnew MT4Handler()); - MtAdapter::GetInstance()->AddExpert(port, expert); + return Execute([&expertHandle, &port]() { + auto mt_handler = std::make_unique(expertHandle); + MtService::GetInstance().InitExpert(port, expertHandle, std::move(mt_handler)); return true; }, err, false); } @@ -54,214 +52,39 @@ _DLLAPI bool _stdcall initExpert(int expertHandle, int port, wchar_t* symbol, do _DLLAPI bool _stdcall deinitExpert(int expertHandle, wchar_t* err) { return Execute([&expertHandle]() { - MtAdapter::GetInstance()->RemoveExpert(expertHandle); + MtService::GetInstance().DeinitExpert(expertHandle); return true; }, err, false); } -_DLLAPI bool _stdcall updateQuote(int expertHandle, wchar_t* symbol, double bid, double ask, wchar_t* err) +_DLLAPI bool _stdcall sendEvent(int expertHandle, int event_type, const wchar_t* payload, wchar_t* err) { - return Execute([&expertHandle, symbol, &bid, &ask]() { - MtAdapter::GetInstance()->SendQuote(expertHandle, gcnew String(symbol), bid, ask); + return Execute([&expertHandle, &event_type, payload]() { + MtService::GetInstance().SendEvent(expertHandle, event_type, convertWString(payload)); return true; - }, err, false); + }, err, false); } - -_DLLAPI bool _stdcall sendEvent(int expertHandle, int eventType, wchar_t* payload, wchar_t* err) +_DLLAPI int _stdcall sendResponse(int expertHandle, const wchar_t* response, wchar_t* err) { - return Execute([&expertHandle, &eventType, payload]() { - MtAdapter::GetInstance()->SendEvent(expertHandle, eventType, gcnew String(payload)); - return true; - }, err, false); + return Execute([&expertHandle, response]() { + MtService::GetInstance().SendResponse(expertHandle, convertWString(response)); + return 1; + }, err, 0); } -_DLLAPI bool _stdcall sendIntResponse(int expertHandle, int response, wchar_t* err) +_DLLAPI int _stdcall getCommandType(int expertHandle, int& res, wchar_t* err) { - return Execute([&expertHandle, &response]() { - MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseInt(response)); - return true; - }, err, false); + return Execute([&expertHandle, &res]() { + res = MtService::GetInstance().GetCommandType(expertHandle); + return 1; + }, err, 0); } -_DLLAPI bool _stdcall sendBooleanResponse(int expertHandle, bool response, wchar_t* err) +_DLLAPI int _stdcall getPayload(int expertHandle, wchar_t* res, wchar_t* err) { - return Execute([&expertHandle, &response]() { - MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseBool(response)); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall sendDoubleResponse(int expertHandle, double response, wchar_t* err) -{ - return Execute([&expertHandle, &response]() { - MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseDouble(response)); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall sendStringResponse(int expertHandle, wchar_t* response, wchar_t* err) -{ - return Execute([&expertHandle, response]() { - MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseString(gcnew String(response))); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall sendErrorResponse(int expertHandle, int code, wchar_t* message, wchar_t* err) -{ - return Execute([&expertHandle, &code, message]() { - MtResponseString^ res = gcnew MtResponseString(gcnew String(message)); - res->ErrorCode = code; - MtAdapter::GetInstance()->SendResponse(expertHandle, res); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall sendVoidResponse(int expertHandle, wchar_t* err) -{ - return Execute([&expertHandle]() { - MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseObject(nullptr)); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall sendDoubleArrayResponse(int expertHandle, double* values, int size, wchar_t* err) -{ - return Execute([&expertHandle, values, &size]() { - array^ list = gcnew array(size); - for (int i = 0; i < size; i++) - { - list[i] = values[i]; - } - MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseDoubleArray(list)); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall sendIntArrayResponse(int expertHandle, int* values, int size, wchar_t* err) -{ - return Execute([&expertHandle, values, &size]() { - array^ list = gcnew array(size); - for (int i = 0; i < size; i++) - { - list[i] = values[i]; - } - MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseIntArray(list)); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall sendLongResponse(int expertHandle, __int64 response, wchar_t* err) -{ - return Execute([&expertHandle, &response]() { - MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseLong(response)); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall getCommandType(int expertHandle, int* res, wchar_t* err) -{ - return Execute([&expertHandle, res]() { - *res = MtAdapter::GetInstance()->GetCommandType(expertHandle); - return true; - }, err, false); -} - -// --- index parameters - -_DLLAPI bool _stdcall getIntValue(int expertHandle, int paramIndex, int* res, wchar_t* err) -{ - return Execute([&expertHandle, ¶mIndex, res]() { - *res = MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall getDoubleValue(int expertHandle, int paramIndex, double* res, wchar_t* err) -{ - return Execute([&expertHandle, ¶mIndex, res]() { - *res = MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall getStringValue(int expertHandle, int paramIndex, wchar_t* res, wchar_t* err) -{ - return Execute([&expertHandle, ¶mIndex, res]() { - convertSystemString(res, MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex)); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall getBooleanValue(int expertHandle, int paramIndex, int* res, wchar_t* err) -{ - return Execute([&expertHandle, ¶mIndex, res]() { - *res = MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall getLongValue(int expertHandle, int paramIndex, __int64* res, wchar_t* err) -{ - return Execute([&expertHandle, ¶mIndex, res]() { - *res = MtAdapter::GetInstance()->GetCommandParameter<__int64>(expertHandle, paramIndex); - return true; - }, err, false); -} - -// --- named parameters - -_DLLAPI bool _stdcall containsNamedValue(int expertHandle, wchar_t* paramName) -{ - wchar_t err[1000]; - return Execute([&expertHandle, paramName]() { - return MtAdapter::GetInstance()->ContainsNamedParameter(expertHandle, gcnew String(paramName)); - }, err, false); -} - -_DLLAPI bool _stdcall getNamedIntValue(int expertHandle, wchar_t* paramName, int* res, wchar_t* err) -{ - return Execute([&expertHandle, paramName, res]() { - System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName)); - *res = (int)obj; - return true; - }, err, false); -} - -_DLLAPI bool _stdcall getNamedDoubleValue(int expertHandle, wchar_t* paramName, double* res, wchar_t* err) -{ - return Execute([&expertHandle, paramName, res]() { - System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName)); - *res = (double)obj; - return true; - }, err, false); -} - -_DLLAPI bool _stdcall getNamedStringValue(int expertHandle, wchar_t* paramName, wchar_t* res, wchar_t* err) -{ - return Execute([&expertHandle, paramName, res]() { - System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName)); - convertSystemString(res, (String^)obj); - return true; - }, err, false); -} - -_DLLAPI bool _stdcall getNamedBooleanValue(int expertHandle, wchar_t* paramName, int* res, wchar_t* err) -{ - return Execute([&expertHandle, paramName, res]() { - System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName)); - *res = (bool)obj; - return true; - }, err, false); -} - -_DLLAPI bool _stdcall getNamedLongValue(int expertHandle, wchar_t* paramName, __int64* res, wchar_t* err) -{ - return Execute([&expertHandle, paramName, res]() { - System::Object^ obj = MtAdapter::GetInstance()->GetNamedParameter(expertHandle, gcnew String(paramName)); - if (obj != nullptr) - *res = (__int64)obj; - return true; - }, err, false); + return Execute([&expertHandle, res]() { + convertSystemString(res, MtService::GetInstance().GetCommandPayload(expertHandle)); + return 1; + }, err, 0); } \ No newline at end of file diff --git a/MTConnector/MTConnector.vcxproj b/MTConnector/MTConnector.vcxproj index e4c8780a..740eb14d 100755 --- a/MTConnector/MTConnector.vcxproj +++ b/MTConnector/MTConnector.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -19,41 +19,37 @@ + 17.0 + Win32Proj {2BE5FAC1-7FB7-45C3-9215-31A5BC4B964F} - v4.5.2 - ManagedCProj MTConnector - MTConnector - 10.0.17134.0 + 10.0 DynamicLibrary true - true - NotSet - v141 + Unicode + v143 DynamicLibrary true - true Unicode - v141 + v143 DynamicLibrary false - true - NotSet - v141 + Unicode + v143 DynamicLibrary false true - MultiByte - v141 + Unicode + v143 @@ -73,26 +69,30 @@ false - $(SolutionDir)\build\products\$(Configuration)\ + $(SolutionDir)build\products\$(Configuration)\x86\ + $(SolutionDir)thirdparty\libs;$(LibraryPath) false - $(SolutionDir)\build\products\$(Configuration)\$(Platform)\ + $(SolutionDir)build\products\$(Configuration)\$(Platform)\ + $(SolutionDir)thirdparty\libs;$(LibraryPath) false - $(SolutionDir)\build\products\$(Configuration)\ + $(SolutionDir)build\products\$(Configuration)\x86\ + $(SolutionDir)thirdparty\libs;$(LibraryPath) false - $(SolutionDir)\build\products\$(Configuration)\$(Platform)\ + $(SolutionDir)build\products\$(Configuration)\$(Platform)\ + $(SolutionDir)thirdparty\libs;$(LibraryPath) Level3 - Disabled WIN32;_DEBUG;%(PreprocessorDefinitions) Use + $(SolutionDir)MtService;%(AdditionalIncludeDirectories) true @@ -105,6 +105,7 @@ Disabled WIN32;_DEBUG;%(PreprocessorDefinitions) Use + $(SolutionDir)MtService;%(AdditionalIncludeDirectories) true @@ -116,6 +117,7 @@ Level3 WIN32;NDEBUG;%(PreprocessorDefinitions) Use + $(SolutionDir)MtService;%(AdditionalIncludeDirectories) true @@ -127,6 +129,7 @@ Level3 WIN32;NDEBUG;%(PreprocessorDefinitions) Use + $(SolutionDir)MtService;%(AdditionalIncludeDirectories) true @@ -140,7 +143,6 @@ - Create @@ -156,20 +158,12 @@ - - - - - {de76d5c7-b99c-4467-8408-78173bdd84e0} - false - true - false - true - false + + {3effff03-2a1d-48aa-a49a-9f90889f31e0} diff --git a/MTConnector/MTConnector.vcxproj.filters b/MTConnector/MTConnector.vcxproj.filters index 6300f738..2a17ae22 100755 --- a/MTConnector/MTConnector.vcxproj.filters +++ b/MTConnector/MTConnector.vcxproj.filters @@ -29,9 +29,6 @@ - - Source Files - Source Files diff --git a/MTConnector/Stdafx.h b/MTConnector/Stdafx.h index 2e525d43..e2867f83 100755 --- a/MTConnector/Stdafx.h +++ b/MTConnector/Stdafx.h @@ -4,4 +4,4 @@ #pragma once - +#define _DLLAPI extern "C" __declspec(dllexport) \ No newline at end of file