Go to the documentation of this file.00001
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
00010
00011 ACE_RCSID(ace, SOCK_CODgram, "$Id: SOCK_CODgram.cpp 82540 2008-08-06 13:02:53Z johnnyw $")
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
00023 }
00024
00025
00026
00027 ACE_SOCK_CODgram::ACE_SOCK_CODgram (const ACE_Addr &remote,
00028 const ACE_Addr &local,
00029 int protocol_family,
00030 int protocol,
00031 int reuse_addr)
00032 {
00033 ACE_TRACE ("ACE_SOCK_CODgram::ACE_SOCK_CODgram");
00034 if (this->open (remote,
00035 local,
00036 protocol_family,
00037 protocol,
00038 reuse_addr) == -1)
00039 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_SOCK_CODgram")));
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 int
00064 ACE_SOCK_CODgram::open (const ACE_Addr &remote, const ACE_Addr &local,
00065 int protocol_family, int protocol,
00066 int reuse_addr)
00067 {
00068 ACE_TRACE ("ACE_SOCK_CODgram::open");
00069
00070
00071
00072 if (remote != ACE_Addr::sap_any)
00073 {
00074 if (local == ACE_Addr::sap_any)
00075 protocol_family = remote.get_type ();
00076 else
00077 {
00078 if (local.get_type () != remote.get_type ())
00079 {
00080 errno = EAFNOSUPPORT;
00081 return -1;
00082 }
00083 protocol_family = remote.get_type ();
00084 }
00085 }
00086 else
00087 {
00088 if (local != ACE_Addr::sap_any)
00089 {
00090 protocol_family = local.get_type ();
00091 }
00092 }
00093 if (ACE_SOCK::open (SOCK_DGRAM,
00094 protocol_family,
00095 protocol,
00096 reuse_addr) == -1)
00097 {
00098 return -1;
00099 }
00100 else
00101 {
00102 bool error = false;
00103
00104 if (local == ACE_Addr::sap_any && remote == ACE_Addr::sap_any)
00105 {
00106
00107 if ((protocol_family == PF_INET
00108 #if defined (ACE_HAS_IPV6)
00109 || protocol_family == PF_INET6
00110 #endif
00111 ) && ACE::bind_port (this->get_handle ()) == -1)
00112 error = true;
00113 }
00114
00115 else if (local != ACE_Addr::sap_any && remote == ACE_Addr::sap_any)
00116 {
00117 if (ACE_OS::bind (this->get_handle (),
00118 (sockaddr *) local.get_addr (),
00119 local.get_size ()) == -1)
00120 error = true;
00121 }
00122
00123 else if (local == ACE_Addr::sap_any && remote != ACE_Addr::sap_any)
00124 {
00125 if (ACE_OS::connect (this->get_handle (),
00126 (sockaddr *) remote.get_addr (),
00127 remote.get_size ()) == -1)
00128 error = true;
00129 }
00130
00131
00132 else
00133 {
00134 if (ACE_OS::bind (this->get_handle (),
00135 (sockaddr *) local.get_addr (),
00136 local.get_size ()) == -1
00137 || ACE_OS::connect (this->get_handle (),
00138 (sockaddr *) remote.get_addr (),
00139 remote.get_size ()) == -1)
00140 error = true;
00141 }
00142 if (error)
00143 {
00144 this->close ();
00145 this->set_handle (ACE_INVALID_HANDLE);
00146 }
00147 return error ? -1 : 0;
00148 }
00149 }
00150
00151 ACE_END_VERSIONED_NAMESPACE_DECL