summaryrefslogtreecommitdiff
path: root/ACE/ace/Event_Handler.h
blob: 9c2b97b13d76e059d82c5ce17517523ad7577eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* -*- C++ -*- */

//==========================================================================
/**
 *  @file    Event_Handler.h
 *
 *  $Id$
 *
 *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
 */
//==========================================================================

#ifndef ACE_EVENT_HANDLER_H
#define ACE_EVENT_HANDLER_H
#include /**/ "ace/pre.h"

#include "ace/ACE_export.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/os_include/os_signal.h"
#include "ace/Atomic_Op.h"
#include "ace/Synch_Traits.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

// Forward declaration.
class ACE_Message_Block;
class ACE_Reactor;
class ACE_Reactor_Timer_Interface;
class ACE_Thread_Manager;
class ACE_Process;

typedef unsigned long ACE_Reactor_Mask;

/**
 * @class ACE_Event_Handler
 *
 * @brief Provides an abstract interface for handling various types of
 * I/O, timer, and signal events.
 *
 * Subclasses read/write input/output on an I/O descriptor,
 * handle an exception raised on an I/O descriptor, handle a
 * timer's expiration, or handle a signal.
 */
class ACE_Export ACE_Event_Handler
{
public:
  enum
  {
    LO_PRIORITY = 0,
    HI_PRIORITY = 10,
    NULL_MASK = 0,
#if defined (ACE_USE_POLL)
    READ_MASK = POLLIN,
    WRITE_MASK = POLLOUT,
    EXCEPT_MASK = POLLPRI,
#else /* USE SELECT */
    READ_MASK = (1 << 0),
    WRITE_MASK = (1 << 1),
    EXCEPT_MASK = (1 << 2),
#endif /* ACE_USE_POLL */
    ACCEPT_MASK = (1 << 3),
    CONNECT_MASK = (1 << 4),
    TIMER_MASK = (1 << 5),
    QOS_MASK = (1 << 6),
    GROUP_QOS_MASK = (1 << 7),
    SIGNAL_MASK = (1 << 8),
    ALL_EVENTS_MASK = READ_MASK |
                      WRITE_MASK |
                      EXCEPT_MASK |
                      ACCEPT_MASK |
                      CONNECT_MASK |
                      TIMER_MASK |
                      QOS_MASK |
                      GROUP_QOS_MASK |
                      SIGNAL_MASK,
    RWE_MASK = READ_MASK |
               WRITE_MASK |
               EXCEPT_MASK,
    DONT_CALL = (1 << 9)
  };

  /// Destructor is virtual to enable proper cleanup.
  virtual ~ACE_Event_Handler (void);

  /// Get the I/O handle.
  virtual ACE_HANDLE get_handle (void) const;

  /// Set the I/O handle.
  virtual void set_handle (ACE_HANDLE);

  // = Get/set priority

  // Priorities run from MIN_PRIORITY (which is the "lowest priority")
  // to MAX_PRIORITY (which is the "highest priority").
  /// Get the priority of the Event_Handler.
  virtual int priority (void) const;

  /// Set the priority of the Event_Handler.
  virtual void priority (int priority);

  /// Called when input events occur (e.g., connection or data).
  virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);

  /// Called when output events are possible (e.g., when flow control
  /// abates or non-blocking connection completes).
  virtual int handle_output (ACE_HANDLE fd = ACE_INVALID_HANDLE);

  /// Called when an exceptional events occur (e.g., SIGURG).
  virtual int handle_exception (ACE_HANDLE fd = ACE_INVALID_HANDLE);

  /**
   * Called when timer expires.  <current_time> represents the current
   * time that the <Event_Handler> was selected for timeout
   * dispatching and <act> is the asynchronous completion token that
   * was passed in when <schedule_timer> was invoked.
   */
  virtual int handle_timeout (const ACE_Time_Value &current_time,
                              const void *act = 0);

  /// Called when a process exits.
  virtual int handle_exit (ACE_Process *);

  /// Called when a <handle_*()> method returns -1 or when the
  /// <remove_handler> method is called on an ACE_Reactor.  The
  /// <close_mask> indicates which event has triggered the
  /// <handle_close> method callback on a particular @a handle.
  virtual int handle_close (ACE_HANDLE handle,
                            ACE_Reactor_Mask close_mask);

  /// Called when object is signaled by OS (either via UNIX signals or
  /// when a Win32 object becomes signaled).
  virtual int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0);

  enum
    {
      /// The handler is not resumed at all. Could lead to deadlock..
      ACE_EVENT_HANDLER_NOT_RESUMED = -1,
      /// The reactor takes responsibility of resuming the handler and
      /// is the default
      ACE_REACTOR_RESUMES_HANDLER = 0,
      /// The application takes responsibility of resuming the handler
      ACE_APPLICATION_RESUMES_HANDLER
    };
  /**
   * Called to figure out whether the handler needs to resumed by the
   * reactor or the application can take care of it. The default
   * value of 0 would be returned which would allow the reactor to
   * take care of resumption of the handler. The application can
   * return a value more than zero and decide to resume the handler
   * themseleves.
   *
   * @note This method is only useful for the ACE_TP_Reactor. Sad
   * that we have to have this method in a class that is supposed to
   * be used across different components in ACE.
   */
  virtual int resume_handler (void);

  virtual int handle_qos (ACE_HANDLE = ACE_INVALID_HANDLE);
  virtual int handle_group_qos (ACE_HANDLE = ACE_INVALID_HANDLE);

  // = Accessors to set/get the various event demultiplexors.
  /// Set the event demultiplexors.
  virtual void reactor (ACE_Reactor *reactor);

  /// Get the event demultiplexors.
  virtual ACE_Reactor *reactor (void) const;

  /// Get only the reactor's timer related interface.
  virtual ACE_Reactor_Timer_Interface *reactor_timer_interface (void) const;

  //#if !defined (ACE_HAS_WINCE)
  /**
   * Used to read from non-socket ACE_HANDLEs in our own thread to
   * work around Win32 limitations that don't allow us to <select> on
   * non-sockets (such as ACE_STDIN).  This is commonly used in
   * situations where the Reactor is used to demultiplex read events
   * on ACE_STDIN on UNIX.  Note that @a event_handler must be a
   * subclass of ACE_Event_Handler.  If the <get_handle> method of
   * this event handler returns <ACE_INVALID_HANDLE> we default to
   * reading from ACE_STDIN.
   */
  static ACE_THR_FUNC_RETURN read_adapter (void *event_handler);

  /**
   * Abstracts away from the differences between Win32 and ACE with
   * respect to reading from ACE_STDIN, which is non-<select>'able on
   * Win32.
   */
  static int register_stdin_handler (ACE_Event_Handler *eh,
                                     ACE_Reactor *reactor,
                                     ACE_Thread_Manager *thr_mgr,
                                     int flags = THR_DETACHED);

  /// Performs the inverse of the <register_stdin_handler> method.
  static int remove_stdin_handler (ACE_Reactor *reactor,
                                   ACE_Thread_Manager *thr_mgr);
  //#endif /* ACE_HAS_WINCE */

  /// Reference count type.
  typedef long Reference_Count;

  /// Increment reference count on the handler.
  /**
   * This method is called when the handler is registered with the
   * Reactor and when the Reactor makes an upcall on the handler.
   * Reference count is 1 when the handler is created.
   *
   * @return Current reference count.
   */
  virtual Reference_Count add_reference (void);

  /// Decrement reference count on the handler.
  /**
   * This method is called when the handler is removed from the
   * Reactor and when an upcall made on the handler by the Reactor
   * completes.  Handler is deleted when the reference count reaches
   * 0.
   *
   * @return Current reference count.
   */
  virtual Reference_Count remove_reference (void);

  /**
   * @class Policy
   *
   * @brief Base class for all handler policies.
   */
  class ACE_Export Policy
  {

  public:

    /// Virtual destructor.
    virtual ~Policy (void);
  };

  /**
   * @class Reference_Counting_Policy
   *
   * @brief This policy dictates the reference counting requirements
   * for the handler.
   *
   * This policy allows applications to configure whether it wants the
   * Reactor to call add_reference() and remove_reference() during
   * registrations, removals, and upcalls.
   *
   * <B>Default:</B> DISABLED.
   */
  class ACE_Export Reference_Counting_Policy : public Policy
  {
    /// This policy can only be created by the handler.
    friend class ACE_Event_Handler;

  public:

    enum Value
      {
        /// Perform reference counting.
        ENABLED,
        /// Don't perform reference counting.
        DISABLED
      };

    /// Current Reference_Counting_Policy.
    Value value (void) const;

    /// Update Reference_Counting_Policy.
    void value (Value value);

  private:

    /// Private constructor.
    Reference_Counting_Policy (Value value);

    /// The value of the policy.
    Value value_;
   };

  /// Current Reference_Counting_Policy.
  Reference_Counting_Policy &reference_counting_policy (void);

protected:
  /// Force ACE_Event_Handler to be an abstract base class.
  ACE_Event_Handler (ACE_Reactor * = 0,
                     int priority = ACE_Event_Handler::LO_PRIORITY);

  /// Typedef for implementation of reference counting.
  typedef ACE_Atomic_Op<ACE_SYNCH_MUTEX, Reference_Count> Atomic_Reference_Count;

  /// Reference count.
  Atomic_Reference_Count reference_count_;

private:

  /// Priority of this Event_Handler.
  int priority_;

  /// Pointer to the various event demultiplexors.
  ACE_Reactor *reactor_;

  /// Reference counting requirements.
  Reference_Counting_Policy reference_counting_policy_;
};

/**
 * @class ACE_Event_Handler_var
 *
 * @brief Auto pointer like class for Event Handlers.
 *
 * Used to manage lifecycle of handlers. This class calls
 * ACE_Event_Handler::remove_reference() in its destructor.
 */
class ACE_Export ACE_Event_Handler_var
{

public:

  /// Default constructor.
  ACE_Event_Handler_var (void);

  /// Construct with a handler.
  ACE_Event_Handler_var (ACE_Event_Handler *p);

  /// Copy constructor.
  ACE_Event_Handler_var (const ACE_Event_Handler_var &b);

  /// Destructor.
  ~ACE_Event_Handler_var (void);

  /// Assignment to a handler.
  ACE_Event_Handler_var &operator= (ACE_Event_Handler *p);

  /// Assignment to a ACE_Event_Handler_var.
  ACE_Event_Handler_var &operator= (const ACE_Event_Handler_var &b);

  /// Overloaded "->".
  ACE_Event_Handler *operator-> () const;

  /// Access the handler.
  ACE_Event_Handler *handler (void) const;

  /// Release the handler.
  ACE_Event_Handler *release (void);

  /// Reset the handler.
  void reset (ACE_Event_Handler *p = 0);

private:

  /// Handler.
  ACE_Event_Handler *ptr_;
};

/**
 * @class ACE_Notification_Buffer
 *
 * @brief Simple wrapper for passing <ACE_Event_Handler *>s and
 * ACE_Reactor_Masks between threads.
 */
class ACE_Export ACE_Notification_Buffer
{
public:
  ACE_Notification_Buffer (void);

  ACE_Notification_Buffer (ACE_Event_Handler *eh,
                           ACE_Reactor_Mask mask);

  /// Default dtor.
  ~ACE_Notification_Buffer (void);

  /// Pointer to the Event_Handler that will be dispatched
  /// by the main event loop.
  ACE_Event_Handler *eh_;

  /// Mask that indicates which method to call.
  ACE_Reactor_Mask mask_;
};

ACE_END_VERSIONED_NAMESPACE_DECL

#if defined (__ACE_INLINE__)
#include "ace/Event_Handler.inl"
#endif /* __ACE_INLINE__ */

#include /**/ "ace/post.h"
#endif /* ACE_EVENT_HANDLER_H */