1 /** Copyright (c) 2012 GENIVI Alliance
2 * Copyright (c) 2012 BMW
4 * \author Christian Mueller, BMW
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
12 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
13 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 #ifndef SOCKETHANDLER_H_
18 #define SOCKETHANDLER_H_
20 #include "audiomanagertypes.h"
21 #include <sys/socket.h>
32 static volatile sig_atomic_t gDispatchDone = 0; //this global is used to stop the mainloop
34 typedef uint16_t sh_timerHandle_t; //!<this is a handle for a timer to be used with the SocketHandler
35 typedef uint16_t sh_pollHandle_t; //!<this is a handle for a filedescriptor to be used with the SocketHandler
37 class CAmShPollPrepare;
40 class CAmShPollDispatch;
41 class CAmShTimerCallBack;
43 class CAmSocketHandler
47 virtual ~CAmSocketHandler();
49 am_Error_e addFDPoll(const int fd, const short event, CAmShPollPrepare *prepare, CAmShPollFired *fired, CAmShPollCheck *check, CAmShPollDispatch *dispatch, void* userData, sh_pollHandle_t& handle);
50 am_Error_e removeFDPoll(const sh_pollHandle_t handle);
51 am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events);
52 am_Error_e addTimer(const timespec timeouts, CAmShTimerCallBack*& callback, sh_timerHandle_t& handle, void* userData);
53 am_Error_e removeTimer(const sh_timerHandle_t handle);
54 am_Error_e restartTimer(const sh_timerHandle_t handle, const timespec timeouts);
55 am_Error_e stopTimer(const sh_timerHandle_t handle);
56 void start_listenting();
57 void stop_listening();
59 struct sh_timer_s //!<struct that holds information of timers
61 sh_timerHandle_t handle; //!<the handle of the timer
62 timespec countdown; //!<the countdown, this value is decreased every time the timer is up
63 timespec timeout; //!<the original timer value
64 CAmShTimerCallBack* callback; //!<the callbackfunction
65 void * userData; //!<saves a void pointer together with the rest.
68 class CAmShSubstractTime //!<functor to easy substract from each countdown value
73 CAmShSubstractTime(timespec param) :
75 void operator()(sh_timer_s& t) const;
78 struct sh_poll_s //!<struct that holds information about polls
80 sh_pollHandle_t handle; //!<handle to uniquely adress a filedesriptor
81 CAmShPollPrepare *prepareCB;
82 CAmShPollFired *firedCB;
83 CAmShPollCheck *checkCB;
84 CAmShPollDispatch *dispatchCB;
85 pollfd pollfdValue; //!<the array for polling the filedescriptors
86 void *userData; //!<userdata saved together with the callback.
89 typedef std::vector<pollfd> mListPollfd_t; //!<vector of filedescriptors
90 typedef std::vector<sh_poll_s> mListPoll_t; //!<list for the callbacks
95 mListPollfd_t& mArray;
97 CAmShCopyPollfd(mListPollfd_t& dest) :
99 void operator()(const sh_poll_s& row);
102 bool fdIsValid(const int fd) const;
105 int timespec2ms(const timespec& time);
106 timespec* insertTime(timespec& buffertime);
107 static bool compareCountdown(const sh_timer_s& a, const sh_timer_s& b)
109 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);
112 static bool onlyFiredEvents(const pollfd& a)
114 return a.revents == 0 ? false : true;
117 //todo: maybe we could simplify mListActiveTimer to hold only the handle and the countdown ....
118 mListPollfd_t mfdPollingArray;
119 mListPoll_t mListPoll;
120 std::list<sh_timer_s> mListTimer; //!<list of all timers
121 std::list<sh_timer_s> mListActiveTimer; //!<list of all currently active timers
122 sh_timerHandle_t mNextTimer;
123 sh_timerHandle_t mLastInsertedHandle;
124 sh_pollHandle_t mLastInsertedPollHandle;
125 bool mRecreatePollfds;
130 * classic functor for the BasicTimerCallback
132 class CAmShTimerCallBack
135 virtual void Call(const sh_timerHandle_t handle, void* userData)=0;
136 virtual ~CAmShTimerCallBack(){};
139 class CAmShPollPrepare
142 virtual void Call(const sh_pollHandle_t handle, void* userData)=0;
143 virtual ~CAmShPollPrepare(){};
149 virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)=0;
150 virtual ~ CAmShPollFired(){};
156 virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
157 virtual ~ CAmShPollCheck(){};
160 class CAmShPollDispatch
163 virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
164 virtual ~ CAmShPollDispatch() {};
168 * template to create the functor for a class
170 template<class TClass> class TAmShTimerCallBack: public CAmShTimerCallBack
174 void (TClass::*mFunction)(sh_timerHandle_t handle, void* userData);
177 TAmShTimerCallBack(TClass* instance, void(TClass::*function)(sh_timerHandle_t handle, void* userData)) :
178 mInstance(instance),//
179 mFunction(function) {};
181 virtual void Call(sh_timerHandle_t handle, void* userData)
183 (*mInstance.*mFunction)(handle, userData);
188 * template to create the functor for a class
190 template<class TClass> class TAmShPollPrepare: public CAmShPollPrepare
194 void (TClass::*mFunction)(const sh_timerHandle_t handle, void* userData);
197 TAmShPollPrepare(TClass* instance, void(TClass::*function)(const sh_timerHandle_t handle, void* userData)) :
198 mInstance(instance), //
199 mFunction(function){};
201 virtual void Call(const sh_timerHandle_t handle, void* userData)
203 (*mInstance.*mFunction)(handle, userData);
207 template<class TClass> class TAmShPollFired: public CAmShPollFired
211 void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
214 TAmShPollFired(TClass* instance, void(TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) :
215 mInstance(instance), //
216 mFunction(function){};
218 virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
220 (*mInstance.*mFunction)(pollfd, handle, userData);
224 template<class TClass> class TAmShPollCheck: public CAmShPollCheck
228 bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
231 TAmShPollCheck(TClass* instance, bool(TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
232 mInstance(instance), //
233 mFunction(function){};
235 virtual bool Call(const sh_pollHandle_t handle, void* userData)
237 return (*mInstance.*mFunction)(handle, userData);
241 template<class TClass> class TAmShPollDispatch: public CAmShPollDispatch
245 bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
248 TAmShPollDispatch(TClass* instance, bool(TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
249 mInstance(instance), //
250 mFunction(function) {};
252 virtual bool Call(const sh_pollHandle_t handle, void* userData)
254 return (*mInstance.*mFunction)(handle, userData);
258 #endif /* SOCKETHANDLER_H_ */