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
 
   #include "rtbersrc/ASN1BERDecodeStream.h"
 
   #include "rtxsrc/OSRTFileInputStream.h"
 

 
   int main ()
 
   {
 
      ASN1TAG      tag;
 
      int          i, len;
 
      const char*  filename = "message.dat";
 
      OSBOOL       trace = TRUE;
 

 
      // Decode 
 

 
      ASN1BERDecodeStream in (new OSRTFileInputStream (filename));
 
      if (in.getStatus () != 0) {
 
         in.printErrorInfo ();
 
         return -1;
 
      }
 

 
      ASN1T_PersonnelRecord msgData;
 
      ASN1C_PersonnelRecord employee (msgData);
 

 
      for (;;) { 
 
         if (in.peekTagAndLen (tag, len) != 0) {
 
            printf ("peekTagAndLen failed\n");
 
            in.printErrorInfo ();
 
            return -1;
 
         }
 

 
         // Now switch on initial tag value to determine what 
 
         // type of message was received..
 

 
         switch (msgtag)
 
         {
 
            case TV_PersonnelRecord:  // compiler generated
 
                                      // constant
 
            {
 
               in >> employee;
 

 
               if (in.getStatus () != 0) {
 
                  printf ("decode of PersonnelRecord failed\n");
 
                  in.printErrorInfo ();
 
                  return -1;
 
               }
 
        
 
               // or employee.DecodeFrom (in);
 

 
            }
 

 
            case TV_ ...	// handle other known messages
 
               ...
 
         }
 

 
         // Need to reinitialize objects for next iteration
 

 
         employee.memFreeAll ();
 

 
      }  // end of loop
 

 
 	return 0;
 
   }
 


This is quite similar to the first example. Note that we have pulled the ASN1T_Employee and ASN1C_Employee object creation logic out of the switch statement and moved it above the loop. These objects can now be reused to process each received message.

The only other change was the call to employee.memFreeAll that was added at the bottom of the loop. Since the objects are not deleted to automatically release allocated memory, we need to do it manually. This call will free all memory held within the decoding context. This will allow the loop to start again with no outstanding memory allocations for the next pass.


Objective Systems, Inc.

55 Dowlin Forge Road
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