Networking

Although this isn’t quite ontopic with SDL, this list seems to have
alot of cross platform devel knowledge. What do people do for
cross platform networking between different platforms? That is how do you
send items like structs between platforms with different data alignment
and endianess?

http://www.mongeese.org

  1. Send structs field-by-field and/or use a defined alignment (or pack
    your structs with no padding)

  2. Send everything in network byte order and use the usual functions
    to convert to host byte order.On Sat, Dec 23, 2000 at 11:04:01PM -0500, Garrett Banuk wrote:

Although this isn’t quite ontopic with SDL, this list seems to have
alot of cross platform devel knowledge. What do people do for
cross platform networking between different platforms? That is how do you
send items like structs between platforms with different data alignment
and endianess?


program, n.:
A magic spell cast over a computer allowing it to turn one’s input
into error messages. tr.v. To engage in a passtime similar to banging
one’s head against a wall, but with fewer opportunities for reward.

Quoting QuoteMstr :

Although this isn’t quite ontopic with SDL, this list seems to
have

alot of cross platform devel knowledge. What do people do for
cross platform networking between different platforms? That is how
do you

send items like structs between platforms with different data
alignment

and endianess?

  1. Send structs field-by-field and/or use a defined alignment (or
    pack
    your structs with no padding)

  2. Send everything in network byte order and use the usual functions
    to convert to host byte order.

For first time I have the sense of having done it right.
Take a look at Arianne Source ( http://www.arianne.cx )

We do the next thing: ( We use C++, you perhaps need to apply it to C)
We create three method on the class: GetSize(), FromVector(Vector )
and ToVector(Vector ).
With these methods we transform a class in an array ob bytes, that are
totally portable, how we do this?

There are two functions on SDL ( or SDL_net ) to convert Uint16 or
Uint32 to a stream of bytes that are portable ( are you aware of
endianess ? )

I am not going to tell you that this is the best way, but it is easy
to use and once you have it running, you are not going to have more
bugs on this.

We create a lot of classes that mimic the basic type data: AUint32,
AUint16, AUint8 and AString. So in the classes we use instead of
doing:

class foo
{
Uint32 a;
Uint32 b;
}

we do

class foo
{
AUint32 a;
AUint32 b;
}

So that these basic classes already have the methods FromVector,
ToVector and GetSize.
So to transform it to a stream of bytes, you should do this:

void foo::ToVector(Vector)
{
Uint8 Dword[4];

SDLNet_…(a,Dword);
Vector->Add(Dword);
SDLNet_…(b,Dword);
Vector->Add(Dword);
}
(And supposing that you have a good vector type)
But by creating the basic classes you just do:

void foo::ToVector(Vector)
{
a.ToVector(Vector);
b.ToVector(Vector);
}

The functions are at the end of SDLNet.h, but I don’t remember its
name now.

Regards,
Miguel> On Sat, Dec 23, 2000 at 11:04:01PM -0500, Garrett Banuk wrote:

So far the method I’ve come up with is to put a string as the first
member of all the structs that will be sent across the network that say
what it’s members are. Basically how a printf() reads arguments off of the
stack. So the first param could be “iidl” meaning int,
int, double, long. This way you only need one routine to send structs
across the network because it will just read the information on what the
struct is from the struct itself. Then the data can be sent across the
network using something along the form of the XDR standard.

The only problem is that it relys on the programmer to initialize the
struct correctly every time a struct is declared.

Has anyone looked at RPC as a cross platform network solution? Or parts of
it?

http://www.mongeese.orgOn Sun, 24 Dec 2000, MIGUEL ANGEL BLANCH LARDIN wrote:

Quoting QuoteMstr :

On Sat, Dec 23, 2000 at 11:04:01PM -0500, Garrett Banuk wrote:

Although this isn’t quite ontopic with SDL, this list seems to
have

alot of cross platform devel knowledge. What do people do for
cross platform networking between different platforms? That is how
do you

send items like structs between platforms with different data
alignment

and endianess?

  1. Send structs field-by-field and/or use a defined alignment (or
    pack
    your structs with no padding)

  2. Send everything in network byte order and use the usual functions
    to convert to host byte order.

  1. Send structs field-by-field and/or use a defined alignment (or pack
    your structs with no padding)

never use any kind of compiler directive to pack struct members, unless it’s
for a quick and hacky port. Better do it manually, like:

struct good { struct bad pragma(do_what_I_mean) {
Uint8 a; char a;
Uint8 b[4]; int b;
Uint8 c[2]; instead of short c;
Uint8 d[8]; double d;
}; };

If you are designing your own file format or network protocol you may
use careful padding and layout. I’d say this is ok if you know what
you are doing and use natural alignment of all members:

struct probably_ok {
Uint8 a;
Uint8 pad0;
Uint16 c;
Uint32 b;
IEEEFloat64 d;
};

Of course byte order must still be taken care of