Boost logo

Boost :

From: Christopher Kohlhoff (chris_at_[hidden])
Date: 2006-06-06 02:33:30


Hi Yuval,

Yuval Ronen <ronen_yuval_at_[hidden]> wrote:
> In a 'receiver makes right' protocols, as you describe it, the
> sender sends the data it in its native endianness. I assume it
> probably also sends the type of its native endianness, big or
> little, otherwise the receiver won't be able to know it.

Yes, this information may be transmitted when the connection
is first established or in a message header.

> Then, the receiver reads the endianness, and then reads all
> data according to it (which means run-time selection of
> endianness, hopefully possible using boost::variant).

Yep, although I'm thinking generic code using templates, where
the template parameter is based on the endianness.

> All of this is great, but I can't see the relevance to the
> existence of a 'native' option in the endianness enum, sorry.
> Can you elaborate, please?

It lets me define my message structures once to cater for all
types of endianness. E.g. a very simple example:

  tempalte <endianness E>
  struct message
  {
    endian<E, short, 2> first;
    endian<E, int, 4> second;
    endian<E, int, 4> third;
    ...
  };

Sender may write:

  message<native> m;
  m.first = ...
  write(sock, buffer(&m, sizeof(m)));

Receiver may write:

  if (peer_is_big_endian)
  {
    message<big> m;
    read(sock, buffer(&m, sizeof(m)));
    process_message(m);
  }
  else
  {
    message<little> m;
    read(sock, buffer(&m, sizeof(m)));
    process_message(m);
  }

And both sender and receiver may make use of template functions
for processing message structures:

  template <endianness E>
  void process_message(message<E>& m)
  {
    ...
  }

Cheers,
Chris


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk