示例#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 varWidthSizeChecks()
 {
     Assert.Equal(0, FudgeFieldPrefixCodec.GetFieldWidthByteCount(0x98));
     Assert.Equal(1, FudgeFieldPrefixCodec.GetFieldWidthByteCount(0x20));
     Assert.Equal(2, FudgeFieldPrefixCodec.GetFieldWidthByteCount(0x40));
     Assert.Equal(4, FudgeFieldPrefixCodec.GetFieldWidthByteCount(0x60));
 }
 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));
 }
示例#5
0
        /**
         *
         */
        protected void ConsumeFieldData() //throws IOException
        {
            sbyte fieldPrefix = Reader.ReadSByte();
            int   typeId      = Reader.ReadByte();
            int   nRead       = 2;
            bool  fixedWidth  = FudgeFieldPrefixCodec.IsFixedWidth(fieldPrefix);
            bool  hasOrdinal  = FudgeFieldPrefixCodec.HasOrdinal(fieldPrefix);
            bool  hasName     = FudgeFieldPrefixCodec.HasName(fieldPrefix);

            int?ordinal = null;

            if (hasOrdinal)
            {
                ordinal = Reader.ReadInt16();
                nRead  += 2;
            }

            string name = null;

            if (hasName)
            {
                int nameSize = Reader.ReadByte();
                nRead++;
                name   = StringFieldType.ReadString(Reader, nameSize);
                nRead += nameSize;
            }
            else if (ordinal != null)
            {
                if (Taxonomy != null)
                {
                    name = Taxonomy.GetFieldName((short)ordinal.Value);
                }
            }

            FudgeFieldType type = FudgeContext.TypeDictionary.GetByTypeId(typeId);

            if (type == null)
            {
                if (fixedWidth)
                {
                    throw new FudgeRuntimeException("Unknown fixed width type " + typeId + " for field " + ordinal + ":" + name + " cannot be handled.");
                }
                type = FudgeContext.TypeDictionary.GetUnknownType(typeId);
            }

            int varSize = 0;

            if (!fixedWidth)
            {
                int varSizeBytes = FudgeFieldPrefixCodec.GetFieldWidthByteCount(fieldPrefix);
                switch (varSizeBytes)
                {
                case 0: varSize = 0; break;

                case 1: varSize = Reader.ReadByte(); nRead += 1; break;

                case 2: varSize = Reader.ReadInt16(); nRead += 2; break;

                case 4: varSize = Reader.ReadInt32(); nRead += 4; break;

                default:
                    throw new FudgeRuntimeException("Illegal number of bytes indicated for variable width encoding: " + varSizeBytes);
                }
            }

            FieldName    = name;
            FieldOrdinal = ordinal;
            FieldType    = type;
            MessageProcessingState currMsgProcessingState = processingStack.Peek();

            currMsgProcessingState.Consumed += nRead;
            if (typeId == FudgeTypeDictionary.FUDGE_MSG_TYPE_ID)
            {
                CurrentElement = FudgeStreamElement.SubmessageFieldStart;
                FieldValue     = null;
                MessageProcessingState subState = new MessageProcessingState();
                subState.MessageSize = varSize;
                subState.Consumed    = 0;
                processingStack.Push(subState);
            }
            else
            {
                CurrentElement = FudgeStreamElement.SimpleField;
                FieldValue     = ReadFieldValue(Reader, FieldType, varSize);
                if (fixedWidth)
                {
                    currMsgProcessingState.Consumed += type.FixedSize;
                }
                else
                {
                    currMsgProcessingState.Consumed += varSize;
                }
            }
        }
 public void hasOrdinalChecks()
 {
     Assert.False(FudgeFieldPrefixCodec.HasOrdinal(0x20));
     Assert.True(FudgeFieldPrefixCodec.HasOrdinal(0x98));
 }
 public void fixedWidthChecks()
 {
     Assert.False(FudgeFieldPrefixCodec.IsFixedWidth(0x20));
     Assert.True(FudgeFieldPrefixCodec.IsFixedWidth(0x98));
 }
 public void HasNameChecks()
 {
     Assert.False(FudgeFieldPrefixCodec.HasName(0x20));
     Assert.True(FudgeFieldPrefixCodec.HasName(0x98));
 }