--- libaitrpc/src/srv.c 2011/08/31 17:11:58 1.4.2.4 +++ libaitrpc/src/srv.c 2011/09/07 09:10:55 1.5.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: srv.c,v 1.4.2.4 2011/08/31 17:11:58 misho Exp $ +* $Id: srv.c,v 1.5.2.2 2011/09/07 09:10:55 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -97,44 +97,48 @@ rpc_srv_dispatchCall(void *arg) break; } if (ret < sizeof(struct tagRPCCall)) { - rpc_SetErr(EMSGSIZE, "Error:: too short RPC packet ...\n"); + rpc_SetErr(ERPCMISMATCH, "Error:: too short RPC packet ...\n"); ret = -4; break; } else rpc = (struct tagRPCCall*) buf; /* check RPC packet session info */ - if (memcmp(&rpc->call_session, &s->srv_session, sizeof rpc->call_session)) { - rpc_SetErr(EINVAL, "Error:: get invalid RPC session ...\n"); + if (rpc_chkPktSession(&rpc->call_session, &s->srv_session)) { + rpc_SetErr(ERPCMISMATCH, "Error:: get invalid RPC session ...\n"); ret = -5; goto makeReply; } else Limit = sizeof(struct tagRPCCall); /* RPC is OK! Go decapsulate variables ... */ - if (rpc->call_argc) { - arr = io_buffer2vals(buf + Limit, s->srv_netbuf - Limit, rpc->call_argc, 1); + if (ntohs(rpc->call_argc)) { + arr = io_buffer2vals(buf + Limit, s->srv_netbuf - Limit, + ntohs(rpc->call_argc), 1); if (!arr) { ret = -5; goto makeReply; } - } + } else + arr = NULL; /* execute call */ argc = 0; memcpy(&ses, &rpc->call_session, sizeof ses); - if (!(f = rpc_srv_getCall(s, rpc->call_tag, rpc->call_hash))) { - rpc_SetErr(EINVAL, "Error:: call not found into RPC server ...\n"); + if (!(f = rpc_srv_getCall(s, ntohs(rpc->call_tag), ntohl(rpc->call_hash)))) { + rpc_SetErr(EPROGUNAVAIL, "Error:: call not found into RPC server ...\n"); ret = -6; } else if ((ret = rpc_srv_execCall(f, rpc, arr)) == -1) ret = -9; else { - io_arrayDestroy(&arr); + if (arr) + io_arrayDestroy(&arr); argc = rpc_srv_getVars(f, &arr); goto makeReply; /* Call finish OK */ } - io_arrayDestroy(&arr); + if (arr) + io_arrayDestroy(&arr); makeReply: /* Made reply */ @@ -145,22 +149,22 @@ makeReply: memcpy(&rrpc->ret_session, &ses, sizeof(rpc_sess_t)); rrpc->ret_tag = rpc->call_tag; rrpc->ret_hash = rpc->call_hash; - rrpc->ret_errno = rpc_Errno; - rrpc->ret_retcode = ret; - rrpc->ret_argc = argc; + rrpc->ret_errno = htonl(rpc_Errno); + rrpc->ret_retcode = htonl(ret); + rrpc->ret_argc = htons(argc); if (argc && arr) { /* Go Encapsulate variables ... */ if ((i = io_vals2buffer(buf + Limit, s->srv_netbuf - Limit, arr)) == -1) { - rpc_srv_freeVars(f); + rpc_srv_freeVals(f); argc = 0; ret = -7; - rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet values (-7) ...\n"); + rpc_SetErr(EBADRPC, "Error:: in prepare RPC packet values (-7) ...\n"); goto makeReply; } else { Limit += i; - rpc_srv_freeVars(f); + rpc_srv_freeVals(f); } } @@ -171,7 +175,7 @@ makeReply: break; } if (ret != Limit) { - rpc_SetErr(ECANCELED, "Error:: in send RPC request, should be send %d bytes, " + rpc_SetErr(EPROCUNAVAIL, "Error:: in send RPC request, should be send %d bytes, " "really is %d\n", Limit, ret); ret = -9; break; @@ -238,7 +242,7 @@ rpc_srv_dispatchVars(void *arg) break; } if (ret < sizeof(struct tagBLOBHdr)) { - rpc_SetErr(EMSGSIZE, "Error:: too short BLOB packet ...\n"); + rpc_SetErr(ERPCMISMATCH, "Error:: too short BLOB packet ...\n"); ret = -4; break; } else @@ -282,7 +286,7 @@ rpc_srv_dispatchVars(void *arg) ret = -7; break; default: - rpc_SetErr(EINVAL, "Error:: unsupported BLOB command (%d)...\n", + rpc_SetErr(EPROCUNAVAIL, "Error:: unsupported BLOB command (%d)...\n", blob->hdr_cmd); ret = -7; } @@ -298,7 +302,7 @@ makeReply: break; } if (ret != sizeof buf) { - rpc_SetErr(ECANCELED, "Error:: in send BLOB reply, should be send %d bytes, " + rpc_SetErr(EPROCUNAVAIL, "Error:: in send BLOB reply, should be send %d bytes, " "really is %d\n", sizeof buf, ret); ret = -9; break; @@ -308,7 +312,7 @@ makeReply: shutdown(c->cli_sock, SHUT_RDWR); close(c->cli_sock); memset(c, 0, sizeof(rpc_cli_t)); - return (void*) (long)ret; + return (void*) ((long)ret); } // ------------------------------------------------- @@ -408,12 +412,10 @@ rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_s pthread_mutex_init(&srv->srv_blob.mtx, NULL); - pthread_mutex_lock(&srv->srv_mtx); rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0); - rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 0); - rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 0); - rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 1); - pthread_mutex_unlock(&srv->srv_mtx); + rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 1); + rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 1); + rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 0); srv->srv_blob.state = enable; /* enable BLOB */ return 0; @@ -448,13 +450,13 @@ rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv) shutdown(c->cli_sock, SHUT_RDWR); close(srv->srv_blob.server.cli_sock); + pthread_mutex_lock(&srv->srv_blob.mtx); if (srv->srv_blob.clients) { free(srv->srv_blob.clients); srv->srv_blob.clients = NULL; } /* detach blobs */ - pthread_mutex_lock(&srv->srv_blob.mtx); while ((f = srv->srv_blob.blobs)) { srv->srv_blob.blobs = f->blob_next; rpc_srv_blobFree(srv, f); @@ -467,12 +469,12 @@ rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv) } /* - * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests + * rpc_srv_loopBLOB() Execute Main BLOB server loop and wait for clients requests * @srv = RPC Server instance * return: -1 error or 0 ok, infinite loop ... */ int -rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv) +rpc_srv_loopBLOB(rpc_srv_t * __restrict srv) { socklen_t salen = sizeof(struct sockaddr); register int i; @@ -496,9 +498,10 @@ rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv) if (!c->cli_sa.sa_family) break; if (i >= srv->srv_numcli) { - usleep(1000000); #ifdef HAVE_PTHREAD_YIELD pthread_yield(); +#else + usleep(1000000); #endif continue; } @@ -528,7 +531,7 @@ rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv) pthread_detach(c->cli_tid); } - srv->srv_blob.state = disable; + srv->srv_blob.state = kill; return 0; } @@ -672,9 +675,9 @@ rpc_srv_initServer(u_int regProgID, u_int regProcID, i pthread_mutex_init(&srv->srv_mtx, NULL); rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0); - rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0); - rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0); - rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0); + rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 1); + rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 4); + rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 1); return srv; } @@ -727,12 +730,12 @@ rpc_srv_endServer(rpc_srv_t * __restrict srv) } /* - * rpc_srv_execServer() Execute Main server loop and wait for clients requests + * rpc_srv_loopServer() Execute Main server loop and wait for clients requests * @srv = RPC Server instance * return: -1 error or 0 ok, infinite loop ... */ int -rpc_srv_execServer(rpc_srv_t * __restrict srv) +rpc_srv_loopServer(rpc_srv_t * __restrict srv) { socklen_t salen = sizeof(struct sockaddr); register int i; @@ -746,6 +749,9 @@ rpc_srv_execServer(rpc_srv_t * __restrict srv) return -1; } + /* activate BLOB server worker if srv->srv_blob.state == enable */ + rpc_srv_execBLOBServer(srv); + if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) { LOGERR; return -1; @@ -756,9 +762,10 @@ rpc_srv_execServer(rpc_srv_t * __restrict srv) if (!c->cli_sa.sa_family) break; if (i >= srv->srv_numcli) { - usleep(1000000); #ifdef HAVE_PTHREAD_YIELD pthread_yield(); +#else + usleep(1000000); #endif continue; } @@ -821,7 +828,7 @@ rpc_srv_execCall(rpc_func_t * __restrict call, struct func = dlsym(dl, (char*) call->func_name); if (func) - ret = func(call, rpc->call_argc, args); + ret = func(call, ntohs(rpc->call_argc), args); else { rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror()); ret = -1;