diff options
author | Christian Linke <Christian.Linke@bmw.de> | 2016-02-16 17:44:08 +0100 |
---|---|---|
committer | Christian Linke <Christian.Linke@bmw.de> | 2016-02-16 17:44:08 +0100 |
commit | c7ef1ef0126023ca8a377f00ae9aebb81bc34d37 (patch) | |
tree | fd04e68508d3ec32f2ec6eee98e8f2759d331e00 | |
parent | 5bcd206b9270d9a79e212f91723ea1a08a4d4859 (diff) | |
download | audiomanager-c7ef1ef0126023ca8a377f00ae9aebb81bc34d37.tar.gz |
Added overflow handling for timer and poll handles
Signed-off-by: Christian Linke <Christian.Linke@bmw.de>
-rw-r--r-- | AudioManagerUtilities/include/CAmSocketHandler.h | 7 | ||||
-rw-r--r-- | AudioManagerUtilities/src/CAmSocketHandler.cpp | 44 |
2 files changed, 47 insertions, 4 deletions
diff --git a/AudioManagerUtilities/include/CAmSocketHandler.h b/AudioManagerUtilities/include/CAmSocketHandler.h index 1a988c5..edf19fd 100644 --- a/AudioManagerUtilities/include/CAmSocketHandler.h +++ b/AudioManagerUtilities/include/CAmSocketHandler.h @@ -23,6 +23,7 @@ #include <sys/poll.h> #include <list> #include <map> +#include <set> #include <signal.h> #include <iostream> //todo: remove me @@ -30,6 +31,8 @@ namespace am { #define MAX_NS 1000000000L +#define MAX_TIMERHANDLE INT16_MAX +#define MAX_POLLHANDLE INT16_MAX typedef uint16_t sh_timerHandle_t; //!<this is a handle for a timer to be used with the SocketHandler typedef uint16_t sh_pollHandle_t; //!<this is a handle for a filedescriptor to be used with the SocketHandler @@ -178,7 +181,7 @@ private: int mDispatchDone; //this starts / stops the mainloop struct sh_timer_s //!<struct that holds information of timers { - sh_timerHandle_t handle; //!<the handle of the timer + sh_timerHandle_t handle; //!<the handle of the timer timespec countdown; //!<the countdown, this value is decreased every time the timer is up IAmShTimerCallBack* callback; //!<the callbackfunction void * userData; //!<saves a void pointer together with the rest. @@ -433,7 +436,9 @@ private: }; mListPollfd_t mfdPollingArray; //!<the polling array for ppoll + std::set<sh_pollHandle_t> mSetPollKeys; //!A set of all used ppoll keys mListPoll_t mListPoll; //!<list that holds all information for the ppoll + std::set<sh_timerHandle_t> mSetTimerKeys; //!A set of all used timer keys std::list<sh_timer_s> mListTimer; //!<list of all timers std::list<sh_timer_s> mListActiveTimer; //!<list of all currently active timers sh_timerHandle_t mLastInsertedHandle; //!<keeps track of the last inserted timer handle diff --git a/AudioManagerUtilities/src/CAmSocketHandler.cpp b/AudioManagerUtilities/src/CAmSocketHandler.cpp index 8947a5a..233af6a 100644 --- a/AudioManagerUtilities/src/CAmSocketHandler.cpp +++ b/AudioManagerUtilities/src/CAmSocketHandler.cpp @@ -194,10 +194,29 @@ am_Error_e CAmSocketHandler::addFDPoll(const int fd, const short event, IAmShPol { if (!fdIsValid(fd)) return (E_NON_EXISTENT); + + //create a new handle for the poll + sh_pollHandle_t lastHandle(mLastInsertedPollHandle); + do + { + ++mLastInsertedPollHandle; + if (mLastInsertedPollHandle == MAX_POLLHANDLE) + { + mLastInsertedPollHandle = 0; + } + if (mLastInsertedPollHandle==lastHandle) + { + logError(__PRETTY_FUNCTION__,"Could not create new polls, too many open!"); + return (am_Error_e::E_NOT_POSSIBLE); + } + + } while (mSetPollKeys.find(mLastInsertedPollHandle) != mSetPollKeys.end()); + + mSetPollKeys.insert(mLastInsertedPollHandle); sh_poll_s pollData; pollData.pollfdValue.fd = fd; - pollData.handle = ++mLastInsertedPollHandle; + pollData.handle = mLastInsertedPollHandle; pollData.pollfdValue.events = event; pollData.pollfdValue.revents = 0; pollData.userData = userData; @@ -229,6 +248,7 @@ am_Error_e CAmSocketHandler::removeFDPoll(const sh_pollHandle_t handle) if (iterator->handle == handle) { iterator = mListPoll.erase(iterator); + mSetPollKeys.erase(handle); mRecreatePollfds = true; return (E_OK); } @@ -255,8 +275,25 @@ am_Error_e CAmSocketHandler::addTimer(const timespec timeouts, IAmShTimerCallBac sh_timer_s timerItem; //create a new handle for the timer - handle = ++mLastInsertedHandle; //todo: overflow ruling !o - timerItem.handle = handle; + sh_timerHandle_t lastTimerHandle(mLastInsertedHandle); + do + { + ++mLastInsertedHandle; + if (mLastInsertedHandle == MAX_TIMERHANDLE) + { + mLastInsertedHandle = 0; + } + if (lastTimerHandle==mLastInsertedHandle) + { + logError(__PRETTY_FUNCTION__,"Could not create new timers, too many open!"); + return (am_Error_e::E_NOT_POSSIBLE); + } + + } while (mSetTimerKeys.find(mLastInsertedHandle) != mSetTimerKeys.end()); + + mSetTimerKeys.insert(mLastInsertedHandle); + + timerItem.handle = mLastInsertedHandle; timerItem.countdown = timeouts; timerItem.callback = callback; timerItem.userData = userData; @@ -292,6 +329,7 @@ am_Error_e CAmSocketHandler::removeTimer(const sh_timerHandle_t handle) if (it->handle == handle) { it = mListTimer.erase(it); + mSetTimerKeys.erase(handle); return (E_OK); } } |