TOC PREV NEXT INDEX


Encoding a Series of Messages Using the Stream-Oriented C++ Control Class Interface



Encoding a series of messages using the stream-oriented C++ control class is similar to the C method of encoding. All that is necessary is to create a loop in which EncodeTo or Encode methods will be called (or the overloaded << streaming operator). It is also possible to call different EncodeTo methods (or Encode or operator <<) one after another. An example showing how to do this is as follows:

   #include employee.h         // include file generated by ASN1C
 
   #include "rtbersrc/ASN1BEREncodeStream.h"
 
   #include "rtxsrc/OSRTFileOutputStream.h"
 

 
   int main ()
 
   {
 
      const OSOCTET* msgptr;
 
      OSOCTET msgbuf[1024];
 
      int     msglen;
 
      const char* filename = "message.dat"
 

 
      // step 1: construct stream object. 
 

 
      ASN1BEREncodeStream out (new OSRTFileOutputStream (filename));
 
      if (out.getStatus () != 0) {
 
         out.printErrorInfo ();
 
         return -1;
 
      }
 

 
      // step 2: construct ASN1C C++ generated class.
 

 
      ASN1T_PersonnelRecord msgData;
 
      ASN1C_PersonnelRecord employee (msgData);
 

 
      for (;;) {
 
         // step 3: populate msgData structure with data to be
 
         // encoded. (note: this uses the generated assignment
 
         // operator to assign a string).
 

 
         msgData.name = "SMITH";
 
         ...
 

 
         // step 4: invoke << operator or EncodeTo method
 

 
         out << employee; 
 
         // or employee.EncodeTo (out); can be used here.
 

 
         // step 5: fetch and check status
 

 
         if (out.getStatus () != 0) {
 
            printf ("Encoding failed. Status = %i\n", out.getStatus());
 
            out.printErrorInfo ();
 
            return -1;
 
         }
 

 
         if (trace) {
 
            printf ("Encoding was successful\n");
 
         }
 
      }
 
   }
 

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