
Procedure for Using the Stream-Oriented C++ Control Class Decode Method
Normally the receiving message can be one of several different message types. It is therefore necessary to determine the type of message that was received so that the appropriate decode function can be called to decode it. The ASN1BERDecodeStream class has standard methods for parsing the initial tag/length from a message to determine the type of message received. These calls are used in conjunction with a switch statement on generated tag constants for the known message set. Each switch case statement contains logic to create an object instance of a specific ASN1C generated control class and to invoke and then to invoke that object's decode method.
#include "Employee.h" // include file generated by ASN1C #include "rtbersrc/ASN1BERDecodeStream.h" #include "rtxsrc/OSRTFileInputStream.h" 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; } if (in.peekTagAndLen (tag, len) != 0) { printf ("peekTagAndLen failed\n"); in.printErrorInfo (); return -1; }// type of message was received.. switch (msgtag) { case TV_PersonnelRecord: // compiler generated // constant { ASN1T_PersonnelRecord msgData; ASN1C_PersonnelRecord employee (msgData); in >> employee; if (in.getStatus () != 0) { printf ("decode of PersonnelRecord failed\n"); in.printErrorInfo (); return -1; } // or employee.DecodeFrom (in); break; } case TV_ ... // handle other known messages ... } } return 0; }Note that the call to free memory and the stream close method are not required to release dynamic memory when using the C++ interface. This is because the control class hides all of the details of managing the context and releasing dynamic memory. The memory is automatically released when both the input stream object (ASN1BERDecodeStream and derived classes) and the control class object (ASN1C_<ProdName>) are deleted or go out of scope. Reference counting of a context variable shared by both interfaces is used to accomplish this.
Objective Systems, Inc.55 Dowlin Forge RoadExton, 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 |