What are you trying to accomplish? There are two cases to consider:
-
Fixed-length data: you are sending information back and forth that is
either a constant or predictable size. If that’s the case, you shouldn’t
have a problem.
-
Variable length: you are sending information back and forth with
unpredictable sizes. Is it possible for you to chunk up the data into
predictable sizes and send it?
Presuming that you’re sending variable length, and possibly a string,
you’ll want to chunk things up into something like this:
#define UDP_STRING_CHUNK_SIZE 5
// Hurray, this is a predictable size
struct UDPStringChunk {
char pieces[UDP_STRING_CHUNK_SIZE];
unsigned int order;
unsigned int max;
};
unsigned int string_chunkify( const char *data, UDPStringChunk *chunks ) {
unsigned int num = (unsigned int)ceil( float(strlen( data )
/ UDP_STRING_CHUNK_SIZE ) );
chunks = (UDPStringChunk *)calloc( num, sizeof( UDPStringChunk ) );
for(unsigned int i=0; i < num; i++) {
chunks[i].order = i;
chunks[i].max = (num - 1);
int cindex = i * UDP_STRING_CHUNK_SIZE;
for(int c=0; c < UDP_STRING_CHUNK_SIZE; c++) {
unsigned int id = cindex + c;
if( id >= num ) break;
chunks[i].pieces[c] = data[id];
}
}
// Also, you may want to check your
endianness; SDL_BYTEORDER==SDL_BIG_ENDIAN
return num;
}
UDPStringChunk *my_data;
unsigned int n = string_chunkify( “This is a test string”, my_data );
for(unsigned int i=0; i < n; i++) {
UDPpacket *packet = SDLNet_AllocPacket( sizeof( my_data[i] ) );
if( packet ) {
SDLNet_UDP_Send( your_socket, your_channel, packet)
SDLNet_FreePacket( packet );
}
}
This sort of concept is transferable to any sort of dynamically sized data
being sent over UDP. Your packets should always be predictable. If they
can’t be, then you’ll need to size your packet, as stated, to the maximum
size just in case.
I don’t know if the packet is dynamically resized if it’s too big, so I
can’t say definitively if that’s the case.
Hopefully this helps,
-AlexOn Tue, Jun 12, 2012 at 12:52 PM, cyberfish wrote:
**
That would make sense, but it’s not what the documentation says, though.
http://www.libsdl.org/projects/SDL_net/docs/SDL_net_52.html#SEC52
Quote:
Code:
maxlen
This is size of the data buffer, which may be larger than the meaningful
length. This is only used for packet creation on the senders side.
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org