[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Communications Programming Concepts

Showing How RPC Passes Arbitrary Data Types Example

The first two examples show how Remote Procedure Call (RPC) handles arbitrary data types.

Passing a Simple User-Defined Structure Example

struct simple {
        int a;
       short b;
} simple;
callrpc(hostname, PROGNUM, VERSNUM, PROCNUM,
        xdr_simple, &simple ...);

The xdr_simple function is written as:

#include <rpc/rpc.h>
xdr_simple(xdrsp, simplep)
    XDR *xdrsp;
    struct simple *simplep;
{
    if (!xdr_int(xdrsp, &simplep->a))
        return (0);
    if (!xdr_short(xdrsp, &simplep->b))
        return (0);
    return (1);
}

Passing a Variable-Length Array Example

struct varintarr {
    int *data;
    int arrlnth;
} arr;

callrpc(hostname, PROGNUM, VERSNUM, PROCNUM,
        xdr_varintarr, &arr...);

The xdr_varintarr subroutine is defined as:

xdr_varintarr(xdrsp, arrp)
    XDR *xdrsp;
    struct varintarr *arrp;
{
    return (xdr_array(xdrsp, &arrp->data, &arrp->arrlnth, 
        MAXLEN, sizeof(int), xdr_int));
}

This routine's parameters are the eXternal Data Representation (XDR) handle (xdrsp), a pointer to the array (aarp->data), a pointer to the size of the array (aarp->arrlnth), the maximum allowable array size (MAXLEN), the size of each array element (sizeof), and an XDR routine for handling each array element (xdr_int).

Passing a Fixed-Length Array Example

If the size of the array is known in advance, the programmer can call the xdr_vector subroutine to serialize fixed-length arrays, as in the following example:

int intarr[SIZE];
xdr_intarr(xdrsp, intarr)
    XDR *xdrsp;
    int intarr[];
{
    int i;
    return (xdr_vector(xdrsp, intarr, SIZE, sizeof(int),
        xdr_int));
}

Passing Structure with Pointers Example

The following example calls the previously written xdr_simple routine as well as the built-in xdr_string and xdr_reference functions. The xdr_reference routine chases pointers.

struct finalexample {
    char *string;
    struct simple *simplep;
} finalexample;
xdr_finalexample(xdrsp, finalp)
    XDR *xdrsp;
    struct finalexample *finalp;
{
    if (!xdr_string(xdrsp, &finalp->string, MAXSTRLEN))
        return (0);
    if (!xdr_reference(xdrsp, &finalp->simplep,
     sizeof(struct simple), xdr_simple);
        return (0);
    return (1);
}

[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]