TOC PREV NEXT INDEX


Decoding a Series of Messages Using the C++ Control Class Interface



The above example is fine as a sample for decoding a single message, but what happens in the more typical scenario of having a long-running loop that continuously decodes messages? The logic shown above would not be optimal from a performance standpoint because of the constant creation and destruction of the message processing objects. It would be much better to create all of the required objects outside of the loop and then reuse them to decode and process each message.

A code fragment showing a way to do this is as follows:

    #include employee.h         // include file generated by ASN1C
 
    main ()
 
    {
 
        ASN1OCTET msgbuf[1024];
 
        int       msglen, stat;
 
        ASN1BOOL  aligned = TRUE;
 

 
        // step 1: instantiate a PER decode buffer object
 

 
        ASN1PERDecodeBuffer decodeBuffer (msgbuf, msglen, aligned);
 

 
        // step 2: instantiate an ASN1T_<ProdName> object
 

 
        ASN1T_PersonnelRecord msgData;
 

 
        // step 3: instantiate an ASN1C_<ProdName> object
 

 
        ASN1C_PersonnelRecord employee (decodeBuffer, msgData);
 

 
        // loop to continuously decode records
 

 
        for (;;) {
 
            .. logic to read message into msgbuf ..
 

 
            stat = employee.Decode ();
 
            // step 5: check the return status
 

 
            if (stat == ASN_OK)
 
            {
 
                process received data..
 
            }
 
            else {
 
                // error processing..
 
                decodeBuffer.PrintErrorInfo ();
 
            }
 

 
            // step 6: free dynamic memory 
 

 
            employee.memFreeAll ();
 
        }
 
    }
 

The only difference between this and the previous example is the addition of the decoding loop and the modification of step 6 in the procedure. The decoding loop is an infinite loop to continuously read and decode messages from some interface such as a network socket. The decode calls are the same, but before in step 6, we were counting on the message buffer and control objects to go out of scope to cause the memory to be released. Since the objects are now being reused, this will not happen. So the call to the memFreeAll method that is defined in the ASN1C_Type base class will force all memory held at that point to be released.



Objective Systems, Inc.

102 Pickering Way, Suite #506
Exton, Pennsylvania 19341
http://www.obj-sys.com
Phone: (484) 875-9841
Toll-free: (877) 307-6855 (US only)
Fax: (484) 875-9830
info@obj-sys.com
TOC PREV NEXT INDEX