From c0d9501bcfa14ea7d851e5dd1cdc89e03a651342 Mon Sep 17 00:00:00 2001 From: Don Al Date: Mon, 26 Feb 2018 17:24:31 +0100 Subject: Remainning fixes from utility update Signed-off-by: Christian Linke Change-Id: I3c8a2b474bc8fac62c76a46545a231c718edb2a9 --- .../include/CAmCommonAPIWrapper.h | 105 ++++++++++--------- AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp | 2 +- AudioManagerUtilities/src/CAmSocketHandler.cpp | 111 +++++++++++---------- 3 files changed, 114 insertions(+), 104 deletions(-) diff --git a/AudioManagerUtilities/include/CAmCommonAPIWrapper.h b/AudioManagerUtilities/include/CAmCommonAPIWrapper.h index 0f770c4..7d64a04 100644 --- a/AudioManagerUtilities/include/CAmCommonAPIWrapper.h +++ b/AudioManagerUtilities/include/CAmCommonAPIWrapper.h @@ -98,12 +98,12 @@ class CAmCommonAPIWrapper void registerTimeout(CommonAPI::Timeout* timeout, const CommonAPI::DispatchPriority dispatchPriority); void deregisterTimeout(CommonAPI::Timeout* timeout); void deregisterAllTimeouts(); - - CommonAPI::Watch* watchWithHandle(const sh_pollHandle_t handle); + + CommonAPI::Watch* watchWithHandle(const sh_pollHandle_t handle); CommonAPI::Timeout* timeoutWithHandle(const sh_pollHandle_t handle); - + protected: - CAmCommonAPIWrapper ( CAmSocketHandler* socketHandler, const std::string& applicationName = "" ) ; + CAmCommonAPIWrapper(CAmSocketHandler* socketHandler, const std::string& applicationName = "") ; public: @@ -191,42 +191,42 @@ public: return mRuntime->unregisterService(domain, interface, instance); } - /** + /** * \brief Deprecated method. Instead you should use bool registerService(const std::shared_ptr & shStub, const std::string & domain, const std::string & instance). - * - * Register stub objects. - * - * Example: std::shared_ptr aStub; - * registerService( aStub, "local:com.your_company.interface_name:com.your_company.instance_name"); - * - * @param shStub: Shared pointer to a stub instance - * @param address: Complete common-api address as example "local:com.your_company.interface_name:com.your_company.instance_name" - * - */ - template bool __attribute__((deprecated)) registerStub(const std::shared_ptr & shStub, const std::string & address) - { - std::vector parts = CommonAPI::split(address, ':'); - assert(parts.size()==3); - - return registerService(shStub, parts[0], parts[2]); - } - - /** - * \brief Deprecated method. Instead you should use bool unregisterService(const std::string &domain, const std::string &interface, const std::string &instance). - * - * Unregister stub objects. - * - * @param address: Complete common-api address as example "local:com.your_company.interface_name:com.your_company.instance_name" - * - */ - bool __attribute__((deprecated)) unregisterStub(const std::string & address) - { - std::vector parts = CommonAPI::split(address, ':'); - assert(parts.size()==3); - - return unregisterService(parts[0], parts[1], parts[2]); - } - + * + * Register stub objects. + * + * Example: std::shared_ptr aStub; + * registerService( aStub, "local:com.your_company.interface_name:com.your_company.instance_name"); + * + * @param shStub: Shared pointer to a stub instance + * @param address: Complete common-api address as example "local:com.your_company.interface_name:com.your_company.instance_name" + * + */ + template bool __attribute__((deprecated)) registerStub(const std::shared_ptr & shStub, const std::string & address) + { + std::vector parts = CommonAPI::split(address, ':'); + assert(parts.size()==3); + + return registerService(shStub, parts[0], parts[2]); + } + + /** + * \brief Deprecated method. Instead you should use bool unregisterService(const std::string &domain, const std::string &interface, const std::string &instance). + * + * Unregister stub objects. + * + * @param address: Complete common-api address as example "local:com.your_company.interface_name:com.your_company.instance_name" + * + */ + bool __attribute__((deprecated)) unregisterStub(const std::string & address) + { + std::vector parts = CommonAPI::split(address, ':'); + assert(parts.size()==3); + + return unregisterService(parts[0], parts[1], parts[2]); + } + /** * \brief Deprecated method. This class is used only in single connection applications and no connectionId is needed. Instead you should use buildProxy(const std::string &domain, const std::string &instance). * @@ -241,26 +241,25 @@ public: template class ProxyClass, typename ... AttributeExtensions> std::shared_ptr> __attribute__((deprecated)) buildProxy(const std::string &domain, const std::string &instance, const CommonAPI::ConnectionId_t __attribute__((__unused__)) & /*connectionId*/) { - return mRuntime->buildProxy(domain, instance, mContext); + return mRuntime->buildProxy(domain, instance, mContext); } - /** - * \brief Build proxy objects. - * - * Example: std::shared_ptr> aProxy = buildProxy("local", "com.your_company.instance_name"); - * - * @param domain: A string with the domain name, usually "local" - * @param instance: Common-api instance string as example "com.your_company.instance_name" - * - * @return A proxy object. - */ + /** + * \brief Build proxy objects. + * + * Example: std::shared_ptr> aProxy = buildProxy("local", "com.your_company.instance_name"); + * + * @param domain: A string with the domain name, usually "local" + * @param instance: Common-api instance string as example "com.your_company.instance_name" + * + * @return A proxy object. + */ template class ProxyClass, typename ... AttributeExtensions> std::shared_ptr> buildProxy(const std::string &domain, const std::string &instance) { return mRuntime->buildProxy(domain, instance, mContext); } - - + /** * \brief Deprecated method. Instead you should use buildProxy(const std::string &domain, const std::string &instance). * @@ -278,7 +277,7 @@ public: assert(parts.size()==3); return buildProxy(parts[0], parts[2]); - } + } }; diff --git a/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp b/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp index 702d384..4a6accf 100644 --- a/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp +++ b/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp @@ -52,7 +52,7 @@ bool timeoutToTimespec(const int64_t & localTimeout, timespec & pollTimeout) else { pollTimeout.tv_sec = localTimeout / 1000; - pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000; + pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000; } return true; } diff --git a/AudioManagerUtilities/src/CAmSocketHandler.cpp b/AudioManagerUtilities/src/CAmSocketHandler.cpp index 4c46dd1..7cf5594 100644 --- a/AudioManagerUtilities/src/CAmSocketHandler.cpp +++ b/AudioManagerUtilities/src/CAmSocketHandler.cpp @@ -138,9 +138,9 @@ void CAmSocketHandler::start_listenting() { if (mRecreatePollfds) { - #ifdef WITH_TIMERFD +#ifdef WITH_TIMERFD closeRemovedTimers(); - #endif +#endif fdPollingArray.clear(); //freeze mListPoll by copying it - otherwise we get problems when we want to manipulate it during the next lines mListActivePolls = mListPoll; @@ -178,16 +178,16 @@ void CAmSocketHandler::start_listenting() //todo: here could be a timer that makes sure naughty plugins return! listPoll.clear(); //stage 0+1, call firedCB - for (itMfdPollingArray = fdPollingArray.begin(); itMfdPollingArray != fdPollingArray.end(); itMfdPollingArray++) + for (itMfdPollingArray = fdPollingArray.begin(); itMfdPollingArray != fdPollingArray.end(); ++itMfdPollingArray) { itMfdPollingArray->revents &= itMfdPollingArray->events | POLLERR | POLLHUP; if ( itMfdPollingArray->revents!=0 ) - { + { listmPollIt = mListActivePolls.begin(); std::advance(listmPollIt, std::distance(fdPollingArray.begin(), itMfdPollingArray)); - + sh_poll_s & pollObj = *listmPollIt; - + listPoll.push_back(&pollObj); CAmSocketHandler::fire(&pollObj); itMfdPollingArray->revents = 0; @@ -333,26 +333,31 @@ am_Error_e CAmSocketHandler::listenToSignals(const std::vector & listSi auto actionPoll = [this](const pollfd pollfd, const sh_pollHandle_t, void*) { - const VectorSignalHandlers_t & signalHandlers = mSignalHandlers; - /* We have a valid signal, read the info from the fd */ - struct signalfd_siginfo info; - ssize_t bytes = read(pollfd.fd, &info, sizeof(info)); - if(bytes != sizeof(info)) - { - //error received... - logError("Failed to read from signal fd"); - throw std::runtime_error(std::string("Failed to read from signal fd.")); - } - - /* Notify all listeners */ - for(auto it: signalHandlers) - it.callback(it.handle, info, it.userData); + const VectorSignalHandlers_t & signalHandlers = mSignalHandlers; + /* We have a valid signal, read the info from the fd */ + struct signalfd_siginfo info; + ssize_t bytes = read(pollfd.fd, &info, sizeof(info)); + if(bytes == -1) + { + if (errno == EAGAIN) //Something wrong, check for EAGAIN + bytes = read(pollfd.fd, &info, sizeof(info)); + } + if(bytes != sizeof(info)) + { + //Failed to read from fd... + logError("Failed to read from signal fd"); + throw std::runtime_error(std::string("Failed to read from signal fd.")); + } + + /* Notify all listeners */ + for(auto it: signalHandlers) + it.callback(it.handle, info, it.userData); }; /* We're going to add the signal fd through addFDPoll. At this point we don't have any signal listeners. */ return addFDPoll(signalHandlerFd, POLLIN | POLLERR | POLLHUP, NULL, actionPoll, [](const sh_pollHandle_t, void*) { return (false);}, NULL, NULL, mSignalFdHandle); } - else + else { int signalHandlerFd = signalfd(sgPollData.pollfdValue.fd, &sigset, 0); if (signalHandlerFd == -1) @@ -458,33 +463,34 @@ am::am_Error_e CAmSocketHandler::addFDPoll(const int fd, const short event, IAmS am_Error_e CAmSocketHandler::removeFDPoll(const sh_pollHandle_t handle) { CHECK_CALLER_THREAD_ID() - - VectorListPoll_t::iterator iterator = mListPoll.begin(); - - for (; iterator != mListPoll.end(); ++iterator) + + bool handleRemoved = false; + + for (auto it = mListPoll.begin(); it != mListPoll.end(); ++it) { - if (iterator->handle == handle) + if (it->handle == handle) { - iterator = mListPoll.erase(iterator); + it = mListPoll.erase(it); mSetPollKeys.pollHandles.erase(handle); - mRecreatePollfds = true; + handleRemoved = true; break; } } - - if (iterator == mListPoll.end()) + + if ( false == handleRemoved ) return (E_UNKNOWN); - - VectorListPoll_t::iterator iteratorActivePolls = mListActivePolls.begin(); - for (; iteratorActivePolls != mListActivePolls.end(); ++iteratorActivePolls) + + mRecreatePollfds = true; + + for (auto it = mListActivePolls.begin(); it != mListActivePolls.end(); ++it) { - if (iteratorActivePolls->handle == handle) + if (it->handle == handle) { - iteratorActivePolls->isValid = false; + it->isValid = false; break; } } - + return (E_OK); } @@ -616,9 +622,16 @@ am_Error_e CAmSocketHandler::addTimer(const timespec & timeouts, std::functioncheckCB || false==a->isValid ) + if (nullptr == a->checkCB || false == a->isValid) return (true); return (!a->checkCB(a->handle, a->userData)); } @@ -1065,7 +1078,7 @@ bool CAmSocketHandler::noDispatching(const sh_poll_s* a) bool CAmSocketHandler::dispatchingFinished(const sh_poll_s* a) { //remove from list of there is no dispatchCB - if (nullptr == a->dispatchCB || false==a->isValid ) + if (nullptr == a->dispatchCB || false == a->isValid) return (true); return (!a->dispatchCB(a->handle, a->userData)); } @@ -1083,16 +1096,16 @@ inline timespec* CAmSocketHandler::insertTime(timespec& buffertime) buffertime = mListActiveTimer.front().countdown; return (&buffertime); } - else -#endif + else +#endif { return (NULL); } } -#ifdef WITH_TIMERFD +#ifdef WITH_TIMERFD am_Error_e CAmSocketHandler::createTimeFD(const itimerspec & timeouts, int & fd) -{ +{ fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); if (fd < 0) { @@ -1110,17 +1123,15 @@ am_Error_e CAmSocketHandler::createTimeFD(const itimerspec & timeouts, int & fd) void CAmSocketHandler::closeRemovedTimers() { - std::list::iterator it(mListRemovedTimers.begin()); - while (it != mListRemovedTimers.end()) + for (auto it : mListRemovedTimers) { - if( it->fd > -1 ) - close( it->fd ); - ++it; + if( it.fd > -1 ) + close( it.fd ); } mListRemovedTimers.clear(); } -#endif +#endif void CAmSocketHandler::callTimer(sh_timer_s& a) { -- cgit v1.2.1