Procedure for Using the C++ Control Class Encode Method
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 of this document.
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 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 ASN1OCTET* msgptr; ASN1OCTET 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 // (note: this uses the generated assignment operator to assign // a string).. employee.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 () { ASN1OCTET* msgptr; int msglen; // construct encodeBuffer class with no arguments ASN1BEREncodeBuffer encodeBuffer; ASN1T_PersonnelRecord msgData; ASN1C_PersonnelRecord employee (encodeBuffer, msgData); // populate msgData structure employee.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.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 |