Re: [gnet] Using pack to pack an array



Jon,

I think you might be confusing packing with serializing [1].  Packing in
the gnet_pack() sense is really about making a data stream out of
certain standard types.  So, if you need to be able to send a stream of
data across the network to some other application, you can meticulously
create that stream of data using gnet_pack() and then unpack it with
gnet_unpack.  But, these functions won't be able to handle structures or
anything like that.  Remember, a structure in memory is just a pointer
to the first entity in that structure.  There really is no structure per
se.  Languages other than C like PHP [2] offer functions that can
serialize arbitrary data structures and objects, etc.

Basically, if you want to send this data somewhere by packing it up with
gnet you'll have to just send the integers in the structure one after
the other and whatever is receiving the data will have to be smart
enough to make new structures that contain each pair of integers.  There
are other things you could do that might make more sense for you (like
use a real grown-up RPC mechanism).  But, if all you need to do is send
10 pairs of integers across the network every now and then, you might
just want to do the dirty work yourself.

I used gnet_pack() and gnet_unpack() in an application of my own that
was speaking to a solid-state device over the network.  The device
expected the data that was coming in to it to be in a particular format
and would send the data to my application in another particular format.
I was able to create the data streams that I was sending and pick apart
the data streams that I was receiving using gnet_pack() and
gnet_unpack().  But, they won't create structures or anything like that
for you.  You'll have to do that on your own.

Mark.

[1] http://en.wikipedia.org/wiki/Serialization
[2] http://www.php.net/serialize

On Wed, 2006-07-26 at 22:26 +0200, Jon Kristensen wrote:
> Hello people!
> 
> First of all, thanks for a great library! :o)
> 
> My problem is that I want to send a standard C++ array of structs using
> pack, while I can't figure out a way to do it. This is what I want to
> pack (and unpack):
> 
> #define MAX_NUMBER_OF_CLIENTS 10
> 
> struct pos
> {
> 	int x, y;
> };
> 
> static pos positions[MAX_NUMBER_OF_CLIENTS];
> 
> Can you give me some hints? I've been reading on
> http://metric.it.uab.edu/doc/linux/gnet-1.1.0/html/gnet-pack.html and
> I'm sure the answer is there somewhere, but I can't find it...
> 
> Thanks in advance!
> 

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]