summaryrefslogtreecommitdiff
path: root/ace/Svc_Handler.cpp
blob: 6e4b94d994ea096b72da828b9e5882fb8a6b6cd2 (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
// Svc_Handler.cpp
// $Id$

#if !defined (ACE_SVC_HANDLER_C)
#define ACE_SVC_HANDLER_C

#define ACE_BUILD_DLL
#include "ace/Svc_Handler.h"
#include "ace/Dynamic.h"
#include "ace/Object_Manager.h"
#include "ace/Strategies.h"

#if !defined (__ACE_INLINE__)
#include "ace/Svc_Handler.i"
#endif /* __ACE_INLINE__ */

#define PR_ST_1 ACE_PEER_STREAM_1
#define PR_ST_2 ACE_PEER_STREAM_2

#if defined (ACE_HAS_SIG_C_FUNC)
extern "C" void
ACE_Svc_Handler_cleanup (void *object, void *)
{
  ACE_TRACE ("ACE_Svc_Handler_cleanup");

  // This won't call the object's destructor, but it will deallocate
  // its storage.  That's better than nothing.
  // (This function is only used if ACE_HAS_SIG_C_FUNC, e.g., on MVS.)
  delete object;
}
#endif /* ACE_HAS_SIG_C_FUNC */

template <PR_ST_1, ACE_SYNCH_1> void
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::cleanup (void *object, void *)
{
  ACE_TRACE ("ACE_Svc_Handler::cleanup");

  delete (ACE_TSS_TYPE (ACE_Dynamic) *) object;
}

template <PR_ST_1, ACE_SYNCH_1> ACE_Dynamic *
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::instance (void)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::allocated");

  static ACE_TSS_TYPE (ACE_Dynamic) *instance_;
  // Determines if we were dynamically allocated.  Note that this
  // should be inside of ACE_Svc_Handler, but G++ is too lame to
  // support this...

  // Implement the Double Check pattern.

  if (instance_ == 0)
    {
      ACE_MT (ACE_Thread_Mutex *lock =
        ACE_Managed_Object<ACE_Thread_Mutex>::get_preallocated_object
          (ACE_Object_Manager::ACE_SVC_HANDLER_LOCK);
        ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, *lock, 0));

      if (instance_ == 0)
        ACE_NEW_RETURN (instance_, ACE_TSS_TYPE (ACE_Dynamic), 0);

      // Register for destruction with ACE_Object_Manager.
#if defined (ACE_HAS_SIG_C_FUNC)
      ACE_Object_Manager::at_exit (instance_, ACE_Svc_Handler_cleanup, 0);
#else
      ACE_Object_Manager::at_exit (instance_, cleanup, 0);
#endif /* ACE_HAS_SIG_C_FUNC */
    }

  return ACE_TSS_GET (instance_, ACE_Dynamic);
}

template <PR_ST_1, ACE_SYNCH_1> void *
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::operator new (size_t n)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::operator new");
  // Allocate the memory and store it (usually in thread-specific
  // storage, depending on config flags).
  ACE_Svc_Handler<ACE_PEER_STREAM_2, ACE_SYNCH_2>::instance ()->set ();
  return ::new char[n];
}

template <PR_ST_1, ACE_SYNCH_1> void
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::destroy (void) 
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::destroy");

  // Only delete ourselves if we've been allocated dynamically.
  if (this->dynamic_ && this->closing_ == 0)
    // Will call the destructor, which automatically calls <shutdown>.
    // Note that if we are *not* allocated dynamically then the
    // destructor will call <shutdown> automatically when it gets run
    // during cleanup.
    delete this; 
}

template <PR_ST_1, ACE_SYNCH_1> void
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::operator delete (void *obj) 
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::delete");
  // You cannot delete a 'void*' (X3J16/95-0087 5.3.5.3), but we know
  // the pointer was created using new char[] (see operator new code),
  // so we use a cast:
  char *tmp = (char *) obj;
  ::delete [] tmp;
}

// Default constructor.

template <PR_ST_1, ACE_SYNCH_1> 
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::ACE_Svc_Handler (ACE_Thread_Manager *tm,
                                                        ACE_Message_Queue<ACE_SYNCH_2> *mq,
                                                        ACE_Reactor *reactor)
  : ACE_Task<ACE_SYNCH_2> (tm, mq),
    closing_ (0),
    recycler_ (0),
    recycling_act_ (0)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::ACE_Svc_Handler");
  
  this->reactor (reactor);

  // This clever idiom transparently checks if we were allocated
  // dynamically.  This information is used by the <destroy> method to
  // decide if we need to delete <this>...  The idiom is based on a
  // paper by Michael van Rooyen (mrooyen@cellnet.co.uk) that appeared
  // in the April '96 issue of the C++ Report.  We've spruced it up to
  // work correctly in multi-threaded programs by using our ACE_TSS
  // class.
  this->dynamic_ = 
    ACE_Svc_Handler<ACE_PEER_STREAM_2, ACE_SYNCH_2>::instance()->is_dynamic ();
  if (this->dynamic_)
    // Make sure to reset the flag
    ACE_Svc_Handler<ACE_PEER_STREAM_2, ACE_SYNCH_2>::instance()->reset ();
}

// Default behavior for a ACE_Svc_Handler object is to be registered with
// the ACE_Reactor (thereby ensuring single threading).

template <PR_ST_1, ACE_SYNCH_1> int
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::open (void *)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::open");
#if defined (DEBUGGING)
  char buf[BUFSIZ];
  ACE_PEER_STREAM_ADDR client_addr;

  if (this->peer_.get_remote_addr (client_addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "get_remote_addr"), -1);
    
  if (client_addr.addr_to_string (buf, sizeof buf) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", 
                      "can't obtain peer's address"), -1);

  ACE_DEBUG ((LM_DEBUG, "connected to %s on fd %d\n", 
             buf, this->peer_.get_handle ()));
#endif /* DEBUGGING */
  if (this->reactor () 
      && this->reactor ()->register_handler 
          (this, ACE_Event_Handler::READ_MASK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p", 
                      "unable to register client handler"), -1);
  return 0;
}

// Perform termination activities.

template <PR_ST_1, ACE_SYNCH_1> void
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::shutdown (void)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::shutdown");

  // Deregister this handler with the ACE_Reactor.
  if (this->reactor ())
    {
      ACE_Reactor_Mask mask = ACE_Event_Handler::ALL_EVENTS_MASK |
                              ACE_Event_Handler::DONT_CALL;

      // Make sure there are no timers.
      this->reactor ()->cancel_timer (this);

      // Remove self from reactor.
      this->reactor ()->remove_handler (this, mask);
    }

  // Remove self from the recycler.
  if (this->recycler ())
    this->recycler ()->purge (this->recycling_act_);

  this->peer ().close ();
}

template <PR_ST_1, ACE_SYNCH_1> void
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::dump (void) const
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::dump");
}

template <PR_ST_1, ACE_SYNCH_1> ACE_PEER_STREAM &
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::peer (void) const
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::peer");
  return (ACE_PEER_STREAM &) this->peer_;
}

// Extract the underlying I/O descriptor.

template <PR_ST_1, ACE_SYNCH_1> ACE_HANDLE
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::get_handle (void) const
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::get_handle");
  return this->peer_.get_handle ();
}

// Set the underlying I/O descriptor.

template <PR_ST_1, ACE_SYNCH_1> void
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::set_handle (ACE_HANDLE h)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::set_handle");
  this->peer_.set_handle (h);
}

template <PR_ST_1, ACE_SYNCH_1> 
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::~ACE_Svc_Handler (void)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::~ACE_Svc_Handler");

  if (this->closing_ == 0)
    {
      // We're closing down now, so make sure not to call ourselves
      // recursively via other calls to handle_close() (e.g., from the
      // Timer_Queue).
      this->closing_ = 1;

      this->shutdown ();
    }
}

template <PR_ST_1, ACE_SYNCH_1> int
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::handle_close (ACE_HANDLE, 
                                                     ACE_Reactor_Mask)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::handle_close");

  this->destroy ();
  return 0;
}

template <PR_ST_1, ACE_SYNCH_1> int
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::handle_timeout (const ACE_Time_Value &,
                                                       const void *)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::handle_timeout");
  return this->handle_close ();
}

template <PR_ST_1, ACE_SYNCH_1> int
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::close (unsigned long)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::close");
  return this->handle_close ();
}

template <PR_ST_1, ACE_SYNCH_1> int
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::init (int, char *[])
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::init");
  return -1;
}

template <PR_ST_1, ACE_SYNCH_1> int
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::fini (void)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::fini");
  return -1;
}

template <PR_ST_1, ACE_SYNCH_1> int
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::info (char **, size_t) const
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::info");
  return -1;
}

template <PR_ST_1, ACE_SYNCH_1> int
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::idle (u_long flags)
{
  if (this->recycler ())
    return this->recycler ()->cache (this->recycling_act_);
  else
    return this->close (flags);
}

template <PR_ST_1, ACE_SYNCH_1> void 
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::recycler (ACE_Connection_Recycling_Strategy *recycler, 
                                                 const void *recycling_act)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::recycler");
  this->recycler_ = recycler;
  this->recycling_act_ = recycling_act;
}

template <PR_ST_1, ACE_SYNCH_1> ACE_Connection_Recycling_Strategy *
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::recycler (void) const
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::recycler");
  return this->recycler_;
}

template <PR_ST_1, ACE_SYNCH_1> int 
ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::recycle (void *)
{
  ACE_TRACE ("ACE_Svc_Handler<PR_ST_2, ACE_SYNCH_2>::recycle");
  // By default, the object is ready and willing to be recycled.
  return 0;
}

#undef PR_ST_1
#undef PR_ST_2
#endif /* ACE_SVC_HANDLER_C */