From a59a7352c85bc701e640fc81eb3e02a73057505b Mon Sep 17 00:00:00 2001 From: Mike <76995924+CodeDestroyer19@users.noreply.github.com> Date: Tue, 18 Jul 2023 11:22:02 +0200 Subject: [PATCH] Added ZeroMQ server scripts --- app/MQHScripts/ZeroMQServer.mql5 | 59 +++++++++++++++ app/MQHScripts/ZmqMql4Connector.mql5 | 105 +++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 app/MQHScripts/ZeroMQServer.mql5 create mode 100644 app/MQHScripts/ZmqMql4Connector.mql5 diff --git a/app/MQHScripts/ZeroMQServer.mql5 b/app/MQHScripts/ZeroMQServer.mql5 new file mode 100644 index 0000000..e277ea9 --- /dev/null +++ b/app/MQHScripts/ZeroMQServer.mql5 @@ -0,0 +1,59 @@ +#import + +// Define the ZeroMQ server endpoint +#define ZMQ_SERVER_ENDPOINT "tcp://*:5900" +// Replace 'metatrader-port' with the port number you want to use for communication + +// Define the ZeroMQ socket and context +CZmqMql4Server g_server; +CZmqContext g_context; + +// Define a function to handle incoming messages +void OnMessageReceived(string message) +{ + // Process the received message and perform necessary actions + // ... + + // Send a response message (if needed) + string responseMessage = "Response from MetaTrader"; + g_server.Send(responseMessage); +} + +// The start function that is called when the EA/script is initialized +int OnInit() +{ + // Initialize the ZeroMQ server + if (!g_server.Initialize(ZMQ_SERVER_ENDPOINT, g_context, OnMessageReceived)) + { + Print("Failed to initialize ZeroMQ server"); + return INIT_FAILED; + } + + // Start the ZeroMQ server + if (!g_server.Start()) + { + Print("Failed to start ZeroMQ server"); + return INIT_FAILED; + } + + // ... + + return INIT_SUCCEEDED; +} + +// The main function that is called on each tick +void OnTick() +{ + // ... +} + +// The deinitialization function that is called when the EA/script is stopped +void OnDeinit(const int reason) +{ + // Stop the ZeroMQ server + g_server.Stop(); + + // Deinitialize the ZeroMQ server and context + g_server.Deinitialize(); + g_context.Terminalize(); +} diff --git a/app/MQHScripts/ZmqMql4Connector.mql5 b/app/MQHScripts/ZmqMql4Connector.mql5 new file mode 100644 index 0000000..2570636 --- /dev/null +++ b/app/MQHScripts/ZmqMql4Connector.mql5 @@ -0,0 +1,105 @@ +// ZmqMql4Connector.mqh + +// Define the ZMQ message callback function type +typedef void OnZmqMessageReceived(string message); + +class CZmqMql4Server +{ +private: + string m_endpoint; // ZeroMQ server endpoint + int m_socket; // ZeroMQ socket + int m_context; // ZeroMQ context + OnZmqMessageReceived @m_callback; // Message callback function + +public: + // Constructor + CZmqMql4Server() + { + m_socket = -1; + m_context = -1; + } + + // Destructor + ~CZmqMql4Server() + { + Deinitialize(); + } + + // Initialize the ZeroMQ server + bool Initialize(string endpoint, OnZmqMessageReceived @callback) + { + m_endpoint = endpoint; + m_callback = @callback; + + m_context = zmq_init(1); + if (m_context == -1) + return false; + + m_socket = zmq_socket(m_context, ZMQ_REP); + if (m_socket == -1) + return false; + + int bindResult = zmq_bind(m_socket, m_endpoint); + if (bindResult == -1) + return false; + + return true; + } + + // Start the ZeroMQ server + bool Start() + { + if (m_socket == -1 || m_context == -1) + return false; + + while (true) + { + string message = Receive(); + if (message != "") + m_callback(message); + } + + return true; + } + + // Stop the ZeroMQ server + void Stop() + { + if (m_socket != -1) + zmq_close(m_socket); + m_socket = -1; + } + + // Send a message from the server + void Send(string message) + { + if (m_socket != -1) + zmq_send(m_socket, message, StringLen(message), 0); + } + + // Receive a message in the server + string Receive() + { + if (m_socket != -1) + { + string message; + int receivedBytes = zmq_recv(m_socket, message, 4096, 0); + if (receivedBytes > 0) + return message; + } + + return ""; + } + + // Deinitialize the ZeroMQ server + void Deinitialize() + { + if (m_socket != -1) + zmq_close(m_socket); + if (m_context != -1) + zmq_term(m_context); + + m_socket = -1; + m_context = -1; + } +};