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 ASN1OCTET* msgptr;
 
        ASN1OCTET msgbuf[1024];
 
        int       msglen;
 
	  ASN1CTXT  	ctxt;
 
        PersonnelRecord data;
 

 
   	  /* Init context structure */
 

 
   	  if ((stat = rtInitContext (&ctxt)) != ASN_OK) {
 
      		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";
 
	      ...
 

 
            // invoke Encode method 
 

 
	      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 the rtMemReset to reset the memory heap for the next
 
		// iteration. Note, all data allocated by rtMemAlloc will become
 
		// invalid after this call.
 
            rtMemReset (&ctxt);
 
        }
 

 
	  rtFreeContext (&ctxt);
 
    }
 

Note, the rtMemReset actually does not free the memory heap - it just marks it as empty and it will be reused in the next iteration. Thus, all allocated by rtMemAlloc memory will be overwritten and data will be lost. If user really needs to keep some data allocated by rtMemAlloc through all iterations you need either to copy it or use another context's instance to allocate memory for such data.


Objective Systems, Inc.

102 Pickering Way, Suite #506
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