Forming protocol messages with structure instances

P

pramod

Two differenet applications (running on different OS, different
compilers) are communicating thru protocol consisting of functions and
arguments.
Communication link can be TCP/IP, or serial port.

What is the best way to map structure instance to protocol messages ?

The solutions we are thinking of -
1. declare each structure member as packed, and use memcpy for struct
size.
The target application should directly memcpy this area, to the
desired structure, which is already instantiated.

2. serialize each entry with member data type, followed by value, and
transmit it over link.
Value needs to be decoded using data type flag.


1st option might not be portable.
2nd option requires extra efforts and overhead.

Is there any better alternative?


regards,
Pramod
 
A

Artie Gold

pramod said:
Two differenet applications (running on different OS, different
compilers) are communicating thru protocol consisting of functions and
arguments.
Communication link can be TCP/IP, or serial port.

What is the best way to map structure instance to protocol messages ?

The solutions we are thinking of -
1. declare each structure member as packed, and use memcpy for struct
size.
The target application should directly memcpy this area, to the
desired structure, which is already instantiated.

Portability problems:
Endian issues
Data type size issues (possibly less of a problem in C99)
2. serialize each entry with member data type, followed by value, and
transmit it over link.
Value needs to be decoded using data type flag.
Indeed.



1st option might not be portable.
Right.

2nd option requires extra efforts and overhead.

Also correct.
About all you can do is come up with the lightest weight
serialization/deserialization protocol (or use some third party software
to help you achieve this).
Is there any better alternative?

Not really. ;-(
Standard C is much more concerned with cross platform source
compatibility than data compatibility (at least partially due to
historical reasons).

HTH,
--ag
 
D

Derk Gwen

(e-mail address removed) (pramod) wrote:
# Two differenet applications (running on different OS, different
# compilers) are communicating thru protocol consisting of functions and
# arguments.
# Communication link can be TCP/IP, or serial port.
#
# What is the best way to map structure instance to protocol messages ?
#
# The solutions we are thinking of -
# 1. declare each structure member as packed, and use memcpy for struct
# size.
# The target application should directly memcpy this area, to the
# desired structure, which is already instantiated.

Not at all safe with different compilers.

# 2. serialize each entry with member data type, followed by value, and
# transmit it over link.
# Value needs to be decoded using data type flag.

You might want to look into ASN-1 and its encoding rules, rather than start
over from scratch. ASN software and libraries are available.

# Is there any better alternative?

If at all possible encode into an ASCII string. Much more likely to be
portable, avoids byte order problems, eases debugging, and if you redirect
traffic to a file, you can use off the shelf programming tools to look at it.
The overhead of network communications will likely overwhelm encoding
and decoding costs.
 
O

Olivier Dubuisson

Derk Gwen said:
# 2. serialize each entry with member data type, followed by value, and
# transmit it over link.
# Value needs to be decoded using data type flag.

You might want to look into ASN-1 and its encoding rules, rather than start
over from scratch. ASN software and libraries are available.

FYI a lot of ASN.1 compilers and libraries are listed at
http://asn1.elibel.tm.fr/links/#tools

O. Dubuisson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top