- Fix the timing issue when server reply too fast.
authorWonkyu Kwon <wonkyu.kwon@samsung.com>
Fri, 23 Nov 2012 10:33:53 +0000 (19:33 +0900)
committerWonkyu Kwon <wonkyu.kwon@samsung.com>
Fri, 23 Nov 2012 10:33:53 +0000 (19:33 +0900)
 - Adjust memory new/free owner.

Change-Id: I41bdce2d32166eb6fa69c01da5526bcd3f407727

client/ClientChannel.cpp
client/ClientDispatcher.cpp
client/ClientIPC.cpp
client/Reader.cpp
client/SEService.cpp
client/Session.cpp
common/DispatcherHelper.cpp
common/IPCHelper.cpp
common/include/DispatcherMsg.h
packaging/smartcard-service.spec
server/ServerIPC.cpp

index 0a903ac..4f30f27 100644 (file)
@@ -75,16 +75,20 @@ namespace smartcard_service_api
                                msg.caller = (void *)this;
                                msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
-                               ClientIPC::getInstance().sendMessage(&msg);
-
                                syncLock();
-                               rv = waitTimedCondition(0);
-                               syncUnlock();
-
-                               if (rv < 0)
+                               if (ClientIPC::getInstance().sendMessage(&msg) == true)
                                {
-                                       SCARD_DEBUG_ERR("closeSync failed [%d]", rv);
+                                       rv = waitTimedCondition(0);
+                                       if (rv < 0)
+                                       {
+                                               SCARD_DEBUG_ERR("closeSync failed [%d]", rv);
+                                       }
                                }
+                               else
+                               {
+                                       SCARD_DEBUG_ERR("sendMessage failed");
+                               }
+                               syncUnlock();
 
                                channelNum = -1;
                        }
@@ -98,6 +102,8 @@ namespace smartcard_service_api
 
        int ClientChannel::close(closeCallback callback, void *userParam)
        {
+               int result = 0;
+
                if (isClosed() == false)
                {
                        if (getSession()->getReader()->isSecureElementPresent() == true)
@@ -114,15 +120,23 @@ namespace smartcard_service_api
                                msg.callback = (void *)callback;
                                msg.userParam = userParam;
 
-                               ClientIPC::getInstance().sendMessage(&msg);
+                               if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                               {
+                                       result = 0;
+                               }
+                               else
+                               {
+                                       result = -1;
+                               }
                        }
                        else
                        {
                                SCARD_DEBUG_ERR("unavailable channel");
+                               result = -1;
                        }
                }
 
-               return 0;
+               return result;
        }
 
        int ClientChannel::transmitSync(ByteArray command, ByteArray &result)
@@ -143,31 +157,33 @@ namespace smartcard_service_api
                        msg.caller = (void *)this;
                        msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
                        syncLock();
-                       rv = waitTimedCondition(0);
-                       syncUnlock();
-
-                       if (rv >= 0)
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
                        {
-                               result = response;
+                               rv = waitTimedCondition(0);
+                               if (rv >= 0)
+                               {
+                                       result = response;
+
+                                       rv = 0;
+                               }
+                               else
+                               {
+                                       SCARD_DEBUG_ERR("clientIPC is null");
 
-                               rv = 0;
+                                       rv = -1;
+                               }
                        }
                        else
                        {
-                               SCARD_DEBUG_ERR("clientIPC is null");
-
-                               rv = -1;
+                               SCARD_DEBUG_ERR("sendMessage failed");
                        }
+                       syncUnlock();
 #endif
                }
                else
                {
                        SCARD_DEBUG_ERR("unavailable channel");
-
-                       rv = -1;
                }
 
                return rv;
@@ -175,6 +191,8 @@ namespace smartcard_service_api
 
        int ClientChannel::transmit(ByteArray command, transmitCallback callback, void *userParam)
        {
+               int result = -1;
+
                if (getSession()->getReader()->isSecureElementPresent() == true)
                {
                        Message msg;
@@ -189,14 +207,21 @@ namespace smartcard_service_api
                        msg.callback = (void *)callback;
                        msg.userParam = userParam;
 
-                       ClientIPC::getInstance().sendMessage(&msg);
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               result = 0;
+                       }
+                       else
+                       {
+                               result = -1;
+                       }
                }
                else
                {
                        SCARD_DEBUG_ERR("unavailable channel");
                }
 
-               return 0;
+               return result;
        }
 
        bool ClientChannel::dispatcherCallback(void *message)
@@ -271,8 +296,6 @@ namespace smartcard_service_api
                        break;
                }
 
-               delete msg;
-
                return result;
        }
 } /* namespace smartcard_service_api */
index 7dcb3c3..0ba0184 100644 (file)
@@ -99,35 +99,35 @@ namespace smartcard_service_api
                case Message::MSG_REQUEST_READERS :
                case Message::MSG_REQUEST_SHUTDOWN :
                        {
-                               DispatcherMsg *tempMsg = new DispatcherMsg(msg);
-
-                               if (msg->callback != msg->caller)
+                               if (msg->isSynchronousCall() == false)
                                {
+                                       DispatcherMsg *tempMsg = new DispatcherMsg(msg);
+
                                        /* Asynchronous call */
                                        g_idle_add((GSourceFunc)&SEService::dispatcherCallback, (gpointer)tempMsg);
                                }
                                else
                                {
                                        /* Synchronous call */
-                                       SEService::dispatcherCallback(tempMsg);
+                                       SEService::dispatcherCallback(msg);
                                }
                        }
                        break;
 
-                       /* Reader requests */
+               /* Reader requests */
                case Message::MSG_REQUEST_OPEN_SESSION :
                        {
-                               DispatcherMsg *tempMsg = new DispatcherMsg(msg);
-
-                               if (msg->callback != msg->caller)
+                               if (msg->isSynchronousCall() == false)
                                {
+                                       DispatcherMsg *tempMsg = new DispatcherMsg(msg);
+
                                        /* Asynchronous call */
                                        g_idle_add((GSourceFunc)&Reader::dispatcherCallback, (gpointer)tempMsg);
                                }
                                else
                                {
                                        /* Synchronous call */
-                                       Reader::dispatcherCallback(tempMsg);
+                                       Reader::dispatcherCallback(msg);
                                }
                        }
                        break;
@@ -138,17 +138,17 @@ namespace smartcard_service_api
                case Message::MSG_REQUEST_CLOSE_SESSION :
                case Message::MSG_REQUEST_GET_CHANNEL_COUNT :
                        {
-                               DispatcherMsg *tempMsg = new DispatcherMsg(msg);
-
-                               if (msg->callback != msg->caller)
+                               if (msg->isSynchronousCall() == false)
                                {
+                                       DispatcherMsg *tempMsg = new DispatcherMsg(msg);
+
                                        /* Asynchronous call */
                                        g_idle_add((GSourceFunc)&Session::dispatcherCallback, (gpointer)tempMsg);
                                }
                                else
                                {
                                        /* Synchronous call */
-                                       Session::dispatcherCallback(tempMsg);
+                                       Session::dispatcherCallback(msg);
                                }
                        }
                        break;
@@ -157,17 +157,17 @@ namespace smartcard_service_api
                case Message::MSG_REQUEST_TRANSMIT :
                case Message::MSG_REQUEST_CLOSE_CHANNEL :
                        {
-                               DispatcherMsg *tempMsg = new DispatcherMsg(msg);
-
-                               if (msg->callback != msg->caller)
+                               if (msg->isSynchronousCall() == false)
                                {
+                                       DispatcherMsg *tempMsg = new DispatcherMsg(msg);
+
                                        /* Asynchronous call */
                                        g_idle_add((GSourceFunc)&ClientChannel::dispatcherCallback, (gpointer)tempMsg);
                                }
                                else
                                {
                                        /* Synchronous call */
-                                       ClientChannel::dispatcherCallback(tempMsg);
+                                       ClientChannel::dispatcherCallback(msg);
                                }
                        }
                        break;
index 3b10538..8c50ff8 100644 (file)
@@ -202,19 +202,18 @@ namespace smartcard_service_api
                        msg = retrieveMessage();
                        if (msg != NULL)
                        {
-                               DispatcherMsg *dispMsg = new DispatcherMsg(msg);
+                               DispatcherMsg dispMsg(msg);
 
                                /* set peer socket */
-                               dispMsg->setPeerSocket(ipcSocket);
+                               dispMsg.setPeerSocket(ipcSocket);
 
                                /* push to dispatcher */
                                if (dispatcher != NULL)
                                {
 #ifdef CLIENT_IPC_THREAD
-                                       dispatcher->processMessage(dispMsg);
-                                       delete dispMsg;
+                                       dispatcher->processMessage(&dispMsg);
 #else
-                                       dispatcher->pushMessage(dispMsg);
+                                       dispatcher->pushMessage(&dispMsg);
 #endif
                                }
 
index 1c9c4bc..01c0c65 100644 (file)
@@ -109,16 +109,20 @@ namespace smartcard_service_api
                        msg.caller = (void *)this;
                        msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
                        syncLock();
-                       rv = waitTimedCondition(0);
-                       syncUnlock();
-
-                       if (rv != 0)
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               rv = waitTimedCondition(0);
+                               if (rv != 0)
+                               {
+                                       SCARD_DEBUG_ERR("time over");
+                               }
+                       }
+                       else
                        {
-                               SCARD_DEBUG_ERR("time over");
+                               SCARD_DEBUG_ERR("sendMessage failed");
                        }
+                       syncUnlock();
 #endif
                }
                else
@@ -150,9 +154,14 @@ namespace smartcard_service_api
                        msg.callback = (void *)callback;
                        msg.userParam = userData;
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
-                       result = 0;
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               result = 0;
+                       }
+                       else
+                       {
+                               SCARD_DEBUG_ERR("sendMessage failed");
+                       }
                }
                else
                {
@@ -229,8 +238,6 @@ namespace smartcard_service_api
                        break;
                }
 
-               delete msg;
-
                SCARD_END();
 
                return result;
index 02b471a..082aec3 100644 (file)
@@ -124,11 +124,10 @@ namespace smartcard_service_api
                        msg.caller = (void *)this;
                        msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
+                       syncLock();
                        if (ClientIPC::getInstance().sendMessage(&msg) == true)
                        {
-                               syncLock();
                                rv = waitTimedCondition(0);
-                               syncUnlock();
 
                                if (rv == 0)
                                {
@@ -145,6 +144,7 @@ namespace smartcard_service_api
                        {
                                SCARD_DEBUG_ERR("sendMessage failed");
                        }
+                       syncUnlock();
                }
 #endif
        }
@@ -404,8 +404,6 @@ namespace smartcard_service_api
                        break;
                }
 
-               delete msg;
-
                SCARD_END();
 
                return result;
index 3eed95b..8bad4e8 100644 (file)
@@ -87,16 +87,20 @@ namespace smartcard_service_api
                        msg.caller = (void *)this;
                        msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
                        syncLock();
-                       rv = waitTimedCondition(0);
-                       syncUnlock();
-
-                       if (rv != 0)
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               rv = waitTimedCondition(0);
+                               if (rv != 0)
+                               {
+                                       SCARD_DEBUG_ERR("time over");
+                               }
+                       }
+                       else
                        {
-                               SCARD_DEBUG_ERR("time over");
+                               SCARD_DEBUG_ERR("sendMessage failed");
                        }
+                       syncUnlock();
 #endif
                }
                else
@@ -123,9 +127,10 @@ namespace smartcard_service_api
                        msg.callback = (void *)callback;
                        msg.userParam = userData;
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
-                       result = 0;
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               result = 0;
+                       }
                }
                else
                {
@@ -153,22 +158,28 @@ namespace smartcard_service_api
                        msg.caller = (void *)this;
                        msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
                        syncLock();
-                       rv = waitTimedCondition(0);
-                       syncUnlock();
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               rv = waitTimedCondition(0);
 
-                       if (rv != 0)
+                               if (rv != 0)
+                               {
+                                       SCARD_DEBUG_ERR("time over");
+                               }
+                       }
+                       else
                        {
-                               SCARD_DEBUG_ERR("time over");
+                               SCARD_DEBUG_ERR("sendMessage failed");
                        }
+                       syncUnlock();
                }
 #endif
        }
 
        int Session::close(closeSessionCallback callback, void *userData)
        {
+               int result = -1;
                Message msg;
 
                if (isClosed() == false)
@@ -184,10 +195,13 @@ namespace smartcard_service_api
                        msg.callback = (void *)callback;
                        msg.userParam = userData;
 
-                       ClientIPC::getInstance().sendMessage(&msg);
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               result = 0;
+                       }
                }
 
-               return 0;
+               return result;
        }
 
        unsigned int Session::getChannelCountSync()
@@ -207,18 +221,22 @@ namespace smartcard_service_api
                        msg.caller = (void *)this;
                        msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
-                       ClientIPC::getInstance().sendMessage(&msg);
+                       channelCount = -1;
 
                        syncLock();
-                       rv = waitTimedCondition(0);
-                       syncUnlock();
-
-                       if (rv != 0)
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
                        {
-                               SCARD_DEBUG_ERR("time over");
-
-                               channelCount = -1;
+                               rv = waitTimedCondition(0);
+                               if (rv != 0)
+                               {
+                                       SCARD_DEBUG_ERR("time over");
+                               }
+                       }
+                       else
+                       {
+                               SCARD_DEBUG_ERR("sendMessage failed");
                        }
+                       syncUnlock();
 #endif
                }
                else
@@ -244,9 +262,10 @@ namespace smartcard_service_api
                        msg.callback = (void *)callback;
                        msg.userParam = userData;
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
-                       result = 0;
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               result = 0;
+                       }
                }
                else
                {
@@ -275,16 +294,20 @@ namespace smartcard_service_api
                        msg.caller = (void *)this;
                        msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
                        syncLock();
-                       rv = waitTimedCondition(0);
-                       syncUnlock();
-
-                       if (rv != 0)
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               rv = waitTimedCondition(0);
+                               if (rv != 0)
+                               {
+                                       SCARD_DEBUG_ERR("time over");
+                               }
+                       }
+                       else
                        {
-                               SCARD_DEBUG_ERR("time over");
+                               SCARD_DEBUG_ERR("sendMessage failed");
                        }
+                       syncUnlock();
 #endif
                }
                else
@@ -313,9 +336,10 @@ namespace smartcard_service_api
                        msg.callback = (void *)callback;
                        msg.userParam = userData;
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
-                       result = 0;
+                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                       {
+                               result = 0;
+                       }
                }
                else
                {
@@ -504,8 +528,6 @@ namespace smartcard_service_api
                        break;
                }
 
-               delete msg;
-
                return result;
        }
 } /* namespace smartcard_service_api */
index caf84b6..2b21985 100644 (file)
@@ -62,9 +62,11 @@ namespace smartcard_service_api
 
        void DispatcherHelper::pushMessage(DispatcherMsg *msg)
        {
+               DispatcherMsg *pushMsg = new DispatcherMsg(msg);
+
                syncLock();
 
-               messageQ.push(msg);
+               messageQ.push(pushMsg);
 
                signalCondition();
                syncUnlock();
index c1c9da9..b8a8993 100644 (file)
@@ -203,18 +203,17 @@ ERROR :
                        {
                                if (pollEvents[i].data.fd == fdPoll)
                                {
-                                       SCARD_DEBUG("pollEvents[%d].data.fd == fdPoll", i);
-                                       SCARD_DEBUG("pollEvents[%d].events [%X]", pollEvents[i].events);
+                                       SCARD_DEBUG("pollEvents[%d].events [%X]", i, pollEvents[i].events);
 
-                                       if (pollEvents[i].events & EPOLLIN)
+                                       if ((pollEvents[i].events & EPOLLHUP) || (pollEvents[i].events & EPOLLERR))
                                        {
-                                               result = 1;
+                                               SCARD_DEBUG_ERR("connection is closed");
+                                               result = 0;
                                                break;
                                        }
-                                       else if ((pollEvents[i].events & EPOLLHUP) || (pollEvents[i].events & EPOLLERR))
+                                       else if (pollEvents[i].events & EPOLLIN)
                                        {
-                                               SCARD_DEBUG_ERR("connection is closed");
-                                               result = 0;
+                                               result = 1;
                                                break;
                                        }
                                }
index 426f31f..8aa0ddc 100644 (file)
@@ -51,6 +51,19 @@ namespace smartcard_service_api
                        userParam = msg->userParam;
                }
 
+               DispatcherMsg(DispatcherMsg *msg):Message()
+               {
+                       peerSocket = msg->peerSocket;
+                       message = msg->message;
+                       param1 = msg->param1;
+                       param2 = msg->param2;
+                       error = msg->error;
+                       data = msg->data;
+                       caller = msg->caller;
+                       callback = msg->callback;
+                       userParam = msg->userParam;
+               }
+
                DispatcherMsg(Message *msg, int socket):Message()
                {
                        peerSocket = socket;
index 82a323a..4f63354 100644 (file)
@@ -1,7 +1,7 @@
 Name:       smartcard-service
 Summary:    Smartcard Service FW
 Version:    0.1.0
-Release:    26
+Release:    27
 Group:      libs
 License:    Samsung Proprietary License
 Source0:    %{name}-%{version}.tar.gz
index 5066504..c6c1a59 100644 (file)
@@ -136,7 +136,7 @@ namespace smartcard_service_api
                        goto ERROR;
                }
 
-               SCARD_DEBUG("client socket is bound with g_io_channel");
+               SCARD_DEBUG("client socket is bond with g_io_channel");
 
                if (ServerResource::getInstance().createClient(client_channel, client_sock_fd, client_src_id, 0, -1) == false)
                {
@@ -252,10 +252,10 @@ ERROR :
                                /* read message */
                                if ((msg = retrieveMessage(peerSocket)) != NULL)
                                {
-                                       DispatcherMsg *dispMsg = new DispatcherMsg(msg, peerSocket);
+                                       DispatcherMsg dispMsg(msg, peerSocket);
 
                                        /* push to dispatcher */
-                                       ServerDispatcher::getInstance()->pushMessage(dispMsg);
+                                       ServerDispatcher::getInstance()->pushMessage(&dispMsg);
 
                                        result = TRUE;