TOC PREV NEXT INDEX


Encoding a Series of Messages Using the C Encode Functions



A common application of BER encoding is the repetitive encoding of a series of the same type of message over and over again. For example, a TAP3 batch application might read billing data out of a database table and encode each of the records for a batch transmission.

If a user was to repeatedly allocate/free memory and reinitialize the C objects involved in the encoding of a message, performance would suffer. This is not necessary however, because the C objects and memory heap can be reused to allow multiple messages to be encoded. As example showing how to do this is as follows:

    #include employee.h         /* include file generated by ASN1C */
 

 
    main ()
 
    {
 
        const OSOCTET* msgptr;
 
        OSOCTET   msgbuf[1024];
 
        int       msglen;
 
        OSCTXT    ctxt;
 
        PersonnelRecord data;
 

 
        /* Init context structure */
 

 
        if ((stat = rtInitContext (&ctxt)) != 0) {
 
           printf ("rtInitContext failed; stat = %d\n", stat);
 
           return -1;
 
        }
 

 
        /* Encode loop starts here, this will repeatedly use the 
 
         * objects declared above to encode the messages */
 

 
        for (;;) {
 

 
           xe_setp (&ctxt, msgbuf, sizeof(msgbuf));
 

 
           /* logic here to read record from some source (database, 
 
            * flat file, socket, etc.).. */
 

           /* populate structure with data to be encoded */
 

 
           data.name = "SMITH";
 
           ...
 

 
            /* call encode function */
 

 
            if ((msglen = asn1E_PersonnelRecord (&ctxt, &data, ASN1EXPL)) > 0) {
 

 
                /* encoding successful, get pointer to start of message */
 

 
                msgptr = xe_getp (&ctxt);
 

 
                /* do something with the encoded message */
 

 
                ...
 
            }
 
            else
 
               error processing...
 

 
            /* Call rtxMemReset to reset the memory heap for the next
 
             * iteration. Note, all data allocated by rtxMemAlloc will 
 
             * become invalid after this call. */
 

 
            rtxMemReset (&ctxt);
 
        }
 

 
        rtFreeContext (&ctxt);
 
    }
 

 
The rtxMemReset call does not free memory; instead, it marks it as empty so that it may be reused in the next iteration. Thus, all memory allocated by rtxMemAlloc will be overwritten and data will be lost.


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