diff options
author | christian mueller <christian.ei.mueller@bmw.de> | 2012-03-23 13:43:37 +0100 |
---|---|---|
committer | christian mueller <christian.ei.mueller@bmw.de> | 2012-03-23 13:43:37 +0100 |
commit | f2f42846720bcb6278e30d9234e911152507de8b (patch) | |
tree | e53337cdc001e6eabff7484f5dc537d4521eac13 /include/shared/CAmSocketHandler.h | |
parent | debbef650e72298d5c9545e02b1266899d4ad832 (diff) | |
download | audiomanager-f2f42846720bcb6278e30d9234e911152507de8b.tar.gz |
* [GAM-46] improvements on CAmSockthandler.
* Needed adoptions on CAmSockethandler tests
* Small bug in CAmSerializer
Signed-off-by: christian mueller <christian.ei.mueller@bmw.de>
Diffstat (limited to 'include/shared/CAmSocketHandler.h')
-rw-r--r-- | include/shared/CAmSocketHandler.h | 293 |
1 files changed, 218 insertions, 75 deletions
diff --git a/include/shared/CAmSocketHandler.h b/include/shared/CAmSocketHandler.h index 9c3b6e7..83d2352 100644 --- a/include/shared/CAmSocketHandler.h +++ b/include/shared/CAmSocketHandler.h @@ -21,7 +21,6 @@ #include "audiomanagertypes.h" #include <sys/socket.h> -#include <sys/time.h> #include <stdint.h> #include <sys/poll.h> #include <list> @@ -36,11 +35,55 @@ static volatile sig_atomic_t gDispatchDone = 0; //this global is used to stop th 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 -class IAmShPollPrepare; -class IAmShPollCheck; -class IAmShPollFired; -class IAmShPollDispatch; -class IAmShTimerCallBack; +/** + * prototype for poll prepared callback + */ +class IAmShPollPrepare +{ +public: + virtual void Call(const sh_pollHandle_t handle, void* userData)=0; + virtual ~IAmShPollPrepare(){}; +}; + +/** + * prototype for poll fired callback + */ +class IAmShPollFired +{ +public: + virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)=0; + virtual ~ IAmShPollFired(){}; +}; + +/** + * prototype for poll check callback + */ +class IAmShPollCheck +{ +public: + virtual bool Call(const sh_pollHandle_t handle, void* userData)=0; + virtual ~ IAmShPollCheck(){}; +}; + +/** + * prototype for dispatch callback + */ +class IAmShPollDispatch +{ +public: + virtual bool Call(const sh_pollHandle_t handle, void* userData)=0; + virtual ~ IAmShPollDispatch(){}; +}; + +/** + * prototype for the timer callback + */ +class IAmShTimerCallBack +{ +public: + virtual void Call(const sh_timerHandle_t handle, void* userData)=0; + virtual ~IAmShTimerCallBack(){}; +}; /** * The am::CAmSocketHandler implements a mainloop for the AudioManager. Plugins and different parts of the AudioManager add their filedescriptors to the handler @@ -72,16 +115,16 @@ private: void * userData; //!<saves a void pointer together with the rest. }; + typedef std::reverse_iterator<sh_timer_s> rListTimerIter; + class CAmShSubstractTime //!<functor to easy substract from each countdown value { private: timespec param; public: CAmShSubstractTime(timespec param) : - param(param) - { - } - void operator()(sh_timer_s& t) const; + param(param){} + inline void operator()(sh_timer_s& t) const; }; struct sh_poll_s //!<struct that holds information about polls @@ -104,88 +147,192 @@ private: mListPollfd_t& mArray; public: CAmShCopyPollfd(mListPollfd_t& dest) : - mArray(dest) + mArray(dest){} + void operator()(const sh_poll_s& row) + { + pollfd temp = row.pollfdValue; + temp.revents = 0; + mArray.push_back(temp); + } + }; + + class CAmShMatchingFd + { + private: + int mFD, mEventFlag; + public: + CAmShMatchingFd(int fd, int eventFlag) : + mFD(fd), // + mEventFlag(eventFlag) + { } + bool operator()(const sh_poll_s& a) + { + return (((a.pollfdValue.fd == mFD) && (a.pollfdValue.events == mEventFlag)) ? true : false); + } + }; + + class CAmShCallFire + { + public: + CAmShCallFire(){}; + void operator()(sh_poll_s& row) + { + row.firedCB->Call(row.pollfdValue, row.handle, row.userData); + } + }; + + class CAmShCallPrep + { + public: + CAmShCallPrep(){}; + void operator()(sh_poll_s& row) + { + if (row.prepareCB) + row.prepareCB->Call(row.handle, row.userData); + } + }; + + class CAmShCallTimer + { + public: + CAmShCallTimer(){}; + void operator()(sh_timer_s& row) + { + row.callback->Call(row.handle, row.userData); + } + }; + + class CAmShCountdownUp + { + public: + CAmShCountdownUp(){}; + bool operator()(sh_timer_s& row) + { + if (row.countdown.tv_nsec == 0 && row.countdown.tv_sec == 0) + return (true); + return (false); + } + }; + + class CAmShCountdownSame + { + private: + sh_timer_s mCompareValue; + public: + CAmShCountdownSame(sh_timer_s& a) : + mCompareValue(a) + {} + bool operator()(const sh_timer_s& b) { + return ((mCompareValue.countdown.tv_sec == b.countdown.tv_sec) && (mCompareValue.countdown.tv_nsec == b.countdown.tv_nsec) ? true : false); } - void operator()(const sh_poll_s& row); }; bool fdIsValid(const int fd) const; void initTimer(); void timerUp(); + void timerCorrection(); int timespec2ms(const timespec& time); timespec* insertTime(timespec& buffertime); + static bool compareCountdown(const sh_timer_s& a, const sh_timer_s& b) { return ((a.countdown.tv_sec == b.countdown.tv_sec) ? (a.countdown.tv_nsec < b.countdown.tv_nsec) : (a.countdown.tv_sec < b.countdown.tv_sec)); } - static bool onlyFiredEvents(const pollfd& a) + /** + * Subtracts b from a + * @param a + * @param b + * @return subtracted value + */ + static timespec timespecSub(const timespec& a, const timespec& b) + { + timespec result; + + if ((a.tv_sec < b.tv_sec) || ((a.tv_sec == b.tv_sec) && (a.tv_nsec <= b.tv_nsec))) + { + result.tv_sec = result.tv_nsec = 0; + } + else + { + result.tv_sec = a.tv_sec - b.tv_sec; + if (a.tv_nsec < b.tv_nsec) + { + result.tv_nsec = a.tv_nsec + 1000000000L - b.tv_nsec; + result.tv_sec--; /* Borrow a second. */ + } + else + { + result.tv_nsec = a.tv_nsec - b.tv_nsec; + } + } + return (result); + } + + static timespec timespecAdd(const timespec& a, const timespec b) + { + timespec result; + result.tv_sec = a.tv_sec + b.tv_sec; + result.tv_nsec = a.tv_nsec + b.tv_nsec; + if (result.tv_nsec >= 1000000000L) + { + result.tv_sec++; + result.tv_nsec = result.tv_nsec - 1000000000L; + } + return (result); + } + + static int timespecCompare(const timespec& a, const timespec& b) + { + //less + if (a.tv_sec < b.tv_sec) + return (-1); + //greater + else if (a.tv_sec > b.tv_sec) + return (1); + //less + else if (a.tv_nsec < b.tv_nsec) + return (-1); + //greater + else if (a.tv_nsec > b.tv_nsec) + return (1); + //equal + else + return (0); + } + + inline static bool eventFired(const pollfd& a) { return (a.revents == 0 ? false : true); } - //todo: maybe we could simplify mListActiveTimer to hold only the handle and the countdown .... + inline static bool noDispatching(const sh_poll_s& a) + { + //remove from list of there is no checkCB + if (!a.checkCB) + return (true); + return (!a.checkCB->Call(a.handle, a.userData)); + } + + inline static bool dispatchingFinished(const sh_poll_s& a) + { + //remove from list of there is no dispatchCB + if (!a.dispatchCB) + return (true); + return (!a.dispatchCB->Call(a.handle, a.userData)); + } + mListPollfd_t mfdPollingArray; mListPoll_t mListPoll; 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 mNextTimer; sh_timerHandle_t mLastInsertedHandle; sh_pollHandle_t mLastInsertedPollHandle; bool mRecreatePollfds; - timespec mTimeout; -}; - -/** - * prototype for the timer callback - */ -class IAmShTimerCallBack -{ -public: - virtual void Call(const sh_timerHandle_t handle, void* userData)=0; - virtual ~IAmShTimerCallBack(){}; -}; - -/** - * prototype for poll prepared callback - */ -class IAmShPollPrepare -{ -public: - virtual void Call(const sh_pollHandle_t handle, void* userData)=0; - virtual ~IAmShPollPrepare(){}; -}; - -/** - * prototype for poll fired callback - */ -class IAmShPollFired -{ -public: - virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)=0; - virtual ~ IAmShPollFired(){}; -}; - -/** - * prototype for poll check callback - */ -class IAmShPollCheck -{ -public: - virtual bool Call(const sh_pollHandle_t handle, void* userData)=0; - virtual ~ IAmShPollCheck(){}; -}; - -/** - * prototype for dispatch callback - */ -class IAmShPollDispatch -{ -public: - virtual bool Call(const sh_pollHandle_t handle, void* userData)=0; - virtual ~ IAmShPollDispatch(){}; -}; + timespec mStartTime; +} +; /** * template to create the functor for a class @@ -224,8 +371,7 @@ public: virtual void Call(const sh_timerHandle_t handle, void* userData) { (*mInstance.*mFunction)(handle, userData); - } - ; + }; }; /** @@ -245,8 +391,7 @@ public: virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData) { (*mInstance.*mFunction)(pollfd, handle, userData); - } - ; + }; }; /** @@ -266,8 +411,7 @@ public: virtual bool Call(const sh_pollHandle_t handle, void* userData) { return ((*mInstance.*mFunction)(handle, userData)); - } - ; + }; }; /** @@ -287,8 +431,7 @@ public: virtual bool Call(const sh_pollHandle_t handle, void* userData) { return ((*mInstance.*mFunction)(handle, userData)); - } - ; + }; }; } /* namespace am */ #endif /* SOCKETHANDLER_H_ */ |