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.
#include employee.h // include file generated by ASN1C int main () { ASN1TAG tag; int i, len; const char* filename = "message.dat"; ASN1BOOL trace = TRUE; try { // Decode ASN1BERFileInputStream in (filename); ASN1T_PersonnelRecord msgData; ASN1C_PersonnelRecord employee (msgData); try { for (;;) { in.mark (32); in.decodeTagAndLen (tag, len); in.reset (); // Now switch on initial tag value to determine what // type of message was received.. switch (msgtag) { case TV_PersonnelRecord: // compiler generated // constant { in >> employee; // or employee.DecodeFrom (in); } case TV_ ... // handle other known messages ... } // Need to reinitialize objects for next iteration employee.memFreeAll (); } // end of loop } catch (ASN1RTLException& ex) { printf ("decode of PersonnelRecord failed\n"); printf ("Exception thrown: Status = %i\n", ex.getStatus()); in.printErrorInfo (); return -1; } } catch (ASN1RTLException& ex) { printf ("Exception thrown: Status = %i\n", ex.getStatus()); return -1; } 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.102 Pickering Way, Suite #506Exton, 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 |