Debian Codegraph @

protolib/include/protoChannel.h

raw
1 #ifndef _PROTO_CHANNEL
2 #define _PROTO_CHANNEL
3 /**
4 * @class ProtoChannel
5 *
6 * @brief This class serves as a base class for Protokit classes
7 * which require asynchronous I/O.
8 *
9 * This uses a sort of "signal/slot" design pattern
10 * for managing a single listener.
11 *
12 * On Unix, a file descriptor serves as the "NotifyHandle"
13 * On Win32, events and overlapping I/O are used
14 *
15 * NOTE: This is a work-in-progress !!!
16 * The intent of this class will be to serve as a _base_
17 * class for any class requiring asynchronous I/O
18 * notification and event dispatch (eventually including ProtoSockets?)
19 * This will help extend & simplify the utility of the
20 * ProtoDispatcher class
21 */
22
23 #include "protoDefs.h"
24 #include "protoNotify.h"
25
26 class ProtoChannel : public ProtoNotify
27 {
28 public:
29 virtual ~ProtoChannel();
30
31 #ifdef WIN32
32 typedef HANDLE Handle; // WIN32 uses "HANDLE" type for descriptors
33 #else
34 typedef int Handle; // UNIX uses "int" type for descriptors
35 #endif // if/else WIN32
36 static const Handle INVALID_HANDLE;
37
38 typedef NotifyFlag Notification; // TBD - deprecate this
39
40 // Derived classes MUST _end_ their own "Open()" method
41 // with a call to this
42 bool Open()
43 {
44 StartInputNotification(); // enable input notifications by default
45 return UpdateNotification();
46 }
47 // Derived classes MUST _begin_ their own "Close()" method
48 // with a call to this
49 void Close() ;
50
51 // (TBD) Should this be made virtual???
52 bool IsOpen() const
53 {
54 #ifdef WIN32
55 return ((INVALID_HANDLE != input_handle) ||
56 (INVALID_HANDLE != output_handle));
57 #else
58 return (INVALID_HANDLE != descriptor);
59 #endif // if/else WIN32/UNIX
60 }
61 /**
62 * @class Notifier
63 *
64 */
65 class Notifier
66 {
67 public:
68 virtual ~Notifier() {}
69 // This should usually be overridden
70 virtual bool UpdateChannelNotification(ProtoChannel& theChannel,
71 int notifyFlags)
72 {
73 return true;
74 }
75 };
76 Notifier* GetNotifier() const {return notifier;}
77 bool SetNotifier(ProtoChannel::Notifier* theNotifier);
78 bool SetBlocking(bool status);
79
80 bool StartInputNotification();
81 void StopInputNotification();
82 bool InputNotification()
83 {return NotifyFlagIsSet(NOTIFY_INPUT);}
84
85 bool StartOutputNotification();
86 void StopOutputNotification();
87 bool OutputNotification()
88 {return NotifyFlagIsSet(NOTIFY_OUTPUT);}
89
90 bool UpdateNotification();
91
92 void OnNotify(NotifyFlag theFlag)
93 {
94 if (listener) listener->on_event(*this, theFlag);
95 }
96
97
98 #ifdef WIN32
99 Handle GetInputEventHandle() {return input_event_handle;}
100 Handle GetOutputEventHandle() {return output_event_handle;}
101 bool IsOutputReady() {return output_ready;}
102 bool IsInputReady() {return input_ready;}
103 bool IsReady() {return (input_ready || output_ready);}
104 #else
105 Handle GetInputEventHandle() const {return descriptor;}
106 Handle GetOutputEventHandle() const {return descriptor;}
107 #endif // if/else WIN32/UNIX
108
109 // NOTE: For VC++ 6.x Debug builds "/ZI" or "/Z7" compile options must NOT be specified
110 // (or else VC++ 6.x experiences an "internal compiler error")
111 template <class listenerType>
112 bool SetListener(listenerType* theListener, void(listenerType::*eventHandler)(ProtoChannel&, NotifyFlag))
113 {
114 bool doUpdate = ((NULL != theListener) || (NULL != listener));
115 if (NULL != listener) delete listener;
116 listener = (NULL != theListener) ? new LISTENER_TYPE<listenerType>(theListener, eventHandler) : NULL;
117 bool result = (NULL != theListener) ? (NULL != listener) : true;
118 return result ? (doUpdate ? UpdateNotification() : true) : false;
119 }
120 bool HasListener() {return (NULL != listener);}
121
122 protected:
123 ProtoChannel();
124
125 private:
126 /**
127 * @class Listener
128 *
129 */
130
131 class Listener
132 {
133 public:
134 virtual ~Listener() {}
135 virtual void on_event(ProtoChannel& theChannel, NotifyFlag theFlag) = 0;
136 virtual Listener* duplicate() = 0;
137 };
138 template <class listenerType>
139 class LISTENER_TYPE : public Listener
140 {
141 public:
142 LISTENER_TYPE(listenerType* theListener,
143 void(listenerType::*eventHandler)(ProtoChannel&, NotifyFlag))
144 : listener(theListener), event_handler(eventHandler) {}
145 void on_event(ProtoChannel& theChannel, NotifyFlag theFlag)
146 {(listener->*event_handler)(theChannel, theFlag);}
147 Listener* duplicate()
148 {return (static_cast<Listener*>(new LISTENER_TYPE<listenerType>(listener, event_handler)));}
149 private:
150 listenerType* listener;
151 void(listenerType::*event_handler)(ProtoChannel&, NotifyFlag);
152 };
153
154 protected:
155 #ifdef WIN32
156 // On WIN32, the input/output handles may be different than the
157 // event handles (but they can be set the same by child classes
158 // if applicable. (ProtoDispatcher uses the event handles while
159 // read/write calls use the regular handles
160 HANDLE input_handle; // handle used for ReadFile
161 HANDLE input_event_handle; // event handle for notification
162 HANDLE output_handle;
163 HANDLE output_event_handle;
164 bool input_ready; // input_ready helps us get level-triggered nofify behaviors
165 bool output_ready; // output_ready helps us get level-triggered nofify behaviors
166 // The followng stuff facilitates using Win32 overlapped I/O in ProtoChannel subclasses
167 bool InitOverlappedIO(); // must be called before overlapped i/o routines can be used
168 bool StartOverlappedRead(); // use to "kickstart" overlapped I/O aync notifications
169 bool OverlappedRead(char* buffer, unsigned int& numBytes);
170 bool OverlappedWrite(const char* buffer, unsigned int& numBytes);
171 enum {OVERLAPPED_BUFFER_SIZE = 8192};
172 OVERLAPPED overlapped_read;
173 char* overlapped_read_buffer;
174 unsigned int overlapped_read_count;
175 unsigned int overlapped_read_index;
176 OVERLAPPED overlapped_write;
177 char* overlapped_write_buffer;
178 unsigned int overlapped_write_count;
179 unsigned int overlapped_write_index;
180 #else
181 int descriptor;
182 #endif // WIN32
183
184 private:
185 #ifdef UNIX
186 bool blocking_status;
187 #endif // UNIX
188 Listener* listener;
189 Notifier* notifier;
190
191 }; // end class ProtoChannel
192
193 #endif // PROTO_CHANNEL