namespace StockSharp.Fix.Native;
///
/// Data reader/writer base class.
///
///
/// Initialize .
///
/// The stream.
/// Text encoding.
/// Whether to dispose the stream when this instance is disposed.
public abstract class FixBase(Stream stream, Encoding encoding, bool ownsStream = false) : Disposable
{
private readonly AllocationArray _dump = new(FileSizes.KB);
private readonly bool _ownsStream = ownsStream;
///
/// The stream.
///
public Stream Stream { get; } = stream ?? throw new ArgumentNullException(nameof(stream));
///
/// Text encoding.
///
public Encoding Encoding { get; } = encoding ?? throw new ArgumentNullException(nameof(encoding));
///
/// disabled.
///
public bool CheckSumDisabled { get; set; }
///
/// Check sum.
///
#pragma warning disable 3003
public uint CheckSum { get; set; }
#pragma warning restore 3003
///
/// Gets a value indicating whether the log incoming data required.
///
public bool IsDump { get; set; }
///
/// Last tag.
///
public FixTags LastTag { get; protected set; }
///
/// Whether the tag value was read.
///
public bool IsValueRead { get; protected set; }
private int _maxBytes = int.MaxValue;
///
/// Gets and sets the maximum allowed bytes per read/write operation.
///
public int MaxBytes
{
get => _maxBytes;
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException();
_maxBytes = value;
}
}
private int _bytesCount;
///
/// Total read/write bytes.
///
public int BytesCount
{
get => _bytesCount;
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value), value, LocalizedStrings.InvalidValue);
if (MaxBytes != int.MaxValue && value > MaxBytes)
throw new InvalidOperationException(LocalizedStrings.MaxBytesExceeded.Put(value, MaxBytes));
_bytesCount = value;
}
}
///
/// Calculate the check sum.
///
/// New byte.
protected void CalcCheckSum(byte value)
{
// Count bytes even when the checksum is disabled, so the MaxBytes cap still fires and an
// unbounded (no-SOH) field cannot grow without limit and OOM the host.
BytesCount++;
if (CheckSumDisabled)
return;
CheckSum += value;
}
///
/// Calculate the check sum.
///
/// Buffer.
protected void CalcCheckSum(Span value)
{
// Count bytes even when the checksum is disabled, so the MaxBytes cap still fires (see the
// single-byte overload).
BytesCount += value.Length;
if (CheckSumDisabled)
return;
for (int i = 0; i < value.Length; i++)
CheckSum += value[i];
}
///
/// Calculate the check sum.
///
/// Buffer.
protected void CalcCheckSum(ReadOnlySpan value)
{
if (CheckSumDisabled)
return;
BytesCount += value.Length;
for (int i = 0; i < value.Length; i++)
CheckSum += value[i];
}
///
/// Add to the data log a new byte.
///
/// New byte.
protected void Dump(byte value)
{
if (!IsDump)
return;
_dump.Add(value.TryReplaceSoh());
}
///
/// Add to the data log a news byte.
///
/// Buffer.
protected void Dump(Span value)
{
if (!IsDump)
return;
var copy = value.ToArray();
for (int i = 0; i < copy.Length; i++)
copy[i] = copy[i].TryReplaceSoh();
_dump.Add(copy, 0, copy.Length);
}
///
/// Add to the data log a news byte.
///
/// Buffer.
protected void Dump(ReadOnlySpan value)
{
if (!IsDump)
return;
var copy = value.ToArray();
for (int i = 0; i < copy.Length; i++)
copy[i] = copy[i].TryReplaceSoh();
_dump.Add(copy, 0, copy.Length);
}
///
/// Add to the data log a new string.
///
/// Type of value.
/// Value.
protected void Dump(T value)
{
if (!IsDump)
return;
var str = value.To() ?? string.Empty;
var bytes = Encoding.GetBytes(str);
_dump.Add(bytes, 0, bytes.Length);
_dump.Add((byte)'|');
}
///
/// Clear state.
///
public virtual void ClearState()
{
LastTag = Extensions.EmptyTag;
IsValueRead = false;
CheckSum = 0;
BytesCount = 0;
}
///
/// Get data log.
///
/// Data log.
public string FlushDump()
{
var temp = Encoding.GetString(_dump.Buffer, 0, _dump.Count);
_dump.Reset();
return temp;
}
///
protected override void DisposeManaged()
{
if (_ownsStream)
Stream.Dispose();
base.DisposeManaged();
}
}