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