SOCK_CODgram.cpp

Go to the documentation of this file.
00001 // SOCK_CODgram.cpp,v 4.15 2005/10/28 16:14:55 ossama Exp
00002 
00003 #include "ace/SOCK_CODgram.h"
00004 #include "ace/Log_Msg.h"
00005 #include "ace/OS_NS_sys_socket.h"
00006 
00007 #if !defined (__ACE_INLINE__)
00008 #include "ace/SOCK_CODgram.inl"
00009 #endif /* __ACE_INLINE__ */
00010 
00011 ACE_RCSID(ace, SOCK_CODgram, "SOCK_CODgram.cpp,v 4.15 2005/10/28 16:14:55 ossama Exp")
00012 
00013 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00014 
00015 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_CODgram)
00016 
00017 void
00018 ACE_SOCK_CODgram::dump (void) const
00019 {
00020 #if defined (ACE_HAS_DUMP)
00021   ACE_TRACE ("ACE_SOCK_CODgram::dump");
00022 #endif /* ACE_HAS_DUMP */
00023 }
00024 
00025 // Here's the general-purpose constructor.
00026 
00027 ACE_SOCK_CODgram::ACE_SOCK_CODgram (const ACE_Addr &remote, const ACE_Addr &local,
00028                                     int protocol_family, int protocol,
00029                                     int reuse_addr)
00030 {
00031   ACE_TRACE ("ACE_SOCK_CODgram::ACE_SOCK_CODgram");
00032   if (this->open (remote, local,
00033                   protocol_family, protocol, reuse_addr) == -1)
00034     ACE_ERROR ((LM_ERROR, ACE_LIB_TEXT ("%p\n"), ACE_LIB_TEXT ("ACE_SOCK_CODgram")));
00035 }
00036 
00037 /* This is the general-purpose open routine.  Note that it performs
00038    a different set of functions depending on the LOCAL and REMOTE
00039    addresses passed to it.  Here's the basic logic:
00040 
00041    1. remote == ACE_Addr::sap_any && local == ACE_Addr::sap_any
00042          if protocol_family == PF_INET then
00043              bind the local address to a randomly generated port number...
00044 
00045    2. remote == ACE_Addr::sap_any && local != ACE_Addr::sap_any
00046          we are just binding the local address
00047          (used primarily by servers)
00048 
00049    3. remote != ACE_Addr::sap_any && local == ACE_Addr::sap_any
00050          we are connecting to the remote address
00051          (used primarily by clients)
00052 
00053    4. remote != ACE_Addr::sap_any && local != ACE_Addr::sap_any
00054          we are binding to the local address
00055          and connecting to the remote address
00056 */
00057 
00058 int
00059 ACE_SOCK_CODgram::open (const ACE_Addr &remote, const ACE_Addr &local,
00060                         int protocol_family, int protocol,
00061                         int reuse_addr)
00062 {
00063   ACE_TRACE ("ACE_SOCK_CODgram::open");
00064   if (ACE_SOCK::open (SOCK_DGRAM, protocol_family,
00065                       protocol, reuse_addr) == -1)
00066     return -1;
00067   else
00068     {
00069       int error = 0;
00070 
00071       if (local == ACE_Addr::sap_any && remote == ACE_Addr::sap_any)
00072         {
00073           // Assign an arbitrary port number from the transient range!!
00074 
00075           if (protocol_family == PF_INET
00076               && ACE::bind_port (this->get_handle ()) == -1)
00077             error = 1;
00078         }
00079       // We are binding just the local address.
00080       else if (local != ACE_Addr::sap_any && remote == ACE_Addr::sap_any)
00081         {
00082           if (ACE_OS::bind (this->get_handle (), (sockaddr *) local.get_addr (),
00083                       local.get_size ()) == -1)
00084             error = 1;
00085         }
00086       // We are connecting to the remote address.
00087       else if (local == ACE_Addr::sap_any && remote != ACE_Addr::sap_any)
00088         {
00089           if (ACE_OS::connect (this->get_handle (), (sockaddr *) remote.get_addr (),
00090                          remote.get_size ()) == -1)
00091             error = 1;
00092         }
00093       // We are binding to the local address and connecting to the
00094       // remote addresses.
00095       else
00096         {
00097           if (ACE_OS::bind (this->get_handle (), (sockaddr *) local.get_addr (),
00098                       local.get_size ()) == -1
00099               || ACE_OS::connect (this->get_handle (), (sockaddr *) remote.get_addr (),
00100                             remote.get_size ()) == -1)
00101             error = 1;
00102         }
00103       if (error)
00104         {
00105           this->close ();
00106           this->set_handle (ACE_INVALID_HANDLE);
00107         }
00108       return error ? -1 : 0;
00109     }
00110 }
00111 
00112 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:42:04 2006 for ACE by doxygen 1.3.6