Skip to content

Commit

Permalink
Added Encoding option to session settings
Browse files Browse the repository at this point in the history
  • Loading branch information
amibar authored and gbirchmeier committed Jun 26, 2019
1 parent fb719b9 commit e7d59ce
Show file tree
Hide file tree
Showing 20 changed files with 384 additions and 57 deletions.
9 changes: 5 additions & 4 deletions QuickFIXn/Fields/FieldBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;

namespace QuickFix.Fields
{
Expand Down Expand Up @@ -87,23 +88,23 @@ public override int GetHashCode()
/// <summary>
/// length of formatted field (including tag=val\001)
/// </summary>
public override int getLength()
public override int getLength(Encoding encoding)
{
if (_changed)
makeStringFields();
return System.Text.Encoding.UTF8.GetByteCount(_stringField) + 1; // +1 for SOH
return encoding.GetByteCount(_stringField) + 1; // +1 for SOH
}

/// <summary>
/// checksum
/// </summary>
public override int getTotal()
public override int getTotal(Encoding encoding)
{
if (_changed)
makeStringFields();

int sum = 0;
byte[] array = System.Text.Encoding.UTF8.GetBytes(_stringField);
byte[] array = encoding.GetBytes(_stringField);
foreach (byte b in array)
{
sum += b;
Expand Down
5 changes: 3 additions & 2 deletions QuickFIXn/Fields/IField.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;

namespace QuickFix.Fields
{
Expand All @@ -24,11 +25,11 @@ public abstract class IField
/// <summary>
/// length of formatted field (including tag=val\001)
/// </summary>
public abstract int getLength();
public abstract int getLength(Encoding encoding);

/// <summary>
/// checksum
/// </summary>
public abstract int getTotal();
public abstract int getTotal(Encoding encoding);
}
}
14 changes: 11 additions & 3 deletions QuickFIXn/FileStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public MsgDef(long index, int size)
private System.IO.FileStream msgFile_;
private System.IO.StreamWriter headerFile_;

private Encoding encoding_;

private MemoryStore cache_ = new MemoryStore();

System.Collections.Generic.Dictionary<int, MsgDef> offsets_ = new Dictionary<int, MsgDef>();
Expand All @@ -55,8 +57,10 @@ public static string Prefix(SessionID sessionID)
return prefix.ToString();
}

public FileStore(string path, SessionID sessionID)
public FileStore(SessionID sessionID, SessionSettings settings)
{
string path = settings.Get(sessionID).GetString(SessionSettings.FILE_STORE_PATH);

if (!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);

Expand All @@ -67,6 +71,10 @@ public FileStore(string path, SessionID sessionID)
headerFileName_ = System.IO.Path.Combine(path, prefix + ".header");
sessionFileName_ = System.IO.Path.Combine(path, prefix + ".session");

encoding_ = settings.Get(sessionID).Has(SessionSettings.ENCODING)
? Encoding.GetEncoding(settings.Get(sessionID).GetString(SessionSettings.ENCODING))
: SessionFactory.DefaultEncoding;

open();
}

Expand Down Expand Up @@ -183,7 +191,7 @@ public void Get(int startSeqNum, int endSeqNum, List<string> messages)
byte[] msgBytes = new byte[offsets_[i].size];
msgFile_.Read(msgBytes, 0, msgBytes.Length);

messages.Add(Encoding.UTF8.GetString(msgBytes));
messages.Add(encoding_.GetString(msgBytes));
}
}

Expand All @@ -200,7 +208,7 @@ public bool Set(int msgSeqNum, string msg)
msgFile_.Seek(0, System.IO.SeekOrigin.End);

long offset = msgFile_.Position;
byte[] msgBytes = Encoding.UTF8.GetBytes(msg);
byte[] msgBytes = encoding_.GetBytes(msg);
int size = msgBytes.Length;

StringBuilder b = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion QuickFIXn/FileStoreFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public FileStoreFactory(SessionSettings settings)
/// <returns></returns>
public IMessageStore Create(SessionID sessionID)
{
return new FileStore(settings_.Get(sessionID).GetString(SessionSettings.FILE_STORE_PATH), sessionID);
return new FileStore(sessionID, settings_);
}

#endregion
Expand Down
16 changes: 8 additions & 8 deletions QuickFIXn/Message/FieldMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,30 +549,30 @@ public bool IsEmpty()
return ((_fields.Count == 0) && (_groups.Count == 0));
}

public int CalculateTotal()
public int CalculateTotal(Encoding encoding)
{
int total = 0;
foreach (Fields.IField field in _fields.Values)
{
if (field.Tag != Fields.Tags.CheckSum)
total += field.getTotal();
total += field.getTotal(encoding);
}

foreach (Fields.IField field in this.RepeatedTags)
{
if (field.Tag != Fields.Tags.CheckSum)
total += field.getTotal();
total += field.getTotal(encoding);
}

foreach (List<Group> groupList in _groups.Values)
{
foreach (Group group in groupList)
total += group.CalculateTotal();
total += group.CalculateTotal(encoding);
}
return total;
}

public int CalculateLength()
public int CalculateLength(Encoding encoding)
{
int total = 0;
foreach (Fields.IField field in _fields.Values)
Expand All @@ -582,7 +582,7 @@ public int CalculateLength()
&& field.Tag != Tags.BodyLength
&& field.Tag != Tags.CheckSum)
{
total += field.getLength();
total += field.getLength(encoding);
}
}

Expand All @@ -593,14 +593,14 @@ public int CalculateLength()
&& field.Tag != Tags.BodyLength
&& field.Tag != Tags.CheckSum)
{
total += field.getLength();
total += field.getLength(encoding);
}
}

foreach (List<Group> groupList in _groups.Values)
{
foreach (Group group in groupList)
total += group.CalculateLength();
total += group.CalculateLength(encoding);
}

return total;
Expand Down
18 changes: 11 additions & 7 deletions QuickFIXn/Message/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public class Message : FieldMap
public const string SOH = "\u0001";
private int field_ = 0;
private bool validStructure_;


public Encoding Encoding { get; set; }

#region Properties

public Header Header { get; private set; }
Expand All @@ -61,6 +63,7 @@ public class Message : FieldMap

public Message()
{
Encoding = SessionFactory.DefaultEncoding;
this.Header = new Header();
this.Trailer = new Trailer();
this.validStructure_ = true;
Expand Down Expand Up @@ -93,6 +96,7 @@ public Message(string msgstr, DataDictionary.DataDictionary sessionDataDictionar
public Message(Message src)
: base(src)
{
Encoding = src.Encoding;
this.Header = new Header(src.Header);
this.Trailer = new Trailer(src.Trailer);
this.validStructure_ = src.validStructure_;
Expand Down Expand Up @@ -565,11 +569,11 @@ public void Validate()
{
int receivedBodyLength = this.Header.GetInt(Tags.BodyLength);
if (BodyLength() != receivedBodyLength)
throw new InvalidMessage("Expected BodyLength=" + BodyLength() + ", Received BodyLength=" + receivedBodyLength);
throw new InvalidMessage("Expected BodyLength=" + BodyLength() + ", Received BodyLength=" + receivedBodyLength + ", Message.SeqNum=" + this.Header.GetInt(Tags.MsgSeqNum));

int receivedCheckSum = this.Trailer.GetInt(Tags.CheckSum);
if (CheckSum() != receivedCheckSum)
throw new InvalidMessage("Expected CheckSum=" + CheckSum() + ", Received CheckSum=" + receivedCheckSum);
throw new InvalidMessage("Expected CheckSum=" + CheckSum() + ", Received CheckSum=" + receivedCheckSum + ", Message.SeqNum=" + this.Header.GetInt(Tags.MsgSeqNum));
}
catch (FieldNotFoundException e)
{
Expand Down Expand Up @@ -705,9 +709,9 @@ public void ReverseRoute(Header header)
public int CheckSum()
{
return (
(this.Header.CalculateTotal()
+ CalculateTotal()
+ this.Trailer.CalculateTotal()) % 256);
(this.Header.CalculateTotal(Encoding)
+ CalculateTotal(Encoding)
+ this.Trailer.CalculateTotal(Encoding)) % 256);
}

public bool IsAdmin()
Expand Down Expand Up @@ -783,7 +787,7 @@ public override string ToString()

protected int BodyLength()
{
return this.Header.CalculateLength() + CalculateLength() + this.Trailer.CalculateLength();
return this.Header.CalculateLength(Encoding) + CalculateLength(Encoding) + this.Trailer.CalculateLength(Encoding);
}
}
}
7 changes: 6 additions & 1 deletion QuickFIXn/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class MessageBuilder

private Message _message;
private QuickFix.Fields.ApplVerID _defaultApplVerId;
private Encoding _encoding;

public string OriginalString { get { return _msgStr; } }
public QuickFix.Fields.MsgType MsgType { get { return _msgType; } }
Expand All @@ -34,14 +35,16 @@ internal MessageBuilder(
bool validateLengthAndChecksum,
DataDictionary.DataDictionary sessionDD,
DataDictionary.DataDictionary appDD,
IMessageFactory msgFactory)
IMessageFactory msgFactory,
Encoding encoding)
{
_msgStr = msgStr;
_defaultApplVerId = new ApplVerID(defaultApplVerId);
_validateLengthAndChecksum = validateLengthAndChecksum;
_sessionDD = sessionDD;
_appDD = appDD;
_msgFactory = msgFactory;
_encoding = encoding;

_msgType = Message.IdentifyType(_msgStr);
_beginString = Message.ExtractBeginString(_msgStr);
Expand All @@ -50,6 +53,7 @@ internal MessageBuilder(
internal Message Build()
{
Message message = _msgFactory.Create(_beginString, _defaultApplVerId, _msgType.Obj);
message.Encoding = _encoding; //TODO: maybe add this to message constructor
message.FromString(
_msgStr,
_validateLengthAndChecksum,
Expand All @@ -66,6 +70,7 @@ internal Message RejectableMessage()
return _message;

Message message = _msgFactory.Create(_beginString, _msgType.Obj);
message.Encoding = _encoding;
message.FromString(
_msgStr,
false,
Expand Down
20 changes: 15 additions & 5 deletions QuickFIXn/Parser.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@


using System.Text;

namespace QuickFix
{
/// <summary>
Expand All @@ -6,6 +10,13 @@ public class Parser
{
private byte[] buffer_ = new byte[512];
int usedBufferLength = 0;
private Encoding encoding_;

public Parser(Encoding encoding)
{
encoding_ = encoding;
}

public void AddToStream(ref byte[] data, int bytesAdded)
{
if (buffer_.Length < usedBufferLength + bytesAdded)
Expand All @@ -16,9 +27,8 @@ public void AddToStream(ref byte[] data, int bytesAdded)

public void AddToStream(string data)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
byte[] bytes = encoding_.GetBytes(data);
AddToStream(ref bytes, bytes.Length);

}


Expand Down Expand Up @@ -74,7 +84,7 @@ public bool ReadFixMessage(out string msg)

public bool ExtractLength(out int length, out int pos, string buf)
{
return ExtractLength(out length, out pos, System.Text.Encoding.UTF8.GetBytes(buf));
return ExtractLength(out length, out pos, encoding_.GetBytes(buf));
}

public bool ExtractLength(out int length, out int pos, byte[] buf)
Expand Down Expand Up @@ -118,7 +128,7 @@ private bool Fail(string what)

private int IndexOf(byte[] arrayToSearchThrough, string stringPatternToFind, int offset)
{
byte[] patternToFind = System.Text.Encoding.UTF8.GetBytes(stringPatternToFind);
byte[] patternToFind = encoding_.GetBytes(stringPatternToFind);
if (patternToFind.Length > arrayToSearchThrough.Length)
return -1;
for (int i = offset; i <= arrayToSearchThrough.Length - patternToFind.Length; i++)
Expand Down Expand Up @@ -151,7 +161,7 @@ private string Substring(byte[] array, int startIndex, int length)
{
byte[] returnByte = new byte[length];
System.Buffer.BlockCopy(array, startIndex, returnByte, 0, length);
return System.Text.Encoding.UTF8.GetString(returnByte);
return encoding_.GetString(returnByte);
}
}
}
7 changes: 6 additions & 1 deletion QuickFIXn/Session.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using QuickFix.Fields;
using QuickFix.Fields.Converters;

Expand Down Expand Up @@ -226,6 +227,7 @@ public TimeStampPrecision TimeStampPrecision
public DataDictionaryProvider DataDictionaryProvider { get; set; }
public DataDictionary.DataDictionary SessionDataDictionary { get; private set; }
public DataDictionary.DataDictionary ApplicationDataDictionary { get; private set; }
public Encoding Encoding { get; set; }

/// <summary>
/// Returns whether the Session has a Responder. This method is synchronized
Expand All @@ -250,6 +252,7 @@ public Session(
this.schedule_ = sessionSchedule;
this.msgFactory_ = msgFactory;
this.appDoesEarlyIntercept_ = app is IApplicationExt;
this.Encoding = SessionFactory.DefaultEncoding;

this.SenderDefaultApplVerID = senderDefaultApplVerID;

Expand Down Expand Up @@ -558,7 +561,8 @@ private void NextMessage(string msgStr)
this.ValidateLengthAndChecksum,
this.SessionDataDictionary,
this.ApplicationDataDictionary,
this.msgFactory_);
this.msgFactory_,
this.Encoding);

Next(msgBuilder);
}
Expand Down Expand Up @@ -1637,6 +1641,7 @@ protected bool SendRaw(Message message, int seqNum)
{
lock (sync_)
{
message.Encoding = this.Encoding;
string msgType = message.Header.GetField(Fields.Tags.MsgType);

InitializeHeader(message, seqNum);
Expand Down
Loading

0 comments on commit e7d59ce

Please sign in to comment.