Skip to content

Commit

Permalink
connamara#760 Support unsigned 64-bit integers for sequence numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
ettie933 committed Mar 14, 2023
1 parent c4e8171 commit 7a5f829
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 228 deletions.
16 changes: 16 additions & 0 deletions QuickFIXn/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ public long GetLong(string key)
}
}

public ulong GetSequence(string key)
{
try
{
return Convert.ToUInt64(GetString(key));
}
catch (FormatException)
{
throw new ConfigError("Incorrect data type");
}
catch (QuickFIXException)
{
throw new ConfigError("No value for key: " + key);
}
}

public double GetDouble(string key)
{
try
Expand Down
41 changes: 41 additions & 0 deletions QuickFIXn/Fields/Converters/SequenceConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

namespace QuickFix.Fields.Converters
{
/// <summary>
/// convert UInt64 to/from string
/// </summary>
public static class SequenceConverter
{
/// <summary>
/// Converts sequence number string to ulong.
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static ulong Convert(string i)
{
try
{
if ((null == i) || (i.Length < 1))
throw new FieldConvertError("The argument string cannot be null or empty");

return System.Convert.ToUInt64(i);
}
catch (System.FormatException e)
{
throw new FieldConvertError("Could not convert string to unsigned long (" + i + ")", e);
}
catch (System.OverflowException e)
{
throw new FieldConvertError("Could not convert string to unsigned long (" + i + ")", e);
}
}

/// <summary>
/// convert integer to string
/// </summary>
public static string Convert(System.UInt64 i)
{
return i.ToString();
}
}
}
270 changes: 135 additions & 135 deletions QuickFIXn/Fields/Fields.cs

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions QuickFIXn/Fields/SequenceField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace QuickFix.Fields
{
/// <summary>
/// An integer message field
/// </summary>
public class SequenceField : FieldBase<ulong>
{
public SequenceField(int tag)
: base(tag, 0) { }

public SequenceField(int tag, ulong val)
: base(tag, val) {}

// quickfix compat
public ulong getValue()
{ return Obj; }

public void setValue(ulong v)
{ Obj = v; }

protected override string makeString()
{
return Converters.SequenceConverter.Convert(Obj);
}
}
}
20 changes: 10 additions & 10 deletions QuickFIXn/FileStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public MsgDef(long index, int size)

private MemoryStore cache_ = new MemoryStore();

System.Collections.Generic.Dictionary<int, MsgDef> offsets_ = new Dictionary<int, MsgDef>();
System.Collections.Generic.Dictionary<ulong, MsgDef> offsets_ = new Dictionary<ulong, MsgDef>();

public static string Prefix(SessionID sessionID)
{
Expand Down Expand Up @@ -132,7 +132,7 @@ private void ConstructFromFileCache()
string[] headerParts = line.Split(',');
if (headerParts.Length == 3)
{
offsets_[Convert.ToInt32(headerParts[0])] = new MsgDef(
offsets_[Convert.ToUInt64(headerParts[0])] = new MsgDef(
Convert.ToInt64(headerParts[1]), Convert.ToInt32(headerParts[2]));
}
}
Expand All @@ -146,8 +146,8 @@ private void ConstructFromFileCache()
string[] parts = seqNumReader.ReadToEnd().Split(':');
if (parts.Length == 2)
{
cache_.NextSenderMsgSeqNum = Convert.ToInt32(parts[0]);
cache_.NextTargetMsgSeqNum = Convert.ToInt32(parts[1]);
cache_.NextSenderMsgSeqNum = Convert.ToUInt64(parts[0]);
cache_.NextTargetMsgSeqNum = Convert.ToUInt64(parts[1]);
}
}
}
Expand Down Expand Up @@ -181,9 +181,9 @@ private void InitializeSessionCreateTime()
/// <param name="startSeqNum"></param>
/// <param name="endSeqNum"></param>
/// <param name="messages"></param>
public void Get(int startSeqNum, int endSeqNum, List<string> messages)
public void Get(ulong startSeqNum, ulong endSeqNum, List<string> messages)
{
for (int i = startSeqNum; i <= endSeqNum; i++)
for (ulong i = startSeqNum; i <= endSeqNum; i++)
{
if (offsets_.ContainsKey(i))
{
Expand All @@ -203,7 +203,7 @@ public void Get(int startSeqNum, int endSeqNum, List<string> messages)
/// <param name="msgSeqNum"></param>
/// <param name="msg"></param>
/// <returns></returns>
public bool Set(int msgSeqNum, string msg)
public bool Set(ulong msgSeqNum, string msg)
{
msgFile_.Seek(0, System.IO.SeekOrigin.End);

Expand All @@ -225,15 +225,15 @@ public bool Set(int msgSeqNum, string msg)
return true;
}

public int NextSenderMsgSeqNum {
public ulong NextSenderMsgSeqNum {
get { return cache_.NextSenderMsgSeqNum; }
set {
cache_.NextSenderMsgSeqNum = value;
setSeqNum();
}
}

public int NextTargetMsgSeqNum {
public ulong NextTargetMsgSeqNum {
get { return cache_.NextTargetMsgSeqNum; }
set {
cache_.NextTargetMsgSeqNum = value;
Expand All @@ -258,7 +258,7 @@ private void setSeqNum()
seqNumsFile_.Seek(0, System.IO.SeekOrigin.Begin);
System.IO.StreamWriter writer = new System.IO.StreamWriter(seqNumsFile_);

writer.Write(NextSenderMsgSeqNum.ToString("D10") + " : " + NextTargetMsgSeqNum.ToString("D10") + " ");
writer.Write(NextSenderMsgSeqNum.ToString("D20") + " : " + NextTargetMsgSeqNum.ToString("D20") + " ");
writer.Flush();
}

Expand Down
15 changes: 13 additions & 2 deletions QuickFIXn/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,14 @@ public string SessionDetails(HttpListenerRequest request)

if (request.QueryString["next incoming"] != null)
{
int value = Convert.ToInt16(request.QueryString["next incoming"]);
ulong value = Convert.ToUInt64(request.QueryString["next incoming"]);
sessionDetails.NextTargetMsgSeqNum = value <= 0 ? 1 : value;
url = RemoveQueryStringByKey(urlOriginalString, "next incoming");
}

if (request.QueryString["Next Outgoing"] != null)
{
int value = Convert.ToInt16(request.QueryString["Next Outgoing"]);
ulong value = Convert.ToUInt64(request.QueryString["Next Outgoing"]);
sessionDetails.NextSenderMsgSeqNum = value <= 0 ? 1 : value;
url = RemoveQueryStringByKey(urlOriginalString, "Next Outgoing");
}
Expand Down Expand Up @@ -538,6 +538,17 @@ private static string AddRow(string colName, int value, string url = "")
$"<a href=\" {url}&{colName}={value + 10} \"> >> </a>";
return AddRow(colName, value.ToString(), innerHtml);
}

private static string AddRow(string colName, ulong value, string url = "")
{
string innerHtml = $"<a href=\" {url}&{colName}={value - 10} \"> << </a>" +
$"<a href=\" {url}&{colName}={value - 1} \"> < </a>" +
" | " +
$"<a href=\" {url}&{colName}={value + 1} \"> > </a>" +
$"<a href=\" {url}&{colName}={value + 10} \"> >> </a>";
return AddRow(colName, value.ToString(), innerHtml);
}

private static string AddRow(string colName, string value, string innerHtml = "")
{
StringBuilder row = new StringBuilder();
Expand Down
8 changes: 4 additions & 4 deletions QuickFIXn/IMessageStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ public interface IMessageStore : IDisposable
/// <param name="startSeqNum">the starting message sequence number</param>
/// <param name="endSeqNum">the ending message sequence number</param>
/// <param name="messages">the retrieved messages (out parameter)</param>
void Get(int startSeqNum, int endSeqNum, List<string> messages);
void Get(ulong startSeqNum, ulong endSeqNum, List<string> messages);

/// <summary>
/// Adds a raw fix message to the store with the give sequence number
/// </summary>
/// <param name="msgSeqNum">the sequence number</param>
/// <param name="msg">the raw FIX message string</param>
/// <returns>true if successful, false otherwise</returns>
bool Set(int msgSeqNum, string msg);
bool Set(ulong msgSeqNum, string msg);

int NextSenderMsgSeqNum { get; set; }
int NextTargetMsgSeqNum { get; set; }
ulong NextSenderMsgSeqNum { get; set; }
ulong NextTargetMsgSeqNum { get; set; }

void IncrNextSenderMsgSeqNum();
void IncrNextTargetMsgSeqNum();
Expand Down
14 changes: 7 additions & 7 deletions QuickFIXn/MemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ public class MemoryStore : IMessageStore
{
#region Private Members

System.Collections.Generic.Dictionary<int, string> messages_;
System.Collections.Generic.Dictionary<ulong, string> messages_;
DateTime? creationTime;

#endregion

public MemoryStore()
{
messages_ = new System.Collections.Generic.Dictionary<int, string>();
messages_ = new System.Collections.Generic.Dictionary<ulong, string>();
Reset();
}

public void Get(int begSeqNo, int endSeqNo, List<string> messages)
public void Get(ulong begSeqNo, ulong endSeqNo, List<string> messages)
{
for (int current = begSeqNo; current <= endSeqNo; current++)
for (ulong current = begSeqNo; current <= endSeqNo; current++)
{
if (messages_.ContainsKey(current))
messages.Add(messages_[current]);
Expand All @@ -32,14 +32,14 @@ public void Get(int begSeqNo, int endSeqNo, List<string> messages)

#region MessageStore Members

public bool Set(int msgSeqNum, string msg)
public bool Set(ulong msgSeqNum, string msg)
{
messages_[msgSeqNum] = msg;
return true;
}

public int NextSenderMsgSeqNum { get; set; }
public int NextTargetMsgSeqNum { get; set; }
public ulong NextSenderMsgSeqNum { get; set; }
public ulong NextTargetMsgSeqNum { get; set; }

public void IncrNextSenderMsgSeqNum()
{ ++NextSenderMsgSeqNum; }
Expand Down
34 changes: 34 additions & 0 deletions QuickFIXn/Message/FieldMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ public Fields.IntField GetField(Fields.IntField field)
return field;
}

/// <summary>
/// Gets a sequence field; saves its value into the parameter object, which is also the return value.
/// </summary>
/// <param name="field">this field's tag is used to extract the value from the message; that value is saved back into this object</param>
/// <exception cref="FieldNotFoundException">thrown if <paramref name="field"/> isn't found</exception>
/// <returns><paramref name="field"/></returns>
public Fields.SequenceField GetField(Fields.SequenceField field)
{
field.Obj = GetSequence(field.Tag);
return field;
}

/// <summary>
/// Gets a decimal field; saves its value into the parameter object, which is also the return value.
/// </summary>
Expand Down Expand Up @@ -328,6 +340,28 @@ public int GetInt(int tag)
}
}

/// <summary>
/// Gets the unsigned long value of a sequence number field
/// </summary>
/// <param name="tag">the FIX tag</param>
/// <returns>the unsigned long sequence field value</returns>
/// <exception cref="FieldNotFoundException" />
public ulong GetSequence(int tag)
{
try
{
Fields.IField fld = _fields[tag];
if (fld.GetType() == typeof(SequenceField))
return ((SequenceField)fld).Obj;
else
return SequenceConverter.Convert(fld.ToString());
}
catch (System.Collections.Generic.KeyNotFoundException)
{
throw new FieldNotFoundException(tag);
}
}

/// <summary>
/// Gets the DateTime value of a field
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions QuickFIXn/ResendRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
{
public class ResendRange
{
public int BeginSeqNo { get; set; }
public int EndSeqNo { get; set; }
public int ChunkEndSeqNo { get; set; }
public ulong BeginSeqNo { get; set; }
public ulong EndSeqNo { get; set; }
public ulong ChunkEndSeqNo { get; set; }

public ResendRange()
{
BeginSeqNo = 0;
EndSeqNo = 0;
ChunkEndSeqNo = -1;
ChunkEndSeqNo = 0;
}

public override string ToString()
Expand Down
Loading

0 comments on commit 7a5f829

Please sign in to comment.