libmemcache - custom structures

Jure Petrovic fonz at siol.net
Tue Dec 12 16:55:35 UTC 2006



Got it!

It doesn't produce a 16 byte string. Whole input buffer is used. When
using xdrmem_create, the buffer for XDR_ENCODE and XDR_DECODE must have
the same length.
So, instead of

xdrmem_create(xdrs, input, strlen(input), XDR_DECODE);

I used:

xdrmem_create(xdrs, input, 32, XDR_DECODE);

and it works. Like it was so hard :=) In this case, I also have to send 
all 32 bytes to memcached, even though not all are used by my 2
structures.
But I don't want network to deal with overhead. So what is the smallest
buffer size to use? Would simple sizeof(struct1) + sizeof(struct2) do?

Regards,
Jure



On Tue, 2006-12-12 at 15:29 +0100, Jure Petrovic wrote:
> Hello,
> 
> Maybe someone already used XDR...
> Having 2 structures like this:
> 
> bool_t xdr_doubles (XDR *xdrs, doubles *objp)
> {
> 	register int32_t *buf;
> 
> 	 if (!xdr_double (xdrs, &objp->a))
> 		 return FALSE;
> 	 if (!xdr_double (xdrs, &objp->b))
> 		 return FALSE;
> 	 if (!xdr_reference (xdrs, (char **)&objp->c, sizeof (person),
> (xdrproc_t)xdr_person))
> 		 return FALSE;
> 	return TRUE;
> } 
> 
> bool_t xdr_person (XDR *xdrs, person *objp)
> {
> 	register int32_t *buf;
> 
> 	 if (!xdr_int (xdrs, &objp->age))
> 		 return FALSE;
> 	return TRUE;
> }
> 
> Now encoding with:
> 
> XDR *xdrs = malloc(sizeof(XDR));
> char *input = malloc(32);
> xdrmem_create(xdrs, input, 32, XDR_ENCODE);
> if (xdr_doubles(xdrs, structure)) printf("encode()::success\n");
> 
> works great and produces a 16-byte string. But when decoding with:
> 
> XDR *xdrs = malloc(sizeof(XDR));
> doubles *structure = malloc(sizeof(doubles));
> structure->c = NULL; // if null, XDR should allocate memory!!
> xdrmem_create(xdrs, input, strlen(input), XDR_DECODE);
> if (xdr_doubles(xdrs, structure)) printf("decode()::success\n");
> 
> structure->c->age doesn't get filled. As I understand from manuals,
> memory for deserializing should get allocated automatically, but it
> seems that is not the case.
> 
> If I use xdr_pointer() instead of xdr_reference() as suggested, I get a
> null pointer for structure->c. 
> 
> Any ideas?
> 
> 
> Thanks, 
> Jure
> 
> 



More information about the memcached mailing list