Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

SOCK_CODgram.cpp

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

Generated on Mon Jun 16 11:21:18 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002