mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-13 02:38:07 +00:00
Removed ConnectionManager from MtApi4 and MtApi5 installers. Api server side listens all newtwork connections
This commit is contained in:
+123
-66
@@ -8,6 +8,7 @@ using System.Net;
|
||||
using System.Diagnostics;
|
||||
using System.ServiceModel.Channels;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace MTApiService
|
||||
{
|
||||
@@ -19,38 +20,28 @@ namespace MTApiService
|
||||
#endregion
|
||||
|
||||
#region ctor
|
||||
public MtServer(MtConnectionProfile profile)
|
||||
public MtServer(int port)
|
||||
{
|
||||
Profile = profile;
|
||||
Port = port;
|
||||
mService = new MtService(this);
|
||||
|
||||
mHosts = new List<ServiceHost>();
|
||||
|
||||
mExecutorManager = new MtCommandExecutorManager();
|
||||
mExecutorManager.CommandExecuted += mExecutorManager_CommandExecuted;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
public MtConnectionProfile Profile { get; private set; }
|
||||
public int Port { get; private set; }
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
public void Start()
|
||||
{
|
||||
lock (mHostLocker)
|
||||
{
|
||||
if (mHost != null)
|
||||
return;
|
||||
|
||||
ServiceHost host = new ServiceHost(mService);
|
||||
|
||||
string mServerUrlAdress = CreateConnectionAddress(Profile);
|
||||
Binding mBinding = CreateConnectionBinding(Profile);
|
||||
|
||||
host.AddServiceEndpoint(typeof(IMtApi), mBinding, mServerUrlAdress);
|
||||
host.Open();
|
||||
|
||||
mHost = host;
|
||||
}
|
||||
bool hostsInitialized = InitHosts(Port);
|
||||
if (hostsInitialized == false)
|
||||
return;
|
||||
|
||||
lock (mExpertsLocker)
|
||||
{
|
||||
@@ -58,6 +49,69 @@ namespace MTApiService
|
||||
}
|
||||
}
|
||||
|
||||
private bool InitHosts(int port)
|
||||
{
|
||||
lock (mHostLocker)
|
||||
{
|
||||
if (mHosts.Count > 0)
|
||||
return false;
|
||||
|
||||
//init local pipe host
|
||||
string localUrl = CreateConnectionAddress(null, port, true);
|
||||
ServiceHost localServiceHost = CreateServiceHost(localUrl, true);
|
||||
if (localServiceHost != null)
|
||||
{
|
||||
mHosts.Add(localServiceHost);
|
||||
}
|
||||
|
||||
//init network hosts
|
||||
IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());
|
||||
if (ips != null)
|
||||
{
|
||||
foreach (IPAddress ipAddress in ips.AddressList)
|
||||
{
|
||||
if (ipAddress != null)
|
||||
{
|
||||
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
string ip = ipAddress.ToString();
|
||||
string networkUrl = CreateConnectionAddress(ip, port, false);
|
||||
ServiceHost serviceHost = CreateServiceHost(networkUrl, false);
|
||||
if (serviceHost != null)
|
||||
{
|
||||
mHosts.Add(serviceHost);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private ServiceHost CreateServiceHost(string serverUrlAdress, bool local)
|
||||
{
|
||||
ServiceHost serviceHost = null;
|
||||
if (serverUrlAdress != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
serviceHost = new ServiceHost(mService);
|
||||
Binding binding = CreateConnectionBinding(local);
|
||||
|
||||
serviceHost.AddServiceEndpoint(typeof(IMtApi), binding, serverUrlAdress);
|
||||
serviceHost.Open();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Debug.WriteLine("CreateServiceHost: Create ServiceHost failed. " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return serviceHost;
|
||||
}
|
||||
|
||||
public void AddExpert(MtExpert expert)
|
||||
{
|
||||
if (expert != null)
|
||||
@@ -148,67 +202,64 @@ namespace MTApiService
|
||||
stopTimer.Elapsed -= stopTimer_Elapsed;
|
||||
}
|
||||
|
||||
private string CreateConnectionAddress(MtConnectionProfile profile)
|
||||
private string CreateConnectionAddress(string host, int port, bool local)
|
||||
{
|
||||
string connectionAddress = null;
|
||||
|
||||
if (profile != null)
|
||||
if (local == true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(profile.Host))
|
||||
//by Pipe
|
||||
connectionAddress = "net.pipe://localhost/MtApiService_" + port.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
//by Socket
|
||||
if (host != null)
|
||||
{
|
||||
//by Pipe
|
||||
connectionAddress = "net.pipe://localhost/MtApiService_" + profile.Port.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
//by Socket
|
||||
connectionAddress = "net.tcp://" + profile.Host.ToString() + ":" + profile.Port.ToString() + "/MtApiService";
|
||||
}
|
||||
connectionAddress = "net.tcp://" + host.ToString() + ":" + port.ToString() + "/MtApiService";
|
||||
}
|
||||
}
|
||||
|
||||
return connectionAddress;
|
||||
}
|
||||
|
||||
private static Binding CreateConnectionBinding(MtConnectionProfile profile)
|
||||
private static Binding CreateConnectionBinding(bool local)
|
||||
{
|
||||
Binding connectionBinding = null;
|
||||
|
||||
if (profile != null)
|
||||
if (local == true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(profile.Host))
|
||||
{
|
||||
//by Pipe
|
||||
var bind = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
|
||||
bind.MaxReceivedMessageSize = 2147483647;
|
||||
bind.MaxBufferSize = 2147483647;
|
||||
//by Pipe
|
||||
var bind = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
|
||||
bind.MaxReceivedMessageSize = 2147483647;
|
||||
bind.MaxBufferSize = 2147483647;
|
||||
|
||||
// Commented next statement since it is not required
|
||||
bind.MaxBufferPoolSize = 2147483647;
|
||||
bind.ReaderQuotas.MaxArrayLength = 2147483647;
|
||||
bind.ReaderQuotas.MaxBytesPerRead = 2147483647;
|
||||
bind.ReaderQuotas.MaxDepth = 2147483647;
|
||||
bind.ReaderQuotas.MaxStringContentLength = 2147483647;
|
||||
bind.ReaderQuotas.MaxNameTableCharCount = 2147483647;
|
||||
// Commented next statement since it is not required
|
||||
bind.MaxBufferPoolSize = 2147483647;
|
||||
bind.ReaderQuotas.MaxArrayLength = 2147483647;
|
||||
bind.ReaderQuotas.MaxBytesPerRead = 2147483647;
|
||||
bind.ReaderQuotas.MaxDepth = 2147483647;
|
||||
bind.ReaderQuotas.MaxStringContentLength = 2147483647;
|
||||
bind.ReaderQuotas.MaxNameTableCharCount = 2147483647;
|
||||
|
||||
connectionBinding = bind;
|
||||
}
|
||||
else
|
||||
{
|
||||
//by Socket
|
||||
var bind = new NetTcpBinding(SecurityMode.None);
|
||||
bind.MaxReceivedMessageSize = 2147483647;
|
||||
bind.MaxBufferSize = 2147483647;
|
||||
connectionBinding = bind;
|
||||
}
|
||||
else
|
||||
{
|
||||
//by Socket
|
||||
var bind = new NetTcpBinding(SecurityMode.None);
|
||||
bind.MaxReceivedMessageSize = 2147483647;
|
||||
bind.MaxBufferSize = 2147483647;
|
||||
|
||||
// Commented next statement since it is not required
|
||||
bind.MaxBufferPoolSize = 2147483647;
|
||||
bind.ReaderQuotas.MaxArrayLength = 2147483647;
|
||||
bind.ReaderQuotas.MaxBytesPerRead = 2147483647;
|
||||
bind.ReaderQuotas.MaxDepth = 2147483647;
|
||||
bind.ReaderQuotas.MaxStringContentLength = 2147483647;
|
||||
bind.ReaderQuotas.MaxNameTableCharCount = 2147483647;
|
||||
// Commented next statement since it is not required
|
||||
bind.MaxBufferPoolSize = 2147483647;
|
||||
bind.ReaderQuotas.MaxArrayLength = 2147483647;
|
||||
bind.ReaderQuotas.MaxBytesPerRead = 2147483647;
|
||||
bind.ReaderQuotas.MaxDepth = 2147483647;
|
||||
bind.ReaderQuotas.MaxStringContentLength = 2147483647;
|
||||
bind.ReaderQuotas.MaxNameTableCharCount = 2147483647;
|
||||
|
||||
connectionBinding = bind;
|
||||
}
|
||||
connectionBinding = bind;
|
||||
}
|
||||
|
||||
return connectionBinding;
|
||||
@@ -220,23 +271,29 @@ namespace MTApiService
|
||||
|
||||
lock (mHostLocker)
|
||||
{
|
||||
if (mHost == null)
|
||||
if (mHosts.Count == 0)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
mHost.Close();
|
||||
foreach (var host in mHosts)
|
||||
{
|
||||
host.Close();
|
||||
}
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
mHost.Abort();
|
||||
foreach (var host in mHosts)
|
||||
{
|
||||
host.Abort();
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
mHost = null;
|
||||
mHosts.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,7 +374,7 @@ namespace MTApiService
|
||||
|
||||
#region Fields
|
||||
private readonly MtService mService;
|
||||
private ServiceHost mHost;
|
||||
private readonly List<ServiceHost> mHosts;
|
||||
|
||||
private readonly MtCommandExecutorManager mExecutorManager;
|
||||
private List<MtExpert> mExperts;
|
||||
|
||||
@@ -31,37 +31,22 @@ namespace MTApiService
|
||||
|
||||
|
||||
#region Public Methods
|
||||
public void InitExpert(int expertHandle, string profileName, string symbol, double bid, double ask, IMetaTraderHandler mtHandler)
|
||||
public void InitExpert(int expertHandle, int port, string symbol, double bid, double ask, IMetaTraderHandler mtHandler)
|
||||
{
|
||||
Debug.WriteLine("MtApiServerInstance::InitExpert: symbol = {0}, expertHandle = {1}, profileName = {2}", symbol, expertHandle, profileName);
|
||||
|
||||
if (profileName == null)
|
||||
{
|
||||
string errorMessage = string.Format("Connection profile is null or empty");
|
||||
throw new Exception(errorMessage);
|
||||
}
|
||||
Debug.WriteLine("MtApiServerInstance::InitExpert: symbol = {0}, expertHandle = {1}, port = {2}", symbol, expertHandle, port);
|
||||
|
||||
MtServer server = null;
|
||||
lock (mServersDictionary)
|
||||
{
|
||||
if (mServersDictionary.ContainsKey(profileName))
|
||||
if (mServersDictionary.ContainsKey(port))
|
||||
{
|
||||
server = mServersDictionary[profileName];
|
||||
server = mServersDictionary[port];
|
||||
}
|
||||
else
|
||||
{
|
||||
var profile = MtRegistryManager.LoadConnectionProfile(profileName);
|
||||
|
||||
if (profile == null)
|
||||
{
|
||||
string errorMessage = string.Format("Connection profile '{0}' is not found", profileName);
|
||||
throw new Exception(errorMessage);
|
||||
}
|
||||
|
||||
server = new MtServer(profile);
|
||||
server = new MtServer(port);
|
||||
server.Stopped += new EventHandler(server_Stopped);
|
||||
|
||||
mServersDictionary[profile.Name] = server;
|
||||
mServersDictionary[port] = server;
|
||||
|
||||
server.Start();
|
||||
}
|
||||
@@ -69,7 +54,7 @@ namespace MTApiService
|
||||
|
||||
var expert = new MtExpert(expertHandle, new MtQuote(symbol, bid, ask), mtHandler);
|
||||
|
||||
lock(mExpertsDictionary)
|
||||
lock (mExpertsDictionary)
|
||||
{
|
||||
mExpertsDictionary[expert.Handle] = expert;
|
||||
}
|
||||
@@ -167,15 +152,12 @@ namespace MTApiService
|
||||
MtServer server = (MtServer)sender;
|
||||
server.Stopped -= server_Stopped;
|
||||
|
||||
var profile = server.Profile;
|
||||
if (profile != null)
|
||||
var port = server.Port;
|
||||
lock (mServersDictionary)
|
||||
{
|
||||
lock (mServersDictionary)
|
||||
if (mServersDictionary.ContainsKey(port))
|
||||
{
|
||||
if (mServersDictionary.ContainsKey(profile.Name))
|
||||
{
|
||||
mServersDictionary.Remove(profile.Name);
|
||||
}
|
||||
mServersDictionary.Remove(port);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,7 +165,7 @@ namespace MTApiService
|
||||
|
||||
#region Fields
|
||||
private readonly MtRegistryManager mConnectionManager = new MtRegistryManager();
|
||||
private readonly Dictionary<string, MtServer> mServersDictionary = new Dictionary<string, MtServer>();
|
||||
private readonly Dictionary<int, MtServer> mServersDictionary = new Dictionary<int, MtServer>();
|
||||
private readonly Dictionary<int, MtExpert> mExpertsDictionary = new Dictionary<int, MtExpert>();
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.15.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.15.0")]
|
||||
[assembly: AssemblyVersion("1.0.16.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.16.0")]
|
||||
|
||||
Reference in New Issue
Block a user