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, "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
00023 }
00024
00025
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
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
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
00074
00075 if (protocol_family == PF_INET
00076 && ACE::bind_port (this->get_handle ()) == -1)
00077 error = 1;
00078 }
00079
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
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
00094
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