Thursday, March 31, 2011

What's the 'best' way to parse a stream into a struct or class?

Hi Everyone,

actually i'm working with .Net Framework 3.5, so i have all these nice little features like lambdas, linq, etc.

Given is a serial connection (or to be more abstract: a stream) where you receive some data, which will be in a format like this:

struct Packet
{
    byte STX
    UInt16 DataLength
    string Data
    byte CRC
    byte ETX
}

Using a simple mapping of the incoming data doesn't help due to the fact, that you don't really know how long one packet will be, cause it's written within the structure (the DataLength).

So my first idea would be to read the stream by byte and put it into ???. Yes, that's the next question. Where to store this first Raw data? Into a simple byte array, with the maximum possible length (that would be 65540 bytes, due to the fact, that DataLength is an UInt16 plus the additional bytes from the other fields). Or should i open up a Queue and fill it up will all the incoming bytes or maybe exists there another nice possibility?

Let's assume these problems are cleared and i have some kind of local buffer, that holds all the raw bytes from the stream. What's the nicest way to interpret it by the given structure?? Just doing some kind of for- or foreach-loop or exists there a smarter (with better performance) way (e.g. with regex or linq)?

Best regards, Oliver

From stackoverflow
  • I would store them in a byte array and recreate them from there, its a fast and simple way to do it!

    I would read the bytes and convert them With BitConverter, Encoding.UTF8..

  • Check this out, anyways it boils down to using the [Serializable] attribute, and your done http://www.ondotnet.com/pub/a/dotnet/2002/08/26/serialization.html

    Oliver : Sorry, serialization doesn't work, due to the fact, that i'm just a receiver of the data and can't change the layout to get serialization to work.
    Robert Gould : That's too bad :(
  • How about...

    struct Packet
    {
        public byte STX;
        public UInt16 DataLength;
        public string Data;
        public byte CRC;
        public byte ETX;
    }
    
    //Warning: Need to add error handling
    class StreamPacket
    {
        private BinaryReader _reader;
    
        public StreamPacket(Stream stream)
        {
            _reader = new BinaryReader(stream);
        }
    
        Packet ToPacket()
        {
            var packet = new Packet();
            packet.STX = _reader.ReadByte();
            packet.DataLength = _reader.ReadUInt16();
            packet.Data = Encoding.ASCII.GetString(
                _reader.ReadBytes(packet.DataLength));
            packet.CRC = _reader.ReadByte();
            packet.ETX = _reader.ReadByte();
    
            return packet;
        }
    }
    

    Please note: I have not used BinaryReader.ReadString() on purpose because it is designed to operate on strings generated by BinaryWriter.WriteString(). The encoding is a bit different even though its a length prefixed string.

    Marc Gravell : I'd probably add IDisposable, and perhaps call it ReadPacket - but a good answer, +1
    SDX2000 : But why is IDisposable required here? I am not using any unmanaged resources, why not let the garbage collector do it for me?
    Marc Gravell : "unmanaged resources" would be a finalizer. IDisposable is because you are encapsulating/representing something *else* that is IDisposable - the Stream and BinaryReader. Your Dispose() should call _reader.Dispose();
    Oliver : Seems to be a good shot. As already mentioned some error handling is needed, but i think i'll give it a try.
    SDX2000 : @Marc Gravell: The stream was not created by me in the first place and closing it inside my class would be counter intuitive.
    Marc Gravell : @SDX2000 - fair enough, but ownership is often assumed to transfer in such uses - BinaryReader, for example, assumes ownership in this way. The main thing is doucmenting it, I guess...

0 comments:

Post a Comment