-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathXProvider.EventEmitter.cs
More file actions
168 lines (149 loc) · 5.64 KB
/
Copy pathXProvider.EventEmitter.cs
File metadata and controls
168 lines (149 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using SmartQuant;
namespace QuantBox
{
public partial class XProvider
{
#region Unimportance
internal interface IEventEmitter
{
void EmitData(DataObject data, bool queued = true);
void EmitExecutionReport(ExecutionReport report, bool queued = true);
void EmitAccountData(AccountData data);
void EmitAccountReport(AccountReport report, bool queued = true);
void Open();
void Close();
}
private class EventEmitter : IEventEmitter
{
private readonly XProvider _provider;
public EventEmitter(XProvider provider)
{
_provider = provider;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EmitData(DataObject data, bool queued = true)
{
_provider.EmitData(data, queued);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EmitExecutionReport(ExecutionReport report, bool queued = true)
{
_provider.EmitExecutionReport(report, queued);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EmitAccountData(AccountData data)
{
_provider.EmitAccountData(data);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EmitAccountReport(AccountReport report, bool queued = true)
{
_provider.EmitAccountReport(report, queued);
}
public void Open()
{
}
public void Close()
{
}
}
#endregion
private class EventDebugEmitter : IEventEmitter
{
private readonly XProvider _provider;
private readonly EventPipe _dataPipe;
private readonly EventQueue _dataQueue;
private readonly EventPipe _executionPipe;
private readonly EventQueue _executionQueue;
private readonly EventQueue _accountQueue;
private Thread _thread;
private bool _exit;
private void ThreadRun()
{
while (true) {
if (_exit) {
return;
}
Event e = null;
if (!_executionPipe.IsEmpty()) {
e = _executionPipe.Read();
}
if (!_dataPipe.IsEmpty() && e == null) {
e = _dataPipe.Read();
}
if (e != null) {
switch (e.TypeId) {
case EventType.AccountData:
_provider.EmitAccountData((AccountData)e);
break;
case EventType.AccountReport:
_provider.EmitAccountReport((AccountReport)e);
break;
case EventType.ExecutionReport:
_provider.EmitExecutionReport((ExecutionReport)e);
break;
default:
_provider.EmitData((DataObject)e);
break;
}
}
if (e == null) {
Thread.Sleep(1);
}
}
}
public EventDebugEmitter(XProvider provider)
{
_provider = provider;
_dataPipe = new EventPipe(provider.framework);
_dataQueue = new EventQueue(1, 0, 2, 25000, provider.framework.EventBus);
_dataPipe.Add(_dataQueue);
_executionPipe = new EventPipe(provider.framework);
_executionQueue = new EventQueue(2, 0, 2, 25000, provider.framework.EventBus);
_executionPipe.Add(_executionQueue);
_accountQueue = new EventQueue(2, 0, 2, 100, provider.framework.EventBus);
_executionPipe.Add(_accountQueue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EmitData(DataObject data, bool queued = true)
{
_dataQueue.Enqueue(data);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EmitExecutionReport(ExecutionReport report, bool queued = true)
{
_executionQueue.Enqueue(report);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EmitAccountData(AccountData data)
{
_accountQueue.Enqueue(data);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EmitAccountReport(AccountReport report, bool queued = true)
{
_accountQueue.Enqueue(report);
}
public void Open()
{
if (_thread == null) {
_exit = false;
_dataQueue.Clear();
_executionQueue.Clear();
_thread = new Thread(ThreadRun);
_thread.IsBackground = true;
_thread.Start();
}
}
public void Close()
{
_exit = true;
_thread?.Join();
_thread = null;
}
}
}
}