17 #ifndef SOCKETHANDLER_H_
18 #define SOCKETHANDLER_H_
21 #include <sys/socket.h>
32 #define MAX_NS 1000000000L
33 #define MAX_TIMERHANDLE INT16_MAX
34 #define MAX_POLLHANDLE INT16_MAX
101 void (TClass::*mFunction)(
const pollfd pollfd,
const sh_pollHandle_t handle,
void* userData);
106 mFunction(function) {};
110 (*mInstance.*mFunction)(pollfd, handle, userData);
118 bool (TClass::*mFunction)(
const sh_pollHandle_t handle,
void* userData);
123 mFunction(function) {};
127 return ((*mInstance.*mFunction)(handle, userData));
174 typedef std::reverse_iterator<sh_timer_s> rListTimerIter;
187 typedef std::vector<pollfd> mListPollfd_t;
188 typedef std::vector<sh_poll_s> mListPoll_t;
190 bool fdIsValid(
const int fd)
const;
192 void timerCorrection();
193 timespec* insertTime(timespec& buffertime);
201 inline static bool compareCountdown(
const sh_timer_s& a,
const sh_timer_s& b)
203 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));
212 inline static timespec timespecSub(
const timespec& a,
const timespec& b)
216 if ((a.tv_sec < b.tv_sec) || ((a.tv_sec == b.tv_sec) && (a.tv_nsec <= b.tv_nsec)))
218 result.tv_sec = result.tv_nsec = 0;
222 result.tv_sec = a.tv_sec - b.tv_sec;
223 if (a.tv_nsec < b.tv_nsec)
225 result.tv_nsec = a.tv_nsec +
MAX_NS - b.tv_nsec;
230 result.tv_nsec = a.tv_nsec - b.tv_nsec;
242 inline timespec timespecAdd(
const timespec& a,
const timespec& b)
245 result.tv_sec = a.tv_sec + b.tv_sec;
246 result.tv_nsec = a.tv_nsec + b.tv_nsec;
247 if (result.tv_nsec >=
MAX_NS)
250 result.tv_nsec = result.tv_nsec -
MAX_NS;
261 inline int timespecCompare(
const timespec& a,
const timespec& b)
264 if (a.tv_sec < b.tv_sec)
267 else if (a.tv_sec > b.tv_sec)
270 else if (a.tv_nsec < b.tv_nsec)
273 else if (a.tv_nsec > b.tv_nsec)
284 inline static bool eventFired(
const pollfd& a)
286 return (a.revents == 0 ?
false :
true);
294 inline static bool noDispatching(
const sh_poll_s& a)
299 return (!a.checkCB->Call(a.handle, a.userData));
307 inline static bool dispatchingFinished(
const sh_poll_s& a)
312 return (!a.dispatchCB->Call(a.handle, a.userData));
315 class CAmShCopyPollfd
318 mListPollfd_t& mArray;
320 CAmShCopyPollfd(mListPollfd_t& dest) : mArray(dest) {}
321 void operator()(
const sh_poll_s& row);
328 void operator()(sh_poll_s& row);
335 void operator()(sh_poll_s& row);
342 void operator()(sh_timer_s& row);
345 class CAmShCountdownUp
350 CAmShCountdownUp(
const timespec& differenceTime) : mDiffTime(differenceTime) {};
351 bool operator()(
const sh_timer_s& row);
354 class CAmShCountdownZero
357 CAmShCountdownZero() {};
358 bool operator()(
const sh_timer_s& row);
361 class CAmShSubstractTime
366 CAmShSubstractTime(timespec param) : param(param) {}
367 inline void operator()(sh_timer_s& t)
369 t.countdown = timespecSub(t.countdown, param);
373 mListPollfd_t mfdPollingArray;
374 std::set<sh_pollHandle_t> mSetPollKeys;
375 mListPoll_t mListPoll;
376 std::set<sh_timerHandle_t> mSetTimerKeys;
377 std::list<sh_timer_s> mListTimer;
378 std::list<sh_timer_s> mListActiveTimer;
381 bool mRecreatePollfds;
398 mFunction(function) {};
402 (*mInstance.*mFunction)(handle, userData);
418 mFunction(function) {};
422 (*mInstance.*mFunction)(handle, userData);
433 void (TClass::*mFunction)(
const pollfd pollfd,
const sh_pollHandle_t handle,
void* userData);
438 mFunction(function) {};
442 (*mInstance.*mFunction)(pollfd, handle, userData);
453 bool (TClass::*mFunction)(
const sh_pollHandle_t handle,
void* userData);
458 mFunction(function) {};
462 return ((*mInstance.*mFunction)(handle, userData));
473 bool (TClass::*mFunction)(
const sh_pollHandle_t handle,
void* userData);
478 mFunction(function) {};
482 return ((*mInstance.*mFunction)(handle, userData));
TAmShPollFired(TClass *instance, void(TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void *userData))
am_Error_e
the errors of the audiomanager.
virtual bool Call(const sh_pollHandle_t handle, void *userData)=0
virtual void Call(sh_timerHandle_t handle, void *userData)
prototype for poll fired callback
am_Error_e restartTimer(const sh_timerHandle_t handle)
restarts a timer with the original value
make private, not public template for a callback
The am::CAmSocketHandler implements a mainloop for the AudioManager.
virtual bool Call(const sh_pollHandle_t handle, void *userData)=0
virtual void Call(const sh_timerHandle_t handle, void *userData)
uint16_t sh_pollHandle_t
this is a handle for a filedescriptor to be used with the SocketHandler
am_Error_e addTimer(const timespec timeouts, IAmShTimerCallBack *callback, sh_timerHandle_t &handle, void *userData)
adds a timer to the list of timers.
virtual ~IAmShPollPrepare()
TAmShPollFired< CAmSocketHandler > receiverCallbackT
prototype for poll check callback
am_Error_e removeFDPoll(const sh_pollHandle_t handle)
removes a filedescriptor from the poll loop
prototype for poll prepared callback
virtual ~IAmShPollDispatch()
TAmShPollDispatch(TClass *instance, bool(TClass::*function)(const sh_pollHandle_t handle, void *userData))
TAmShPollCheck(TClass *instance, bool(TClass::*function)(const sh_pollHandle_t handle, void *userData))
uint16_t sh_timerHandle_t
this is a handle for a timer to be used with the SocketHandler
am_Error_e removeTimer(const sh_timerHandle_t handle)
removes a timer from the list of timers
am_Error_e stopTimer(const sh_timerHandle_t handle)
stops a timer
virtual bool Call(const sh_pollHandle_t handle, void *userData)
am_Error_e addFDPoll(const int fd, const short event, IAmShPollPrepare *prepare, IAmShPollFired *fired, IAmShPollCheck *check, IAmShPollDispatch *dispatch, void *userData, sh_pollHandle_t &handle)
Adds a filedescriptor to the polling loop.
virtual void Call(const sh_pollHandle_t handle, void *userData)=0
am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events)
updates the eventFlags of a poll
TAmShPollFired(TClass *instance, void(TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void *userData))
void start_listenting()
start the block listening for filedescriptors.
prototype for dispatch callback
virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)
virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)=0
TAmShPollPrepare(TClass *instance, void(TClass::*function)(const sh_timerHandle_t handle, void *userData))
TAmShPollCheck< CAmSocketHandler > checkerCallbackT
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.
am_Error_e updateTimer(const sh_timerHandle_t handle, const timespec timeouts)
restarts a timer and updates with a new interva
void receiverCallback(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)
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))
virtual ~IAmShPollCheck()
prototype for the timer callback
virtual ~IAmShPollFired()
virtual void Call(const sh_timerHandle_t handle, void *userData)=0
bool checkerCallback(const sh_pollHandle_t handle, void *userData)
virtual ~IAmShTimerCallBack()
void stop_listening()
exits the loop
virtual bool Call(const sh_pollHandle_t handle, void *userData)
template to create the functor for a class