dispatch.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. #ifndef DISPATCH_H
  19. #define DISPATCH_H
  20. //////////////////////////////////////////////////////////////////////////////
  21. // Headers.
  22. //////////////////////////////////////////////////////////////////////////////
  23. #include "btime.h"
  24. #include "rttypes.h"
  25. #include "slist.h"
  26. #include "filter.h"
  27. //////////////////////////////////////////////////////////////////////////////
  28. // Macros.
  29. //////////////////////////////////////////////////////////////////////////////
  30. #define RET_FREE 0x0000
  31. #define RET_DONTFREE 0x0001
  32. //////////////////////////////////////////////////////////////////////////////
  33. // Typedefs.
  34. //////////////////////////////////////////////////////////////////////////////
  35. // This type is used to call the user to allow them to allocate space for the
  36. // data and return it to be filled. Return NULL if unable to allocate the
  37. // space or other errror.
  38. typedef UCHAR* (*ALLOC_DISPATCHFUNC)( long lSize, USHORT usType, UCHAR ucFlags,
  39. long lUser);
  40. // This type is used to call the user to allow them to DEallocate space al-
  41. // located by a previous call to their ALLOC_DISPATCHFUNC.
  42. typedef void (*FREE_DISPATCHFUNC)( UCHAR* puc, USHORT usType, UCHAR ucFlags,
  43. long lUser);
  44. // This type is used to pass the copied chunk to the user ready to be used.
  45. // Returning RET_FREE will cause puc to get freed if it meets the criteria
  46. // for freeing described in the dispatch.cpp header comment. Return
  47. // RET_DONTFREE to avoid this.
  48. typedef short (*USE_DISPATCHFUNC)( UCHAR* puc, long lSize, USHORT usType,
  49. UCHAR ucFlags,
  50. long lTime, long lUser);
  51. // This type is used to pass messages to the handler.
  52. typedef short (*MSG_DISPATCHFUNC)(USHORT usMsg);
  53. // For custom time handlers.
  54. typedef long (*TIME_DISPATCHFUNC)(long lTimeUser);
  55. class CDispatch
  56. {
  57. protected: // Local types.
  58. // Items waiting to be dispatched.
  59. typedef struct
  60. {
  61. UCHAR* puc; // Data.
  62. long lSize; // Size of data in bytes.
  63. USHORT usType; // Type of data.
  64. UCHAR ucFlags; // Flags for data.
  65. long lTime; // Time data is to be dispatched.
  66. } RTITEM, *PRTITEM;
  67. public: // Construction/Destruction.
  68. // Default constructor.
  69. CDispatch();
  70. // Destructor.
  71. ~CDispatch();
  72. public: // Methods.
  73. // Sets the data handler for usType to fnUse.
  74. void SetDataHandler(USHORT usType, USE_DISPATCHFUNC fnUse);
  75. // Sets the type handler for usType to fnAlloc.
  76. void SetAllocHandler(USHORT usType, ALLOC_DISPATCHFUNC fnAlloc);
  77. // Sets the type handler for usType to fnUse.
  78. void SetFreeHandler(USHORT usType, FREE_DISPATCHFUNC fnFree);
  79. // Sets the user value for usType to lUser.
  80. void SetUserVal(USHORT usType, long lUser);
  81. // Set filter.
  82. void SetFilter(CFilter* pfilter);
  83. // Start spewing/blowing chunks.
  84. // Returns 0 on success.
  85. short Start(void);
  86. // Stop spewing/blowing chunks.
  87. // Returns 0 on success.
  88. short Suspend(void);
  89. // Sets the time handler function.
  90. void SetTimeFunc(TIME_DISPATCHFUNC fnTime, long lTimeUser)
  91. { m_fnTime = fnTime; m_lTimeUser = lTimeUser; }
  92. // Sends a message to all type handlers.
  93. // Returns the number of handlers that returned an error.
  94. short SendHandlerMessage(USHORT usMsg);
  95. // Adds an item to the list of items to be dispatched.
  96. // Returns 0 on success.
  97. short AddItem( UCHAR* puc, long lSize, USHORT usType, UCHAR ucFlags,
  98. long lTime);
  99. public: // Querries.
  100. // Returns the time from the override function if set or, if not set, from
  101. // Blue.
  102. long GetTime(void)
  103. { return (m_fnTime != NULL ? (*m_fnTime)(m_lTimeUser) : Blu_GetTime()); }
  104. // Returns TRUE if critical handler is Blue's critical list.
  105. short IsActive(void)
  106. { return m_sActive; }
  107. // Returns TRUE if there are any chunks to be dispatched in the lists.
  108. short IsEmpty(void)
  109. { return m_slistRtItems.IsEmpty(); }
  110. protected: // Internal methods.
  111. // Sets members w/o regard for current value.
  112. void Set(void);
  113. // Resets members. Deallocates memory if necessary.
  114. void Reset(void);
  115. // Handles data callbacks from filter.
  116. void UseCall( UCHAR* puc, long lSize, USHORT usType, UCHAR ucFlags,
  117. long lTime);
  118. // Callback dispatcher (calls the implied this version).
  119. static void UseCallStatic( UCHAR* puc, long lSize, USHORT usType,
  120. UCHAR ucFlags, long lTime,
  121. long l_pDispatch);
  122. // Handles alloc callbacks from filter.
  123. UCHAR* AllocCall(long lSize, USHORT usType, UCHAR ucFlags);
  124. // Callback dispatcher (calls the implied this version).
  125. static UCHAR* AllocCallStatic( long lSize,
  126. USHORT usType, UCHAR ucFlags,
  127. long l_pDispatch);
  128. // Handles free callbacks from filter.
  129. void FreeCall(UCHAR* puc, USHORT usType, UCHAR ucFlags);
  130. // Callback dispatcher (calls the implied this version).
  131. static void FreeCallStatic( UCHAR* puc, USHORT usType, UCHAR ucFlags,
  132. long l_pDispatch);
  133. // Called via BlowStatic once Start()'ed. This blows chunks
  134. // at handlers at the chunks' specified time.
  135. void Blow(void);
  136. // Called by Blue critical once Start()'ed. Passes control
  137. // to implied this Blow().
  138. static void BlowStatic(long l_pDispatch);
  139. public: // Members.
  140. protected: // Members.
  141. ALLOC_DISPATCHFUNC m_afnAlloc[NUM_TYPES]; // User defined allocation
  142. // functions.
  143. FREE_DISPATCHFUNC m_afnFree[NUM_TYPES]; // User defined
  144. // de-allocation functions.
  145. USE_DISPATCHFUNC m_afnUse[NUM_TYPES]; // User function to be
  146. // passed data functions.
  147. MSG_DISPATCHFUNC m_afnMsg[NUM_TYPES]; // User function that
  148. // receives messages per-
  149. // tinent to handlers.
  150. long m_alUser[NUM_TYPES]; // User defined values.
  151. // To speed up the following list accesses we could use a hash table
  152. // of lists (like in CRes).
  153. CSList <RTITEM, long> m_slistRtItems; // Sorted list of items
  154. // waiting for their time
  155. // to be dispatched.
  156. CFilter* m_pfilter; // CFilter.
  157. TIME_DISPATCHFUNC m_fnTime; // Time function.
  158. long m_lTimeUser; // User time value sent to
  159. // user time function.
  160. short m_sActive; // TRUE if critical is
  161. // active, FALSE otherwise.
  162. };
  163. #endif // DISPATCH_H
  164. //////////////////////////////////////////////////////////////////////////////
  165. // EOF
  166. //////////////////////////////////////////////////////////////////////////////