Introduction
Very often, we like to serialize a set of data into an arbitrary media such as memory, hard disk, and network in a particular format. As long as you work with a real project, you will most likely meet the problem. Classical C++ standard library provides us input and output streams with extensive use of extraction (>>) and insertion (<<) operators for common primitive data types. MFC uses the class CArchive for the similar purpose. In regards to the .NET framework, it has two ways, binary and XML/SOAP to serialize data into a media. You can find more samples available to you. However, you can't interoperate serialization and de-serialization among these frameworks.
Recently, I have been working with my own framework SocketPro and needed a class named as CUQueue to serialize various data into a memory in binary format. Afterwards, I can send the memory of data into a remote machine by raw socket. The data can be easily de-serialized back to original data in the other end. One particular requirement is that the utility must have very good interoperability among native, .NET and Java codes in the future. You can use the utility to serialize various C/C++ data types (including VARIANT) into a memory and de-serialize these data back to .NET types of data (also including object).
The source codes and sample projects
The .NET version CUQueue is inside the project SocketProAdapter. C++ version of CUQueue is in the file uqueue.h. To demonstrate usage of the utility, I created a client sample project written in C# and a server project in C++. When clicking a client sample application, the sample server first receives the memory chunk and de-serializes a .NET object containing different types of data and objects into a corresponding C++ class object. Secondly, the C++ code serializes the object back to .NET framework. At last, the client C# code de-serializes the C++ memory chunk back to a class object.
If you use VC++ 6, you may have a problem in compiling CUQueue against PushVT and PopVT. Under this case, you should use MS platform SDK no earlier than 2001.
To test client sample project, register usocket.dll first.
Highlights of class CUQueue
The CUQueue class supports all of primitive data types for both C/C++ and the .NET framework. It is so simple that I don't want to speak more about serializing and de-serializing these data types.
- Automatically re-allocate memory if required
When serializing a primitive data or a data structure into a CUQueue memory by calling the method CUQueue::Push or CUQueue::PushVT for variant in C++, the underlying memory may automatically re-allocate memory if the memory chunk does not have enough space available.
- Automatically take care of an array of objects and variants
The CUQueue class fully supports serializing and de-serializing variants in C++ and objects in .NET framework. If an object or variant contains an array of primitive data types or an array of sub-variants or objects, it still works for you, because a variant or object contains the data type information. As a sample, the following code snippet will work.
C# code:
CUQueue UQueue = new CUQueue();
object objMother = null;
object []objKids = new object [3];
object []objGrandKids = new object[3];
objGrandKids[0] = System.DataTime.Now;
objGrandKids[1] = "This is a grand son";
objKids[0] = 1234567;
objKids[1] = "This is not an integer data but a string";
objKids[2] = objGrandKids;
objMother = objKids;
UQueue.Push(objMother, false, false);
CUQueue UQueue;
CComVariant vtData;
UQueue.PopVT(vtData);
- Currency, variant date, variant bool, and system time
The above four native data types cannot be found in managed .NET framework, but they are close to decimal, DateTime, bool and DateTime, respectively. To enable .NET framework code to interoperate with these native date types, the .NET version of CUQueue also provides the four methods PushCY, PushVariantDate, PushVariantBool, and PushSystemTime for serializing .NET types of data into memory chunk, and PopCY, PopVariantDate, PopVariantBool, and PopSystemTime for de-serializing C++ data into .NET framework types of data.
Enhancements in the future
The current simple utility doesn't have a version for Java yet. I will certainly implement a Java version of CUQueue in some time.