@@ -616,37 +616,42 @@ void ServerImpl::processNewConnection(uint64_t socketId)
616616{
617617 RIALTO_IPC_LOG_DEBUG (" processing new connection" );
618618
619- std::unique_lock<std::mutex> socketLocker (m_socketsLock);
619+ int clientSock;
620+ std::string sockPath;
621+ std::function<void (const std::shared_ptr<IClient> &)> connectedCb;
622+ std::function<void (const std::shared_ptr<IClient> &)> disconnectedCb;
620623
621- // find matching socket object
622- auto it = m_sockets.find (socketId);
623- if (it == m_sockets.end ())
624624 {
625- RIALTO_IPC_LOG_ERROR (" failed to find listening socket with id %" PRIu64, socketId);
626- return ;
627- }
625+ std::unique_lock<std::mutex> socketLocker (m_socketsLock);
628626
629- const Socket &kSocket = it->second ;
627+ // find matching socket object
628+ auto it = m_sockets.find (socketId);
629+ if (it == m_sockets.end ())
630+ {
631+ RIALTO_IPC_LOG_ERROR (" failed to find listening socket with id %" PRIu64, socketId);
632+ return ;
633+ }
630634
631- // accept the connection from the client
632- struct sockaddr clientAddr = {0 };
633- socklen_t clientAddrLen = sizeof (clientAddr);
635+ const Socket &kSocket = it->second ;
634636
635- int clientSock = accept4 (kSocket .sockFd , &clientAddr, &clientAddrLen, SOCK_NONBLOCK | SOCK_CLOEXEC);
636- if (clientSock < 0 )
637- {
638- RIALTO_IPC_LOG_SYS_ERROR (errno, " failed to accept client connection" );
639- return ;
640- }
637+ // accept the connection from the client
638+ struct sockaddr clientAddr = {0 };
639+ socklen_t clientAddrLen = sizeof (clientAddr);
641640
642- const std::string kSockPath = kSocket .sockPath ;
643- std::function<void (const std::shared_ptr<IClient> &)> connectedCb = kSocket .connectedCb ;
644- std::function<void (const std::shared_ptr<IClient> &)> disconnectedCb = kSocket .disconnectedCb ;
641+ clientSock = accept4 (kSocket .sockFd , &clientAddr, &clientAddrLen, SOCK_NONBLOCK | SOCK_CLOEXEC);
642+ if (clientSock < 0 )
643+ {
644+ RIALTO_IPC_LOG_SYS_ERROR (errno, " failed to accept client connection" );
645+ return ;
646+ }
645647
646- socketLocker.unlock ();
648+ sockPath = kSocket .sockPath ;
649+ connectedCb = kSocket .connectedCb ;
650+ disconnectedCb = kSocket .disconnectedCb ;
651+ }
647652
648653 // attempt to add the socket to the client list
649- auto client = addClientSocket (clientSock, kSockPath , std::move (disconnectedCb));
654+ auto client = addClientSocket (clientSock, sockPath , std::move (disconnectedCb));
650655 if (!client)
651656 {
652657 close (clientSock);
@@ -736,31 +741,34 @@ static std::vector<FileDescriptor> readMessageFds(const struct msghdr *msg, size
736741 */
737742void ServerImpl::processClientSocket (uint64_t clientId, unsigned events)
738743{
739- // take the lock while accessing the client list
740- std::unique_lock<std::mutex> locker (m_clientsLock) ;
744+ int sockFd;
745+ std::shared_ptr<ClientImpl> clientObj ;
741746
742- auto it = m_clients.find (clientId);
743- if (it == m_clients.end ())
747+ // take the lock while accessing the client list
744748 {
745- // should never happen
746- RIALTO_IPC_LOG_ERROR (" received an event from a socket with no matching client" );
747- return ;
748- }
749+ std::unique_lock<std::mutex> locker (m_clientsLock);
749750
750- // check if the client is marked for closure, if so then just ignore the data
751- if (m_condemnedClients.count (clientId) != 0 )
752- {
753- return ;
754- }
751+ auto it = m_clients.find (clientId);
752+ if (it == m_clients.end ())
753+ {
754+ // should never happen
755+ RIALTO_IPC_LOG_ERROR (" received an event from a socket with no matching client" );
756+ return ;
757+ }
755758
756- // get the socket that corresponds to the client connection
757- const int kSockFd = it->second .sock ;
759+ // check if the client is marked for closure, if so then just ignore the data
760+ if (m_condemnedClients.count (clientId) != 0 )
761+ {
762+ return ;
763+ }
758764
759- // get the client object
760- std::shared_ptr<ClientImpl> clientObj = it->second .client ;
765+ // get the socket that corresponds to the client connection
766+ sockFd = it->second .sock ;
761767
762- // can safely release the lock now we have the clientId and client object
763- locker.unlock ();
768+ // get the client object
769+ clientObj = it->second .client ;
770+ }
771+ // lock is released here automatically
764772
765773 // if there was an error disconnect the socket
766774 if (events & EPOLLERR)
@@ -786,7 +794,7 @@ void ServerImpl::processClientSocket(uint64_t clientId, unsigned events)
786794 msg.msg_controllen = sizeof (m_recvCtrlBuf);
787795
788796 // read one message
789- ssize_t rd = TEMP_FAILURE_RETRY (recvmsg (kSockFd , &msg, MSG_CMSG_CLOEXEC));
797+ ssize_t rd = TEMP_FAILURE_RETRY (recvmsg (sockFd , &msg, MSG_CMSG_CLOEXEC));
790798 if (rd < 0 )
791799 {
792800 if (errno != EWOULDBLOCK)
@@ -1394,21 +1402,22 @@ bool ServerImpl::sendEvent(uint64_t clientId, const std::shared_ptr<google::prot
13941402 }
13951403
13961404 // finally, take the lock (so the socket is not closed beneath us) and send the reply
1397- std::unique_lock<std::mutex> locker (m_clientsLock);
1398-
1399- auto it = m_clients.find (clientId);
1400- if (it == m_clients.end () || it->second .sock < 0 )
1401- {
1402- RIALTO_IPC_LOG_WARN (" socket closed before event could be sent" );
1403- return false ;
1404- }
1405- else if (TEMP_FAILURE_RETRY (sendmsg (it->second .sock , header, MSG_NOSIGNAL)) != static_cast <ssize_t >(requiredDataLen))
14061405 {
1407- RIALTO_IPC_LOG_SYS_ERROR (errno, " failed to send the complete event message" );
1408- return false ;
1409- }
1406+ std::unique_lock<std::mutex> locker (m_clientsLock);
14101407
1411- locker.unlock ();
1408+ auto it = m_clients.find (clientId);
1409+ if (it == m_clients.end () || it->second .sock < 0 )
1410+ {
1411+ RIALTO_IPC_LOG_WARN (" socket closed before event could be sent" );
1412+ return false ;
1413+ }
1414+ else if (TEMP_FAILURE_RETRY (sendmsg (it->second .sock , header, MSG_NOSIGNAL)) !=
1415+ static_cast <ssize_t >(requiredDataLen))
1416+ {
1417+ RIALTO_IPC_LOG_SYS_ERROR (errno, " failed to send the complete event message" );
1418+ return false ;
1419+ }
1420+ }
14121421
14131422 RIALTO_IPC_LOG_DEBUG (" event{ %s } - { %s }" , eventMessage->GetTypeName ().c_str (),
14141423 eventMessage->ShortDebugString ().c_str ());
0 commit comments