Small refactoring MtApi (MT5) and MtService. Started version 1.0.22 of MtService

This commit is contained in:
vdemydiuk
2016-10-10 15:49:53 +03:00
parent b4b8e7eaf0
commit 3785ee4ace
7 changed files with 488 additions and 524 deletions
+73 -112
View File
@@ -9,97 +9,93 @@ namespace MTApiService
[CallbackBehavior(UseSynchronizationContext = false)] [CallbackBehavior(UseSynchronizationContext = false)]
public class MtClient: IMtApiCallback, IDisposable public class MtClient: IMtApiCallback, IDisposable
{ {
private static string SERVICE_NAME = "MtApiService"; private const string ServiceName = "MtApiService";
public delegate void MtQuoteHandler(MtQuote quote); public delegate void MtQuoteHandler(MtQuote quote);
#region Fields
private MtApiProxy _proxy;
private bool _isConnected;
#endregion
#region Public Methods #region Public Methods
public void Open(string host, int port) public void Open(string host, int port)
{ {
Debug.WriteLine("[INFO] MtClient::Open"); Debug.WriteLine("[INFO] MtClient::Open");
if (string.IsNullOrEmpty(host)) if (string.IsNullOrEmpty(host))
throw new ArgumentNullException("host", "host is null or empty"); throw new ArgumentNullException(nameof(host), "host is null or empty");
if (port < 0 || port > 65536) if (port < 0 || port > 65536)
throw new ArgumentOutOfRangeException("port", "port value is invalid"); throw new ArgumentOutOfRangeException(nameof(port), "port value is invalid");
var urlService = string.Format("net.tcp://{0}:{1}/{2}", host, port, SERVICE_NAME); var urlService = $"net.tcp://{host}:{port}/{ServiceName}";
lock (mClientLocker) if (_proxy != null)
return;
var bind = new NetTcpBinding(SecurityMode.None)
{ {
if (mProxy != null) MaxReceivedMessageSize = 2147483647,
return; MaxBufferSize = 2147483647,
MaxBufferPoolSize = 2147483647,
var bind = new NetTcpBinding(SecurityMode.None) ReaderQuotas =
{ {
MaxReceivedMessageSize = 2147483647, MaxArrayLength = 2147483647,
MaxBufferSize = 2147483647, MaxBytesPerRead = 2147483647,
MaxBufferPoolSize = 2147483647, MaxDepth = 2147483647,
ReaderQuotas = MaxStringContentLength = 2147483647,
{ MaxNameTableCharCount = 2147483647
MaxArrayLength = 2147483647, }
MaxBytesPerRead = 2147483647, };
MaxDepth = 2147483647, // Commented next statement since it is not required
MaxStringContentLength = 2147483647,
MaxNameTableCharCount = 2147483647
}
};
// Commented next statement since it is not required
mProxy = new MtApiProxy(new InstanceContext(this), bind, new EndpointAddress(urlService)); _proxy = new MtApiProxy(new InstanceContext(this), bind, new EndpointAddress(urlService));
mProxy.Faulted += mProxy_Faulted; _proxy.Faulted += ProxyFaulted;
}
} }
public void Open(int port) public void Open(int port)
{ {
if (port < 0 || port > 65536) if (port < 0 || port > 65536)
throw new ArgumentOutOfRangeException("port", "port value is invalid"); throw new ArgumentOutOfRangeException(nameof(port), "port value is invalid");
var urlService = string.Format("net.pipe://localhost/{0}_{1}", SERVICE_NAME, port); var urlService = $"net.pipe://localhost/{ServiceName}_{port}";
lock (mClientLocker) if (_proxy != null)
return;
var bind = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
{ {
if (mProxy != null) MaxReceivedMessageSize = 2147483647,
return; MaxBufferSize = 2147483647,
MaxBufferPoolSize = 2147483647,
var bind = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) ReaderQuotas =
{ {
MaxReceivedMessageSize = 2147483647, MaxArrayLength = 2147483647,
MaxBufferSize = 2147483647, MaxBytesPerRead = 2147483647,
MaxBufferPoolSize = 2147483647, MaxDepth = 2147483647,
ReaderQuotas = MaxStringContentLength = 2147483647,
{ MaxNameTableCharCount = 2147483647
MaxArrayLength = 2147483647, }
MaxBytesPerRead = 2147483647, };
MaxDepth = 2147483647, // Commented next statement since it is not required
MaxStringContentLength = 2147483647,
MaxNameTableCharCount = 2147483647
}
};
// Commented next statement since it is not required
mProxy = new MtApiProxy(new InstanceContext(this), bind, new EndpointAddress(urlService)); _proxy = new MtApiProxy(new InstanceContext(this), bind, new EndpointAddress(urlService));
mProxy.Faulted += mProxy_Faulted; _proxy.Faulted += ProxyFaulted;
}
} }
public void Close() public void Close()
{ {
Debug.WriteLine("[INFO] MtClient::Close"); Debug.WriteLine("[INFO] MtClient::Close");
lock (mClientLocker) if (_proxy != null)
{ {
if (mProxy != null) _proxy.Faulted -= ProxyFaulted;
{ _proxy.Dispose();
mProxy.Faulted -= mProxy_Faulted; _proxy = null;
mProxy.Dispose();
mProxy = null;
}
mIsConnected = false;
} }
_isConnected = false;
} }
public void Connect() public void Connect()
@@ -108,16 +104,13 @@ namespace MTApiService
try try
{ {
lock (mClientLocker) if (_proxy != null && _isConnected)
{ return;
if (mProxy != null && mIsConnected == true)
return;
mIsConnected = mProxy.Connect(); _isConnected = _proxy.Connect();
if (mIsConnected == false) if (_isConnected == false)
throw new Exception("Connected failed"); throw new Exception("Connected failed");
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -125,7 +118,7 @@ namespace MTApiService
Close(); Close();
throw new CommunicationException(string.Format("Connection failed to service")); throw new CommunicationException("Connection failed to service");
} }
} }
@@ -135,13 +128,9 @@ namespace MTApiService
try try
{ {
lock (mClientLocker) _isConnected = false;
{
mIsConnected = false;
if (mProxy != null) _proxy?.Disconnect();
mProxy.Disconnect();
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -159,11 +148,8 @@ namespace MTApiService
try try
{ {
lock (mClientLocker) if (_proxy != null && _isConnected)
{ result = _proxy.SendCommand(new MtCommand(commandType, commandParameters));
if (mProxy != null && mIsConnected == true)
result = mProxy.SendCommand(new MtCommand(commandType, commandParameters));
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -185,11 +171,8 @@ namespace MTApiService
try try
{ {
lock (mClientLocker) if (_proxy != null && _isConnected)
{ result = _proxy.GetQuotes();
if (mProxy != null && mIsConnected == true)
result = mProxy.GetQuotes();
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -209,15 +192,11 @@ namespace MTApiService
public void OnQuoteUpdate(MtQuote quote) public void OnQuoteUpdate(MtQuote quote)
{ {
if (quote != null) if (quote == null) return;
{
if (QuoteUpdated != null)
{
QuoteUpdated(quote);
}
Debug.WriteLine("[INFO] MtClient::OnQuoteUpdate: " + quote); QuoteUpdated?.Invoke(quote);
}
Debug.WriteLine("[INFO] MtClient::OnQuoteUpdate: " + quote);
} }
public void OnQuoteAdded(MtQuote quote) public void OnQuoteAdded(MtQuote quote)
@@ -252,31 +231,19 @@ namespace MTApiService
#endregion #endregion
#region Properties #region Properties
public bool IsConnected public bool IsConnected => _proxy.State == CommunicationState.Opened && _isConnected;
{
get
{
lock (mClientLocker)
{
return mProxy.State == CommunicationState.Opened && mIsConnected == true;
}
}
}
#endregion #endregion
#region Private Methods #region Private Methods
void mProxy_Faulted(object sender, EventArgs e) private void ProxyFaulted(object sender, EventArgs e)
{ {
Debug.WriteLine("[INFO] MtClient::mProxy_Faulted"); Debug.WriteLine("[INFO] MtClient::ProxyFaulted");
Close(); Close();
if (ServerFailed != null) ServerFailed?.Invoke(this, EventArgs.Empty);
{
ServerFailed(this, EventArgs.Empty);
}
} }
#endregion #endregion
@@ -300,11 +267,5 @@ namespace MTApiService
public event EventHandler ServerFailed; public event EventHandler ServerFailed;
public event EventHandler<MtEventArgs> MtEventReceived; public event EventHandler<MtEventArgs> MtEventReceived;
#endregion #endregion
#region Fields
private readonly object mClientLocker = new object();
private MtApiProxy mProxy = null;
private bool mIsConnected = false;
#endregion
} }
} }
-2
View File
@@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Collections; using System.Collections;
+2 -2
View File
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.21.0")] [assembly: AssemblyVersion("1.0.22.0")]
[assembly: AssemblyFileVersion("1.0.21.0")] [assembly: AssemblyFileVersion("1.0.22.0")]
+3 -12
View File
@@ -1,28 +1,19 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MtApi5 namespace MtApi5
{ {
static class ExtensionMethods internal static class ExtensionMethods
{ {
#region Event Methods #region Event Methods
public static void FireEvent(this EventHandler eventHandler, object sender) public static void FireEvent(this EventHandler eventHandler, object sender)
{ {
if (eventHandler != null) eventHandler?.Invoke(sender, EventArgs.Empty);
{
eventHandler(sender, EventArgs.Empty);
}
} }
public static void FireEvent<T>(this EventHandler<T> eventHandler, object sender, T e) public static void FireEvent<T>(this EventHandler<T> eventHandler, object sender, T e)
where T : EventArgs where T : EventArgs
{ {
if (eventHandler != null) eventHandler?.Invoke(sender, e);
{
eventHandler(sender, e);
}
} }
#endregion #endregion
+1
View File
@@ -35,6 +35,7 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration.Install" /> <Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
+407 -394
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -316,9 +316,9 @@
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Text="symbol_name"/> <TextBox Grid.Row="0" Text="symbolName"/>
<TextBox Grid.Row="1" Text="timeframe"/> <TextBox Grid.Row="1" Text="timeframe"/>
<TextBox Grid.Row="2" Text="start_pos"/> <TextBox Grid.Row="2" Text="startPos"/>
<TextBox Grid.Row="3" Text="count"/> <TextBox Grid.Row="3" Text="count"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding TimeSeriesValues.SymbolValue}"/> <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding TimeSeriesValues.SymbolValue}"/>
<ComboBox Grid.Row="1" Grid.Column="1" <ComboBox Grid.Row="1" Grid.Column="1"