Click here to Skip to main content
15,891,633 members
Articles / Programming Languages / C++
Tip/Trick

Encode and Decode messages send to and from apps

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
29 Mar 2013CPOL1 min read 13.2K   123   5   6
Makes message handling between applications easier, by encoding/decoding to/from one data block.

Introduction

Image 1

The code is a Data Encoder / Decoder library. It makes inter application communication easier, by providing a way to encode and decode a message based on a user defined protocol.

It also makes a simple compression of the "message" created by the encoder part. The decoder part will then decompress and decode message, based on the provided protocol

Background

It is using a simplified version of ASN (Abstract Syntax Notation) to encode / decode the data, and the compression algorithm is based on a simplified version of Hoffman coding

Using the code

The DataEncoderDecoder is a standard static library and should be added to your project as a header file and a library file - the library is static which means that it will be embedded in your projects executable

Example: Creating an event message to be send to a server asking it to use method MusicPlayer passing on a transaction id and action Start/Stop.  All data is in pCompressedData!!! It is this memory that should be send:

C++
// Client code
// Create DataEncoderDecoder response
// Encode
DED_START_ENCODER(encoder_ptr);
DED_PUT_STRUCT_START( encoder_ptr, "event" );
    DED_PUT_METHOD    ( encoder_ptr, "name",      "MusicPlayer" );
    DED_PUT_USHORT    ( encoder_ptr, "trans_id",  trans_id);
    DED_PUT_BOOL    ( encoder_ptr, "startstop", action );
DED_PUT_STRUCT_END( encoder_ptr, "event" );
DED_GET_ENCODED_DATA(encoder_ptr,data_ptr,iLengthOfTotalData,pCompressedData,sizeofCompressedData);

// Data to be sent is in pCompressedData

// Server code
// retrieve data ...
//...

CString strName,strValue;
unsigned short iValue;
bool bValue;

DED_PUT_DATA_IN_DECODER(decoder_ptr,pCompressedData,sizeofCompressedData);

// decode data ...
if( DED_GET_STRUCT_START( decoder_ptr, "event" ) &&
     DED_GET_METHOD    ( decoder_ptr, "name", strValue ) &&
     DED_GET_USHORT    ( decoder_ptr, "trans_id", iValue) &&
     DED_GET_BOOL    ( decoder_ptr, "startstop", bValue ) &&
 DED_GET_STRUCT_END( decoder_ptr, "event" ))
{
    TRACE0(_T("FAIL!!!\n"));
}
else
{
    TRACE0(_T("SUCCESS!!!\n"));
}

Points of Interest

When trying to use this type of data encoder / decoder, then remember from which type of computer you are sending / receiving, I am thinking about the Big/Little  Endian principle - so far the DataEncoderDecoder does not take care of this (perhaps a smart reader could add this)  Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) serupIT
Denmark Denmark
(BS.E.E)
I have been working with languages ranging from assembly 68000 to COBOL, OOC, MySQL, Perl, Java 8, JavaScript, C#, C++17, VC++ MFC and WinSockets.

I am currently working as a freelance software developer

Comments and Discussions

 
QuestionTwo years later... Pin
OriginalGriff29-Mar-13 2:41
mveOriginalGriff29-Mar-13 2:41 
GeneralNot an article yet... [modified] Pin
Dave Kreskowiak14-May-11 3:10
mveDave Kreskowiak14-May-11 3:10 
JokeRe: Not an article yet... Pin
Richard MacCutchan14-May-11 22:15
mveRichard MacCutchan14-May-11 22:15 
GeneralRe: Not an article yet... Pin
Dave Kreskowiak15-May-11 2:30
mveDave Kreskowiak15-May-11 2:30 
GeneralRe: Not an article yet... Pin
serup9-Jun-11 20:41
serup9-Jun-11 20:41 
GeneralRe: Not an article yet... Pin
Dave Kreskowiak10-Jun-11 1:32
mveDave Kreskowiak10-Jun-11 1:32 
Yeah, at the very least it might get approved for public viewing.

Personally, I hate it when it takes me 20 seconds to read through an article and it doesn't explain anynthing and leave me wanting more.

Depending on the reader to download the sample and figure out the code directly makes for a lousy article. You're making the huge assumption that the reader understands what every line of code does.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.