--- libaitrpc/src/srv.c 2012/05/15 16:10:58 1.9.2.5 +++ libaitrpc/src/srv.c 2012/05/15 22:47:10 1.9.2.7 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: srv.c,v 1.9.2.5 2012/05/15 16:10:58 misho Exp $ +* $Id: srv.c,v 1.9.2.7 2012/05/15 22:47:10 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -52,7 +52,7 @@ closeClient(sched_task_t *task) rpc_cli_t *c = TASK_ARG(task); rpc_srv_t *s = c->cli_parent; - schedCancelby(s->srv_root, taskMAX, CRITERIA_FD, (void*) c->cli_sock, NULL); + schedCancelby(s->srv_root, taskMAX, CRITERIA_ARG, TASK_ARG(task), NULL); /* close client socket */ if (TASK_VAL(task)) @@ -75,21 +75,20 @@ txPacket(sched_task_t *task) u_char buf[USHRT_MAX] = { 0 }; struct tagRPCCall *rpc = (struct tagRPCCall*) buf; int ret, wlen = sizeof(struct tagRPCCall); - array_t *arr = NULL; /* copy RPC header */ memcpy(buf, TASK_DATA(task), wlen); if (rpc->call_argc) { - f = rpc_srv_getCall(s, ntohs(rpc->call_tag), ntohl(rpc->call_hash)); + f = rpc_srv_getCall(s, ntohs(rpc->call_tag)); if (!f) { rpc->call_argc ^= rpc->call_argc; rpc->call_rep.ret = RPC_ERROR(-1); rpc->call_rep.eno = RPC_ERROR(rpc_Errno); } else { - rpc->call_argc = htons(rpc_srv_getVars(f, &arr)); + rpc->call_argc = htons(io_arraySize(f->func_vars)); /* Go Encapsulate variables */ - ret = io_vars2buffer(buf + wlen, sizeof buf - wlen, arr); + ret = io_vars2buffer(buf + wlen, sizeof buf - wlen, f->func_vars); /* Free return values */ io_clrVars(f->func_vars); if (ret == -1) { @@ -143,7 +142,7 @@ execCall(sched_task_t *task) } } - if (!(f = rpc_srv_getCall(s, ntohs(rpc->call_tag), ntohl(rpc->call_hash)))) { + if (!(f = rpc_srv_getCall(s, ntohs(rpc->call_tag)))) { rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server"); rpc->call_argc ^= rpc->call_argc; rpc->call_rep.ret = RPC_ERROR(-1); @@ -162,7 +161,7 @@ execCall(sched_task_t *task) io_clrVars(f->func_vars); rpc->call_argc ^= rpc->call_argc; } else - rpc->call_argc = htons(rpc_srv_getVars(f, NULL)); + rpc->call_argc = htons(io_arraySize(f->func_vars)); } } @@ -265,18 +264,19 @@ acceptClients(sched_task_t *task) c->cli_parent = srv; } + /* alloc empty buffer */ + AIT_SET_BUF2(&c->cli_buf, 0, srv->srv_netbuf); + /* accept client */ c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa.sa, &salen); if (c->cli_sock == -1) { LOGERR; + AIT_FREE_VAL(&c->cli_buf); io_arrayDel(srv->srv_clients, i, 42); goto end; } else fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK); - /* alloc empty buffer */ - AIT_SET_BUF2(&c->cli_buf, 0, srv->srv_netbuf); - schedRead(TASK_ROOT(task), rxPacket, c, c->cli_sock, NULL, 0); end: schedReadSelf(task); @@ -707,7 +707,7 @@ rpc_srv_loopBLOB(rpc_srv_t * __restrict srv) * return: NULL == error or !=NULL bind and created RPC server instance */ rpc_srv_t * -rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients, +rpc_srv_initServer(u_int regProgID, u_char regProcID, int concurentClients, int netBuf, const char *csHost, u_short Port) { int n = 1; @@ -736,7 +736,6 @@ rpc_srv_initServer(u_int regProgID, u_int regProcID, i srv->srv_netbuf = netBuf; srv->srv_session.sess_version = RPC_VERSION; - srv->srv_session.sess_timeout = DEF_RPC_TIMEOUT; srv->srv_session.sess_program = regProgID; srv->srv_session.sess_process = regProcID; @@ -824,6 +823,7 @@ rpc_srv_endServer(rpc_srv_t ** __restrict psrv) if (c) { shutdown(c->cli_sock, SHUT_RDWR); close(c->cli_sock); + free(c); io_arrayDel((*psrv)->srv_clients, i, 0); } @@ -836,7 +836,6 @@ rpc_srv_endServer(rpc_srv_t ** __restrict psrv) TAILQ_FOREACH_SAFE(f, &(*psrv)->srv_funcs, func_node, tmp) { io_freeVars(&f->func_vars); AIT_FREE_VAL(&f->func_name); - AIT_FREE_VAL(&f->func_file); free(f); } @@ -854,21 +853,18 @@ rpc_srv_endServer(rpc_srv_t ** __restrict psrv) int rpc_srv_loopServer(rpc_srv_t * __restrict srv) { - /* - int ret; - */ - if (!srv) { rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server"); return -1; } + fcntl(srv->srv_server.cli_sock, F_SETFL, + fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK); + if (listen(srv->srv_server.cli_sock, io_arraySize(srv->srv_clients)) == -1) { LOGERR; return -1; - } else - fcntl(srv->srv_server.cli_sock, F_SETFL, - fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK); + } if (!schedRead(srv->srv_root, acceptClients, srv, srv->srv_server.cli_sock, NULL, 0)) { rpc_SetErr(sched_GetErrno(), "%s", sched_GetError()); @@ -893,29 +889,13 @@ int rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc, array_t * __restrict args) { - void *dl; rpc_callback_t func; - int ret; - if (!call || !rpc || !call->func_parent) { + if (!call || !rpc || !call->func_parent || !AIT_ADDR(&call->func_name)) { rpc_SetErr(EINVAL, "Invalid parameter can`t exec function"); return -1; } - dl = dlopen(AIT_ADDR(&call->func_file), RTLD_LAZY | RTLD_GLOBAL); - if (!dl) { - rpc_SetErr(ENOENT, "Can`t attach module %s!", dlerror()); - return -1; - } - - func = dlsym(dl, (const char*) AIT_GET_STR(&call->func_name)); - if (func) - ret = func(call, ntohs(rpc->call_argc), args); - else { - rpc_SetErr(ENOEXEC, "Can`t find function %s!", dlerror()); - ret = -1; - } - - dlclose(dl); - return ret; + func = AIT_GET_LIKE(&call->func_name, rpc_callback_t); + return func(call, ntohs(rpc->call_argc), args); }