2 * Copyright (C) 2011, BMW AG
4 * GeniviAudioMananger AudioManagerDaemon
6 * \file SocketHandler.h
8 * \date 20-Oct-2011 3:42:04 PM
9 * \author Christian Mueller (christian.ei.mueller@bmw.de)
12 * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13 * Copyright (C) 2011, BMW AG Christian Mueller Christian.ei.mueller@bmw.de
15 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17 * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18 * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19 * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20 * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21 * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
25 #ifndef SOCKETHANDLER_H_
26 #define SOCKETHANDLER_H_
28 #include <audiomanagertypes.h>
29 #include <sys/socket.h>
39 static volatile sig_atomic_t gDispatchDone = 0; //this global is used to stop the mainloop
41 typedef uint16_t sh_timerHandle_t; //!<this is a handle for a timer to be used with the SocketHandler
42 typedef uint16_t sh_pollHandle_t; //!<this is a handle for a filedescriptor to be used with the SocketHandler
48 class shTimerCallBack;
54 virtual ~SocketHandler();
56 am_Error_e addFDPoll(const int fd,const short event, shPollPrepare *prepare,shPollFired *fired,shPollCheck *check,shPollDispatch *dispatch, void* userData,sh_pollHandle_t& handle);
57 am_Error_e removeFDPoll(const sh_pollHandle_t handle);
58 am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events);
59 am_Error_e addTimer(const timespec timeouts,shTimerCallBack*& callback,sh_timerHandle_t& handle, void* userData);
60 am_Error_e removeTimer(const sh_timerHandle_t handle);
61 am_Error_e restartTimer(const sh_timerHandle_t handle, const timespec timeouts);
62 am_Error_e stopTimer(const sh_timerHandle_t handle);
63 void start_listenting();
64 void stop_listening();
66 struct timer_s //!<struct that holds information of timers
68 sh_timerHandle_t handle; //!<the handle of the timer
69 timespec countdown; //!<the countdown, this value is decreased every time the timer is up
70 timespec timeout; //!<the original timer value
71 shTimerCallBack* callback; //!<the callbackfunction
72 void * userData; //!<saves a void pointer together with the rest.
75 class SubstractTime //!<functor to easy substract from each countdown value
80 SubstractTime(timespec param): param(param) {}
81 void operator()(timer_s& t) const;
84 struct sh_poll_s //!<struct that holds information about polls
86 sh_pollHandle_t handle; //!<handle to uniquely adress a filedesriptor
87 shPollPrepare *prepareCB;
90 shPollDispatch *dispatchCB;
91 pollfd pollfdValue; //!<the array for polling the filedescriptors
92 void *userData; //!<userdata saved together with the callback.
95 typedef std::vector<pollfd> mPollfd_t; //!<vector of filedescriptors
96 typedef std::vector<sh_poll_s> mListPoll_t; //!<list for the callbacks
103 CopyPollfd(mPollfd_t& dest): mArray(dest) {}
104 void operator()(const sh_poll_s& row);
107 bool fdIsValid(const int fd) const;
110 int timespec2ms(const timespec& time);
111 static bool compareCountdown(const timer_s& a, const timer_s& b)
113 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);
116 static bool onlyFiredEvents(const pollfd& a)
118 return a.revents==0 ? false : true;
121 //todo: maybe we could simplify mListActiveTimer to hold only the handle and the countdown ....
122 mPollfd_t mfdPollingArray;
123 mListPoll_t mListPoll;
124 std::list<timer_s> mListTimer; //!<list of all timers
125 std::list<timer_s> mListActiveTimer; //!<list of all currently active timers
126 sh_timerHandle_t mNextTimer;
127 sh_timerHandle_t mLastInsertedHandle;
128 sh_pollHandle_t mLastInsertedPollHandle;
130 timespec *mTimeoutPointer; //we need this because for infinite loops we need to set this to NULL mTimeout -1 is not working with ppoll
131 bool mRecreatePollfds;
135 * classic functor for the BasicTimerCallback
137 class shTimerCallBack
140 virtual void Call (const sh_timerHandle_t handle, void* userData)=0;
141 virtual ~shTimerCallBack(){};
147 virtual void Call (const sh_pollHandle_t handle, void* userData)=0;
148 virtual ~shPollPrepare(){};
154 virtual void Call(const pollfd pollfd,const sh_pollHandle_t handle, void* userData)=0;
155 virtual ~ shPollFired(){};
161 virtual bool Call (const sh_pollHandle_t handle, void* userData)=0;
162 virtual ~ shPollCheck(){};
168 virtual bool Call (const sh_pollHandle_t handle, void* userData)=0;
169 virtual ~ shPollDispatch(){};
173 * template to create the functor for a class
175 template <class TClass> class shTimerCallBack_T : public shTimerCallBack
179 void (TClass::*mFunction)(sh_timerHandle_t handle, void* userData);
182 shTimerCallBack_T(TClass* instance, void(TClass::*function)(sh_timerHandle_t handle, void* userData))
183 :mInstance(instance), mFunction(function){};
185 virtual void Call(sh_timerHandle_t handle, void* userData)
187 (*mInstance.*mFunction)(handle, userData);
192 * template to create the functor for a class
194 template <class TClass> class shPollPrepare_T : public shPollPrepare
198 void (TClass::*mFunction)(const sh_timerHandle_t handle, void* userData);
201 shPollPrepare_T(TClass* instance, void(TClass::*function)(const sh_timerHandle_t handle, void* userData))
202 :mInstance(instance), mFunction(function){};
204 virtual void Call(const sh_timerHandle_t handle, void* userData)
206 (*mInstance.*mFunction)(handle,userData);
210 template <class TClass> class shPollFired_T : public shPollFired
214 void (TClass::*mFunction)(const pollfd pollfd,const sh_pollHandle_t handle, void* userData);
217 shPollFired_T(TClass* instance, void(TClass::*function)(const pollfd pollfd,const sh_pollHandle_t handle, void* userData))
218 :mInstance(instance), mFunction(function){};
220 virtual void Call(const pollfd pollfd,const sh_pollHandle_t handle, void* userData)
222 (*mInstance.*mFunction)(pollfd,handle,userData);
226 template <class TClass> class shPollCheck_T : public shPollCheck
230 bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
233 shPollCheck_T(TClass* instance, bool(TClass::*function)(const sh_pollHandle_t handle, void* userData))
234 :mInstance(instance), mFunction(function){};
236 virtual bool Call(const sh_pollHandle_t handle, void* userData)
238 return (*mInstance.*mFunction)(handle,userData);
242 template <class TClass> class shPollDispatch_T : public shPollDispatch
246 bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
249 shPollDispatch_T(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData))
250 :mInstance(instance), mFunction(function){};
252 virtual bool Call(const sh_pollHandle_t handle, void* userData)
254 return (*mInstance.*mFunction)(handle,userData);
258 #endif /* SOCKETHANDLER_H_ */