UsageEnvironment.hh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  15. // Usage Environment
  16. // C++ header
  17. #ifndef _USAGE_ENVIRONMENT_HH
  18. #define _USAGE_ENVIRONMENT_HH
  19. #ifndef _USAGEENVIRONMENT_VERSION_HH
  20. #include "UsageEnvironment_version.hh"
  21. #endif
  22. #ifndef _NETCOMMON_H
  23. #include "NetCommon.h"
  24. #endif
  25. #ifndef _BOOLEAN_HH
  26. #include "Boolean.hh"
  27. #endif
  28. #ifndef _STRDUP_HH
  29. // "strDup()" is used often, so include this here, so everyone gets it:
  30. #include "strDup.hh"
  31. #endif
  32. #ifndef NULL
  33. #define NULL 0
  34. #endif
  35. #ifdef __BORLANDC__
  36. #define _setmode setmode
  37. #define _O_BINARY O_BINARY
  38. #endif
  39. class TaskScheduler; // forward
  40. // An abstract base class, subclassed for each use of the library
  41. class UsageEnvironment {
  42. public:
  43. Boolean reclaim();
  44. // returns True iff we were actually able to delete our object
  45. // task scheduler:
  46. TaskScheduler& taskScheduler() const {return fScheduler;}
  47. // result message handling:
  48. typedef char const* MsgString;
  49. virtual MsgString getResultMsg() const = 0;
  50. virtual void setResultMsg(MsgString msg) = 0;
  51. virtual void setResultMsg(MsgString msg1, MsgString msg2) = 0;
  52. virtual void setResultMsg(MsgString msg1, MsgString msg2, MsgString msg3) = 0;
  53. virtual void setResultErrMsg(MsgString msg, int err = 0) = 0;
  54. // like setResultMsg(), except that an 'errno' message is appended. (If "err == 0", the "getErrno()" code is used instead.)
  55. virtual void appendToResultMsg(MsgString msg) = 0;
  56. virtual void reportBackgroundError() = 0;
  57. // used to report a (previously set) error message within
  58. // a background event
  59. virtual void internalError(); // used to 'handle' a 'should not occur'-type error condition within the library.
  60. // 'errno'
  61. virtual int getErrno() const = 0;
  62. // 'console' output:
  63. virtual UsageEnvironment& operator<<(char const* str) = 0;
  64. virtual UsageEnvironment& operator<<(int i) = 0;
  65. virtual UsageEnvironment& operator<<(unsigned u) = 0;
  66. virtual UsageEnvironment& operator<<(double d) = 0;
  67. virtual UsageEnvironment& operator<<(void* p) = 0;
  68. // a pointer to additional, optional, client-specific state
  69. void* liveMediaPriv;
  70. void* groupsockPriv;
  71. protected:
  72. UsageEnvironment(TaskScheduler& scheduler); // abstract base class
  73. virtual ~UsageEnvironment(); // we are deleted only by reclaim()
  74. private:
  75. TaskScheduler& fScheduler;
  76. };
  77. typedef void TaskFunc(void* clientData);
  78. typedef void* TaskToken;
  79. typedef u_int32_t EventTriggerId;
  80. class TaskScheduler {
  81. public:
  82. virtual ~TaskScheduler();
  83. virtual TaskToken scheduleDelayedTask(int64_t microseconds, TaskFunc* proc,
  84. void* clientData) = 0;
  85. // Schedules a task to occur (after a delay) when we next
  86. // reach a scheduling point.
  87. // (Does not delay if "microseconds" <= 0)
  88. // Returns a token that can be used in a subsequent call to
  89. // unscheduleDelayedTask() or rescheduleDelayedTask()
  90. // (but only if the task has not yet occurred).
  91. virtual void unscheduleDelayedTask(TaskToken& prevTask) = 0;
  92. // (Has no effect if "prevTask" == NULL)
  93. // Sets "prevTask" to NULL afterwards.
  94. // Note: This MUST NOT be called if the scheduled task has already occurred.
  95. virtual void rescheduleDelayedTask(TaskToken& task,
  96. int64_t microseconds, TaskFunc* proc,
  97. void* clientData);
  98. // Combines "unscheduleDelayedTask()" with "scheduleDelayedTask()"
  99. // (setting "task" to the new task token).
  100. // Note: This MUST NOT be called if the scheduled task has already occurred.
  101. // For handling socket operations in the background (from the event loop):
  102. typedef void BackgroundHandlerProc(void* clientData, int mask);
  103. // Possible bits to set in "mask". (These are deliberately defined
  104. // the same as those in Tcl, to make a Tcl-based subclass easy.)
  105. #define SOCKET_READABLE (1<<1)
  106. #define SOCKET_WRITABLE (1<<2)
  107. #define SOCKET_EXCEPTION (1<<3)
  108. virtual void setBackgroundHandling(int socketNum, int conditionSet, BackgroundHandlerProc* handlerProc, void* clientData) = 0;
  109. void disableBackgroundHandling(int socketNum) { setBackgroundHandling(socketNum, 0, NULL, NULL); }
  110. virtual void moveSocketHandling(int oldSocketNum, int newSocketNum) = 0;
  111. // Changes any socket handling for "oldSocketNum" so that occurs with "newSocketNum" instead.
  112. virtual void doEventLoop(char volatile* watchVariable = NULL) = 0;
  113. // Causes further execution to take place within the event loop.
  114. // Delayed tasks, background I/O handling, and other events are handled, sequentially (as a single thread of control).
  115. // (If "watchVariable" is not NULL, then we return from this routine when *watchVariable != 0)
  116. virtual EventTriggerId createEventTrigger(TaskFunc* eventHandlerProc) = 0;
  117. // Creates a 'trigger' for an event, which - if it occurs - will be handled (from the event loop) using "eventHandlerProc".
  118. // (Returns 0 iff no such trigger can be created (e.g., because of implementation limits on the number of triggers).)
  119. virtual void deleteEventTrigger(EventTriggerId eventTriggerId) = 0;
  120. virtual void triggerEvent(EventTriggerId eventTriggerId, void* clientData = NULL) = 0;
  121. // Causes the (previously-registered) handler function for the specified event to be handled (from the event loop).
  122. // The handler function is called with "clientData" as parameter.
  123. // Note: This function (unlike other library functions) may be called from an external thread
  124. // - to signal an external event. (However, "triggerEvent()" should not be called with the
  125. // same 'event trigger id' from different threads.)
  126. // The following two functions are deprecated, and are provided for backwards-compatibility only:
  127. void turnOnBackgroundReadHandling(int socketNum, BackgroundHandlerProc* handlerProc, void* clientData) {
  128. setBackgroundHandling(socketNum, SOCKET_READABLE, handlerProc, clientData);
  129. }
  130. void turnOffBackgroundReadHandling(int socketNum) { disableBackgroundHandling(socketNum); }
  131. virtual void internalError(); // used to 'handle' a 'should not occur'-type error condition within the library.
  132. protected:
  133. TaskScheduler(); // abstract base class
  134. };
  135. #endif