mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 00:21:31 +00:00
A NUMBER THAT CANNOT BE NEGATED. The writer took the absolute value of a negative number before writing its digits, and the most negative long has no positive counterpart - negating it overflows. It builds the magnitude without negating now, and a test writes long.MinValue and reads it back. The Long test beside it still says in a comment that the text format cannot round-trip that value exactly, and works around it with MinValue + 1. If the new test goes red, that comment was right and this is incomplete; it is left standing rather than quietly corrected, because which of the two is true is a question for a run. A TAG THAT IS NOT A TAG. A tag is a positive number followed by an equals sign, and anything else was read on regardless: a zero, a negative number or a tag with no value would each have named a field nobody sent and carried whatever bytes followed into it. Such a frame is refused. A stream that ends between messages still reads as the empty tag, which is how a session that closed cleanly ends, and the reader no longer throws on it. A SCALED NUMBER THAT LOST ITS DIGITS. Its decimal value went through a double, which cannot hold every decimal a price can be; it is built from the mantissa and exponent directly. Adding a scaled zero to a number now leaves that number rather than moving its exponent. AN EMPTY BOOK THAT EMPTIED EVERY BOOK. Told that one instrument has no quotes left, the dialect cleared the quotes it held for all of them. It clears the one it was told about, and the empty book it hands on names the subscription that asked - a book a subscriber cannot attribute is a book it cannot apply. TEXT THAT WAS NOT LATIN. The conversion to Latin ran over the whole string through a helper that returned it unchanged when there was nothing to convert, and built a new string every time regardless; it now walks the characters once and leaves an empty value alone.
169 lines
4.7 KiB
C#
169 lines
4.7 KiB
C#
namespace StockSharp.Fix.Native;
|
|
|
|
/// <summary>
|
|
/// The data recorder which records in the text FIX protocol format.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Initializes a new instance of the <see cref="TextFixWriter"/>.
|
|
/// </remarks>
|
|
/// <param name="stream">Writing stream.</param>
|
|
/// <param name="encoding">Text encoding.</param>
|
|
/// <param name="ownsStream">Whether to dispose the stream when this instance is disposed.</param>
|
|
public class TextFixWriter(Stream stream, Encoding encoding, bool ownsStream = false) : BaseFixWriter(stream, encoding, ownsStream), IFixWriter
|
|
{
|
|
private async ValueTask WriteWithDumpAsync(byte value, CancellationToken cancellationToken)
|
|
{
|
|
await WriteByteAsync(value, cancellationToken);
|
|
Dump(value);
|
|
}
|
|
|
|
private async ValueTask WriteNumberAsync(long value, CancellationToken cancellationToken)
|
|
{
|
|
ulong magnitude;
|
|
|
|
if (value < 0)
|
|
{
|
|
await WriteWithDumpAsync((byte)AsciiSymbols.Minus, cancellationToken);
|
|
magnitude = (ulong)(-(value + 1)) + 1;
|
|
}
|
|
else
|
|
magnitude = (ulong)value;
|
|
|
|
if (magnitude < 10)
|
|
{
|
|
await WriteWithDumpAsync((byte)((int)magnitude + AsciiSymbols.Zero), cancellationToken);
|
|
return;
|
|
}
|
|
else if (magnitude == 10)
|
|
{
|
|
await WriteWithDumpAsync((byte)(1 + AsciiSymbols.Zero), cancellationToken);
|
|
await WriteWithDumpAsync((byte)(0 + AsciiSymbols.Zero), cancellationToken);
|
|
return;
|
|
}
|
|
|
|
var m = 1UL;
|
|
var num = magnitude;
|
|
while (num >= 10)
|
|
{
|
|
num /= 10;
|
|
m *= 10;
|
|
}
|
|
|
|
while (m != 0)
|
|
{
|
|
var digit = (int)(magnitude / m);
|
|
|
|
if (digit < 0 || digit > 9)
|
|
throw new InvalidOperationException();
|
|
|
|
await WriteWithDumpAsync((byte)(digit + AsciiSymbols.Zero), cancellationToken);
|
|
|
|
magnitude -= (ulong)digit * m;
|
|
m /= 10;
|
|
}
|
|
}
|
|
|
|
async ValueTask IFixWriter.WriteAsync(FixTags tag, CancellationToken cancellationToken)
|
|
{
|
|
await WriteNumberAsync((long)tag, cancellationToken);
|
|
await WriteWithDumpAsync((byte)AsciiSymbols.Eq, cancellationToken);
|
|
|
|
LastTag = tag;
|
|
}
|
|
|
|
async ValueTask IFixWriter.WriteAsync(bool value, CancellationToken cancellationToken)
|
|
{
|
|
await WriteWithDumpAsync((byte)(value ? 'Y' : 'N'), cancellationToken);
|
|
await WriteSohAsync(cancellationToken);
|
|
}
|
|
|
|
async ValueTask IFixWriter.WriteAsync(int value, CancellationToken cancellationToken)
|
|
{
|
|
await WriteNumberAsync(value, cancellationToken);
|
|
await WriteSohAsync(cancellationToken);
|
|
}
|
|
|
|
async ValueTask IFixWriter.WriteAsync(long value, CancellationToken cancellationToken)
|
|
{
|
|
await WriteNumberAsync(value, cancellationToken);
|
|
await WriteSohAsync(cancellationToken);
|
|
}
|
|
|
|
async ValueTask IFixWriter.WriteAsync(decimal value, CancellationToken cancellationToken)
|
|
{
|
|
if (value == 0)
|
|
{
|
|
await WriteWithDumpAsync((byte)AsciiSymbols.Zero, cancellationToken);
|
|
await WriteSohAsync(cancellationToken);
|
|
return;
|
|
}
|
|
|
|
if (value < 0)
|
|
{
|
|
value = value.Abs();
|
|
await WriteWithDumpAsync((byte)AsciiSymbols.Minus, cancellationToken);
|
|
}
|
|
|
|
var pow = (int)Math.Floor(Math.Log10((double)value));
|
|
var m = (decimal)Math.Pow(10, pow);
|
|
|
|
var pointAdded = false;
|
|
|
|
if (pow < 0)
|
|
{
|
|
await WriteWithDumpAsync((byte)AsciiSymbols.Zero, cancellationToken);
|
|
await WriteWithDumpAsync((byte)AsciiSymbols.Point, cancellationToken);
|
|
|
|
pointAdded = true;
|
|
pow++;
|
|
|
|
while (pow++ < 0)
|
|
{
|
|
await WriteWithDumpAsync((byte)AsciiSymbols.Zero, cancellationToken);
|
|
}
|
|
}
|
|
|
|
while (value != 0 || 1 <= m)
|
|
{
|
|
var digit = (int)(value / m);
|
|
|
|
if (digit < 0 || 9 < digit)
|
|
throw new InvalidOperationException();
|
|
|
|
if (!pointAdded && m < 1)
|
|
{
|
|
await WriteWithDumpAsync((byte)AsciiSymbols.Point, cancellationToken);
|
|
pointAdded = true;
|
|
}
|
|
|
|
value -= digit * m;
|
|
await WriteWithDumpAsync((byte)(digit + AsciiSymbols.Zero), cancellationToken);
|
|
m /= 10;
|
|
}
|
|
|
|
await WriteSohAsync(cancellationToken);
|
|
}
|
|
|
|
ValueTask IFixWriter.WriteAsync(DateTime value, FastDateTimeParser parser, CancellationToken cancellationToken)
|
|
=> ((IFixWriter)this).WriteAsync(parser.ToString(value), cancellationToken);
|
|
|
|
ValueTask IFixWriter.WriteAsync(TimeSpan value, FastTimeSpanParser parser, CancellationToken cancellationToken)
|
|
=> ((IFixWriter)this).WriteAsync(parser.ToString(value), cancellationToken);
|
|
|
|
async ValueTask IFixWriter.WriteAsync(string value, CancellationToken cancellationToken)
|
|
{
|
|
var bytes = Encoding.GetBytes(value);
|
|
await WriteBytesAsync(bytes, cancellationToken);
|
|
await WriteSohAsync(cancellationToken);
|
|
}
|
|
|
|
async ValueTask IFixWriter.WriteAsync(char value, CancellationToken cancellationToken)
|
|
{
|
|
await WriteWithDumpAsync((byte)value, cancellationToken);
|
|
await WriteSohAsync(cancellationToken);
|
|
}
|
|
|
|
private ValueTask WriteSohAsync(CancellationToken cancellationToken)
|
|
=> WriteWithDumpAsync((byte)AsciiSymbols.Soh, cancellationToken);
|
|
}
|