Question

We have an application where we need to de-serialize some data from one stream into multiple objects.

The Data array represents a number of messages of variable length packed together. There are no message delimiting codes in the stream.

We want to do something like:

void Decode(byte[] Data)  
{  
    Object0.ExtractMessage(Data);
    Object1.ExtractMessage(Data);
    Object2.ExtractMessage(Data);
    ...
}  

where each ProcessData call knows where to start in the array. Ideally we'd do this without passing a DataIx reference in.

To do this in C++ we'd just hand around a pointer into the array, and each ProcessData function would increment it as required.

Each object class knows how its own messages are serialized and can be relied upon (in C++) to return the pointer at the beginning of the next message in the stream.

Is there some inbuilt mechanism we can use to do this (without going unsafe)? The operation is high frequency (~10kps) and very lightweight. We also don't want to go copying or trimming the array.

Thanks for your help.

Was it helpful?

Solution

Could you not just pass in and return the array index? That is basically all that a pointer is anyway, an offset from a fixed memory location.

OTHER TIPS

Well this sounds like you want a simple stream (E.g. just use MemoryStream as a wrapper around your byte array: stream = new MemoryStream (data)). Just wrap the byte array into a stream and every object reads as much from the stream as it needs and then hands over the stream to the next item. It even has the benefit that you aren't forced to loading the entire byte-array at once.

Other than that you can use pointers in C# exactly the way you did in C++ (though pointers require the unsafe keyword and they are discouraged)

Alternatively you could just pass data and an index variable and then increment the index (which is, in effect, the same as using a pointer but doesn't need unsafe).

How about wrapping the data in a MemoryStream and then passing a StreamReader into the ExtractMessage method?

I guess several things come to mind.

You could simulate the action of the pointer by wrapping the byte[] in a class which also maintained the array offset. Whenever you access the array you would access it thru the class, probably via an accessor method, which returned the next byte and also incremented the offset variable. The class instance could be passed between the different ExtractMessage function calls.

How about using C++/CLI? This would allow you to use familiar C/C++ techniques, and yet be directly callable from C# without the dreaded interop.

Then of course there is the dreaded unsafe option, whereby you obtain a C# pointer to the byte[] and perform the required pointer arithmetic.

You could create a stream from the byte array.

Stream stream = new MemoryStream(data);

Then your processor's could work on streams instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top