forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.cs
554 lines (480 loc) · 20 KB
/
Message.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickFix.Fields;
namespace QuickFix
{
public class Header : FieldMap
{
public int[] HEADER_FIELD_ORDER = { Fields.Tags.BeginString, Fields.Tags.BodyLength, Fields.Tags.MsgType };
public Header()
: base()
{ }
public Header(Header src)
: base(src)
{ }
public override string CalculateString()
{
return base.CalculateString(new StringBuilder(), HEADER_FIELD_ORDER);
}
public override string CalculateString(StringBuilder sb, int[] preFields)
{
return base.CalculateString(sb, HEADER_FIELD_ORDER);
}
}
public class Trailer : FieldMap
{
public Trailer()
: base()
{ }
public Trailer(Trailer src)
: base(src)
{ }
}
public class Message : FieldMap
{
public const string SOH = "\u0001";
private int field_ = 0;
private bool validStructure_;
#region Properties
public Header Header { get; private set; }
public Trailer Trailer { get; private set; }
#endregion
#region Constructors
public Message()
{
this.Header = new Header();
this.Trailer = new Trailer();
this.validStructure_ = true;
}
public Message(string msgstr)
: this(msgstr, true)
{ }
public Message(string msgstr, bool validate)
: this(msgstr, null, null, validate)
{ }
public Message(string msgstr, DataDictionaryParser dataDictionary, bool validate)
: this()
{
FromString(msgstr, validate, dataDictionary, dataDictionary);
}
public Message(string msgstr, DataDictionaryParser sessionDataDictionary, DataDictionaryParser applicationDataDictionary, bool validate)
: this()
{
FromStringHeader(msgstr);
if(IsAdmin())
FromString(msgstr, validate, sessionDataDictionary, sessionDataDictionary);
else
FromString(msgstr, validate, sessionDataDictionary, applicationDataDictionary);
}
public Message(Message src)
: base(src)
{
this.Header = new Header(src.Header);
this.Trailer = new Trailer(src.Trailer);
this.validStructure_ = src.validStructure_;
this.field_= src.field_;
}
#endregion
#region Static Methods
public static bool IsAdminMsgType(string msgType)
{
return msgType.Length == 1 && "0A12345h".IndexOf(msgType) != -1;
}
/// <summary>
/// Parse the MsgType from a FIX string
/// </summary>
/// <param name="msgstr">string of a FIX message</param>
/// <returns>MsgType object</returns>
public static Fields.MsgType IdentifyType(string msgstr)
{
int valbeg = msgstr.IndexOf("\u000135=") + 4;
if (valbeg.Equals(-1))
throw new MessageParseError("no tag 35 found in msg: " + msgstr);
int valend = msgstr.IndexOf("\u0001", valbeg);
if (valend.Equals(-1))
throw new MessageParseError("no SOH after tag 35 in msg: " + msgstr);
return (new Fields.MsgType(msgstr.Substring(valbeg, (valend - valbeg))));
}
public static Fields.StringField ExtractField(string msgstr, ref int pos, DataDictionaryParser sessionDD, DataDictionaryParser appDD)
{
try
{
int tagend = msgstr.IndexOf("=", pos);
int tag = Convert.ToInt32(msgstr.Substring(pos, tagend - pos));
pos = tagend + 1;
int fieldvalend = msgstr.IndexOf("\u0001", pos);
Fields.StringField field = new Fields.StringField(tag, msgstr.Substring(pos, fieldvalend - pos));
/** TODO data dict stuff
if (((null != sessionDD) && sessionDD.IsDataField(field.Tag)) || ((null != appDD) && appDD.IsDataField(field.Tag)))
{
string fieldLength = "";
// Assume length field is 1 less
int lenField = field.Tag - 1;
// Special case for Signature which violates above assumption
if (Fields.Tags.Signature.Equals(field.Tag))
lenField = Fields.Tags.SignatureLength;
if ((null != group) && group.isSetField(lenField))
{
fieldLength = group.GetField(lenField);
soh = equalSign + 1 + atol(fieldLength.c_str());
}
else if (isSetField(lenField))
{
fieldLength = getField(lenField);
soh = equalSign + 1 + atol(fieldLength.c_str());
}
}
*/
pos = fieldvalend + 1;
return field;
}
catch (System.ArgumentOutOfRangeException e)
{
throw new MessageParseError("Error at position (" + pos + ") while parsing msg (" + msgstr + ")", e);
}
catch (System.OverflowException e)
{
throw new MessageParseError("Error at position (" + pos + ") while parsing msg (" + msgstr + ")", e);
}
catch (System.FormatException e)
{
throw new MessageParseError("Error at position (" + pos + ") while parsing msg (" + msgstr + ")", e);
}
}
public static Fields.StringField ExtractField(string msgstr, ref int pos)
{
return ExtractField(msgstr, ref pos, null, null);
}
public static bool IsHeaderField(int tag)
{
switch (tag)
{
case Fields.Tags.BeginString:
case Fields.Tags.BodyLength:
case Fields.Tags.MsgType:
case Fields.Tags.SenderCompID:
case Fields.Tags.TargetCompID:
case Fields.Tags.OnBehalfOfCompID:
case Fields.Tags.DeliverToCompID:
case Fields.Tags.SecureDataLen:
case Fields.Tags.MsgSeqNum:
case Fields.Tags.SenderSubID:
case Fields.Tags.SenderLocationID:
case Fields.Tags.TargetSubID:
case Fields.Tags.TargetLocationID:
case Fields.Tags.OnBehalfOfSubID:
case Fields.Tags.OnBehalfOfLocationID:
case Fields.Tags.DeliverToSubID:
case Fields.Tags.DeliverToLocationID:
case Fields.Tags.PossDupFlag:
case Fields.Tags.PossResend:
case Fields.Tags.SendingTime:
case Fields.Tags.OrigSendingTime:
case Fields.Tags.XmlDataLen:
case Fields.Tags.XmlData:
case Fields.Tags.MessageEncoding:
case Fields.Tags.LastMsgSeqNumProcessed:
// case Fields.Tags.OnBehalfOfSendingTime: TODO
return true;
default:
return false;
}
}
public static bool IsHeaderField(int tag, DataDictionaryParser dd)
{
if (IsHeaderField(tag))
return true;
if (null != dd)
return dd.IsHeaderField(tag);
return false;
}
public static bool IsTrailerField(int tag)
{
switch (tag)
{
case Fields.Tags.SignatureLength:
case Fields.Tags.Signature:
case Fields.Tags.CheckSum:
return true;
default:
return false;
}
}
public static bool IsTrailerField(int tag, DataDictionaryParser dd)
{
if (IsTrailerField(tag))
return true;
if (null != dd)
return dd.IsTrailerField(tag);
return false;
}
public static string GetFieldOrDefault(FieldMap fields, int tag, string defaultValue)
{
if (!fields.isSetField(tag))
return defaultValue;
try
{
return fields.GetField(tag);
}
catch (FieldNotFoundException)
{
return defaultValue;
}
}
public static SessionID GetReverseSessionID(Message msg)
{
return new SessionID(
GetFieldOrDefault(msg.Header, Fields.Tags.BeginString, null),
GetFieldOrDefault(msg.Header, Fields.Tags.TargetCompID, null),
GetFieldOrDefault(msg.Header, Fields.Tags.SenderCompID, null)
);
}
/// <summary>
/// FIXME totally bogus
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static SessionID GetReverseSessionID(string msg)
{
Message FIXME = new Message(msg);
return GetReverseSessionID(FIXME);
}
/// <summary>
/// FIXME totally bogus
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static string GetMsgType(string msg)
{
Message FIXME = new Message(msg);
return FIXME.GetField(Fields.Tags.MsgType);
}
#endregion
public bool FromStringHeader(string msgstr)
{
Clear();
int pos = 0;
int count = 0;
while(pos < msgstr.Length)
{
Fields.StringField f = ExtractField(msgstr, ref pos);
if((count < 3) && (Header.HEADER_FIELD_ORDER[count++] != f.Tag))
return false;
if(IsHeaderField(f.Tag))
this.Header.setField(f, false);
else
break;
}
return true;
}
public void FromString(string msgstr, bool validate, DataDictionaryParser sessionDataDictionary, DataDictionaryParser applicationDataDictionary)
{
Clear();
string msg = "";
bool expectingHeader = true;
bool expectingBody = true;
int count = 0;
int pos = 0;
while (pos < msgstr.Length)
{
Fields.StringField f = ExtractField(msgstr, ref pos, sessionDataDictionary, applicationDataDictionary);
if (validate && (count < 3) && (Header.HEADER_FIELD_ORDER[count++] != f.Tag))
throw new InvalidMessage("Header fields out of order");
if (IsHeaderField(f.Tag, sessionDataDictionary))
{
if (!expectingHeader)
{
if (0 == field_)
field_ = f.Tag;
validStructure_ = false;
}
if (Fields.Tags.MsgType.Equals(f.Tag))
msg = string.Copy(f.Obj);
if (!this.Header.setField(f, false))
this.Header.RepeatedTags.Add(f);
/** TODO group stuff
if(null != sessionDataDictionary)
setGroup("_header_", f, msgstr, pos, this.Header, sessionDataDictionary);
*/
}
else if (IsTrailerField(f.Tag, sessionDataDictionary))
{
expectingHeader = false;
expectingBody = false;
if (!this.Trailer.setField(f, false))
this.Trailer.RepeatedTags.Add(f);
/** TODO group stuff
if (null != sessionDataDictionary)
setGroup("_trailer_", f, msgstr, pos, this.Trailer, sessionDataDictionary);
*/
}
else
{
if (!expectingBody)
{
if (0 == field_)
field_ = f.Tag;
validStructure_ = false;
}
expectingHeader = false;
if (!setField(f, false))
this.RepeatedTags.Add(f);
/** TODO group stuff
if(null != applicationDataDictionary)
setGroup(msg, f, msgstr, pos, this, applicationDataDictionary);
*/
}
}
if (validate)
Validate();
}
public bool HasValidStructure(out int field)
{
field = field_;
return validStructure_;
}
public void Validate()
{
try
{
int receivedBodyLength = Fields.Converters.IntConverter.Convert(this.Header.GetField(Fields.Tags.BodyLength)); /// FIXME
if (BodyLength() != receivedBodyLength)
throw new InvalidMessage("Expected BodyLength=" + BodyLength() + ", Received BodyLength=" + receivedBodyLength);
int receivedCheckSum = Fields.Converters.IntConverter.Convert(this.Trailer.GetField(Fields.Tags.CheckSum)); /// FIXME
if (CheckSum() != receivedCheckSum)
throw new InvalidMessage("Expected CheckSum=" + CheckSum() + ", Received CheckSum=" + receivedCheckSum);
}
catch (FieldNotFoundException e)
{
throw new InvalidMessage("BodyLength or CheckSum missing", e);
}
catch (FieldConvertError e)
{
throw new InvalidMessage("BodyLength or Checksum has wrong format", e);
}
}
public void ReverseRoute(Header header)
{
// required routing tags
this.Header.RemoveField(Fields.Tags.BeginString);
this.Header.RemoveField(Fields.Tags.SenderCompID);
this.Header.RemoveField(Fields.Tags.TargetCompID);
if (header.isSetField(Fields.Tags.BeginString))
{
string beginString = header.GetField(Fields.Tags.BeginString);
if (beginString.Length > 0)
this.Header.setField(new BeginString(beginString));
this.Header.RemoveField(Fields.Tags.OnBehalfOfLocationID);
this.Header.RemoveField(Fields.Tags.DeliverToLocationID);
if (beginString.CompareTo("FIX.4.1") >= 0)
{
if (header.isSetField(Fields.Tags.OnBehalfOfLocationID))
{
string onBehalfOfLocationID = header.GetField(Fields.Tags.OnBehalfOfLocationID);
if (onBehalfOfLocationID.Length > 0)
this.Header.setField(new DeliverToLocationID(onBehalfOfLocationID));
}
if (header.isSetField(Fields.Tags.DeliverToLocationID))
{
string deliverToLocationID = header.GetField(Fields.Tags.DeliverToLocationID);
if (deliverToLocationID.Length > 0)
this.Header.setField(new OnBehalfOfLocationID(deliverToLocationID));
}
}
}
if (header.isSetField(Fields.Tags.SenderCompID))
{
SenderCompID senderCompID = new SenderCompID();
header.getField(senderCompID);
if (senderCompID.Obj.Length > 0)
this.Header.setField(new TargetCompID(senderCompID.Obj));
}
if (header.isSetField(Fields.Tags.TargetCompID))
{
TargetCompID targetCompID = new TargetCompID();
header.getField(targetCompID);
if (targetCompID.Obj.Length > 0)
this.Header.setField(new SenderCompID(targetCompID.Obj));
}
// optional routing tags
this.Header.RemoveField(Fields.Tags.OnBehalfOfCompID);
this.Header.RemoveField(Fields.Tags.OnBehalfOfSubID);
this.Header.RemoveField(Fields.Tags.DeliverToCompID);
this.Header.RemoveField(Fields.Tags.DeliverToSubID);
if(header.isSetField(Fields.Tags.OnBehalfOfCompID))
{
string onBehalfOfCompID = header.GetField(Fields.Tags.OnBehalfOfCompID);
if(onBehalfOfCompID.Length > 0)
this.Header.setField(new DeliverToCompID(onBehalfOfCompID));
}
if(header.isSetField(Fields.Tags.OnBehalfOfSubID))
{
string onBehalfOfSubID = header.GetField( Fields.Tags.OnBehalfOfSubID);
if(onBehalfOfSubID.Length > 0)
this.Header.setField(new DeliverToSubID(onBehalfOfSubID));
}
if(header.isSetField(Fields.Tags.DeliverToCompID))
{
string deliverToCompID = header.GetField(Fields.Tags.DeliverToCompID);
if(deliverToCompID.Length > 0)
this.Header.setField(new OnBehalfOfCompID(deliverToCompID));
}
if(header.isSetField(Fields.Tags.DeliverToSubID))
{
string deliverToSubID = header.GetField(Fields.Tags.DeliverToSubID);
if(deliverToSubID.Length > 0)
this.Header.setField(new OnBehalfOfSubID(deliverToSubID));
}
}
public int CheckSum()
{
return (
(this.Header.CalculateTotal()
+ CalculateTotal()
+ this.Trailer.CalculateTotal()) % 256);
}
public bool IsAdmin()
{
if(!isSetField(Fields.Tags.MsgType))
return false;
string msgType = this.Header.GetField(Fields.Tags.MsgType); /// FIXME
return IsAdminMsgType(msgType);
}
public bool IsApp()
{
if (!isSetField(Fields.Tags.MsgType))
return false;
string msgType = this.Header.GetField(Fields.Tags.MsgType); /// FIXME
return !IsAdminMsgType(msgType);
}
/// <summary>
/// FIXME less operator new
/// </summary>
/// <param name="sessionID"></param>
public void SetSessionID(SessionID sessionID)
{
this.Header.setField(new BeginString(sessionID.BeginString));
this.Header.setField(new SenderCompID(sessionID.SenderCompID));
this.Header.setField(new TargetCompID(sessionID.TargetCompID));
}
public override void Clear()
{
field_ = 0;
this.Header.Clear();
base.Clear();
this.Trailer.Clear();
}
public override string ToString()
{
this.Header.setField(new BodyLength(BodyLength()), true);
this.Trailer.setField(new CheckSum(Fields.Converters.CheckSumConverter.Convert(CheckSum())), true);
return this.Header.CalculateString() + CalculateString() + this.Trailer.CalculateString();
}
protected int BodyLength()
{
return this.Header.CalculateLength() + CalculateLength() + this.Trailer.CalculateLength();
}
}
}