4 * Created on: Dec 18, 2011
8 #ifndef SOCKETHANDLER_H_
9 #define SOCKETHANDLER_H_
11 #include <audiomanagertypes.h>
12 #include <sys/socket.h>
21 class TBasicPollCallback;
22 class TBasicTimerCallback;
27 typedef uint16_t timerHandle_t; //!<this is a handle for a timer to be used with the SocketHandler
29 virtual ~SocketHandler();
31 am_Error_e addFDPoll(const int fd,const short event,TBasicPollCallback*& callback);
32 am_Error_e removeFDPoll(const int fd);
33 am_Error_e addTimer(const timespec timeouts,TBasicTimerCallback*& callback,timerHandle_t& handle);
34 am_Error_e removeTimer(const timerHandle_t handle);
35 void start_listenting();
36 void stop_listening();
39 struct timer_s //!<struct that holds information of timers
41 timerHandle_t handle; //!<the handle of the timer
42 timespec countdown; //!<the countdown, this value is decreased every time the timer is up
43 timespec timeout; //!<the original timer value
44 TBasicTimerCallback* callback; //!<the callbackfunction
47 class SubstractTime //!<functor to easy substract from each countdown value
52 SubstractTime(timespec param): param(param) {}
53 void operator()(timer_s& t) const;
56 typedef std::vector<pollfd> mPollfd_t; //!<vector of filedescriptors
57 typedef std::map<int,TBasicPollCallback*> mMapFdCallback_t; //!<map for the callbacks
59 bool fdIsValid(const int fd) const;
62 int timespec2ms(const timespec& time);
63 static bool compareCountdown(const timer_s& a, const timer_s& b)
65 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);
69 mMapFdCallback_t mMapFdCallback;
70 std::list<timer_s> mListTimer;
71 mPollfd_t mListPollfd;
72 timerHandle_t mNextTimer;
73 timerHandle_t mLastInsertedHandle;
79 * classic functor for the BasiCallCallback
81 class TBasicPollCallback
84 virtual void Call (int fd, const short revent)=0;
85 virtual ~TBasicPollCallback(){};
89 * classic functor for the BasicTimerCallback
91 class TBasicTimerCallback
94 virtual void Call (SocketHandler::timerHandle_t handle)=0;
95 virtual ~TBasicTimerCallback(){};
100 * template to create the functor for a class
102 template <class TClass> class TSpecificPollCallback : public TBasicPollCallback
106 void (TClass::*mFunction)(int fd, const short revent);
109 TSpecificPollCallback(TClass* instance, void(TClass::*function)(int fd, const short revent))
110 :mInstance(instance), mFunction(function){};
112 virtual void Call(int fd, const short revent)
114 (*mInstance.*mFunction)(fd,revent);
119 * template to create the functor for a class
121 template <class TClass> class TSpecificTimerCallback : public TBasicTimerCallback
125 void (TClass::*mFunction)(SocketHandler::timerHandle_t handle);
128 TSpecificTimerCallback(TClass* instance, void(TClass::*function)(SocketHandler::timerHandle_t handle))
129 :mInstance(instance), mFunction(function){};
131 virtual void Call(SocketHandler::timerHandle_t handle)
133 (*mInstance.*mFunction)(handle);
137 #endif /* SOCKETHANDLER_H_ */