From 9a5d3878fe1ef93cc84845e0a9c60ef3c28d1c83 Mon Sep 17 00:00:00 2001 From: Viacheslav Demydiuk Date: Sat, 9 Mar 2024 23:17:47 +0200 Subject: [PATCH] MtApi5: make public Connect/Disconnect functions --- MtApi5/MtApi5Client.cs | 86 +++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/MtApi5/MtApi5Client.cs b/MtApi5/MtApi5Client.cs index 3075223d..725ce9ec 100755 --- a/MtApi5/MtApi5Client.cs +++ b/MtApi5/MtApi5Client.cs @@ -28,7 +28,6 @@ namespace MtApi5 #region Private Fields private MtRpcClient? _client; private readonly object _locker = new(); - private volatile bool _isBacktestingMode; private Mt5ConnectionState _connectionState = Mt5ConnectionState.Disconnected; private int _executorHandle; private readonly Dictionary> _mtEventHandlers = []; @@ -55,12 +54,21 @@ namespace MtApi5 /// ///Connect with MetaTrader API. Async method. /// - ///Address of MetaTrader host (ex. 192.168.1.2) + ///Address of MetaTrader host (ex. 192.168.1.2, localhost) ///Port of host connection (default 8222) public void BeginConnect(string host, int port) { Log.Info($"BeginConnect: host = {host}, port = {port}"); - Task.Factory.StartNew(() => Connect(host, port)); + Task.Factory.StartNew(async () => + { + try + { + await Connect(host, port); + } + catch (Exception) + { + } + }); } /// @@ -69,12 +77,22 @@ namespace MtApi5 ///Port of host connection (default 8222) public void BeginConnect(int port) { - Log.Info($"BeginConnect: port = localhost:{port}"); - Task.Factory.StartNew(() => Connect("localhost", port)); + BeginConnect("localhost", port); } - public async void Connect(string host, int port) + /// + ///Connect with MetaTrader API. + /// + ///Address of MetaTrader host (ex. 192.168.1.2, localhost) + ///Port of host connection (default 8222) + /// + /// Thrown when host is null or empty. + /// + public async Task Connect(string host, int port) { + if (string.IsNullOrEmpty(host)) + throw new ArgumentNullException(nameof(host)); + lock (_locker) { if (_connectionState == Mt5ConnectionState.Connected @@ -86,8 +104,7 @@ namespace MtApi5 _connectionState = Mt5ConnectionState.Connecting; } - string message = $"Connect: connecting to {host}:{port}"; - ConnectionStateChanged?.Invoke(this, new Mt5ConnectionEventArgs(Mt5ConnectionState.Connecting, message)); + ConnectionStateChanged?.Invoke(this, new Mt5ConnectionEventArgs(Mt5ConnectionState.Connecting, $"Connecting to {host}:{port}")); var client = new MtRpcClient(host, port, new RpcClientLogger(Log)); client.ExpertList += Client_ExpertList; @@ -97,33 +114,28 @@ namespace MtApi5 client.ConnectionFailed += Client_OnConnectionFailed; client.Disconnected += Client_Disconnected; - var state = Mt5ConnectionState.Failed; try { await client.Connect(); - state = Mt5ConnectionState.Connected; + Log.Info($"Connected to {host}:{port}"); + lock (_locker) + { + _client = client; + _connectionState = Mt5ConnectionState.Connected; + } + client.NotifyClientReady(); + ConnectionStateChanged?.Invoke(this, new Mt5ConnectionEventArgs(Mt5ConnectionState.Connected, $"Connected to {host}:{port}")); } catch (Exception e) { - Log.Warn($"Connect: Failed connection to {host}:{port}. {e.Message}"); - } - - Log.Info($"Connect: connection to {host}:{port} is {state}"); - - lock (_locker) - { - if (state == Mt5ConnectionState.Connected) + Log.Error($"Connect: Failed connection to {host}:{port}. Error: {e.Message}"); + lock (_locker) { - _client = client; + _connectionState = Mt5ConnectionState.Failed; } - - _connectionState = state; + ConnectionStateChanged?.Invoke(this, new Mt5ConnectionEventArgs(Mt5ConnectionState.Failed, e.Message)); + throw new Exception($"Connection to {host}:{port} failed. Error: {e.Message}"); } - - if (state == Mt5ConnectionState.Connected) - OnConnected(); - - ConnectionStateChanged?.Invoke(this, new Mt5ConnectionEventArgs(state, message)); } /// @@ -135,6 +147,15 @@ namespace MtApi5 Task.Factory.StartNew(() => Disconnect(false)); } + /// + ///Disconnect from MetaTrader API. + /// + public void Disconnect() + { + Log.Info("Disconnect called."); + Disconnect(false); + } + /// ///Load quotes connected into MetaTrader API (deprecated) /// @@ -3370,9 +3391,7 @@ namespace MtApi5 QuoteList?.Invoke(this, new(quotes.Values.ToList())); - _isBacktestingMode = IsTesting(); - - if (_isBacktestingMode) + if (IsTesting()) { BacktestingReady(); } @@ -3593,15 +3612,6 @@ namespace MtApi5 return (response.Value == null) ? default : response.Value; } - private void OnConnected() - { - Log.Debug("OnConnected: begin"); - - Client?.NotifyClientReady(); - - Log.Debug("OnConnected: finished"); - } - private void BacktestingReady() { SendCommand(ExecutorHandle, Mt5CommandType.BacktestingReady);