From 722c6aa6bdcac8e1c2f9ef789cd23adb5a175888 Mon Sep 17 00:00:00 2001 From: Carlos Daniel Ruvalcaba Valenzuela Date: Sun, 23 Sep 2007 22:45:30 -0700 Subject: [PATCH] Applied WANG Cong Patch, client src socketipc. --- src/socketipc.cpp | 133 +++++++++++++++++++++++++++--------------------------- 1 file changed, 67 insertions(+), 66 deletions(-) diff --git a/src/socketipc.cpp b/src/socketipc.cpp index 7ef0b28..55e0b97 100644 --- a/src/socketipc.cpp +++ b/src/socketipc.cpp @@ -32,23 +32,23 @@ with this program; if not, write to the Free Software Foundation, Inc., #include SocketIPC::SocketIPC(Socket *s){ - sock = s; + sock = s; } /* Create a socket IPC from uri, create * sockets and parse host and port. */ SocketIPC::SocketIPC(char *uri){ - string shost; + string shost; string options; - pcrecpp::RE re("([\\w\\d\\.]+)\\:(\\d*)[/]*(\\w*)"); - + pcrecpp::RE re("([\\w\\d\\.]+)\\:(\\d*)[/]*(\\w*)"); + re.PartialMatch(uri, &shost, &port, &options); host = (char*)shost.c_str(); sock = Socket::CreateSocket(SOCKET_INET, 0); sock->setPort(port); sock->setAddress(host); - auxsock = NULL; + auxsock = NULL; } /* Connect to remote process for IPC */ @@ -62,24 +62,24 @@ int SocketIPC::RequestIPC(){ break; } - return ret; + return ret; } /* Wait for incoming IPC */ int SocketIPC::ListenIPC(){ - sock->Bind(); - sock->Listen(10); - - auxsock = sock->Accept(); - return 0; + sock->Bind(); + sock->Listen(10); + + auxsock = sock->Accept(); + return 0; } /* Close IPC Session */ int SocketIPC::CloseIPC(){ - sock->Close(); - if (auxsock) - auxsock->Close(); - return 0; + sock->Close(); + if (auxsock) + auxsock->Close(); + return 0; } /* Lousy Parser, just works @@ -235,10 +235,10 @@ int SocketIPC::ParseLoose(){ if (msg != NULL){ //printf("IPC Message Pushed\n"); - msgQueue.push(msg); + msgQueue.push(msg); return 1; } - return 0; + return 0; } /* Not in use, old parsing function. @@ -256,9 +256,9 @@ int SocketIPC::FetchMessage2(){ mlen = 0; margc = 0; msg = NULL; - + if (auxsock == NULL) - auxsock = sock; + auxsock = sock; while (onParse){ //printf("\tReading Buffer\n"); @@ -440,7 +440,6 @@ int SocketIPC::FetchMessage2(){ } int SocketIPC::PeekMessage(){ - if(ParseLoose()) return 1; @@ -485,7 +484,8 @@ IPCMessage *SocketIPC::ReciveMessage(){ char *makemsg(IPCMessage *msg, int *r){ char *ret, *tmp; - int len, i, j, offset; + int len, i, j; + size_t offset; len = 40; @@ -493,6 +493,8 @@ char *makemsg(IPCMessage *msg, int *r){ len += msg->ParamCount() * 15; ret = (char*)malloc(sizeof(char) * len); + if (!ret) + return 0; sprintf(ret, "MSG[%i][%i]%s", strlen(msg->GetMessageName()), msg->ParamCount(), msg->GetMessageName()); offset = strlen(ret); @@ -524,73 +526,72 @@ int SocketIPC::PushMessage(IPCMessage *msg){ char *tmp, buffer[50]; int l, blen, r; - tmp = makemsg(msg, &l); - //printf("IPCMSG: %s\n", tmp); + tmp = makemsg(msg, &l); + //printf("IPCMSG: %s\n", tmp); - sock->Write(tmp, l); - free(tmp); - - delete msg; - - return 0; + sock->Write(tmp, l); + free(tmp); + + delete msg; + return 0; //printf("IPC: Pushing Message\n"); - tmp = msg->GetMessageName(); - l = strlen(tmp); - + tmp = msg->GetMessageName(); + l = strlen(tmp); + sprintf(buffer, "MSG[%i][%i]", l, msg->ParamCount()); blen = strlen(buffer); //printf("\tBegin Header: %s%s\n", buffer, tmp); - sock->Poll(-1, SOCKET_POLL_WRITE); + sock->Poll(-1, SOCKET_POLL_WRITE); r = sock->Write(buffer, blen); + if (r < blen){ + printf("\tError Writting Header\n"); + } + + sock->Poll(-1, SOCKET_POLL_WRITE); + r = sock->Write(tmp, l); + if (r < l){ + printf("\tError Writting Header\n"); + } + + while((tmp = msg->PopParam())){ + l = strlen(tmp); + + sprintf(buffer, "PARAM[%i]", l); + blen = strlen(buffer); + + //printf("\tBegin param: %s%s\n", buffer, tmp); + + sock->Poll(-1, SOCKET_POLL_WRITE); + r = sock->Write(buffer, blen); if (r < blen){ printf("\tError Writting Header\n"); } - - sock->Poll(-1, SOCKET_POLL_WRITE); - r = sock->Write(tmp, l); + sock->Poll(-1, SOCKET_POLL_WRITE); + r = sock->Write(tmp, l); if (r < l){ printf("\tError Writting Header\n"); } - - while((tmp = msg->PopParam())){ - l = strlen(tmp); - - sprintf(buffer, "PARAM[%i]", l); - blen = strlen(buffer); - - //printf("\tBegin param: %s%s\n", buffer, tmp); - - sock->Poll(-1, SOCKET_POLL_WRITE); - r = sock->Write(buffer, blen); - if (r < blen){ - printf("\tError Writting Header\n"); - } - sock->Poll(-1, SOCKET_POLL_WRITE); - r = sock->Write(tmp, l); - if (r < l){ - printf("\tError Writting Header\n"); - } - free(tmp); - } + free(tmp); + } - //printf("IPC Message Send OK!\n"); - - return 1; + //printf("IPC Message Send OK!\n"); + + return 1; } IPCMessage *SocketIPC::PopMessage(){ - IPCMessage *msg; - - msg = msgQueue.front(); - msgQueue.pop(); - - return msg; + IPCMessage *msg; + + msg = msgQueue.front(); + msgQueue.pop(); + + return msg; } int SocketIPC::RawRead(char *buff, int size){ -- 2.11.4.GIT