Message-ID: <20131006162705.2688980931ab9fc1818ac56f at laposte.net>
Content-Type: text/plain; charset=US-ASCII
Hello,
I am using sdl_net-1.2.8.
I have a problem with the “UDPpacket” structure [1] because the "data"
member is of type “Uint8*” whereas the data I want to send over network is a
structure… So I am unable to cast.
Why it is not a “void*” type for the data member ?
Thank you.
Best regards.
8-bit bytes are the standard size of byte to measure network
communications in. However, some machines have a different byte size:
some of the IBM mainframes, for example, use bytes somewhere around 36
bits in size (for a char they apparently cut these in half, but that’s
still 18 bits instead of 8). Thus, it makes sense to explicitly
specify through choice of datatype what the byte characteristics are.
I’ve never heard of SDL_net being ported to a machine that doesn’t use
8-bit bytes, but if it does happen, you can expect the
most-significant bits to be dropped from the value.
Incidentally, just in case you don’t have much exposure to network
coding, you are aware that different compilers and platforms create
structures of different sizes and internal alignments, right? The
Microsoft compilers, for example, apparently place each bitfield in
it’s own byte (or maybe it was an int for each?), whereas GCC places
them into as FEW bytes as it can on most platforms. Thus, even if it
SEEMS to work for you, please always remember to convert structures to
and from a “serialized” form when you’re sending them over the
network, or even just saving them to disk. Or, if nothing else,
remember to send a version id before sending the data, so that it can
be handled appropriately.> Date: Sun, 6 Oct 2013 16:27:05 +0200
From: YuGiOhJCJ Mailing-List
To: sdl at lists.libsdl.org
Subject: [SDL] the “UDPpacket” structure “data” member type
Date: Sun, 6 Oct 2013 18:08:04 +0200
From: YuGiOhJCJ Mailing-List
To: SDL Development List
Subject: Re: [SDL] the “UDPpacket” structure “data” member type
Message-ID: <20131006180804.3a6f93e83b2f985b4b196bd5 at laposte.net>
Content-Type: text/plain; charset=US-ASCII
In fact, I am using the C language, not the C++ language.
So, I have not access to reinterpret_cast.
C language casts are almost always reinterpret casts. C++ just added
the fancy name because:
- C++ has a few more casts, and
- C++ tried to make some things more explicit.
What you did below would qualify under the rules of the C++
static_cast (which does compile-time type checking), as well as
qualifying under the rules of the highly discouraged reinterpret_cast
(which just magically assumes that you’ve made certain that everything
will work, and thus feels free to say that the provided address just
points to a completely different type than it’s declared type:
dangerous, but useful).
I don’t think that it would qualify for dynamic_cast (which I recall
only working on classes with virtual functions), and const_cast would
similarly be the wrong mechanism.
Yes, I specify a size for the packet :
// Create the packet
UDPpacket *packet = NULL;
packet = SDLNet_AllocPacket(1024);
But this size of 1024 is not the same than the one in “packet->len”:
packet->len = 6;
Anyway, below is a source code that is working for a string.
Now, I would like to send a structure instead of this string.
For example, we can take this structure:
data.h :
typedef struct data
{
int integer;
char str[6];
} data_t;
Finally, I found the way how to cast correctly:
data_t data;
strcpy(data.str, “Hello\0”);
data.integer = 123;
packet->data = (Uint8*)&data;
packet->len = sizeof(data);
The data is correctly sent and received between the server and the client.
Problem solved.
From looking at this (
http://hg.libsdl.org/SDL_net/file/c98cd1a60729/SDLnetUDP.c ) file, a
few notes.
- Always set the status value on your packet to 0 before trying to
send it. Afterwards, look at the value stored in status, and compare
it with the packet length. If the status is smaller, then only part of
the data was sent. You’ll need to do another send to get the rest out:
a send which may require some house-keeping data to be sent as well,
depending on the data format.
- Don’t place your own data buffer into the data pointer. There’s
already a buffer there, of the size that you specified. To see what
that size is, look at the maxlen member. If you need to change that
size, use the SDLNet_ResizePacket() function.
- You do seem to have more-or-less parsed the correct use of the len
member: it’s used to specify the amount of data to be sent, or that
has been received. A value of 1 implies data[ 0 ], 2 implies data[ 0 ]
& data[ 1 ], etc.
Unfortunately, for some reason, len is initially set to the size of
the packet structure, instead of being set to 0 or maxlen. I assume
that this is either a bug in the allocate function, or a peculiar
version-checking feature, intended to allow you to check whether the
packet size that the allocator expected is the packet size that YOU
expected.
While in some ways ingenious, this is really the sort of thing that I
would expect a version-checking function to do, especially since such
a function could also check the sizes and alignments of individual
members of the structure. At the very least it should be mentioned in
the documentation, and I didn’t notice such a thing.