18 #ifndef SOCKETHANDLER_H_ 19 #define SOCKETHANDLER_H_ 21 #include <sys/socket.h> 30 #include <sys/signalfd.h> 31 #include <audiomanagerconfig.h> 47 #define MAX_NS 1000000000L 48 #define MAX_TIMERHANDLE UINT16_MAX 49 #define MAX_POLLHANDLE UINT16_MAX 60 virtual void Call(
const sh_pollHandle_t handle,
void* userData) = 0;
70 virtual void Call(
const pollfd pollfd,
const sh_pollHandle_t handle,
void* userData) = 0;
80 virtual bool Call(
const sh_pollHandle_t handle,
void* userData) = 0;
90 virtual bool Call(
const sh_pollHandle_t handle,
void* userData) = 0;
101 virtual void Call(
const sh_timerHandle_t handle,
void* userData) = 0;
112 void (TClass::*mFunction)(
const pollfd pollfd,
const sh_pollHandle_t handle,
void* userData);
115 TAmShPollFired(TClass* instance,
void (TClass::*
function)(
const pollfd pollfd,
const sh_pollHandle_t handle,
void* userData)) :
120 virtual void Call(
const pollfd pollfd,
const sh_pollHandle_t handle,
void* userData)
122 (*mInstance.*mFunction)(pollfd, handle, userData);
133 bool (TClass::*mFunction)(
const sh_pollHandle_t handle,
void* userData);
136 TAmShPollCheck(TClass* instance,
bool (TClass::*
function)(
const sh_pollHandle_t handle,
void* userData)) :
141 virtual bool Call(
const sh_pollHandle_t handle,
void* userData)
143 return ((*mInstance.*mFunction)(handle, userData));
154 bool (TClass::*mFunction)(
const sh_pollHandle_t handle,
void* userData);
157 TAmShPollDispatch(TClass* instance,
bool (TClass::*
function)(
const sh_pollHandle_t handle,
void* userData)) :
162 virtual bool Call(
const sh_pollHandle_t handle,
void* userData)
164 return ((*mInstance.*mFunction)(handle, userData));
175 void (TClass::*mFunction)(sh_timerHandle_t handle,
void* userData);
178 TAmShTimerCallBack(TClass* instance,
void (TClass::*
function)(sh_timerHandle_t handle,
void* userData)) :
183 virtual void Call(sh_timerHandle_t handle,
void* userData)
185 (*mInstance.*mFunction)(handle, userData);
197 void (TClass::*mFunction)(
const sh_pollHandle_t handle,
void* userData);
200 TAmShPollPrepare(TClass* instance,
void (TClass::*
function)(
const sh_pollHandle_t handle,
void* userData)) :
205 virtual void Call(
const sh_pollHandle_t handle,
void* userData)
207 (*mInstance.*mFunction)(handle, userData);
220 sh_pollHandle_t handle;
222 std::function<void(const sh_pollHandle_t handle, void* userData)> prepareCB;
223 std::function<void(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)> firedCB;
224 std::function<bool(const sh_pollHandle_t handle, void* userData)> checkCB;
225 std::function<bool(const sh_pollHandle_t handle, void* userData)> dispatchCB;
229 handle(0), pollfdValue(), prepareCB(), firedCB(), checkCB(), dispatchCB(), userData(0)
235 sh_timerHandle_t handle;
238 itimerspec countdown;
242 std::function<void(const sh_timerHandle_t handle, void* userData)> callback;
249 , countdown(), callback(), userData(0)
255 sh_pollHandle_t handle;
256 std::function<void(const sh_pollHandle_t handle, const signalfd_siginfo & info, void* userData)> callback;
259 handle(0), callback(), userData(0)
263 struct sh_identifier_s
265 std::set<sh_pollHandle_t> pollHandles;
268 sh_identifier_s(
const uint16_t pollLimit = UINT16_MAX) :
269 pollHandles(), limit(pollLimit), lastUsedID(0)
273 typedef std::reverse_iterator<sh_timer_s> rListTimerIter;
274 typedef std::vector<pollfd> VectorListPollfd_t;
275 typedef std::vector<sh_poll_s> VectorListPoll_t;
276 typedef std::vector<sh_signal_s> VectorSignalHandlers_t;
285 typedef uint8_t internal_codes_t;
290 sh_identifier_s mSetPollKeys;
291 VectorListPoll_t mListPoll;
292 sh_identifier_s mSetTimerKeys;
293 std::list<sh_timer_s> mListTimer;
294 std::list<sh_timer_s> mListActiveTimer;
295 sh_identifier_s mSetSignalhandlerKeys;
296 VectorSignalHandlers_t mSignalHandlers;
297 bool mRecreatePollfds;
298 internal_codes_t mInternalCodes;
299 sh_pollHandle_t mSignalFdHandle;
304 bool fdIsValid(
const int fd)
const;
306 timespec* insertTime(timespec& buffertime);
308 am_Error_e createTimeFD(
const itimerspec & timeouts,
int & fd);
312 void timerCorrection();
320 inline static bool compareCountdown(
const sh_timer_s& a,
const sh_timer_s& b)
322 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));
331 inline static timespec timespecSub(
const timespec& a,
const timespec& b)
335 if ((a.tv_sec < b.tv_sec) || ((a.tv_sec == b.tv_sec) && (a.tv_nsec <= b.tv_nsec)))
337 result.tv_sec = result.tv_nsec = 0;
341 result.tv_sec = a.tv_sec - b.tv_sec;
342 if (a.tv_nsec < b.tv_nsec)
344 result.tv_nsec = a.tv_nsec +
MAX_NS - b.tv_nsec;
349 result.tv_nsec = a.tv_nsec - b.tv_nsec;
361 inline timespec timespecAdd(
const timespec& a,
const timespec& b)
364 result.tv_sec = a.tv_sec + b.tv_sec;
365 result.tv_nsec = a.tv_nsec + b.tv_nsec;
366 if (result.tv_nsec >=
MAX_NS)
369 result.tv_nsec = result.tv_nsec -
MAX_NS;
380 inline int timespecCompare(
const timespec& a,
const timespec& b)
383 if (a.tv_sec < b.tv_sec)
386 else if (a.tv_sec > b.tv_sec)
389 else if (a.tv_nsec < b.tv_nsec)
392 else if (a.tv_nsec > b.tv_nsec)
404 inline static void prepare(sh_poll_s& row);
411 inline static void fire(sh_poll_s& a);
418 inline static bool eventFired(
const pollfd& a);
425 inline static bool noDispatching(
const sh_poll_s& a);
432 inline static bool dispatchingFinished(
const sh_poll_s& a);
439 inline static void callTimer(sh_timer_s& a);
446 bool nextHandle(sh_identifier_s & handle);
448 am_Error_e getFDPollData(
const sh_pollHandle_t handle, sh_poll_s & outPollData);
458 am_Error_e listenToSignals(
const std::vector<uint8_t> & listSignals);
460 am_Error_e addFDPoll(
const int fd,
const short event, std::function<
void(
const sh_pollHandle_t handle,
void* userData)> prepare, std::function<
void(
const pollfd pollfd,
const sh_pollHandle_t handle,
void* userData)> fired,
461 std::function<
bool(
const sh_pollHandle_t handle,
void* userData)> check, std::function<
bool(
const sh_pollHandle_t handle,
void* userData)> dispatch,
void* userData, sh_pollHandle_t& handle);
464 am_Error_e removeFDPoll(
const sh_pollHandle_t handle);
465 am_Error_e updateEventFlags(
const sh_pollHandle_t handle,
const short events);
466 am_Error_e addSignalHandler(std::function<
void(
const sh_pollHandle_t handle,
const signalfd_siginfo & info,
void* userData)> callback, sh_pollHandle_t& handle,
void * userData);
467 am_Error_e removeSignalHandler(
const sh_pollHandle_t handle);
470 const bool __attribute__((__unused__)) repeats =
false 472 const bool repeats =
false 475 am_Error_e addTimer(
const timespec & timeouts, std::function<
void(
const sh_timerHandle_t handle,
void* userData)> callback, sh_timerHandle_t& handle,
void* userData,
477 const bool __attribute__((__unused__)) repeats =
false 479 const bool repeats =
false 482 am_Error_e removeTimer(
const sh_timerHandle_t handle);
483 am_Error_e restartTimer(
const sh_timerHandle_t handle);
484 am_Error_e updateTimer(
const sh_timerHandle_t handle,
const timespec & timeouts);
485 am_Error_e stopTimer(
const sh_timerHandle_t handle);
486 void start_listenting();
487 void stop_listening();
488 void exit_mainloop();
490 bool fatalErrorOccurred();
A Common-API wrapper class, which loads the common-api runtime and instantiates all necessary objects...
am_Error_e
the errors of the audiomanager.
virtual void Call(sh_timerHandle_t handle, void *userData)
prototype for poll fired callback
make private, not public template for a callback
virtual void Call(const sh_pollHandle_t handle, void *userData)
The am::CAmSocketHandler implements a mainloop for the AudioManager.
uint16_t sh_pollHandle_t
this is a handle for a filedescriptor to be used with the SocketHandler
virtual ~IAmShPollPrepare()
prototype for poll check callback
prototype for poll prepared callback
TAmShPollDispatch(TClass *instance, bool(TClass::*function)(const sh_pollHandle_t handle, void *userData))
virtual bool Call(const sh_pollHandle_t handle, void *userData)
virtual void Call(const sh_pollHandle_t handle, void *userData)=0
TAmShPollFired(TClass *instance, void(TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void *userData))
prototype for dispatch callback
virtual bool Call(const sh_pollHandle_t handle, void *userData)
virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)
Copyright (C) 2012 - 2014, BMW AG.
TAmShTimerCallBack(TClass *instance, void(TClass::*function)(sh_timerHandle_t handle, void *userData))
TAmShPollCheck(TClass *instance, bool(TClass::*function)(const sh_pollHandle_t handle, void *userData))
prototype for the timer callback
TAmShPollPrepare(TClass *instance, void(TClass::*function)(const sh_pollHandle_t handle, void *userData))
virtual ~IAmShTimerCallBack()
sh_pollHandle_t sh_timerHandle_t
this is a handle for a timer to be used with the SocketHandler
template to create the functor for a class