mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-08 16:37:51 +00:00
Added MtEvent into MtApiService. Added event LastTimeBar into MtApi (MT4)
This commit is contained in:
@@ -60,6 +60,7 @@
|
||||
<Compile Include="MtApiProxy.cs" />
|
||||
<Compile Include="MtClient.cs" />
|
||||
<Compile Include="MtCommandTask.cs" />
|
||||
<Compile Include="MtEvent.cs" />
|
||||
<Compile Include="MtMqlBookInfo.cs" />
|
||||
<Compile Include="MtMqlRates.cs" />
|
||||
<Compile Include="MtMqlTick.cs" />
|
||||
|
||||
+10
-12
@@ -224,20 +224,14 @@ namespace MTApiService
|
||||
{
|
||||
Debug.WriteLine("[INFO] MtClient::OnQuoteAdded");
|
||||
|
||||
if (QuoteAdded != null)
|
||||
{
|
||||
QuoteAdded(quote);
|
||||
}
|
||||
QuoteAdded?.Invoke(quote);
|
||||
}
|
||||
|
||||
public void OnQuoteRemoved(MtQuote quote)
|
||||
{
|
||||
Debug.WriteLine("[INFO] MtClient::OnQuoteRemoved");
|
||||
|
||||
if (QuoteRemoved != null)
|
||||
{
|
||||
QuoteRemoved(quote);
|
||||
}
|
||||
QuoteRemoved?.Invoke(quote);
|
||||
}
|
||||
|
||||
public void OnServerStopped()
|
||||
@@ -246,10 +240,13 @@ namespace MTApiService
|
||||
|
||||
Close();
|
||||
|
||||
if (ServerDisconnected != null)
|
||||
{
|
||||
ServerDisconnected(this, EventArgs.Empty);
|
||||
}
|
||||
ServerDisconnected?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
|
||||
public void OnMtEvent(MtEvent mtEvent)
|
||||
{
|
||||
MtEventReceived?.Invoke(this, new MtEventArgs(mtEvent));
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -301,6 +298,7 @@ namespace MTApiService
|
||||
public event MtQuoteHandler QuoteUpdated;
|
||||
public event EventHandler ServerDisconnected;
|
||||
public event EventHandler ServerFailed;
|
||||
public event EventHandler<MtEventArgs> MtEventReceived;
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace MTApiService
|
||||
{
|
||||
[DataContract]
|
||||
public class MtEvent
|
||||
{
|
||||
[DataMember]
|
||||
public int EventType { get; private set; }
|
||||
|
||||
[DataMember]
|
||||
public string Payload { get; private set; }
|
||||
|
||||
public MtEvent(int eventType, string payload)
|
||||
{
|
||||
EventType = eventType;
|
||||
Payload = payload;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "MtEvent = " + EventType + ", Payload = " + Payload;
|
||||
}
|
||||
}
|
||||
|
||||
public class MtEventArgs: EventArgs
|
||||
{
|
||||
public MtEventArgs(MtEvent e)
|
||||
{
|
||||
Event = e;
|
||||
}
|
||||
|
||||
public MtEvent Event { get; private set; }
|
||||
}
|
||||
}
|
||||
+15
-15
@@ -66,6 +66,7 @@ namespace MTApiService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
@@ -114,6 +115,11 @@ namespace MTApiService
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void SendEvent(MtEvent mtEvent)
|
||||
{
|
||||
FireOnMtEvent(mtEvent);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IMtCommandExecutor
|
||||
@@ -134,29 +140,22 @@ namespace MTApiService
|
||||
|
||||
private void FireOnQuoteChanged(MtQuote quote)
|
||||
{
|
||||
var handler = QuoteChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, quote);
|
||||
}
|
||||
QuoteChanged?.Invoke(this, quote);
|
||||
}
|
||||
|
||||
private void FireOnDeinited()
|
||||
{
|
||||
var handler = Deinited;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, EventArgs.Empty);
|
||||
}
|
||||
Deinited?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private void FireOnCommandExecuted()
|
||||
{
|
||||
var handler = CommandExecuted;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, EventArgs.Empty);
|
||||
}
|
||||
CommandExecuted?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private void FireOnMtEvent(MtEvent mtEvent)
|
||||
{
|
||||
OnMtEvent?.Invoke(this, new MtEventArgs(mtEvent));
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -164,6 +163,7 @@ namespace MTApiService
|
||||
public event EventHandler Deinited;
|
||||
public event MtQuoteHandler QuoteChanged;
|
||||
public event EventHandler CommandExecuted;
|
||||
public event EventHandler<MtEventArgs> OnMtEvent;
|
||||
#endregion
|
||||
|
||||
#region Private Fields
|
||||
|
||||
@@ -107,6 +107,7 @@ namespace MTApiService
|
||||
{
|
||||
expert.Deinited += expert_Deinited;
|
||||
expert.QuoteChanged += expert_QuoteChanged;
|
||||
expert.OnMtEvent += Expert_OnMtEvent;
|
||||
|
||||
lock (_experts)
|
||||
{
|
||||
@@ -308,13 +309,14 @@ namespace MTApiService
|
||||
_service.QuoteUpdate(quote);
|
||||
}
|
||||
|
||||
private void Expert_OnMtEvent(object sender, MtEventArgs e)
|
||||
{
|
||||
_service.OnMtEvent(e.Event);
|
||||
}
|
||||
|
||||
private void FireOnStopped()
|
||||
{
|
||||
var handler = Stopped;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, EventArgs.Empty);
|
||||
}
|
||||
Stopped?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -101,6 +101,24 @@ namespace MTApiService
|
||||
Debug.WriteLine("MtApiServerInstance::SendQuote: finish.");
|
||||
}
|
||||
|
||||
public void SendEvent(int expertHandle, int eventType, string payload)
|
||||
{
|
||||
Debug.WriteLine("MtApiServerInstance::SendEvent called. eventType = {0}, payload = {1}", eventType, payload);
|
||||
|
||||
MtExpert expert = null;
|
||||
lock (mExpertsDictionary)
|
||||
{
|
||||
expert = mExpertsDictionary[expertHandle];
|
||||
}
|
||||
|
||||
if (expert != null)
|
||||
{
|
||||
expert.SendEvent(new MtEvent(eventType, payload));
|
||||
}
|
||||
|
||||
Debug.WriteLine("MtApiServerInstance::SendEvent: finished");
|
||||
}
|
||||
|
||||
public void SendResponse(int expertHandle, MtResponse response)
|
||||
{
|
||||
Debug.WriteLine("MtApiServerInstance::SendResponse: id = {0}, response = {1}", expertHandle, response);
|
||||
|
||||
+38
-132
@@ -35,6 +35,9 @@ namespace MTApiService
|
||||
|
||||
[OperationContract(IsOneWay = true)]
|
||||
void OnQuoteRemoved(MtQuote quote);
|
||||
|
||||
[OperationContract(IsOneWay = true)]
|
||||
void OnMtEvent(MtEvent ntEvent);
|
||||
}
|
||||
|
||||
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
|
||||
@@ -123,171 +126,74 @@ namespace MTApiService
|
||||
#region Public Methods
|
||||
public void OnStopServer()
|
||||
{
|
||||
try
|
||||
{
|
||||
mClientsLocker.AcquireReaderLock(2000);
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var callback in mClientCallbacks)
|
||||
{
|
||||
try
|
||||
{
|
||||
callback.OnServerStopped();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
mClientsLocker.ReleaseReaderLock();
|
||||
}
|
||||
}
|
||||
catch (ApplicationException)
|
||||
{
|
||||
}
|
||||
ExecuteCallbackAction(a => a.OnServerStopped());
|
||||
}
|
||||
|
||||
public void QuoteUpdate(MtQuote quote)
|
||||
{
|
||||
try
|
||||
{
|
||||
mClientsLocker.AcquireReaderLock(200);
|
||||
|
||||
List<IMtApiCallback> crashedClientsCallback = null;
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var callback in mClientCallbacks)
|
||||
{
|
||||
try
|
||||
{
|
||||
callback.OnQuoteUpdate(quote);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (crashedClientsCallback == null)
|
||||
crashedClientsCallback = new List<IMtApiCallback>();
|
||||
|
||||
crashedClientsCallback.Add(callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
mClientsLocker.ReleaseReaderLock();
|
||||
}
|
||||
|
||||
removeCrashedClientCallbacks(crashedClientsCallback);
|
||||
}
|
||||
catch (ApplicationException)
|
||||
{
|
||||
}
|
||||
ExecuteCallbackAction(a => a.OnQuoteUpdate(quote));
|
||||
}
|
||||
|
||||
public void OnQuoteAdded(MtQuote quote)
|
||||
{
|
||||
try
|
||||
{
|
||||
mClientsLocker.AcquireReaderLock(2000);
|
||||
|
||||
List<IMtApiCallback> crashedClientsCallback = null;
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var callback in mClientCallbacks)
|
||||
{
|
||||
try
|
||||
{
|
||||
callback.OnQuoteAdded(quote);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (crashedClientsCallback == null)
|
||||
crashedClientsCallback = new List<IMtApiCallback>();
|
||||
|
||||
crashedClientsCallback.Add(callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
mClientsLocker.ReleaseReaderLock();
|
||||
}
|
||||
|
||||
removeCrashedClientCallbacks(crashedClientsCallback);
|
||||
}
|
||||
catch (ApplicationException)
|
||||
{
|
||||
}
|
||||
ExecuteCallbackAction(a => a.OnQuoteAdded(quote));
|
||||
}
|
||||
|
||||
public void OnQuoteRemoved(MtQuote quote)
|
||||
{
|
||||
try
|
||||
{
|
||||
mClientsLocker.AcquireReaderLock(2000);
|
||||
ExecuteCallbackAction(a => a.OnQuoteRemoved(quote));
|
||||
}
|
||||
|
||||
List<IMtApiCallback> crashedClientsCallback = null;
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var callback in mClientCallbacks)
|
||||
{
|
||||
try
|
||||
{
|
||||
callback.OnQuoteRemoved(quote);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (crashedClientsCallback == null)
|
||||
crashedClientsCallback = new List<IMtApiCallback>();
|
||||
|
||||
crashedClientsCallback.Add(callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
mClientsLocker.ReleaseReaderLock();
|
||||
}
|
||||
|
||||
removeCrashedClientCallbacks(crashedClientsCallback);
|
||||
}
|
||||
catch (ApplicationException)
|
||||
{
|
||||
}
|
||||
public void OnMtEvent(MtEvent mtEvent)
|
||||
{
|
||||
ExecuteCallbackAction(a => a.OnMtEvent(mtEvent));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void removeCrashedClientCallbacks(List<IMtApiCallback> crashedClientCallbacks)
|
||||
private void ExecuteCallbackAction(Action<IMtApiCallback> action)
|
||||
{
|
||||
if (crashedClientCallbacks != null)
|
||||
try
|
||||
{
|
||||
mClientsLocker.AcquireReaderLock(2000);
|
||||
|
||||
List<IMtApiCallback> crashedClientCallbacks = null;
|
||||
|
||||
try
|
||||
{
|
||||
mClientsLocker.AcquireWriterLock(200);
|
||||
foreach (var callback in mClientCallbacks)
|
||||
{
|
||||
try
|
||||
{
|
||||
action(callback);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (crashedClientCallbacks == null)
|
||||
crashedClientCallbacks = new List<IMtApiCallback>();
|
||||
|
||||
try
|
||||
crashedClientCallbacks.Add(callback);
|
||||
}
|
||||
}
|
||||
|
||||
if (crashedClientCallbacks != null)
|
||||
{
|
||||
foreach (var crashedCallback in crashedClientCallbacks)
|
||||
{
|
||||
mClientCallbacks.Remove(crashedCallback);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
mClientsLocker.ReleaseWriterLock();
|
||||
}
|
||||
}
|
||||
catch (ApplicationException)
|
||||
finally
|
||||
{
|
||||
mClientsLocker.ReleaseReaderLock();
|
||||
}
|
||||
}
|
||||
catch (ApplicationException)
|
||||
{
|
||||
//TODO: add logging
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user