CVS update: php31/ext/rpc

From: Date: Thu, 17 Sep 1998 15:23:41 +0000
Subject: CVS update: php31/ext/rpc
Groups: php.dev 
Request: Send a blank email to php-dev+get-1196@lists.php.net to get a copy of this message
Date: Thursday September 17, 1998 @ 11:23 Author: ssb Update of /repository/php31/ext/rpc In directory asf:/u2/tmp/cvs-serv1075 Modified Files: Makefile.am prcd.c rpc.c rpc_protocol.h test.php3 Added Files: rpc_protocol.c serialize.c Log Message: calling remote functions works now, but values are not yet properly returned Index: php31/ext/rpc/Makefile.am diff -c php31/ext/rpc/Makefile.am:1.2 php31/ext/rpc/Makefile.am:1.3 *** php31/ext/rpc/Makefile.am:1.2 Wed Sep 16 21:31:40 1998 --- php31/ext/rpc/Makefile.am Thu Sep 17 11:23:39 1998 *************** *** 2,8 **** INCLUDES=@INCLUDES@ -I@top_srcdir@ -I@top_srcdir@/main LDADD=$(shell echo $$LDADD | sed \-e's!\.\./lib/!../../lib/!g') noinst_LIBRARIES=libphpext_rpc.a ! libphpext_rpc_a_SOURCES=rpc.c noinst_PROGRAMS=prcd ! prcd_SOURCES=prcd.c prcalloc.c prcd_LDADD=libphpext_rpc.a --- 2,8 ---- INCLUDES=@INCLUDES@ -I@top_srcdir@ -I@top_srcdir@/main LDADD=$(shell echo $$LDADD | sed \-e's!\.\./lib/!../../lib/!g') noinst_LIBRARIES=libphpext_rpc.a ! libphpext_rpc_a_SOURCES=rpc.c rpc_protocol.c serialize.c noinst_PROGRAMS=prcd ! prcd_SOURCES=prcd.c prcalloc.c rpc_protocol.c serialize.c prcd_LDADD=libphpext_rpc.a Index: php31/ext/rpc/prcd.c diff -c php31/ext/rpc/prcd.c:1.1 php31/ext/rpc/prcd.c:1.2 *** php31/ext/rpc/prcd.c:1.1 Wed Sep 16 21:31:41 1998 --- php31/ext/rpc/prcd.c Thu Sep 17 11:23:39 1998 *************** *** 27,33 **** +----------------------------------------------------------------------+ */ ! /* $Id: prcd.c,v 1.1 1998/09/17 01:31:41 ssb Exp $ */ #include "php.h" --- 27,33 ---- +----------------------------------------------------------------------+ */ ! /* $Id: prcd.c,v 1.2 1998/09/17 15:23:39 ssb Exp $ */ #include "php.h" *************** *** 58,64 **** --- 58,80 ---- prc_conn_t connections[PRC_MAX_CONNS]; int nconn = 0; + typedef struct { + char *name; + void (*handler)(int, int, pval **); + } prcd_function; + + void prcd_testFunc(int conn, int nparams, pval **params) + { + printf("TESTFUNC CALLED!\n"); + rpc_return_string(conn, "BING"); + } + + prcd_function prcd_functions[] = { + "testFunc", prcd_testFunc, + NULL, NULL + }; + int bind_unix_socket(char *path, int mode) { *************** *** 111,212 **** return -1; } - - rpc_opcode_t - get_opcode(char *data, int *pos, int size) - { - rpc_opcode_t nopcode; - char *ptr = (char *)&nopcode; - int len = sizeof(nopcode); - - if (size - *pos <= 0) { - return RPC_ERROR_OPCODE; - } - - memcpy(ptr, data, len); - *pos += len; - - return ntohs(nopcode); - } - - - unsigned short - get_short(char *data, int *pos, int size) - { - unsigned short nvalue; - char *ptr = (char *)&nvalue; - int len = sizeof(nvalue); - - if (size - *pos <= 0) { - return RPC_ERROR_OPCODE; - } ! memcpy(ptr, data, len); ! *pos += len; ! ! return ntohs(nvalue); ! } ! ! ! pval * ! get_pval(char *data, int *pos, int size) { ! unsigned short ntype, type; ! pval *val; ! ! if (size < (*pos) + sizeof(ntype)) { ! printf("\tget_pval: not enough data 1\n"); ! return NULL; ! } ! val = emalloc(sizeof(pval)); ! memcpy((char *)&ntype, &data[*pos], sizeof(ntype)); ! *pos += sizeof(ntype); ! type = ntohs(ntype); ! switch (type) { ! case RPC_TYPE_STRING: { ! unsigned long nlen, len; ! ! val->type = IS_STRING; ! if (size < (*pos) + sizeof(nlen)) { ! printf("\tget_pval: not enough data 2\n"); ! return NULL; ! } ! memcpy((char *)&nlen, &data[*pos], sizeof(nlen)); ! len = ntohl(nlen); ! *pos += sizeof(nlen); ! if (size < (*pos) + len) { ! printf("\tget_pval: not enough data 3\n"); ! return NULL; ! } ! val->value.str.val = emalloc(len); ! memcpy(val->value.str.val, &data[*pos], len); ! val->value.str.len = len; ! *pos += len; ! break; ! } ! case RPC_TYPE_LONG: { ! unsigned long nvalue, value; ! ! val->type = IS_LONG; ! if (size < (*pos) + sizeof(nvalue)) { ! printf("\tget_pval: not enough data 4\n"); ! return NULL; ! } ! memcpy((char *)&nvalue, &data[*pos], sizeof(nvalue)); ! value = ntohl(nvalue); ! val->value.lval = (long)value; ! *pos += sizeof(nvalue); break; } ! default: ! /* unsupported type */ ! printf("\tget_pval: unsupported type %d\n", type); ! return NULL; } - return val; } void handle_message(int conn, char *data, int size) { --- 127,152 ---- return -1; } ! void ! call_prcd_function(int conn, char *name, int nparams, pval **params) { ! void (*function)(int, int, pval **) = NULL; ! int i = 0; ! while (prcd_functions[i].name != NULL) { ! if (strcasecmp(prcd_functions[i].name, name) == 0) { ! function = prcd_functions[i].handler; break; } ! i++; ! } ! if (function != NULL) { ! function(conn, nparams, params); } } + void handle_message(int conn, char *data, int size) { *************** *** 217,223 **** if (size - pos < sizeof(opcode)) { return; } ! opcode = get_opcode(data, &pos, size); switch (opcode) { case RPC_NOP: printf("[%02x] RPC_NOP\n", pos); --- 157,163 ---- if (size - pos < sizeof(opcode)) { return; } ! opcode = rpc_get_opcode(data, &pos, size); switch (opcode) { case RPC_NOP: printf("[%02x] RPC_NOP\n", pos); *************** *** 227,259 **** unsigned short nparams; pval **params = NULL; int pno; ! void (*function)(int, pval **); rpc_opcode_t tmpcode; printf("[%02x] RPC_FUNCTION_CALL\n", pos); ! fname = get_pval(data, &pos, size); if (fname == NULL) { printf("[%02x] could not get function name\n", pos); return; } ! nparams = get_short(data, &pos, size); if (nparams > 0) { params = ecalloc(nparams, sizeof(pval *)); for (pno = 0; pno < nparams; pno++) { ! printf("[%02x] checking parameter %d\n", pos, pno); ! tmpcode = get_opcode(data, &pos, size); if (tmpcode != RPC_FUNCTION_PARAMETER) { printf("[%02x] expected RPC_FUNCTION_PARAMETER, got `%d'\n", pos, tmpcode); break; } ! if ((params[pno] = get_pval(data, &pos, size)) == NULL) { ! printf("[%02x] get_pval returned NULL\n", pos); } } } /* call function */ printf("[%02x] calling %s(/* %d args... */)\n", pos, ! fname->value.str.val); for (pno = 0; pno < nparams; pno++) { if (params[pno] != NULL) { efree(params[pno]); --- 167,211 ---- unsigned short nparams; pval **params = NULL; int pno; ! void (*function)(int, int, pval **); ! char *function_name = NULL; rpc_opcode_t tmpcode; printf("[%02x] RPC_FUNCTION_CALL\n", pos); ! fname = rpc_get_pval(data, &pos, size); if (fname == NULL) { printf("[%02x] could not get function name\n", pos); return; + } + tmpcode = rpc_get_opcode(data, &pos, size); + if (tmpcode != RPC_FUNCTION_NPARAMS) { + printf("[%02x] expected RPC_FUNCTION_NPARAMS, got %d\n", + pos, tmpcode); + break; } ! nparams = rpc_get_short(data, &pos, size); ! printf("[%02x] RPC_FUNCTION_NPARAMS %d\n", pos, nparams); if (nparams > 0) { params = ecalloc(nparams, sizeof(pval *)); for (pno = 0; pno < nparams; pno++) { ! printf("[%02x] reading parameter %d\n", pos, pno); ! tmpcode = rpc_get_opcode(data, &pos, size); if (tmpcode != RPC_FUNCTION_PARAMETER) { printf("[%02x] expected RPC_FUNCTION_PARAMETER, got `%d'\n", pos, tmpcode); break; } ! if ((params[pno] = rpc_get_pval(data, &pos, size)) == NULL) { ! printf("[%02x] rpc_get_pval returned NULL\n", pos); } } } /* call function */ + function_name = emalloc(fname->value.str.len + 1); + memcpy(function_name, fname->value.str.val, fname->value.str.len); + function_name[fname->value.str.len] = '\0'; printf("[%02x] calling %s(/* %d args... */)\n", pos, ! function_name, nparams); ! call_prcd_function(conn, function_name, nparams, params); for (pno = 0; pno < nparams; pno++) { if (params[pno] != NULL) { efree(params[pno]); Index: php31/ext/rpc/rpc.c diff -c php31/ext/rpc/rpc.c:1.3 php31/ext/rpc/rpc.c:1.4 *** php31/ext/rpc/rpc.c:1.3 Wed Sep 16 21:31:41 1998 --- php31/ext/rpc/rpc.c Thu Sep 17 11:23:40 1998 *************** *** 27,33 **** +----------------------------------------------------------------------+ */ ! /* $Id: rpc.c,v 1.3 1998/09/17 01:31:41 ssb Exp $ */ #ifdef THREAD_SAFE # include "tls.h" --- 27,33 ---- +----------------------------------------------------------------------+ */ ! /* $Id: rpc.c,v 1.4 1998/09/17 15:23:40 ssb Exp $ */ #ifdef THREAD_SAFE # include "tls.h" *************** *** 127,136 **** static int rpc_connect_unix(char *); #endif static int rpc_connect_tcp(char *, int); - static void rpc_msg_opcode(char **, int *, int *, unsigned short); - static void rpc_msg_pval(char **, int *, int *, pval *); - static void rpc_msg_raw(char **, int *, int *, char *, int); - static void rpc_msg_long(char **, int *, int *, long); function_entry rpc_functions[] = { --- 127,132 ---- *************** *** 258,417 **** } - static void - rpc_msg_short(char **buffer, /* data buffer */ - int *pos, /* pointer to current buffer offset */ - int *size, /* pointer to buffer size */ - unsigned short value) /* value to append to the buffer */ - { - int len = sizeof(value); - char *juks; - - value = htons(value); - juks = (char *)&value; - rpc_msg_raw(buffer, pos, size, juks, len); - } - - - static void - rpc_msg_long(char **buffer, /* data buffer */ - int *pos, /* pointer to current buffer offset */ - int *size, /* pointer to buffer size */ - long value) /* value to append to the buffer */ - { - int len = sizeof(value); - char *juks; - - value = htonl(value); - juks = (char *)&value; - rpc_msg_raw(buffer, pos, size, juks, len); - } - - - static void - rpc_msg_pval(char **buffer, /* data buffer */ - int *pos, /* pointer to current buffer offset */ - int *size, /* pointer to buffer size */ - pval *val) /* pval to serialize and append */ - { - int srl_size; - char *srl_buf; - - if (php3i_serialize(val, &srl_buf, &srl_size) == FAILURE) { - return; - } - rpc_msg_raw(buffer, pos, size, srl_buf, srl_size); - efree(srl_buf); - } - - - static void - rpc_msg_raw(char **buffer, /* data buffer */ - int *pos, /* pointer to current buffer offset */ - int *size, /* pointer to buffer size */ - char *data, /* data to append to the buffer */ - int len) /* number of bytes to append */ - { - if ((*pos + len) >= *size) { - while ((*pos + len) >= *size) { - *size *= 2; - } - *buffer = erealloc(*buffer, *size); - } - memcpy(&(*buffer)[*pos], data, len); - *pos += len; - } - - - int - php3i_serialize(pval *val, char **resultp, int *sizep) - { - unsigned short type; - unsigned long size, value; - - switch (val->type) { - case IS_STRING: - type = htons(RPC_TYPE_STRING); - size = htonl((unsigned long)val->value.str.len); - *sizep = sizeof(type) + sizeof(size) + val->value.str.len; - *resultp = emalloc(*sizep); - if (*resultp == NULL) { - return FAILURE; - } - memcpy(*resultp, (char *)&type, sizeof(type)); - memcpy(*resultp+sizeof(type), (char *)&size, sizeof(size)); - memcpy(*resultp+sizeof(type)+sizeof(size), val->value.str.val, - val->value.str.len); - return SUCCESS; - case IS_LONG: - type = htons(RPC_TYPE_LONG); - value = htonl((unsigned long)val->value.lval); - *sizep = sizeof(type) + sizeof(value); - *resultp = emalloc(*sizep); - if (*resultp == NULL) { - return FAILURE; - } - memcpy(*resultp, (char *)&type, sizeof(type)); - memcpy(*resultp+sizeof(type), (char *)&value, sizeof(value)); - return SUCCESS; - } - return FAILURE; /* unknown type */ - } - - - int - php3i_unserialize(pval *val, char *buffer, int size) - { - int pos = 0; - unsigned short ntype, type; - - if (size < pos + sizeof(ntype)) { - php3_error(E_WARNING, "unserialize: no size"); - return FAILURE; - } - memcpy((char *)&ntype, &buffer[pos], sizeof(ntype)); - pos += sizeof(ntype); - type = ntohs(ntype); - switch (type) { - case RPC_TYPE_STRING: { - unsigned long nlen, len; - - val->type = IS_STRING; - if (size < pos + sizeof(nlen)) { - return FAILURE; - } - memcpy((char *)&nlen, &buffer[pos], sizeof(nlen)); - len = ntohl(nlen); - pos += sizeof(nlen); - if (size < pos + len) { - return FAILURE; - } - val->value.str.val = emalloc(len); - memcpy(val->value.str.val, &buffer[pos], len); - val->value.str.len = len; - pos += len; - break; - } - case RPC_TYPE_LONG: { - unsigned long nvalue, value; - - val->type = IS_LONG; - if (size < pos + sizeof(nvalue)) { - return FAILURE; - } - memcpy((char *)&nvalue, &buffer[pos], sizeof(nvalue)); - value = ntohl(nvalue); - val->value.lval = (long)value; - pos += sizeof(nvalue); - break; - } - default: - /* unsupported type */ - return FAILURE; - } - return SUCCESS; - } - /* * PHP variables (so far only strings and longs) are serialized * like this: --- 254,259 ---- *************** *** 474,482 **** php3_rpc_call(INTERNAL_FUNCTION_PARAMETERS) { pval **argv; ! int argc, i, size, len, sock; ! unsigned short opcode; ! char *buf; RPC_TLS_VARS; argc = ARG_COUNT(ht); --- 316,324 ---- php3_rpc_call(INTERNAL_FUNCTION_PARAMETERS) { pval **argv; ! int argc, i, size, len, sock, readlen, writelen, pos; ! char *buf, readbuf[1024]; /* XXX FIXME hardcoded limit! */ ! rpc_opcode_t opcode; RPC_TLS_VARS; argc = ARG_COUNT(ht); *************** *** 496,510 **** size = 50; /* size will be doubled every time the buffer is too small */ buf = emalloc(size); len = 0; ! rpc_msg_opcode(&buf, &len, &size, RPC_FUNCTION_CALL); ! rpc_msg_pval(&buf, &len, &size, argv[0]); ! rpc_msg_opcode(&buf, &len, &size, RPC_FUNCTION_NPARAMS); ! rpc_msg_short(&buf, &len, &size, argc - 1); for (i = 1; i < argc; i++) { ! rpc_msg_opcode(&buf, &len, &size, RPC_FUNCTION_PARAMETER); ! rpc_msg_pval(&buf, &len, &size, argv[i]); } - write(sock, buf, len); } /* --- 338,389 ---- size = 50; /* size will be doubled every time the buffer is too small */ buf = emalloc(size); len = 0; ! rpc_put_opcode(&buf, &len, &size, RPC_FUNCTION_CALL); ! rpc_put_pval(&buf, &len, &size, argv[0]); ! rpc_put_opcode(&buf, &len, &size, RPC_FUNCTION_NPARAMS); ! rpc_put_short(&buf, &len, &size, argc - 1); for (i = 1; i < argc; i++) { ! rpc_put_opcode(&buf, &len, &size, RPC_FUNCTION_PARAMETER); ! rpc_put_pval(&buf, &len, &size, argv[i]); ! } ! writelen = write(sock, buf, len); ! if (writelen != len) { ! php3_error(E_WARNING, "XXX FIXME have to make a write loop"); ! RETURN_FALSE; ! } ! /* XXX FIXME not efficient use of read() here */ ! readlen = read(sock, (char *)&opcode, sizeof(opcode)); ! if (readlen != sizeof(opcode)) { ! php3_error(E_WARNING, "malformed RPC response"); ! RETURN_FALSE; ! } ! php3_printf("opcode=%d\n", opcode); ! switch (opcode) { ! case RPC_RETURN_VALUE: { ! pval *val; ! ! val = rpc_get_pval(readbuf, &pos, readlen); ! if (val == NULL) { ! RETVAL_FALSE; ! } else { ! memcpy(return_value, val, sizeof(pval)); ! } ! break; ! } ! case RPC_ERROR: { ! rpc_error_t error; ! ! error = rpc_get_opcode(readbuf, &pos, readlen); ! php3_error(E_WARNING, "RPC error %d", error); ! RETVAL_FALSE; ! break; ! } ! default: { ! php3_error(E_WARNING, "malformed RPC response"); ! RETVAL_FALSE; ! break; ! } } } /* Index: php31/ext/rpc/rpc_protocol.h diff -c php31/ext/rpc/rpc_protocol.h:1.1 php31/ext/rpc/rpc_protocol.h:1.2 *** php31/ext/rpc/rpc_protocol.h:1.1 Wed Sep 16 21:31:42 1998 --- php31/ext/rpc/rpc_protocol.h Thu Sep 17 11:23:40 1998 *************** *** 31,36 **** --- 31,37 ---- # define _PHP3_RPC_PROTOCOL_H typedef unsigned short rpc_opcode_t; + typedef unsigned short rpc_error_t; # define RPC_ERROR_OPCODE 0 *************** *** 41,46 **** --- 42,48 ---- # define RPC_FUNCTION_NPARAMS 3 # define RPC_FUNCTION_PARAMETER 4 # define RPC_RETURN_VALUE 5 + # define RPC_ERROR 6 /* value passing opcodes */ # define RPC_TYPE_LONG 10 *************** *** 50,56 **** # define RPC_DONE 99 ! # define rpc_msg_opcode rpc_msg_short #endif --- 52,71 ---- # define RPC_DONE 99 ! # define rpc_put_opcode rpc_put_short ! # define rpc_put_error rpc_put_short ! ! void rpc_error(int, rpc_error_t); ! void rpc_return_string(int, char *); ! unsigned short rpc_get_short(char *, int *, int); ! rpc_opcode_t rpc_get_opcode(char *, int *, int); ! rpc_error_t rpc_get_error(char *, int *, int); ! unsigned long rpc_get_long(char *, int *, int); ! pval *rpc_get_pval(char *, int *, int); ! void rpc_put_short(char **, int *, int *, unsigned short); ! void rpc_put_long(char **, int *, int *, long); ! void rpc_put_pval(char **, int *, int *, pval *); ! void rpc_put_raw(char **, int *, int *, char *, int); #endif Index: php31/ext/rpc/test.php3 diff -c php31/ext/rpc/test.php3:1.2 php31/ext/rpc/test.php3:1.3 *** php31/ext/rpc/test.php3:1.2 Wed Sep 16 19:11:04 1998 --- php31/ext/rpc/test.php3 Thu Sep 17 11:23:40 1998 *************** *** 11,16 **** printf("serialized: `%s' (%s)\n", rawurlencode($ser), gettype($ser)); printf("unserialized: `%s' (%s)\n", $uns, gettype($uns)); ! rpc_call("testFunc", 2, 3, "bing"); ?> --- 11,17 ---- printf("serialized: `%s' (%s)\n", rawurlencode($ser), gettype($ser)); printf("unserialized: `%s' (%s)\n", $uns, gettype($uns)); ! $ret = rpc_call("testFunc", 2, 3, "bing"); ! printf("RPC call returned `%s'\n", $ret); ?> -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#1196) next »