Home Products Consulting Download Registration Support Events

Home
Up

Brief introduction about memory queue -- CUQueue object

UDAParts
support@udaparts.com
Updated on January 03, 2011

Contents

  1. Introduction
  2. C# and VB.NET samples
  3. Key design considerations
  4. Compatibility among different development languages
  5. Native VARIANT and .NET object
  6. Use of .NET serialization with CUQueue
  7. String serialization
  8. Generics Save/Load and operators <</>> in C++
  9. IUSerializer for complex user-defined classes and structures
  10. .NET null/nothing objects serialization supported

1.    Introduction
        Development with SocketPro requires converting data types into bytes. The conversion is not difficult at all, but it is indeed tedious considerably. In order to ease SocketPro development, SocketPro provides a helper class CUQueue to efficiently convert various data types into bytes and manage memory. Later, you can effortlessly exchange a request and a returned result in binary format into original data types at both server and client sides.

2.    C# and VB.NET samples
        See demo C# and VB.NET projects in side directory ..\samples\UQueueTest.

3.    Key design considerations
        CUQueue is designed with a set of key considerations:
        a.    Easy to use. You are never required to manage internal memory directly.
        b.    Auto-manage memory. When you push or insert a data type into an instance of CUQueue, it will automatically increase memory if required.
        c.    Highly efficient. Use of CUQueue can avoid allocating and de-allocating memories repeatedly, which significantly helps the performance and reduces memory resources and page fault.
        d.    Great compatibility among different development languages like VB.NET, C# and C/C++.

4.    Compatibility among different development languages
        SocketPro supports development using different languages. For example, you can evenly develop C++ client application that directly talks with any dotNet languages without requiring any other middle components. To achieve this key feature, the class CUQueue is written with the mind of compatibility among different development languages for a set of basic data types. Here is the table about the compatibility among four development languages, JavaScript, VB.Net, C# and C/C++.
       

Data Type Size (byte) VB.Net C# JavaScript plug-in (save/load) C/C++
unsigned byte 1 byte byte Byte unsigned char
signed byte 1 byte sbyte Byte signed char
bool 1 boolean bool Bool bool
short 2 short short Int16 short
int 4 Integer int Int32 int
long 4 Integer int Int32 long
float 4 Single float Float float
double 8 Double double Double double
unsigned short 2 short ushort Int16 unsigned short
unsigned int 4 Integer uint Int32 unsigned int
unsigned long 4 Integer uint Int32 unsigned long
decimal* 16 decimal decimal Decimal DECIMAL
Currency 8 decimal decimal   Currency
LONGLONG 8 Long long Int64 LONGLONG
ULONGLONG 8 Long ulong Int64 ULONGLONG
wide string   string string String wide string
variant bool 2 short ushort   unsigned short
variant date 8 DateTime DateTime Date (FileTime) double
wide char 2 Char char   wide char
GUID 16 Guid Guid   GUID
File Time 8 FILETIME FILETIME   FILETIME
Others*         Others

        
5.    Native VARIANT and dotNet object
        All of data types or their array can be boxed into object in dotNet platform, which is similar with VARIANT in the native C/C++ development environment. SocketPro adapters support conversion between C/C++ VARIANT and .NET object with a few exceptions to the above table as indicated with character "*". SocketProAdapter for JavaScript supports serialization and de-serialization of objects (VARIANT) too. An object or VARAINT can contain an array of its children, which may contain their own children. These children can be either the same or different data primitive types, strings or their arrays.

6.    Use of dotNet serialization within CUQueue
        Dotnet version of CUQueue can take advantage of dotNet serialization if your development is involved with managed world only. You can call  CUQueue::Serialize and Deserialize to push and pop a managed object into and from CUQueue, respectively, with one of the two formats, binary and XML.

7.    String serialization
        To serialize a string, we first save a length in unsigned int (4 bytes) in bytes and actual string context afterward by calling the method Save in .NET or operator << in C++. If the length is 0xFFFFFFFF, the string is a null for C++ and C# or nothing for VB.NET. As you can see, we first pop out a length in unsigned int first and call the method Load in .NET or operator >> in C++ for reading a string from an instance of CUQueue.

8.    Generics Save/Load in .NET and operators <</>> in C++
        To make code simpler, SocketPro adapter for .NET now uses the generics methods Save and Load for saving and reading data to or from an instance of CUQueue. Internally, .NET adapter will uses a proper method (Push/Pop for primitive data types, Save/Load for string, IUSerializer or Serialize/Deserialize for .NET objects) to save or load data from generics data type. In regards to C++, you may need to override operators << and >> to save instances of complicate user-defined classes. For simple data types or structures, adapter for C++ will automatically handle them for you correctly through templates.

9.    IUSerializer for .NET complex user-defined classes or structures preferred over .NET serialization
         To serialize or de-serialized a complex user-defined class or structure, it is highly recommend that the class or structure should be implemented with the interface IUSerializer. The implementation is truly simple. Besides, you can easily make .NET code and native compatible to each other. Also, it makes code run much faster than .NET serialization. At the last, .NET serialization is not available to .NET compact environment, but IUSerializer is fully supported on devices. To see the details, please refer to the above sample code and the tutorial three.

10.    .NET null/nothing object serialization supported
        At this writing, .NET null/nothing objects serialization and de-serialization are well supported for three types of objects. The first one is simple object containing primitive data types, string and arrays of primitive data types and string. The second type is an instance of a user-defined class or structure implemented with the interface IUSerializer. The third type is a native .NET serializable object. Also, you can easily make the first two types of objects compatible with not only native code development like C/C++ but also .NET compact framework. As described in the above section, you can not make compatible communication between native .NET serializable object and native code development like C/C++ as well as .NET compact framework.