Refactored MTConnector with new architecure

This commit is contained in:
Viacheslav Demydiuk
2024-06-16 13:30:24 +03:00
parent 07b26a6ac1
commit 46a5ca4d04
6 changed files with 83 additions and 303 deletions
+44 -221
View File
@@ -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 <vcclr.h>
#include <functional>
#include <string>
#include <codecvt>
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<const wchar_t> wch = PtrToStringChars(src);
memcpy(dest, wch, wcslen(wch) * sizeof(wchar_t));
dest[wcslen(wch)] = '\0';
using convert_typeX = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> 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<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.to_bytes(string_to_convert);
}
template <typename T> T Execute(std::function<T()> func, wchar_t* err, T default_value)
@@ -34,19 +32,19 @@ template <typename T> T Execute(std::function<T()> 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<bool>([&expertHandle, &port, symbol, &bid, &ask]() {
auto expert = gcnew MtExpert(expertHandle, gcnew String(symbol), bid, ask, gcnew MT4Handler());
MtAdapter::GetInstance()->AddExpert(port, expert);
return Execute<bool>([&expertHandle, &port]() {
auto mt_handler = std::make_unique<MT4Handler>(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<bool>([&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<bool>([&expertHandle, symbol, &bid, &ask]() {
MtAdapter::GetInstance()->SendQuote(expertHandle, gcnew String(symbol), bid, ask);
return Execute<bool>([&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<bool>([&expertHandle, &eventType, payload]() {
MtAdapter::GetInstance()->SendEvent(expertHandle, eventType, gcnew String(payload));
return true;
}, err, false);
return Execute<int>([&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<bool>([&expertHandle, &response]() {
MtAdapter::GetInstance()->SendResponse(expertHandle, gcnew MtResponseInt(response));
return true;
}, err, false);
return Execute<int>([&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<bool>([&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<bool>([&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<bool>([&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<bool>([&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<bool>([&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<bool>([&expertHandle, values, &size]() {
array<double>^ list = gcnew array<double>(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<bool>([&expertHandle, values, &size]() {
array<int>^ list = gcnew array<int>(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<bool>([&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<bool>([&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<bool>([&expertHandle, &paramIndex, res]() {
*res = MtAdapter::GetInstance()->GetCommandParameter<int>(expertHandle, paramIndex);
return true;
}, err, false);
}
_DLLAPI bool _stdcall getDoubleValue(int expertHandle, int paramIndex, double* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, &paramIndex, res]() {
*res = MtAdapter::GetInstance()->GetCommandParameter<double>(expertHandle, paramIndex);
return true;
}, err, false);
}
_DLLAPI bool _stdcall getStringValue(int expertHandle, int paramIndex, wchar_t* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, &paramIndex, res]() {
convertSystemString(res, MtAdapter::GetInstance()->GetCommandParameter<String^>(expertHandle, paramIndex));
return true;
}, err, false);
}
_DLLAPI bool _stdcall getBooleanValue(int expertHandle, int paramIndex, int* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, &paramIndex, res]() {
*res = MtAdapter::GetInstance()->GetCommandParameter<bool>(expertHandle, paramIndex);
return true;
}, err, false);
}
_DLLAPI bool _stdcall getLongValue(int expertHandle, int paramIndex, __int64* res, wchar_t* err)
{
return Execute<bool>([&expertHandle, &paramIndex, 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<bool>([&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<bool>([&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<bool>([&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<bool>([&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<bool>([&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<bool>([&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<int>([&expertHandle, res]() {
convertSystemString(res, MtService::GetInstance().GetCommandPayload(expertHandle));
return 1;
}, err, 0);
}