TOC PREV NEXT INDEX


Procedure for Using the C++ Control Class Encode Method



The procedure to encode a message using the C++ class interface is as follows:


The constructor of the ASN1C_<type> class takes a message buffer object argument. This makes it possible to specify a static encode message buffer when the class variable is declared. A static buffer can improve encoding performance greatly as it relieves the internal software from having to repeatedly resize the buffer to hold the encoded message. If you know the general size of the messages you will be sending, or have a fixed size maximum message length, then a static buffer should be used. The message buffer argument can also be used to specify the start address and length of a received message to be decoded.

After the data to be encoded is set, the Encode method is called. This method returns the length of the encoded message or a negative value indicating that an error occurred. The error codes can be found in the asn1type.h run-time header file or in Appendix A of the C/C++ Common Functions Reference Manual.

If encoding is successful, a pointer to the encoded message can be obtained by using the getMsgPtr or getMsgCopy methods available in the ASN1BEREncodeBuffer class. The getMsgPtr method is faster as it simply returns a pointer to the actual start-of-message that is maintained within the message buffer object. The getMsgCopy method will return a copy of the message. Memory for this copy will be allocated using the standard new operator, so it is up to the user to free this memory using delete when finished with the copy.

A program fragment that could be used to encode an employee record is as follows. This example uses a static encode buffer:

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

 
    main ()
 
    {
 
        const OSOCTET* msgptr;
 
        OSOCTET msgbuf[1024];
 
        int       msglen;
 

 
        // step 1: construct ASN1C C++ generated class. 
 
        // this specifies a static encode message buffer
 

 
        ASN1BEREncodeBuffer encodeBuffer (msgbuf, sizeof(msgbuf));
 
        ASN1T_PersonnelRecord msgData;
 
        ASN1C_PersonnelRecord employee (encodeBuffer, msgData);
 

 
        // step 2: populate msgData structure with data to be encoded
 

 
        msgData.name = "SMITH";
 
	  ...
 

 

 
        // step 3: invoke Encode method 
 

 
        if ((msglen = employee.Encode ()) > 0) {
 
           // encoding successful, get pointer to start of message
 
           msgptr = encodeBuffer.getMsgPtr();
 
        }
 
        else
 
          error processing...
 
    }
 

The following code fragment illustrates encoding using a dynamic buffer. This also illustrates using the getMsgCopy method to fetch a copy of the encoded message:

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

 
    main ()
 
    {
 
        OSOCTET*  msgptr;
 
        int       msglen;
 

 
        // construct encodeBuffer class with no arguments
 

 
        ASN1BEREncodeBuffer encodeBuffer;
 
        ASN1T_PersonnelRecord msgData;
 
        ASN1C_PersonnelRecord employee (encodeBuffer, msgData);
 

 
        // populate msgData structure
 

 
        msgData.name = "SMITH";
 
        ...
 

 
        // call Encode method
 

 
        if ((msglen = employee.Encode ()) > 0) {
 
          // encoding successful, get copy of message
 
          msgptr = encodeBuffer.getMsgCopy();
 
          ...
 

 
          delete [] msgptr;  // free the dynamic memory!
 
        }
 
        else
 
          error processing...
 
    }
 



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