예제 #1
0
        /// <inheritdoc/>
        public void EndSubMessage()
        {
            long currentPos = memStream.Position;
            var  state      = stack.Pop();
            int  size       = state.FinalMessageContentsSize;

            // Overwrite the prefix now we know the size
            int newFieldPrefix = FudgeFieldPrefixCodec.ComposeFieldPrefix(false, size, state.HasOrdinal, state.HasName);

            memStream.Position = state.PrefixPos;
            memWriter.Write((byte)newFieldPrefix);

            // Write the size into the four bytes we reserved earlier
            memStream.Position = state.SizePos;
            memWriter.Write(state.FinalMessageContentsSize);

            // Skip back to the end
            memStream.Position = currentPos;

            // Add this message's size to the parent message's total final size
            int sizeSize         = FudgeFieldPrefixCodec.CalculateVarSizeSize(size);
            int totalMessageSize = state.FinalMessageContentsSize + state.HeaderSize + sizeSize;       // Although we took four bytes writing the size we won't when it's finally output

            stack.Peek().FinalMessageContentsSize += totalMessageSize;
        }
예제 #2
0
        private static int WriteFieldHeader(BinaryWriter bw, int valueSize, bool variableSize, int typeId, short?ordinal, string name)
        {
            int nWritten = 0;

            int fieldPrefix = FudgeFieldPrefixCodec.ComposeFieldPrefix(!variableSize, valueSize, (ordinal != null), (name != null));

            bw.Write((byte)fieldPrefix);
            nWritten++;
            bw.Write((byte)typeId);
            nWritten++;
            if (ordinal != null)
            {
                bw.Write(ordinal.Value);
                nWritten += 2;
            }
            if (name != null)
            {
                int utf8size = StringFieldType.Encoding.GetByteCount(name);
                if (utf8size > 0xFF)
                {
                    throw new ArgumentOutOfRangeException("UTF-8 encoded field name cannot exceed 255 characters. Name \"" + name + "\" is " + utf8size + " bytes encoded.");
                }
                bw.Write((byte)utf8size);
                nWritten++;
                nWritten += StringFieldType.WriteString(bw, name);
            }

            return(nWritten);
        }
 public void FieldPrefixComposition()
 {
     Assert.Equal(0x20, FudgeFieldPrefixCodec.ComposeFieldPrefix(false, 10, false, false));
     Assert.Equal(0x40, FudgeFieldPrefixCodec.ComposeFieldPrefix(false, 1024, false, false));
     Assert.Equal(0x60, FudgeFieldPrefixCodec.ComposeFieldPrefix(false, short.MaxValue + 1000, false, false));
     Assert.Equal(0x98, FudgeFieldPrefixCodec.ComposeFieldPrefix(true, 0, true, true));
 }