forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionState.cs
executable file
·429 lines (369 loc) · 14.6 KB
/
SessionState.cs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
using System;
using System.Collections.Generic;
using QuickFix.Logger;
using QuickFix.Store;
using MessagesBySeqNum = System.Collections.Generic.Dictionary<ulong, QuickFix.Message>;
namespace QuickFix
{
// v2 TODO - consider making this internal
/// <summary>
/// Used by the session communications code. Not intended to be used by applications.
/// </summary>
public class SessionState : IDisposable
{
#region Private Members
private readonly object _sync = new object();
private bool _isEnabled = true;
private bool _receivedLogon = false;
private bool _receivedReset = false;
private bool _sentLogon = false;
private bool _sentLogout = false;
private bool _sentReset = false;
private string _logoutReason = "";
private int _testRequestCounter = 0;
private int _heartBtInt = 0;
private int _heartBtIntAsMilliSecs = 0;
private DateTime _lastReceivedTimeDt = DateTime.MinValue;
private DateTime _lastSentTimeDt = DateTime.MinValue;
private int _logonTimeout = 10;
private long _logonTimeoutAsMilliSecs = 10 * 1000;
private int _logoutTimeout = 2;
private long _logoutTimeoutAsMilliSecs = 2 * 1000;
private readonly ResendRange _resendRange = new ();
private MessagesBySeqNum _msgQueue = new ();
#endregion
#region Unsynchronized Properties
public IMessageStore MessageStore
{ get; }
public bool IsInitiator
{ get; set; }
public bool ShouldSendLogon => IsInitiator && !SentLogon;
public ILog Log { get; }
#endregion
#region Synchronized Properties
public bool IsEnabled
{
get { lock (_sync) { return _isEnabled; } }
set { lock (_sync) { _isEnabled = value; } }
}
public bool ReceivedLogon
{
get { lock (_sync) { return _receivedLogon; } }
set { lock (_sync) { _receivedLogon = value; } }
}
public bool ReceivedReset
{
get { lock (_sync) { return _receivedReset; } }
set { lock (_sync) { _receivedReset = value; } }
}
public bool SentLogon
{
get { lock (_sync) { return _sentLogon; } }
set { lock (_sync) { _sentLogon = value; } }
}
public bool SentLogout
{
get { lock (_sync) { return _sentLogout; } }
set { lock (_sync) { _sentLogout = value; } }
}
public bool SentReset
{
get { lock (_sync) { return _sentReset; } }
set { lock (_sync) { _sentReset = value; } }
}
public string LogoutReason
{
get { lock (_sync) { return _logoutReason; } }
set { lock (_sync) { _logoutReason = value; } }
}
public int TestRequestCounter
{
get { lock (_sync) { return _testRequestCounter; } }
set { lock (_sync) { _testRequestCounter = value; } }
}
public int HeartBtInt
{
get { lock (_sync) { return _heartBtInt; } }
set { lock (_sync) { _heartBtInt = value; _heartBtIntAsMilliSecs = 1000 * value; } }
}
public int HeartBtIntAsMilliSecs
{
get { lock (_sync) { return _heartBtIntAsMilliSecs; } }
}
public DateTime LastReceivedTimeDT
{
get { lock (_sync) { return _lastReceivedTimeDt; } }
set { lock (_sync) { _lastReceivedTimeDt = value; } }
}
public DateTime LastSentTimeDT
{
get { lock (_sync) { return _lastSentTimeDt; } }
set { lock (_sync) { _lastSentTimeDt = value; } }
}
public int LogonTimeout
{
get { lock (_sync) { return _logonTimeout; } }
set { lock (_sync) { _logonTimeout = value; _logonTimeoutAsMilliSecs = 1000 * value; } }
}
public long LogonTimeoutAsMilliSecs
{
get { lock (_sync) { return _logonTimeoutAsMilliSecs; } }
}
public int LogoutTimeout
{
get { lock (_sync) { return _logoutTimeout; } }
set { lock (_sync) { _logoutTimeout = value; _logoutTimeoutAsMilliSecs = 1000 * value; } }
}
public long LogoutTimeoutAsMilliSecs
{
get { lock (_sync) { return _logoutTimeoutAsMilliSecs; } }
}
private MessagesBySeqNum MsgQueue
{
get { lock (_sync) { return _msgQueue; } }
set { lock (_sync) { _msgQueue = value; } }
}
#endregion
public SessionState(bool isInitiator, ILog log, int heartBtInt, IMessageStore messageStore)
{
Log = log;
HeartBtInt = heartBtInt;
IsInitiator = isInitiator;
_lastReceivedTimeDt = DateTime.UtcNow;
_lastSentTimeDt = DateTime.UtcNow;
MessageStore = messageStore;
}
/// <summary>
/// All time args are in milliseconds
/// </summary>
/// <param name="now">current system time</param>
/// <param name="lastReceivedTime">last received time</param>
/// <param name="logonTimeout">number of milliseconds to wait for a Logon from the counterparty</param>
/// <returns></returns>
public static bool LogonTimedOut(DateTime now, long logonTimeout, DateTime lastReceivedTime)
{
return now.Subtract(lastReceivedTime).TotalMilliseconds >= logonTimeout;
}
public bool LogonTimedOut()
{
return LogonTimedOut(DateTime.UtcNow, LogonTimeoutAsMilliSecs, LastReceivedTimeDT);
}
/// <summary>
/// All time args are in milliseconds
/// </summary>
/// <param name="now">current system datetime</param>
/// <param name="heartBtIntMillis">heartbeat interval in milliseconds</param>
/// <param name="lastReceivedTime">last received datetime</param>
/// <returns>true if timed out</returns>
public static bool TimedOut(DateTime now, int heartBtIntMillis, DateTime lastReceivedTime)
{
double elapsed = now.Subtract(lastReceivedTime).TotalMilliseconds;
return elapsed >= (2.4 * heartBtIntMillis);
}
public bool TimedOut()
{
return TimedOut(DateTime.UtcNow, HeartBtIntAsMilliSecs, LastReceivedTimeDT);
}
/// <summary>
/// All time args are in milliseconds
/// </summary>
/// <param name="now">current system time</param>
/// <param name="sentLogout">true if a Logout has been sent to the counterparty, otherwise false</param>
/// <param name="logoutTimeout">number of milliseconds to wait for a Logout from the counterparty</param>
/// <param name="lastSentTime">last sent time</param>
/// <returns></returns>
public static bool LogoutTimedOut(DateTime now, bool sentLogout, long logoutTimeout, DateTime lastSentTime)
{
return sentLogout && (now.Subtract(lastSentTime).TotalMilliseconds >= logoutTimeout);
}
public bool LogoutTimedOut()
{
return LogoutTimedOut(DateTime.UtcNow, SentLogout, LogoutTimeoutAsMilliSecs, LastSentTimeDT);
}
/// <summary>
/// All time args are in milliseconds
/// </summary>
/// <param name="now">current system time</param>
/// <param name="heartBtIntMillis">heartbeat interval in milliseconds</param>
/// <param name="lastReceivedTime">last received time</param>
/// <param name="testRequestCounter">test request counter</param>
/// <returns>true if test request is needed</returns>
public static bool NeedTestRequest(DateTime now, int heartBtIntMillis, DateTime lastReceivedTime, int testRequestCounter)
{
double elapsedMilliseconds = now.Subtract(lastReceivedTime).TotalMilliseconds;
return elapsedMilliseconds >= (1.2 * ((testRequestCounter + 1) * heartBtIntMillis));
}
public bool NeedTestRequest()
{
return NeedTestRequest(DateTime.UtcNow, HeartBtIntAsMilliSecs, LastReceivedTimeDT, TestRequestCounter);
}
/// <summary>
/// All time args are in milliseconds
/// </summary>
/// <param name="now">current system time</param>
/// <param name="heartBtIntMillis">heartbeat interval in milliseconds</param>
/// <param name="lastSentTime">last sent time</param>
/// <param name="testRequestCounter">test request counter</param>
/// <returns>true if heartbeat is needed</returns>
public static bool NeedHeartbeat(DateTime now, int heartBtIntMillis, DateTime lastSentTime, int testRequestCounter)
{
double elapsed = now.Subtract(lastSentTime).TotalMilliseconds;
return (elapsed >= Convert.ToDouble(heartBtIntMillis)) && (0 == testRequestCounter);
}
public bool NeedHeartbeat()
{
return NeedHeartbeat(DateTime.UtcNow, HeartBtIntAsMilliSecs, LastSentTimeDT, TestRequestCounter);
}
/// <summary>
/// All time args are in milliseconds
/// </summary>
/// <param name="now">current system time</param>
/// <param name="heartBtIntMillis">heartbeat interval in milliseconds</param>
/// <param name="lastSentTime">last sent time</param>
/// <param name="lastReceivedTime">last received time</param>
/// <returns>true if within heartbeat interval</returns>
public static bool WithinHeartbeat(DateTime now, int heartBtIntMillis, DateTime lastSentTime, DateTime lastReceivedTime)
{
return (now.Subtract(lastSentTime).TotalMilliseconds < Convert.ToDouble(heartBtIntMillis))
&& (now.Subtract(lastReceivedTime).TotalMilliseconds < Convert.ToDouble(heartBtIntMillis));
}
public bool WithinHeartbeat()
{
return WithinHeartbeat(DateTime.UtcNow, HeartBtIntAsMilliSecs, LastSentTimeDT, LastReceivedTimeDT);
}
public ResendRange GetResendRange()
{
return _resendRange;
}
public void Get(SeqNumType begSeqNo, SeqNumType endSeqNo, List<string> messages)
{
lock (_sync)
{
MessageStore.Get(begSeqNo, endSeqNo, messages);
}
}
public void SetResendRange(SeqNumType begin, SeqNumType end, SeqNumType chunkEnd = ResendRange.NOT_SET)
{
_resendRange.BeginSeqNo = begin;
_resendRange.EndSeqNo = end;
_resendRange.ChunkEndSeqNo = chunkEnd == ResendRange.NOT_SET ? end : chunkEnd;
}
public bool ResendRequested()
{
return !(_resendRange.BeginSeqNo == 0 && _resendRange.EndSeqNo == 0);
}
public void Queue(SeqNumType msgSeqNum, Message msg)
{
if (!MsgQueue.ContainsKey(msgSeqNum))
{
MsgQueue.Add(msgSeqNum, msg);
}
}
public void ClearQueue()
{
MsgQueue.Clear();
}
public QuickFix.Message? Dequeue(SeqNumType num)
{
if (MsgQueue.ContainsKey(num))
{
QuickFix.Message msg = MsgQueue[num];
MsgQueue.Remove(num);
return msg;
}
return null;
}
public Message? Retrieve(SeqNumType msgSeqNum)
{
if (MsgQueue.ContainsKey(msgSeqNum))
{
Message msg = MsgQueue[msgSeqNum];
MsgQueue.Remove(msgSeqNum);
return msg;
}
return null;
}
/// <summary>
/// All time values are displayed in milliseconds.
/// </summary>
/// <returns>a string that represents the session state</returns>
public override string ToString()
{
return new System.Text.StringBuilder("SessionState ")
.Append("[ Now=").Append(DateTime.UtcNow)
.Append(", HeartBtInt=").Append(HeartBtIntAsMilliSecs)
.Append(", LastSentTime=").Append(LastSentTimeDT)
.Append(", LastReceivedTime=").Append(LastReceivedTimeDT)
.Append(", TestRequestCounter=").Append(TestRequestCounter)
.Append(", WithinHeartbeat=").Append(WithinHeartbeat())
.Append(", NeedHeartbeat=").Append(NeedHeartbeat())
.Append(", NeedTestRequest=").Append(NeedTestRequest())
.Append(", ResendRange=").Append(GetResendRange())
.Append(" ]").ToString();
}
#region MessageStore-manipulating Members
public bool Set(SeqNumType msgSeqNum, string msg)
{
lock (_sync) { return MessageStore.Set(msgSeqNum, msg); }
}
public SeqNumType NextSenderMsgSeqNum
{
get { lock (_sync) { return MessageStore.NextSenderMsgSeqNum; } }
set { lock (_sync) { MessageStore.NextSenderMsgSeqNum = value; } }
}
public SeqNumType NextTargetMsgSeqNum
{
get { lock (_sync) { return MessageStore.NextTargetMsgSeqNum; } }
set { lock (_sync) { MessageStore.NextTargetMsgSeqNum = value; } }
}
public void IncrNextSenderMsgSeqNum()
{
lock (_sync) { MessageStore.IncrNextSenderMsgSeqNum(); }
}
public void IncrNextTargetMsgSeqNum()
{
lock (_sync) { MessageStore.IncrNextTargetMsgSeqNum(); }
}
public DateTime? CreationTime
{
get
{
lock (_sync) { return MessageStore.CreationTime; }
}
}
/// <summary>
/// Reset the MessageStore
/// </summary>
/// <param name="reason">this is for the log statement</param>
public void Reset(string reason)
{
lock (_sync)
{
MessageStore.Reset();
Log.OnEvent("Session reset: " + reason);
}
}
public void Refresh()
{
lock (_sync) { MessageStore.Refresh(); }
}
#endregion
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool _disposed = false;
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
Log.Dispose();
MessageStore.Dispose();
}
_disposed = true;
}
~SessionState() => Dispose(false);
}
}