AudioManager  7.5.11
Native Application Runtime Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CAmSocketHandler.h
Go to the documentation of this file.
1 
17 #ifndef SOCKETHANDLER_H_
18 #define SOCKETHANDLER_H_
19 
20 #include "audiomanagertypes.h"
21 #include <sys/socket.h>
22 #include <stdint.h>
23 #include <sys/poll.h>
24 #include <list>
25 #include <map>
26 #include <set>
27 #include <signal.h>
28 
29 namespace am
30 {
31 
32 #define MAX_NS 1000000000L
33 #define MAX_TIMERHANDLE INT16_MAX
34 #define MAX_POLLHANDLE INT16_MAX
35 
36 typedef uint16_t sh_timerHandle_t;
37 typedef uint16_t sh_pollHandle_t;
38 
43 {
44 public:
45  virtual void Call(const sh_pollHandle_t handle, void* userData) = 0;
46  virtual ~IAmShPollPrepare() {};
47 };
48 
53 {
54 public:
55  virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData) = 0;
56  virtual ~ IAmShPollFired() {};
57 };
58 
63 {
64 public:
65  virtual bool Call(const sh_pollHandle_t handle, void* userData) = 0;
66  virtual ~ IAmShPollCheck() {};
67 };
68 
73 {
74 public:
75  virtual bool Call(const sh_pollHandle_t handle, void* userData) = 0;
76  virtual ~ IAmShPollDispatch() {};
77 };
78 
83 {
84 public:
85  virtual void Call(const sh_timerHandle_t handle, void* userData) = 0;
86  virtual ~IAmShTimerCallBack() {};
87 };
88 
95 {
96 public:
97  template<class TClass> class TAmShPollFired: public IAmShPollFired
98  {
99  private:
100  TClass* mInstance;
101  void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
102 
103  public:
104  TAmShPollFired(TClass* instance, void (TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) :
105  mInstance(instance), //
106  mFunction(function) {};
107 
108  virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
109  {
110  (*mInstance.*mFunction)(pollfd, handle, userData);
111  };
112  };
113 
114  template<class TClass> class TAmShPollCheck: public IAmShPollCheck
115  {
116  private:
117  TClass* mInstance;
118  bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
119 
120  public:
121  TAmShPollCheck(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
122  mInstance(instance), //
123  mFunction(function) {};
124 
125  virtual bool Call(const sh_pollHandle_t handle, void* userData)
126  {
127  return ((*mInstance.*mFunction)(handle, userData));
128  };
129  };
130 
133 
134  am_Error_e addFDPoll(const int fd, const short event, IAmShPollPrepare *prepare, IAmShPollFired *fired, IAmShPollCheck *check, IAmShPollDispatch *dispatch, void* userData, sh_pollHandle_t& handle);
136  am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events);
137  am_Error_e addTimer(const timespec timeouts, IAmShTimerCallBack* callback, sh_timerHandle_t& handle, void* userData);
140  am_Error_e updateTimer(const sh_timerHandle_t handle, const timespec timeouts);
141  am_Error_e stopTimer(const sh_timerHandle_t handle);
142  void start_listenting();
143  void stop_listening();
144  void exit_mainloop();
145  void receiverCallback(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
146  {
147  (void) pollfd;
148  (void) handle;
149  (void) userData;
150  };
151  bool checkerCallback(const sh_pollHandle_t handle, void* userData)
152  {
153  (void) handle;
154  (void) userData;
155  return (false);
156  };
157 
160 
161 private:
162 
163  static CAmSocketHandler* mInstance;
164  int mPipe[2];
165  int mDispatchDone; //this starts / stops the mainloop
166  struct sh_timer_s
167  {
168  sh_timerHandle_t handle;
169  timespec countdown;
170  IAmShTimerCallBack* callback;
171  void * userData;
172  };
173 
174  typedef std::reverse_iterator<sh_timer_s> rListTimerIter;
175 
176  struct sh_poll_s
177  {
178  sh_pollHandle_t handle;
179  IAmShPollPrepare *prepareCB;
180  IAmShPollFired *firedCB;
181  IAmShPollCheck *checkCB;
182  IAmShPollDispatch *dispatchCB;
183  pollfd pollfdValue;
184  void *userData;
185  };
186 
187  typedef std::vector<pollfd> mListPollfd_t;
188  typedef std::vector<sh_poll_s> mListPoll_t;
189 
190  bool fdIsValid(const int fd) const;
191  void timerUp();
192  void timerCorrection();
193  timespec* insertTime(timespec& buffertime);
194 
201  inline static bool compareCountdown(const sh_timer_s& a, const sh_timer_s& b)
202  {
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));
204  }
205 
212  inline static timespec timespecSub(const timespec& a, const timespec& b)
213  {
214  timespec result;
215 
216  if ((a.tv_sec < b.tv_sec) || ((a.tv_sec == b.tv_sec) && (a.tv_nsec <= b.tv_nsec)))
217  {
218  result.tv_sec = result.tv_nsec = 0;
219  }
220  else
221  {
222  result.tv_sec = a.tv_sec - b.tv_sec;
223  if (a.tv_nsec < b.tv_nsec)
224  {
225  result.tv_nsec = a.tv_nsec + MAX_NS - b.tv_nsec;
226  result.tv_sec--; /* Borrow a second. */
227  }
228  else
229  {
230  result.tv_nsec = a.tv_nsec - b.tv_nsec;
231  }
232  }
233  return (result);
234  }
235 
242  inline timespec timespecAdd(const timespec& a, const timespec& b)
243  {
244  timespec result;
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)
248  {
249  result.tv_sec++;
250  result.tv_nsec = result.tv_nsec - MAX_NS;
251  }
252  return (result);
253  }
254 
261  inline int timespecCompare(const timespec& a, const timespec& b)
262  {
263  //less
264  if (a.tv_sec < b.tv_sec)
265  return (-1);
266  //greater
267  else if (a.tv_sec > b.tv_sec)
268  return (1);
269  //less
270  else if (a.tv_nsec < b.tv_nsec)
271  return (-1);
272  //greater
273  else if (a.tv_nsec > b.tv_nsec)
274  return (1);
275  //equal
276  return (0);
277  }
278 
284  inline static bool eventFired(const pollfd& a)
285  {
286  return (a.revents == 0 ? false : true);
287  }
288 
294  inline static bool noDispatching(const sh_poll_s& a)
295  {
296  //remove from list of there is no checkCB
297  if (!a.checkCB)
298  return (true);
299  return (!a.checkCB->Call(a.handle, a.userData));
300  }
301 
307  inline static bool dispatchingFinished(const sh_poll_s& a)
308  {
309  //remove from list of there is no dispatchCB
310  if (!a.dispatchCB)
311  return (true);
312  return (!a.dispatchCB->Call(a.handle, a.userData));
313  }
314 
315  class CAmShCopyPollfd
316  {
317  private:
318  mListPollfd_t& mArray;
319  public:
320  CAmShCopyPollfd(mListPollfd_t& dest) : mArray(dest) {}
321  void operator()(const sh_poll_s& row);
322  };
323 
324  class CAmShCallFire
325  {
326  public:
327  CAmShCallFire() {};
328  void operator()(sh_poll_s& row);
329  };
330 
331  class CAmShCallPrep
332  {
333  public:
334  CAmShCallPrep() {};
335  void operator()(sh_poll_s& row);
336  };
337 
338  class CAmShCallTimer
339  {
340  public:
341  CAmShCallTimer() {};
342  void operator()(sh_timer_s& row);
343  };
344 
345  class CAmShCountdownUp
346  {
347  private:
348  timespec mDiffTime;
349  public:
350  CAmShCountdownUp(const timespec& differenceTime) : mDiffTime(differenceTime) {};
351  bool operator()(const sh_timer_s& row);
352  };
353 
354  class CAmShCountdownZero
355  {
356  public:
357  CAmShCountdownZero() {};
358  bool operator()(const sh_timer_s& row);
359  };
360 
361  class CAmShSubstractTime
362  {
363  private:
364  timespec param;
365  public:
366  CAmShSubstractTime(timespec param) : param(param) {}
367  inline void operator()(sh_timer_s& t)
368  {
369  t.countdown = timespecSub(t.countdown, param);
370  }
371  };
372 
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;
379  sh_timerHandle_t mLastInsertedHandle;
380  sh_pollHandle_t mLastInsertedPollHandle;
381  bool mRecreatePollfds;
382  timespec mStartTime;
383 
384 };
385 
389 template<class TClass> class TAmShTimerCallBack: public IAmShTimerCallBack
390 {
391 private:
392  TClass* mInstance;
393  void (TClass::*mFunction)(sh_timerHandle_t handle, void* userData);
394 
395 public:
396  TAmShTimerCallBack(TClass* instance, void (TClass::*function)(sh_timerHandle_t handle, void* userData)) :
397  mInstance(instance), //
398  mFunction(function) {};
399 
400  virtual void Call(sh_timerHandle_t handle, void* userData)
401  {
402  (*mInstance.*mFunction)(handle, userData);
403  }
404 };
405 
409 template<class TClass> class TAmShPollPrepare: public IAmShPollPrepare
410 {
411 private:
412  TClass* mInstance;
413  void (TClass::*mFunction)(const sh_timerHandle_t handle, void* userData);
414 
415 public:
416  TAmShPollPrepare(TClass* instance, void (TClass::*function)(const sh_timerHandle_t handle, void* userData)) :
417  mInstance(instance), //
418  mFunction(function) {};
419 
420  virtual void Call(const sh_timerHandle_t handle, void* userData)
421  {
422  (*mInstance.*mFunction)(handle, userData);
423  };
424 };
425 
429 template<class TClass> class TAmShPollFired: public IAmShPollFired
430 {
431 private:
432  TClass* mInstance;
433  void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
434 
435 public:
436  TAmShPollFired(TClass* instance, void (TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) :
437  mInstance(instance), //
438  mFunction(function) {};
439 
440  virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
441  {
442  (*mInstance.*mFunction)(pollfd, handle, userData);
443  };
444 };
445 
449 template<class TClass> class TAmShPollCheck: public IAmShPollCheck
450 {
451 private:
452  TClass* mInstance;
453  bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
454 
455 public:
456  TAmShPollCheck(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
457  mInstance(instance), //
458  mFunction(function) {};
459 
460  virtual bool Call(const sh_pollHandle_t handle, void* userData)
461  {
462  return ((*mInstance.*mFunction)(handle, userData));
463  };
464 };
465 
469 template<class TClass> class TAmShPollDispatch: public IAmShPollDispatch
470 {
471 private:
472  TClass* mInstance;
473  bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
474 
475 public:
476  TAmShPollDispatch(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
477  mInstance(instance), //
478  mFunction(function) {};
479 
480  virtual bool Call(const sh_pollHandle_t handle, void* userData)
481  {
482  return ((*mInstance.*mFunction)(handle, userData));
483  };
484 };
485 } /* namespace am */
486 #endif /* SOCKETHANDLER_H_ */
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)
template for a callback
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.
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
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
template for a callback
template for a callback
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))
prototype for the timer callback
virtual void Call(const sh_timerHandle_t handle, void *userData)=0
#define MAX_NS
bool checkerCallback(const sh_pollHandle_t handle, void *userData)
void stop_listening()
exits the loop
virtual bool Call(const sh_pollHandle_t handle, void *userData)
template to create the functor for a class