Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
3.  rpcgen Programming Guide rpcgen Tutorial Preprocessing Directives   Previous   Contents   Next 
   
 

cpp Directive

rpcgen supports C preprocessing features. rpcgen defaults to use /usr/ccs/lib/cpp as the C preprocessor. If that fails, rpcgen tries to use /lib/cpp. You can specify a library containing a different cpp to rpcgen with the -Y flag.

For example, if /usr/local/bin/cpp exists, you can specify it to rpcgen as follows:

rpcgen -Y /usr/local/bin test.x

Compile-Time Flags

This section describes the rpcgen options available at compile time. The following table summarizes the options that are discussed in this section.

Table 3-2 rpcgen Compile-Time Flags

Option

Flag

Comments

Templates

-a, -Sc, -Ss, -Sm

See Table 3-3

C-style

-N

Also called Newstyle mode

ANSI C

-C

Often used with the -N option

MT-safe code

-M

For use in multithreaded environments

MT auto mode

-A

-A also turns on -M option

TS-RPC library

-b

TI-RPC library is default

xdr_inline count

-i

Uses five packed elements as default, but other number can be specified

Compile-Time Client and Server Templates

rpcgen generates sample code for the client and server sides. Use the options described in the following table to generate the desired templates.

Table 3-3 rpcgen Template Selection Flags

Flag

Function

-a

Generates all template files

-Sc

Generates client-side template

-Ss

Generates server-side template

-Sm

Generates makefile template

The files can be used as guides by filling in the missing parts. These files are in addition to the stubs generated.

A C-style mode server template is generated from the add.x source by the command rpcgen -N -Ss -o add_server_template.c add.x

The result is stored in the file add_server_template.c. A C-style mode, client template for the same add.x source is generated with the command rpcgen -N -Sc -o add_client_template.c add.x

The result is stored in the file add_client_template.c. A makefile template for the same add.x source is generated with the command rpcgen -N -Sm -o mkfile_template add.x

The result is stored in the file mkfile_template. It can be used to compile the client and the server. The -a flag, used in the command rpcgen -N -a add.x, generates all three template files. The client template is stored in add_client.c, the server template in add_server.c, and the makefile template inmakefile.a. If any of these files already exists, rpcgen displays an error message and exits.


Note - When you generate template files, give them new names to avoid the files being overwritten the next time rpcgen is executed.


Compile-Time C-style Mode

Also called Newstyle mode, the -N flag causes rpcgen to produce code in which arguments are passed by value and multiple arguments are passed without a struct. These changes enable RPC code that is more like C and other high-level languages. For compatibility with existing programs and make files, the previous standard mode of argument passing is the default. The following examples demonstrate the new feature. The source modules for both modes, C-style and default, are shown in Example 3-8 and Example 3-9 respectively.


Example 3-8 C-style Mode Version of add.x

/*
 * This program contains a procedure
 * to add 2 numbers. It demonstrates
 * the C-style mode argument passing.
 * Note that add() has 2 arguments.
 */
program ADDPROG {					/* program number */
	version ADDVER {					/* version number */
		int add(int, int) = 1;		/* procedure */
	} = 1;
} = 0x20000199;


Example 3-9 Default Mode Version of add.x

/*
 * This program contains a procedure
 * to add 2 numbers. It demonstrates
 * the "default" mode argument passing.
 * In this mode rpcgen can process
 * only one argument.
 */
struct add_arg {
	int first;
	int second;
};
program ADDPROG {					/* program number */
	version ADDVER {					/* version number */
		int add (add_arg) = 1;		/* procedure */
	} = 1;
} = 0x20000199;

The next four examples show the resulting client-side templates.


Example 3-10 C-style Mode Client Stub for add.x

/*
 * The C-style client side main
 * routine calls the  add() function
 * on the remote rpc server
 */
#include <stdio.h>
#include "add.h"
 
main(argc, argv)
int argc;
char *argv[];
{
	CLIENT *clnt;
	int *result,x,y;
	
	if(argc != 4) {
		printf("usage: %s host num1 
					num2\n" argv[0]);
		exit(1);
	}
	/* create client handle -
 * bind to server
 */
	clnt = clnt_create(argv[1], ADDPROG,
								ADDVER, "udp");
	if (clnt == NULL) {
		clnt_pcreateerror(argv[1]);
		exit(1);
	}
	x = atoi(argv[2]);
	y = atoi(argv[3]);
	/*
	 * invoke remote procedure: Note that
 * multiple arguments can be passed to
	 * add_l() instead of a pointer
	 */
	result = add_1(x, y, clnt);
	if (result == (int *) NULL) {
		clnt_perror(clnt, "call failed:");
		exit(1);
	} else {
		printf("Success: %d + %d = %d\n", 
					x, y, *result);
	}
	exit(0);
}

The following code example shows how the default mode code differs from C-style mode code.


Example 3-11 Default Mode Client

	arg.first = atoi(argv[2]);
	arg.second = atoi(argv[3]);
	/*
	 * invoke remote procedure -- note 
 * that a pointer to the argument has
	 * to be passed to the client stub
	 */
	result = add_1(&arg, clnt);

The server-side procedure in C-style mode is shown in the following example.


Example 3-12 C-style Mode Server

#include "add.h"

int *
add_1(arg1, arg2, rqstp)
	int arg1;
	int arg2;
	struct svc_req *rqstp;
{
	static int result;

	result = arg1 + arg2;
	return(&result);
}

 
 
 
  Previous   Contents   Next