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 ()
   {
      OSOCTET msgbuf[1024];
      int     msglen, stat;
                     
      // step 1: instantiate a OER decode buffer object
               
      ASN1OERDecodeBuffer decodeBuffer (msgbuf, msglen);
               
      // 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 == 0)
         {
            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.