ACE_POSIX_Asynch_Connect Class Reference

#include <POSIX_Asynch_IO.h>

Inheritance diagram for ACE_POSIX_Asynch_Connect:

Inheritance graph
[legend]
Collaboration diagram for ACE_POSIX_Asynch_Connect:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_POSIX_Asynch_Connect (ACE_POSIX_Proactor *posix_proactor)
 Constructor.

virtual ~ACE_POSIX_Asynch_Connect (void)
 Destructor.

int open (const ACE_Handler::Proxy_Ptr &handler_proxy, ACE_HANDLE handle, const void *completion_key, ACE_Proactor *proactor=0)
int connect (ACE_HANDLE connect_handle, const ACE_Addr &remote_sap, const ACE_Addr &local_sap, int reuse_addr, const void *act, int priority, int signal_number=0)
int cancel (void)
int close (void)
ACE_HANDLE get_handle (void) const
 virtual from ACE_Event_Handler

void set_handle (ACE_HANDLE handle)
 virtual from ACE_Event_Handler

int handle_output (ACE_HANDLE handle)
int handle_close (ACE_HANDLE handle, ACE_Reactor_Mask close_mask)
 virtual from ACE_Event_Handler


Private Types

typedef ACE_Map_Manager< ACE_HANDLE,
ACE_POSIX_Asynch_Connect_Result *,
ACE_SYNCH_NULL_MUTEX > 
MAP_MANAGER
typedef MAP_MANAGER::ITERATOR MAP_ITERATOR
typedef MAP_MANAGER::ENTRY MAP_ENTRY

Private Member Functions

int connect_i (ACE_POSIX_Asynch_Connect_Result *result, const ACE_Addr &remote_sap, const ACE_Addr &local_sap, int reuse_addr)
int post_result (ACE_POSIX_Asynch_Connect_Result *result, bool flg_post)
int cancel_uncompleted (bool flg_notify, ACE_Handle_Set &set)
 Cancel uncompleted connect operations.


Private Attributes

bool flg_open_
MAP_MANAGER result_map_
 Map of Result pointers that correspond to all the pending connects.

ACE_SYNCH_MUTEX lock_

Member Typedef Documentation

typedef MAP_MANAGER::ENTRY ACE_POSIX_Asynch_Connect::MAP_ENTRY [private]
 

Definition at line 899 of file POSIX_Asynch_IO.h.

typedef MAP_MANAGER::ITERATOR ACE_POSIX_Asynch_Connect::MAP_ITERATOR [private]
 

Deprecated:
(Two) Deprecated typedefs. Use the map traits instead.

Definition at line 898 of file POSIX_Asynch_IO.h.

typedef ACE_Map_Manager<ACE_HANDLE, ACE_POSIX_Asynch_Connect_Result *, ACE_SYNCH_NULL_MUTEX> ACE_POSIX_Asynch_Connect::MAP_MANAGER [private]
 

true - Connect is registered in ACE_Asynch_Pseudo_Task false - Aceept is deregisted in ACE_Asynch_Pseudo_Task

Definition at line 895 of file POSIX_Asynch_IO.h.


Constructor & Destructor Documentation

ACE_POSIX_Asynch_Connect::ACE_POSIX_Asynch_Connect ACE_POSIX_Proactor posix_proactor  ) 
 

Constructor.

Definition at line 1192 of file POSIX_Asynch_IO.cpp.

01193   : ACE_POSIX_Asynch_Operation (posix_proactor),
01194     flg_open_ (false)
01195 {
01196 }

ACE_POSIX_Asynch_Connect::~ACE_POSIX_Asynch_Connect void   )  [virtual]
 

Destructor.

Definition at line 1198 of file POSIX_Asynch_IO.cpp.

References close(), and ACE_Event_Handler::reactor().

01199 {
01200   this->close ();
01201   this->reactor(0); // to avoid purge_pending_notifications
01202 }


Member Function Documentation

int ACE_POSIX_Asynch_Connect::cancel void   )  [virtual]
 

Cancel all pending pseudo-asynchronus requests Behavior as usual AIO request

Reimplemented from ACE_POSIX_Asynch_Operation.

Definition at line 1499 of file POSIX_Asynch_IO.cpp.

References ACE_GUARD_RETURN, ACE_SYNCH_MUTEX, ACE_TRACE, cancel_uncompleted(), ACE_POSIX_Proactor::get_asynch_pseudo_task(), ACE_POSIX_Asynch_Operation::posix_proactor(), and ACE_Asynch_Pseudo_Task::remove_io_handler().

01500 {
01501   ACE_TRACE ("ACE_POSIX_Asynch_Connect::cancel");
01502 
01503   // Since this is not a real asynch I/O operation, we can't just call
01504   // ::aiocancel () or ACE_POSIX_Asynch_Operation::cancel ().
01505   // Delegate real cancelation to cancel_uncompleted (1)
01506 
01507   int rc = -1 ;  // ERRORS
01508 
01509   ACE_Handle_Set set;
01510   int num_cancelled = 0;
01511   {
01512     ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1));
01513     num_cancelled = cancel_uncompleted (flg_open_, set);
01514   }
01515   if (num_cancelled == 0)
01516     rc = 1 ;        // AIO_ALLDONE
01517   else if (num_cancelled > 0)
01518     rc = 0 ;        // AIO_CANCELED
01519 
01520   if (!this->flg_open_)
01521     return rc ;
01522 
01523   ACE_Asynch_Pseudo_Task & task =
01524     this->posix_proactor ()->get_asynch_pseudo_task ();
01525 
01526   task.remove_io_handler (set);
01527   return rc;
01528 }

int ACE_POSIX_Asynch_Connect::cancel_uncompleted bool  flg_notify,
ACE_Handle_Set set
[private]
 

Cancel uncompleted connect operations.

  • flg_notify Indicates whether or not we should send notification about canceled accepts. If this is false, don't send notifications about canceled connects. If true, notify user about canceled connects according POSIX standards we should receive notifications on canceled AIO requests.

Definition at line 1469 of file POSIX_Asynch_IO.cpp.

References ACE_TRACE, post_result(), ACE_Handle_Set::reset(), result_map_, ACE_Handle_Set::set_bit(), ACE_POSIX_Asynch_Result::set_bytes_transferred(), and ACE_POSIX_Asynch_Result::set_error().

Referenced by cancel(), and close().

01471 {
01472   ACE_TRACE ("ACE_POSIX_Asynch_Connect::cancel_uncompleted");
01473 
01474   int retval = 0;
01475 
01476   MAP_MANAGER::ITERATOR  iter (result_map_);
01477   MAP_MANAGER::ENTRY *   me = 0;
01478 
01479   set.reset ();
01480 
01481   for (; iter.next (me) != 0;  retval++ , iter.advance ())
01482     {
01483        ACE_HANDLE handle = me->ext_id_;
01484        ACE_POSIX_Asynch_Connect_Result* result = me->int_id_ ;
01485 
01486        set.set_bit (handle);
01487 
01488        result->set_bytes_transferred (0);
01489        result->set_error (ECANCELED);
01490        this->post_result (result, flg_notify);
01491     }
01492 
01493   result_map_.unbind_all ();
01494 
01495   return retval;
01496 }

int ACE_POSIX_Asynch_Connect::close void   ) 
 

Close performs cancellation of all pending requests.

Definition at line 1531 of file POSIX_Asynch_IO.cpp.

References ACE_GUARD_RETURN, ACE_SYNCH_MUTEX, ACE_TRACE, cancel_uncompleted(), ACE_POSIX_Proactor::get_asynch_pseudo_task(), ACE_POSIX_Asynch_Operation::posix_proactor(), and ACE_Asynch_Pseudo_Task::remove_io_handler().

Referenced by ~ACE_POSIX_Asynch_Connect().

01532 {
01533   ACE_TRACE ("ACE_POSIX_Asynch_Connect::close");
01534 
01535   ACE_Handle_Set set ;
01536   int num_cancelled = 0;
01537   {
01538     ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1));
01539     num_cancelled = cancel_uncompleted (flg_open_, set);
01540   }
01541 
01542   if (num_cancelled == 0 || !this->flg_open_)
01543     {
01544       this->flg_open_ = false;
01545       return 0;
01546     }
01547 
01548   ACE_Asynch_Pseudo_Task & task =
01549     this->posix_proactor ()->get_asynch_pseudo_task ();
01550 
01551   task.remove_io_handler (set);
01552   this->flg_open_ = false;
01553 
01554   return 0;
01555 }

int ACE_POSIX_Asynch_Connect::connect ACE_HANDLE  connect_handle,
const ACE_Addr remote_sap,
const ACE_Addr local_sap,
int  reuse_addr,
const void *  act,
int  priority,
int  signal_number = 0
[virtual]
 

This starts off an asynchronous connect.

  • connect_handle will be used for the connect call. If ACE_INVALID_HANDLE is specified, a new handle will be created.

Implements ACE_Asynch_Connect_Impl.

Definition at line 1244 of file POSIX_Asynch_IO.cpp.

References ACE_ERROR, ACE_ERROR_RETURN, ACE_GUARD_RETURN, ACE_LIB_TEXT, ACE_NEW_RETURN, ACE_SYNCH_MUTEX, ACE_TRACE, ACE_POSIX_Asynch_Connect_Result::connect_handle(), connect_i(), ACE_POSIX_Proactor::get_asynch_pseudo_task(), LM_ERROR, ACE_POSIX_Asynch_Operation::posix_proactor(), post_result(), ACE_Asynch_Pseudo_Task::register_io_handler(), result_map_, and ACE_POSIX_Asynch_Result::set_error().

01251 {
01252   ACE_TRACE ("ACE_POSIX_Asynch_Connect::connect");
01253 
01254   if (this->flg_open_ == 0)
01255     ACE_ERROR_RETURN ((LM_ERROR,
01256                        ACE_LIB_TEXT("%N:%l:ACE_POSIX_Asynch_Connect::connect")
01257                        ACE_LIB_TEXT("connector was not opened before\n")),
01258                       -1);
01259 
01260   // Common code for both WIN and POSIX.
01261   // Create future Asynch_Connect_Result
01262   ACE_POSIX_Asynch_Connect_Result *result = 0;
01263   ACE_NEW_RETURN (result,
01264                   ACE_POSIX_Asynch_Connect_Result (this->handler_proxy_,
01265                                                    connect_handle,
01266                                                    act,
01267                                                    this->posix_proactor ()->get_handle (),
01268                                                    priority,
01269                                                    signal_number),
01270                   -1);
01271 
01272   int rc = connect_i (result,
01273                       remote_sap,
01274                       local_sap,
01275                       reuse_addr);
01276 
01277   // update handle
01278   connect_handle = result->connect_handle ();
01279 
01280   if (rc != 0)
01281     return post_result (result, true);
01282 
01283   //  Enqueue result we will wait for completion
01284   {
01285     ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1));
01286 
01287     if (this->result_map_.bind (connect_handle, result) == -1)
01288       {
01289         ACE_ERROR  ((LM_ERROR,
01290                      ACE_LIB_TEXT ("%N:%l:%p\n"),
01291                      ACE_LIB_TEXT ("ACE_POSIX_Asynch_Connect::connect:")
01292                      ACE_LIB_TEXT ("bind")));
01293 
01294         result->set_error (EFAULT);
01295         return post_result (result, true);
01296       }
01297   }
01298 
01299   ACE_Asynch_Pseudo_Task & task =
01300     this->posix_proactor ()->get_asynch_pseudo_task ();
01301 
01302   rc = task.register_io_handler (connect_handle,
01303                                  this,
01304                                  ACE_Event_Handler::CONNECT_MASK,
01305                                  0);  // don't suspend after register
01306   if (rc < 0)
01307     {
01308       {
01309         ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, -1));
01310 
01311         this->result_map_.unbind (connect_handle, result);
01312       }
01313       if (result != 0)
01314         {
01315           result->set_error (EFAULT);
01316           this->post_result (result, true);
01317         }
01318       return -1;
01319     }
01320   else
01321     result = 0;
01322 
01323 
01324   return 0;
01325 }

int ACE_POSIX_Asynch_Connect::connect_i ACE_POSIX_Asynch_Connect_Result result,
const ACE_Addr remote_sap,
const ACE_Addr local_sap,
int  reuse_addr
[private]
 

Definition at line 1358 of file POSIX_Asynch_IO.cpp.

References ACE_ERROR_RETURN, ACE_LIB_TEXT, ACE_NONBLOCK, ACE_OS::bind(), ACE_POSIX_Asynch_Connect_Result::connect_handle(), EINPROGRESS, EWOULDBLOCK, ACE_Addr::get_addr(), ACE_Addr::get_size(), ACE_Addr::get_type(), LM_ERROR, ACE_POSIX_Asynch_Result::set_bytes_transferred(), ACE_POSIX_Asynch_Result::set_error(), ACE::set_flags(), ACE_OS::setsockopt(), and ACE_OS::socket().

Referenced by connect().

01362 {
01363   result->set_bytes_transferred (0);
01364 
01365   ACE_HANDLE handle = result->connect_handle ();
01366 
01367   if (handle == ACE_INVALID_HANDLE)
01368     {
01369       int protocol_family = remote_sap.get_type ();
01370 
01371       handle = ACE_OS::socket (protocol_family,
01372                                SOCK_STREAM,
01373                                0);
01374       // save it
01375       result->connect_handle (handle);
01376       if (handle == ACE_INVALID_HANDLE)
01377         {
01378           result->set_error (errno);
01379           ACE_ERROR_RETURN
01380             ((LM_ERROR,
01381               ACE_LIB_TEXT("ACE_POSIX_Asynch_Connect::connect_i: %p\n"),
01382               ACE_LIB_TEXT("socket")),
01383              -1);
01384         }
01385 
01386       // Reuse the address
01387       int one = 1;
01388       if (protocol_family != PF_UNIX &&
01389           reuse_addr != 0 &&
01390           ACE_OS::setsockopt (handle,
01391                               SOL_SOCKET,
01392                               SO_REUSEADDR,
01393                               (const char*) &one,
01394                               sizeof one) == -1 )
01395         {
01396           result->set_error (errno);
01397           ACE_ERROR_RETURN
01398             ((LM_ERROR,
01399               ACE_LIB_TEXT("ACE_POSIX_Asynch_Connect::connect_i: %p\n"),
01400               ACE_LIB_TEXT("setsockopt")),
01401              -1);
01402         }
01403     }
01404 
01405   if (local_sap != ACE_Addr::sap_any)
01406     {
01407       sockaddr * laddr = reinterpret_cast<sockaddr *> (local_sap.get_addr ());
01408       size_t size = local_sap.get_size ();
01409 
01410       if (ACE_OS::bind (handle, laddr, size) == -1)
01411         {
01412            result->set_error (errno);
01413            ACE_ERROR_RETURN
01414              ((LM_ERROR,
01415                ACE_LIB_TEXT("ACE_POSIX_Asynch_Connect::connect_i: %p\n"),
01416                ACE_LIB_TEXT("bind")),
01417               -1);
01418         }
01419     }
01420 
01421   // set non blocking mode
01422   if (ACE::set_flags (handle, ACE_NONBLOCK) != 0)
01423     {
01424       result->set_error (errno);
01425       ACE_ERROR_RETURN
01426         ((LM_ERROR,
01427           ACE_LIB_TEXT("ACE_POSIX_Asynch_Connect::connect_i: %p\n")
01428           ACE_LIB_TEXT("set_flags")),
01429          -1);
01430     }
01431 
01432   for (;;)
01433     {
01434       int rc = ACE_OS::connect
01435         (handle,
01436          reinterpret_cast<sockaddr *> (remote_sap.get_addr ()),
01437          remote_sap.get_size ());
01438       if (rc < 0)  // failure
01439         {
01440           if (errno == EWOULDBLOCK || errno == EINPROGRESS)
01441             return 0; // connect started
01442 
01443           if (errno == EINTR)
01444              continue;
01445 
01446           result->set_error (errno);
01447         }
01448 
01449       return 1 ;  // connect finished
01450     }
01451 
01452   ACE_NOTREACHED (return 0);
01453 }

ACE_HANDLE ACE_POSIX_Asynch_Connect::get_handle void   )  const [virtual]
 

virtual from ACE_Event_Handler

Reimplemented from ACE_Event_Handler.

Definition at line 1205 of file POSIX_Asynch_IO.cpp.

References ACE_ASSERT.

01206 {
01207   ACE_ASSERT (0);
01208   return  ACE_INVALID_HANDLE;
01209 }

int ACE_POSIX_Asynch_Connect::handle_close ACE_HANDLE  handle,
ACE_Reactor_Mask  close_mask
[virtual]
 

virtual from ACE_Event_Handler

Reimplemented from ACE_Event_Handler.

Definition at line 1594 of file POSIX_Asynch_IO.cpp.

References ACE_GUARD_RETURN, ACE_Reactor_Mask, ACE_SYNCH_MUTEX, ACE_TRACE, ACE_POSIX_Proactor::get_asynch_pseudo_task(), ACE_POSIX_Asynch_Operation::posix_proactor(), post_result(), ACE_Asynch_Pseudo_Task::remove_io_handler(), result_map_, ACE_POSIX_Asynch_Result::set_bytes_transferred(), and ACE_POSIX_Asynch_Result::set_error().

01595 {
01596   ACE_TRACE ("ACE_POSIX_Asynch_Connect::handle_close");
01597 
01598   ACE_Asynch_Pseudo_Task &task =
01599          this->posix_proactor ()->get_asynch_pseudo_task ();
01600 
01601   task.remove_io_handler (fd);
01602 
01603   ACE_POSIX_Asynch_Connect_Result* result = 0;
01604 
01605   {
01606     ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, 0));
01607     if (this->result_map_.unbind (fd, result) != 0) // not found
01608       return -1;
01609   }
01610 
01611   result->set_bytes_transferred (0);
01612   result->set_error (ECANCELED);
01613   this->post_result (result, this->flg_open_);
01614 
01615   return 0;
01616 }

int ACE_POSIX_Asynch_Connect::handle_output ACE_HANDLE  handle  )  [virtual]
 

virtual from ACE_Event_Handler The default action on handle_input() and handle_exception is to return -1. Since that's what we want to do, just reuse them. handle_output(), however, is where successful connects are reported.

Reimplemented from ACE_Event_Handler.

Definition at line 1558 of file POSIX_Asynch_IO.cpp.

References ACE_GUARD_RETURN, ACE_SYNCH_MUTEX, ACE_TRACE, ACE_POSIX_Proactor::get_asynch_pseudo_task(), ACE_OS::getsockopt(), ACE_POSIX_Asynch_Operation::posix_proactor(), post_result(), ACE_Asynch_Pseudo_Task::remove_io_handler(), result_map_, ACE_POSIX_Asynch_Result::set_bytes_transferred(), and ACE_POSIX_Asynch_Result::set_error().

01559 {
01560   ACE_TRACE ("ACE_POSIX_Asynch_Connect::handle_output");
01561 
01562   ACE_POSIX_Asynch_Connect_Result* result = 0;
01563 
01564   {
01565     ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, 0));
01566     if (this->result_map_.unbind (fd, result) != 0) // not found
01567       return -1;
01568   }
01569 
01570   int sockerror  = 0 ;
01571   int lsockerror = sizeof sockerror;
01572 
01573   ACE_OS::getsockopt (fd,
01574                       SOL_SOCKET,
01575                       SO_ERROR,
01576                       (char*) &sockerror,
01577                       &lsockerror);
01578 
01579   result->set_bytes_transferred (0);
01580   result->set_error (sockerror);
01581 
01582   // This previously just did a "return -1" and let handle_close() clean
01583   // things up. However, this entire object may be gone as a result of
01584   // the application's completion handler, so don't count on 'this' being
01585   // legitimate on return from post_result().
01586   // remove_io_handler() contains flag DONT_CALL
01587   this->posix_proactor ()->get_asynch_pseudo_task ().remove_io_handler (fd);
01588   this->post_result (result, this->flg_open_);
01589   return 0;
01590 }

int ACE_POSIX_Asynch_Connect::open const ACE_Handler::Proxy_Ptr handler_proxy,
ACE_HANDLE  handle,
const void *  completion_key,
ACE_Proactor proactor = 0
[virtual]
 

This belongs to ACE_POSIX_Asynch_Operation. We forward this call to that method. We have put this here to avoid the compiler warnings.

Reimplemented from ACE_POSIX_Asynch_Operation.

Definition at line 1218 of file POSIX_Asynch_IO.cpp.

References ACE_TRACE, ACE_POSIX_Asynch_Operation::open(), and ACE_Handler::Proxy_Ptr.

01222 {
01223   ACE_TRACE ("ACE_POSIX_Asynch_Connect::open");
01224 
01225   if (this->flg_open_)
01226     return -1;
01227 
01228   //int result =
01229   ACE_POSIX_Asynch_Operation::open (handler_proxy,
01230                                     handle,
01231                                     completion_key,
01232                                     proactor);
01233 
01234   // Ignore result as we pass ACE_INVALID_HANDLE
01235   //if (result == -1)
01236   //  return result;
01237 
01238   this->flg_open_ = true;
01239 
01240   return 0;
01241 }

int ACE_POSIX_Asynch_Connect::post_result ACE_POSIX_Asynch_Connect_Result result,
bool  flg_post
[private]
 

Definition at line 1327 of file POSIX_Asynch_IO.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_OS::closesocket(), ACE_POSIX_Asynch_Connect_Result::connect_handle(), LM_ERROR, ACE_POSIX_Asynch_Operation::posix_proactor(), and ACE_POSIX_Proactor::post_completion().

Referenced by cancel_uncompleted(), connect(), handle_close(), and handle_output().

01329 {
01330   if (this->flg_open_ && post_enable != 0)
01331     {
01332       if (this->posix_proactor ()->post_completion (result) == 0)
01333         return 0;
01334 
01335       ACE_ERROR ((LM_ERROR,
01336                   ACE_LIB_TEXT("Error:(%P | %t):%p\n"),
01337                   ACE_LIB_TEXT("ACE_POSIX_Asynch_Connect::post_result: ")
01338                   ACE_LIB_TEXT(" <post_completion> failed")));
01339     }
01340 
01341   ACE_HANDLE handle = result->connect_handle ();
01342 
01343   if (handle != ACE_INVALID_HANDLE)
01344     ACE_OS::closesocket (handle);
01345 
01346    delete result;
01347 
01348    return -1;
01349 }

void ACE_POSIX_Asynch_Connect::set_handle ACE_HANDLE  handle  )  [virtual]
 

virtual from ACE_Event_Handler

Reimplemented from ACE_Event_Handler.

Definition at line 1212 of file POSIX_Asynch_IO.cpp.

References ACE_ASSERT.

01213 {
01214   ACE_ASSERT (0) ;
01215 }


Member Data Documentation

bool ACE_POSIX_Asynch_Connect::flg_open_ [private]
 

Definition at line 890 of file POSIX_Asynch_IO.h.

ACE_SYNCH_MUTEX ACE_POSIX_Asynch_Connect::lock_ [private]
 

The lock to protect the result map which is shared. The queue is updated by main thread in the register function call and through the auxillary thread in the asynch pseudo task.

Definition at line 907 of file POSIX_Asynch_IO.h.

MAP_MANAGER ACE_POSIX_Asynch_Connect::result_map_ [private]
 

Map of Result pointers that correspond to all the pending connects.

Definition at line 902 of file POSIX_Asynch_IO.h.

Referenced by cancel_uncompleted(), connect(), handle_close(), and handle_output().


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:26:31 2006 for ACE by doxygen 1.3.6