예제 #1
0
        public static void DumpMessageDefinition(MessageDefinition messageDefinition)
        {
            Debug.WriteLine("Message definition seen: {0}, local message number: {1}",
                            messageDefinition.GlobalMessageNumber,
                            messageDefinition.LocalMessageNumber);

            foreach (var fieldDefinition in messageDefinition.FieldDefinitions)
            {
                Debug.WriteLine("::::Field definition number: {0}, Size: {1}, Type: {2}", fieldDefinition.FieldDefinitionNumber,
                                fieldDefinition.FieldOffset,
                                fieldDefinition.FieldType);
            }
        }
예제 #2
0
        public IEnumerable <Message> GetMessages()
        {
            uint bytesToRead = _fileHeader.DataSize;
            uint bytesRead   = 0;

            while (bytesRead < bytesToRead)
            {
                byte header = _reader.ReadByte();

                // Normal header (vs. timestamp offset header is indicated by bit 7)
                // Message type is indicated by bit 6
                //   1 == definition
                //   0 == record
                byte localMessageNumber = (byte)(header & 0xf);

                // Message definitions are parsed internally by the parser and not exposed to
                // the caller.
                if ((header & 0x80) == 0 && (header & 0x40) == 0x40)
                {
                    // Parse the message definition and store the definition in our array
                    var messageDefinition = new MessageDefinition(header, _reader);
                    _localMessageDefinitions[localMessageNumber] = messageDefinition;
                    bytesRead += (uint)(messageDefinition.MessageDefinitionSize + 1);
                }
                else if ((header & 0x80) == 0 && (header & 0x40) == 0)
                {
                    var currentMessageDefinition = _localMessageDefinitions[localMessageNumber];
                    Debug.Assert(currentMessageDefinition != null);

                    // This design reads the current message into an in-memory byte array.
                    // An alternate design would involve passing in the current binary reader
                    // and allowing the caller of Message to read fields using the binary
                    // reader directly instead of creating a MemoryStream over the byte array
                    // and using a different BinaryReader in the Message. I have done
                    // exactly this and measured the performance, and it is actually SLOWER
                    // than this approach. I haven't root caused why, but would assume that
                    // Seek-ing arbitrarily using the BinaryReader over the FileStream is
                    // slow vs. Seek-ing over a BinaryReader over a MemoryStream.

                    var message = new Message(header, currentMessageDefinition, _reader);
                    yield return(message);

                    bytesRead += (uint)(currentMessageDefinition.Size + 1);
                }
            }
        }
예제 #3
0
 public Message(byte header, MessageDefinition messageDefinition, BinaryReader reader)
 {
     _header            = header;
     _messageDefinition = messageDefinition;
     _messageData       = reader.ReadBytes(_messageDefinition.Size);
 }