00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Acceptor.h 00006 * 00007 * $Id: Acceptor.h 88800 2010-02-01 23:18:34Z shuston $ 00008 * 00009 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_ACCEPTOR_H 00014 #define ACE_ACCEPTOR_H 00015 00016 #include /**/ "ace/pre.h" 00017 00018 #include "ace/Service_Object.h" 00019 00020 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00021 # pragma once 00022 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00023 00024 #include "ace/Strategies_T.h" 00025 #include "ace/Synch_Options.h" 00026 00027 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00028 00029 /** 00030 * @class ACE_Acceptor 00031 * 00032 * @brief Abstract factory for creating a service handler 00033 * (SVC_HANDLER), accepting into the SVC_HANDLER, and 00034 * activating the SVC_HANDLER. 00035 * 00036 * Implements the basic strategy for passively establishing 00037 * connections with clients. An ACE_Acceptor is parameterized 00038 * by concrete types that conform to the interfaces of 00039 * PEER_ACCEPTOR and SVC_HANDLER. The PEER_ACCEPTOR is 00040 * instantiated with a transport mechanism that passively 00041 * establishes connections. The SVC_HANDLER is instantiated 00042 * with a concrete type that performs the application-specific 00043 * service. An ACE_Acceptor inherits from ACE_Service_Object, 00044 * which in turn inherits from ACE_Event_Handler. This enables 00045 * the ACE_Reactor to dispatch the ACE_Acceptor's handle_input 00046 * method when connection events occur. The handle_input method 00047 * performs the ACE_Acceptor's default creation, connection 00048 * establishment, and service activation strategies. These 00049 * strategies can be overridden by subclasses individually or as 00050 * a group. 00051 */ 00052 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1> 00053 class ACE_Acceptor : public ACE_Service_Object 00054 { 00055 public: 00056 00057 // Useful STL-style traits. 00058 typedef ACE_PEER_ACCEPTOR_ADDR addr_type; 00059 typedef ACE_PEER_ACCEPTOR acceptor_type; 00060 typedef SVC_HANDLER handler_type; 00061 typedef typename SVC_HANDLER::stream_type stream_type; 00062 00063 /// "Do-nothing" constructor. 00064 ACE_Acceptor (ACE_Reactor * = 0, 00065 int use_select = 1); 00066 00067 /** 00068 * Open the contained @c PEER_ACCEPTOR object to begin listening, and 00069 * register with the specified reactor for accept events. An 00070 * acceptor can only listen to one port at a time, so make sure to 00071 * @c close() the acceptor before calling @c open() again. 00072 * 00073 * The @c PEER_ACCEPTOR handle is put into non-blocking mode as a 00074 * safeguard against the race condition that can otherwise occur 00075 * between the time when the passive-mode socket handle is "ready" 00076 * and when the actual @c accept() call is made. During this 00077 * interval, the client can shutdown the connection, in which case, 00078 * the @c accept() call can hang. 00079 * 00080 * @param local_addr The address to listen at. 00081 * @param reactor Pointer to the ACE_Reactor instance to register 00082 * this object with. The default is the singleton. 00083 * @param flags Flags to control what mode an accepted socket 00084 * will be put into after it is accepted. The only 00085 * legal value for this argument is @c ACE_NONBLOCK, 00086 * which enables non-blocking mode on the accepted 00087 * peer stream object in @c SVC_HANDLER. The default 00088 * is 0. 00089 * @param use_select Affects behavior when called back by the reactor 00090 * when a connection can be accepted. If non-zero, 00091 * this object will accept all pending connections, 00092 * instead of just the one that triggered the reactor 00093 * callback. Uses ACE_OS::select() internally to 00094 * detect any remaining acceptable connections. 00095 * The default is 1. 00096 * @param reuse_addr Passed to the @c PEER_ACCEPTOR::open() method with 00097 * @p local_addr. Generally used to request that the 00098 * OS allow reuse of the listen port. The default is 1. 00099 */ 00100 ACE_Acceptor (const ACE_PEER_ACCEPTOR_ADDR &local_addr, 00101 ACE_Reactor *reactor = ACE_Reactor::instance (), 00102 int flags = 0, 00103 int use_select = 1, 00104 int reuse_addr = 1); 00105 00106 /** 00107 * Open the contained @c PEER_ACCEPTOR object to begin listening, and 00108 * register with the specified reactor for accept events. An 00109 * acceptor can only listen to one port at a time, so make sure to 00110 * @c close() the acceptor before calling @c open() again. 00111 * 00112 * The @c PEER_ACCEPTOR handle is put into non-blocking mode as a 00113 * safeguard against the race condition that can otherwise occur 00114 * between the time when the passive-mode socket handle is "ready" 00115 * and when the actual @c accept() call is made. During this 00116 * interval, the client can shutdown the connection, in which case, 00117 * the @c accept() call can hang. 00118 * 00119 * @param local_addr The address to listen at. 00120 * @param reactor Pointer to the ACE_Reactor instance to register 00121 * this object with. The default is the singleton. 00122 * @param flags Flags to control what mode an accepted socket 00123 * will be put into after it is accepted. The only 00124 * legal value for this argument is @c ACE_NONBLOCK, 00125 * which enables non-blocking mode on the accepted 00126 * peer stream object in @c SVC_HANDLER. The default 00127 * is 0. 00128 * @param use_select Affects behavior when called back by the reactor 00129 * when a connection can be accepted. If non-zero, 00130 * this object will accept all pending connections, 00131 * instead of just the one that triggered the reactor 00132 * callback. Uses ACE_OS::select() internally to 00133 * detect any remaining acceptable connections. 00134 * The default is 1. 00135 * @param reuse_addr Passed to the @c PEER_ACCEPTOR::open() method with 00136 * @p local_addr. Generally used to request that the 00137 * OS allow reuse of the listen port. The default is 1. 00138 * 00139 * @retval 0 Success 00140 * @retval -1 Failure, @c errno contains an error code. 00141 */ 00142 virtual int open (const ACE_PEER_ACCEPTOR_ADDR &local_addr, 00143 ACE_Reactor *reactor = ACE_Reactor::instance (), 00144 int flags = 0, 00145 int use_select = 1, 00146 int reuse_addr = 1); 00147 00148 /// Close down the Acceptor's resources. 00149 virtual ~ACE_Acceptor (void); 00150 00151 /// Return the underlying PEER_ACCEPTOR object. 00152 virtual operator ACE_PEER_ACCEPTOR &() const; 00153 00154 /// Return the underlying PEER_ACCEPTOR object. 00155 virtual ACE_PEER_ACCEPTOR &acceptor (void) const; 00156 00157 /// Returns the listening acceptor's {ACE_HANDLE}. 00158 virtual ACE_HANDLE get_handle (void) const; 00159 00160 /// Close down the Acceptor 00161 virtual int close (void); 00162 00163 /// In the event that an accept fails, this method will be called and 00164 /// the return value will be returned from handle_input(). 00165 virtual int handle_accept_error (void); 00166 00167 /// Dump the state of an object. 00168 void dump (void) const; 00169 00170 /// Declare the dynamic allocation hooks. 00171 ACE_ALLOC_HOOK_DECLARE; 00172 00173 protected: 00174 // = The following three methods define the Acceptor's strategies 00175 // for creating, accepting, and activating SVC_HANDLER's, 00176 // respectively. 00177 00178 /** 00179 * Bridge method for creating a SVC_HANDLER. The default is to 00180 * create a new {SVC_HANDLER} if {sh} == 0, else {sh} is unchanged. 00181 * However, subclasses can override this policy to perform 00182 * SVC_HANDLER creation in any way that they like (such as creating 00183 * subclass instances of SVC_HANDLER, using a singleton, dynamically 00184 * linking the handler, etc.). Returns -1 on failure, else 0. 00185 */ 00186 virtual int make_svc_handler (SVC_HANDLER *&sh); 00187 00188 /** 00189 * Bridge method for accepting the new connection into the 00190 * @a svc_handler. The default behavior delegates to the 00191 * PEER_ACCEPTOR::accept. 00192 */ 00193 virtual int accept_svc_handler (SVC_HANDLER *svc_handler); 00194 00195 /** 00196 * Bridge method for activating a {svc_handler} with the appropriate 00197 * concurrency strategy. The default behavior of this method is to 00198 * activate the SVC_HANDLER by calling its {open} method (which 00199 * allows the SVC_HANDLER to define its own concurrency strategy). 00200 * However, subclasses can override this strategy to do more 00201 * sophisticated concurrency activations (such as making the 00202 * SVC_HANDLER as an "active object" via multi-threading or 00203 * multi-processing). 00204 */ 00205 virtual int activate_svc_handler (SVC_HANDLER *svc_handler); 00206 00207 // = Demultiplexing hooks. 00208 /// Perform termination activities when {this} is removed from the 00209 /// {reactor}. 00210 virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE, 00211 ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK); 00212 00213 /// Accepts all pending connections from clients, and creates and 00214 /// activates SVC_HANDLERs. 00215 virtual int handle_input (ACE_HANDLE); 00216 00217 // = Dynamic linking hooks. 00218 /// Default version does no work and returns -1. Must be overloaded 00219 /// by application developer to do anything meaningful. 00220 virtual int init (int argc, ACE_TCHAR *argv[]); 00221 00222 /// Calls {handle_close}. 00223 virtual int fini (void); 00224 00225 /// Default version returns address info in {buf}. 00226 virtual int info (ACE_TCHAR **buf, size_t) const; 00227 00228 public: 00229 // = Service management hooks. 00230 /// This method calls {Reactor::suspend}. 00231 virtual int suspend (void); 00232 00233 /// This method calls {Reactor::resume}. 00234 virtual int resume (void); 00235 00236 protected: 00237 /// Concrete factory for accepting connections from clients... 00238 ACE_PEER_ACCEPTOR peer_acceptor_; 00239 00240 /// Needed to reopen the socket if {accept} fails. 00241 ACE_PEER_ACCEPTOR_ADDR peer_acceptor_addr_; 00242 00243 /** 00244 * Flags that indicate how {SVC_HANDLER}'s should be initialized 00245 * prior to being activated. Right now, the only flag that is 00246 * processed is {ACE_NONBLOCK}, which enabled non-blocking I/O on 00247 * the {SVC_HANDLER} when it is opened. 00248 */ 00249 int flags_; 00250 00251 /// Flag that indicates whether it shall use {select} in the 00252 /// {accept}-loop. 00253 int use_select_; 00254 00255 /// Needed to reopen the socket if {accept} fails. 00256 int reuse_addr_; 00257 }; 00258 00259 /** 00260 * @class ACE_Strategy_Acceptor 00261 * 00262 * @brief Abstract factory for creating a service handler 00263 * (SVC_HANDLER), accepting into the SVC_HANDLER, and activating 00264 * the SVC_HANDLER. 00265 * 00266 * Implements a flexible and extensible set of strategies for 00267 * passively establishing connections with clients. There are 00268 * three main strategies: (1) creating a SVC_HANDLER, (2) 00269 * passively accepting a new connection from a client into the 00270 * SVC_HANDLER, and (3) activating the SVC_HANDLER with a 00271 * particular concurrency mechanism. 00272 */ 00273 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1> 00274 class ACE_Strategy_Acceptor 00275 : public ACE_Acceptor <SVC_HANDLER, ACE_PEER_ACCEPTOR_2> 00276 { 00277 public: 00278 00279 // Useful STL-style traits. 00280 typedef ACE_Creation_Strategy<SVC_HANDLER> 00281 creation_strategy_type; 00282 typedef ACE_Accept_Strategy<SVC_HANDLER, ACE_PEER_ACCEPTOR_2> 00283 accept_strategy_type; 00284 typedef ACE_Concurrency_Strategy<SVC_HANDLER> 00285 concurrency_strategy_type; 00286 typedef ACE_Scheduling_Strategy<SVC_HANDLER> scheduling_strategy_type; 00287 typedef ACE_Acceptor <SVC_HANDLER, ACE_PEER_ACCEPTOR_2> 00288 base_type; 00289 00290 // = Define some useful (old style) traits. 00291 typedef ACE_Creation_Strategy<SVC_HANDLER> CREATION_STRATEGY; 00292 typedef ACE_Accept_Strategy<SVC_HANDLER, ACE_PEER_ACCEPTOR_2> ACCEPT_STRATEGY; 00293 typedef ACE_Concurrency_Strategy<SVC_HANDLER> CONCURRENCY_STRATEGY; 00294 typedef ACE_Scheduling_Strategy<SVC_HANDLER> SCHEDULING_STRATEGY; 00295 00296 /// Default constructor. 00297 ACE_Strategy_Acceptor (const ACE_TCHAR service_name[] = 0, 00298 const ACE_TCHAR service_description[] = 0, 00299 int use_select = 1, 00300 int reuse_addr = 1); 00301 00302 /** 00303 * Initialize the appropriate strategies for creation, passive 00304 * connection acceptance, and concurrency, and then register {this} 00305 * with the Reactor and listen for connection requests at the 00306 * designated {local_addr}. 00307 */ 00308 ACE_Strategy_Acceptor (const ACE_PEER_ACCEPTOR_ADDR &local_addr, 00309 ACE_Reactor * = ACE_Reactor::instance (), 00310 ACE_Creation_Strategy<SVC_HANDLER> * = 0, 00311 ACE_Accept_Strategy<SVC_HANDLER, ACE_PEER_ACCEPTOR_2> * = 0, 00312 ACE_Concurrency_Strategy<SVC_HANDLER> * = 0, 00313 ACE_Scheduling_Strategy<SVC_HANDLER> * = 0, 00314 const ACE_TCHAR service_name[] = 0, 00315 const ACE_TCHAR service_description[] = 0, 00316 int use_select = 1, 00317 int reuse_addr = 1); 00318 00319 /** 00320 * Open the contained @c PEER_ACCEPTOR object to begin listening, and 00321 * register with the specified reactor for accept events. 00322 * 00323 * The @c PEER_ACCEPTOR handle is put into non-blocking mode as a 00324 * safeguard against the race condition that can otherwise occur 00325 * between the time when the passive-mode socket handle is "ready" 00326 * and when the actual @c accept call is made. During this 00327 * interval, the client can shutdown the connection, in which case, 00328 * the {accept} call can hang. 00329 * 00330 * @param local_addr The address to listen at. 00331 * @param reactor Pointer to the ACE_Reactor instance to register 00332 * this object with. The default is the singleton. 00333 * @param flags Flags to control what mode an accepted socket 00334 * will be put into after it is accepted. The only 00335 * legal value for this argument is @c ACE_NONBLOCK, 00336 * which enables non-blocking mode on the accepted 00337 * peer stream object in @c SVC_HANDLER. The default 00338 * is 0. 00339 * @param use_select Affects behavior when called back by the reactor 00340 * when a connection can be accepted. If non-zero, 00341 * this object will accept all pending connections, 00342 * instead of just the one that triggered the reactor 00343 * callback. Uses ACE_OS::select() internally to 00344 * detect any remaining acceptable connections. 00345 * The default is 1. 00346 * @param reuse_addr Passed to the @c PEER_ACCEPTOR::open() method with 00347 * @p local_addr. Generally used to request that the 00348 * OS allow reuse of the listen port. The default is 1. 00349 * 00350 * @retval 0 Success 00351 * @retval -1 Failure, @c errno contains an error code. 00352 */ 00353 virtual int open (const ACE_PEER_ACCEPTOR_ADDR &local_addr, 00354 ACE_Reactor *reactor, 00355 int flags = 0, 00356 int use_select = 1, 00357 int reuse_addr = 1); 00358 00359 /** 00360 * Initialize the appropriate strategies for creation, passive 00361 * connection acceptance, and concurrency, and then register {this} 00362 * with the Reactor and listen for connection requests at the 00363 * designated {local_addr}. 00364 */ 00365 virtual int open (const ACE_PEER_ACCEPTOR_ADDR &, 00366 ACE_Reactor * = ACE_Reactor::instance (), 00367 ACE_Creation_Strategy<SVC_HANDLER> * = 0, 00368 ACE_Accept_Strategy<SVC_HANDLER, ACE_PEER_ACCEPTOR_2> * =0, 00369 ACE_Concurrency_Strategy<SVC_HANDLER> * = 0, 00370 ACE_Scheduling_Strategy<SVC_HANDLER> * = 0, 00371 const ACE_TCHAR *service_name = 0, 00372 const ACE_TCHAR *service_description = 0, 00373 int use_select = 1, 00374 int reuse_addr = 1); 00375 00376 /// Close down the Strategy_Acceptor's resources. 00377 virtual ~ACE_Strategy_Acceptor (void); 00378 00379 /// Return the underlying PEER_ACCEPTOR object. 00380 virtual operator ACE_PEER_ACCEPTOR &() const; 00381 00382 /// Return the underlying PEER_ACCEPTOR object. 00383 virtual ACE_PEER_ACCEPTOR &acceptor (void) const; 00384 00385 /// Returns the listening acceptor's {ACE_HANDLE}. 00386 virtual ACE_HANDLE get_handle (void) const; 00387 00388 /// Dump the state of an object. 00389 void dump (void) const; 00390 00391 /// Declare the dynamic allocation hooks. 00392 ACE_ALLOC_HOOK_DECLARE; 00393 00394 // = Service management hooks. 00395 00396 /// This method delegates to the {Scheduling_Strategy}'s {suspend} 00397 /// method. 00398 virtual int suspend (void); 00399 00400 /// This method delegates to the {Scheduling_Strategy}'s {resume} 00401 /// method. 00402 virtual int resume (void); 00403 00404 protected: 00405 00406 /// Calls {handle_close} when dynamically unlinked. 00407 virtual int fini (void); 00408 00409 /// Default version returns address info in {buf}. 00410 virtual int info (ACE_TCHAR **buf, size_t) const; 00411 00412 // = The following three methods define the {Acceptor}'s strategies 00413 // for creating, accepting, and activating {SVC_HANDLER}'s, 00414 // respectively. 00415 00416 /** 00417 * Bridge method for creating a {SVC_HANDLER}. The strategy for 00418 * creating a {SVC_HANDLER} are configured into the Acceptor via 00419 * it's {creation_strategy_}. The default is to create a new 00420 * {SVC_HANDLER} if {sh} == 0, else {sh} is unchanged. However, 00421 * subclasses can override this policy to perform {SVC_HANDLER} 00422 * creation in any way that they like (such as creating subclass 00423 * instances of {SVC_HANDLER}, using a singleton, dynamically 00424 * linking the handler, etc.). Returns -1 on failure, else 0. 00425 */ 00426 virtual int make_svc_handler (SVC_HANDLER *&); 00427 00428 /** 00429 * Bridge method for accepting the new connection into the 00430 * {SVC_HANDLER}. The default behavior delegates to the 00431 * {PEER_ACCEPTOR::accept} in the {Acceptor_Strategy}. 00432 */ 00433 virtual int accept_svc_handler (SVC_HANDLER *svc_handler); 00434 00435 /** 00436 * Bridge method for activating a {SVC_HANDLER} with the appropriate 00437 * concurrency strategy. The default behavior of this method is to 00438 * activate the {SVC_HANDLER} by calling its {open} method (which 00439 * allows the {SVC_HANDLER} to define its own concurrency strategy). 00440 * However, subclasses can override this strategy to do more 00441 * sophisticated concurrency activations (such as creating the 00442 * {SVC_HANDLER} as an "active object" via multi-threading or 00443 * multi-processing). 00444 */ 00445 virtual int activate_svc_handler (SVC_HANDLER *svc_handler); 00446 00447 // = Demultiplexing hooks. 00448 /// Perform termination activities when {this} is removed from the 00449 /// {Reactor}. 00450 virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE, 00451 ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK); 00452 00453 /// Handle SIGINT. 00454 virtual int handle_signal (int signum, siginfo_t *, ucontext_t *); 00455 00456 // = These data members are "logically private" but are put in the 00457 // protected part in case subclasses want to access them. 00458 00459 // = Strategy objects. 00460 00461 /// Creation strategy for an Acceptor. 00462 CREATION_STRATEGY *creation_strategy_; 00463 00464 /// true if {Acceptor} created the creation strategy and thus should 00465 /// delete it, else false. 00466 bool delete_creation_strategy_; 00467 00468 /// Accept strategy for an {Acceptor}. 00469 ACCEPT_STRATEGY *accept_strategy_; 00470 00471 /// true if {Acceptor} created the accept strategy and thus should delete 00472 /// it, else false. 00473 bool delete_accept_strategy_; 00474 00475 /// Concurrency strategy for an {Acceptor}. 00476 CONCURRENCY_STRATEGY *concurrency_strategy_; 00477 00478 /// true if {Acceptor} created the concurrency strategy and thus should 00479 /// delete it, else false. 00480 bool delete_concurrency_strategy_; 00481 00482 /// Scheduling strategy for an {Acceptor}. 00483 SCHEDULING_STRATEGY *scheduling_strategy_; 00484 00485 /// true if {Acceptor} created the scheduling strategy and thus should 00486 /// delete it, else false. 00487 bool delete_scheduling_strategy_; 00488 00489 // = Service information objects. 00490 00491 /// Name of the service. 00492 ACE_TCHAR *service_name_; 00493 00494 /// Description of the service. 00495 ACE_TCHAR *service_description_; 00496 00497 /// Address that the {Strategy_Acceptor} uses to listen for 00498 /// connections. 00499 ACE_PEER_ACCEPTOR_ADDR service_addr_; 00500 }; 00501 00502 /** 00503 * @class ACE_Oneshot_Acceptor 00504 * 00505 * @brief Generic factory for passively connecting clients and creating 00506 * exactly one service handler of the type SVC_HANDLER specified in the 00507 * template. 00508 * 00509 * This class works similarly to the regular ACE_Acceptor, but 00510 * with the following differences: 00511 * -# ACE_Oneshot_Acceptor doesn't automatically register itself with the 00512 * ACE_Reactor; the caller is expected to call the accept() method 00513 * directly. Since a later call to accept() may require a reactor, 00514 * the constructor and open() methods both accept an ACE_Reactor pointer 00515 * which is saved in case it's needed in accept(). 00516 * -# ACE_Oneshot_Acceptor doesn't need an ACE_Creation_Strategy (because 00517 * the user supplies the SVC_HANDLER) or an ACE_Accept_Strategy (because 00518 * this class only accepts one connection and then removes all traces of 00519 * itself from the ACE_Reactor if it was registered for asynchronous 00520 * accepts). 00521 * 00522 * The usage model for ACE_Oneshot_Acceptor is: 00523 * - Instantiate an object and establish its local address to listen at. 00524 * This can be accomplished using either the address-accepting constructor 00525 * (but there's no error indication) or the default constructor followed 00526 * by a call to open(). 00527 * - Call the accept() method. This will attempt to accept a connection 00528 * immediately. If there is no immediately available connection to accept, 00529 * behavior is governed by the ACE_Synch_Options argument passed to open(). 00530 */ 00531 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1> 00532 class ACE_Oneshot_Acceptor : public ACE_Service_Object 00533 { 00534 public: 00535 00536 // Useful STL-style traits. 00537 typedef ACE_PEER_ACCEPTOR_ADDR addr_type; 00538 typedef ACE_PEER_ACCEPTOR acceptor_type; 00539 typedef SVC_HANDLER handler_type; 00540 typedef typename SVC_HANDLER::stream_type stream_type; 00541 00542 /// Constructor. 00543 ACE_Oneshot_Acceptor (void); 00544 00545 /** 00546 * Initialize the appropriate strategies for concurrency and then 00547 * open the acceptor at the designated @a local_addr. Note 00548 * that unlike ACE_Acceptor and ACE_Strategy_Acceptor, this 00549 * method does NOT register this acceptor with the @a reactor at 00550 * this point -- the @a reactor parameter is saved in case it's 00551 * needed later. 00552 */ 00553 ACE_Oneshot_Acceptor (const ACE_PEER_ACCEPTOR_ADDR &local_addr, 00554 ACE_Reactor *reactor = ACE_Reactor::instance (), 00555 ACE_Concurrency_Strategy<SVC_HANDLER> * = 0); 00556 00557 /** 00558 * Initialize the appropriate strategies for concurrency and then 00559 * open the acceptor at the designated @a local_addr. Note 00560 * that unlike ACE_Acceptor and ACE_Strategy_Acceptor, this 00561 * method does NOT register this acceptor with the @a reactor at 00562 * this point -- the @a reactor parameter is saved in case it's 00563 * needed later. 00564 */ 00565 int open (const ACE_PEER_ACCEPTOR_ADDR &, 00566 ACE_Reactor *reactor = ACE_Reactor::instance (), 00567 ACE_Concurrency_Strategy<SVC_HANDLER> * = 0); 00568 00569 /// Close down the {Oneshot_Acceptor}. 00570 virtual ~ACE_Oneshot_Acceptor (void); 00571 00572 // = Explicit factory operation. 00573 /// Create a {SVC_HANDLER}, accept the connection into the 00574 /// {SVC_HANDLER}, and activate the {SVC_HANDLER}. 00575 virtual int accept (SVC_HANDLER * = 0, 00576 ACE_PEER_ACCEPTOR_ADDR *remote_addr = 0, 00577 const ACE_Synch_Options &synch_options = ACE_Synch_Options::defaults, 00578 bool restart = true, 00579 bool reset_new_handle = false); 00580 00581 /// Cancel a oneshot acceptor that was started asynchronously. 00582 virtual int cancel (void); 00583 00584 /// Return the underlying {PEER_ACCEPTOR} object. 00585 virtual operator ACE_PEER_ACCEPTOR &() const; 00586 00587 /// Return the underlying {PEER_ACCEPTOR} object. 00588 virtual ACE_PEER_ACCEPTOR &acceptor (void) const; 00589 00590 /// Close down the {Oneshot_Acceptor}. 00591 virtual int close (void); 00592 00593 /// Dump the state of an object. 00594 void dump (void) const; 00595 00596 /// Declare the dynamic allocation hooks. 00597 ACE_ALLOC_HOOK_DECLARE; 00598 00599 protected: 00600 /** 00601 * Bridge method for activating a {svc_handler} with the appropriate 00602 * concurrency strategy. Default behavior is to activate the 00603 * {SVC_HANDLER} as a "passive object." However, subclasses can 00604 * override this strategy to do more sophisticated concurrency 00605 * activations (such as creating the {SVC_HANDLER} as an "active 00606 * object" via multi-threading or multi-processing). 00607 */ 00608 virtual int activate_svc_handler (SVC_HANDLER *svc_handler); 00609 00610 /// Factors out the code shared between the {accept} and 00611 /// {handle_input} methods. 00612 int shared_accept (SVC_HANDLER *svc_handler, 00613 ACE_PEER_ACCEPTOR_ADDR *remote_addr, 00614 ACE_Time_Value *timeout, 00615 bool restart, 00616 bool reset_new_handle); 00617 00618 // = Demultiplexing hooks. 00619 /// Returns the listening acceptor's {ACE_HANDLE}. 00620 virtual ACE_HANDLE get_handle (void) const; 00621 00622 /// Perform termination activities when {this} is removed from the 00623 /// {reactor}. 00624 virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE, 00625 ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK); 00626 00627 /// Accept one connection from a client and activates the 00628 /// SVC_HANDLER. 00629 virtual int handle_input (ACE_HANDLE); 00630 00631 /// Called when an acceptor times out... 00632 virtual int handle_timeout (const ACE_Time_Value &tv, 00633 const void *arg); 00634 00635 // = Dynamic linking hooks. 00636 /// Default version does no work and returns -1. Must be overloaded 00637 /// by application developer to do anything meaningful. 00638 virtual int init (int argc, ACE_TCHAR *argv[]); 00639 00640 /// Default version does no work and returns -1. Must be overloaded 00641 /// by application developer to do anything meaningful. 00642 virtual int fini (void); 00643 00644 /// Default version returns address info in {buf}. 00645 virtual int info (ACE_TCHAR **, size_t) const; 00646 00647 // = Service management hooks. 00648 /// Default version does no work and returns -1. Must be overloaded 00649 /// by application developer to do anything meaningful. 00650 virtual int suspend (void); 00651 00652 /// Default version does no work and returns -1. Must be overloaded 00653 /// by application developer to do anything meaningful. 00654 virtual int resume (void); 00655 00656 private: 00657 /** 00658 * Insert ourselves into the {ACE_Reactor} so that we can continue 00659 * accepting this connection asynchronously. This method should NOT 00660 * be called by developers directly. 00661 */ 00662 int register_handler (SVC_HANDLER *svc_handler, 00663 const ACE_Synch_Options &options, 00664 bool restart); 00665 00666 /// Hold the svc_handler_ across asynchrony boundaries. 00667 SVC_HANDLER *svc_handler_; 00668 00669 /// Hold the restart flag across asynchrony boundaries. 00670 bool restart_; 00671 00672 /// Factory that establishes connections passively. 00673 ACE_PEER_ACCEPTOR peer_acceptor_; 00674 00675 /// Concurrency strategy for an Acceptor. 00676 ACE_Concurrency_Strategy<SVC_HANDLER> *concurrency_strategy_; 00677 00678 /// true if Acceptor created the concurrency strategy and thus should 00679 /// delete it, else false. 00680 bool delete_concurrency_strategy_; 00681 }; 00682 00683 ACE_END_VERSIONED_NAMESPACE_DECL 00684 00685 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00686 #include "ace/Acceptor.cpp" 00687 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00688 00689 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00690 #pragma implementation ("Acceptor.cpp") 00691 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00692 00693 #include /**/ "ace/post.h" 00694 00695 #endif /* ACE_ACCEPTOR_H */