
Procedure for Calling Stream-Oriented C Encode Functions
This section describes the step-by-step procedure for calling a stream-oriented C BER encode function. This method must be used if C code generation was done. This method can also be used as an alternative to using the control class interface if C++ code generation was done.
Before any encode function can be called; the user must first initialize an encoding context. This is a variable of type OSCTXT. This variable holds all of the working data used during the encoding of a message. The context variable is within the top-level calling function. It must be initialized before use. This can be accomplished by using the berStrmInitContext function:
OSCTXT ctxt; if (berStrmInitContext (&ctxt) != 0) { /* initialization failed, could be a license problem */ printf ("context initialization failed (check license)\n"); return -1; }The next step is to create a stream object within the context. This object is an abstraction of the output device to which the data is to be encoded and is initialized by calling one of the following functions:
- rtxStreamFileOpen
- rtxStreamFileAttach
- rtxStreamSocketAttach
- rtxStreamMemoryCreate
- rtxStreamMemoryAttach
The flags parameter of these functions should be set to the OSRTSTRMF_OUTPUT constant value to indicate an output stream is being created (see the C/C++ Common Run-Time Library Reference Manual for a full description of these functions).
It is also possible to use a simplified form of these function calls to create a writer interface to a file, memory, or socket stream:
After initializing the context and populating a variable of the structure to be encoded, an encode function can be called to encode the message to the stream. The stream must then be closed by calling the rtxStreamClose function.
#include employee.h /* include file generated by ASN1C */ int main () { int stat; OSCTXT ctxt; Employee employee; /* typedef generated by ASN1C */ const char* filename = "message.dat"; /* Step 1: Initialize the context and stream */ if (berStrmInitContext (&ctxt) != 0) { /* initialization failed, could be a license problem */ printf ("context initialization failed (check license)\n"); return -1; } /* Step 2: create a file stream object within the context */ stat = rtxStreamFileCreateWriter (&ctxt, filename); if (stat != 0) { rtxErrPrint (&ctxt); return stat; } /* Step 3: Populate the structure to be encoded */ employee.name = "SMITH"; ... /* Step 4: Call the generated encode function */ stat = asn1BSE_Employee (&ctxt, &employee, ASN1EXPL); /* Step 5: Check the return status and close the stream */ if (stat != 0) { ...error processing... } rtxStreamClose (&ctxt); }In general, stream-oriented encoding is slower than memory buffer based encoding. However, in the case of stream-oriented encoding, it is not necessary to implement code to write or send the encoded data to an output device. The stream-oriented functions also use less memory because there is no need for a large destination memory buffer. For this reason, the final performance of the stream-oriented functions may be the same or better than buffer-oriented functions.
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 |