00001
00002
00003 #include "ace/SOCK.h"
00004 #include "ace/Log_Msg.h"
00005
00006 #if !defined (__ACE_INLINE__)
00007 #include "ace/SOCK.inl"
00008 #endif
00009
00010 ACE_RCSID(ace, SOCK, "$Id: SOCK.cpp 79134 2007-07-31 18:23:50Z johnnyw $")
00011
00012 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00013
00014 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK)
00015
00016 void
00017 ACE_SOCK::dump (void) const
00018 {
00019 #if defined (ACE_HAS_DUMP)
00020 ACE_TRACE ("ACE_SOCK::dump");
00021 #endif
00022 }
00023
00024 ACE_SOCK::ACE_SOCK (void)
00025 {
00026
00027 }
00028
00029
00030
00031
00032 int
00033 ACE_SOCK::get_remote_addr (ACE_Addr &sa) const
00034 {
00035 ACE_TRACE ("ACE_SOCK::get_remote_addr");
00036
00037 int len = sa.get_size ();
00038 sockaddr *addr = reinterpret_cast<sockaddr *> (sa.get_addr ());
00039
00040 if (ACE_OS::getpeername (this->get_handle (),
00041 addr,
00042 &len) == -1)
00043 return -1;
00044
00045 sa.set_size (len);
00046 sa.set_type (addr->sa_family);
00047 return 0;
00048 }
00049
00050 int
00051 ACE_SOCK::get_local_addr (ACE_Addr &sa) const
00052 {
00053 ACE_TRACE ("ACE_SOCK::get_local_addr");
00054
00055 int len = sa.get_size ();
00056 sockaddr *addr = reinterpret_cast<sockaddr *> (sa.get_addr ());
00057
00058 if (ACE_OS::getsockname (this->get_handle (),
00059 addr,
00060 &len) == -1)
00061 return -1;
00062
00063 sa.set_type (addr->sa_family);
00064 sa.set_size (len);
00065 return 0;
00066 }
00067
00068
00069
00070 int
00071 ACE_SOCK::close (void)
00072 {
00073 ACE_TRACE ("ACE_SOCK::close");
00074 int result = 0;
00075
00076 if (this->get_handle () != ACE_INVALID_HANDLE)
00077 {
00078 result = ACE_OS::closesocket (this->get_handle ());
00079 this->set_handle (ACE_INVALID_HANDLE);
00080 }
00081 return result;
00082 }
00083
00084 int
00085 ACE_SOCK::open (int type,
00086 int protocol_family,
00087 int protocol,
00088 int reuse_addr)
00089 {
00090 ACE_TRACE ("ACE_SOCK::open");
00091 int one = 1;
00092
00093 this->set_handle (ACE_OS::socket (protocol_family,
00094 type,
00095 protocol));
00096
00097 if (this->get_handle () == ACE_INVALID_HANDLE)
00098 return -1;
00099 else if (protocol_family != PF_UNIX
00100 && reuse_addr
00101 && this->set_option (SOL_SOCKET,
00102 SO_REUSEADDR,
00103 &one,
00104 sizeof one) == -1)
00105 {
00106 this->close ();
00107 return -1;
00108 }
00109 return 0;
00110 }
00111
00112
00113
00114
00115 ACE_SOCK::ACE_SOCK (int type,
00116 int protocol_family,
00117 int protocol,
00118 int reuse_addr)
00119 {
00120
00121 if (this->open (type,
00122 protocol_family,
00123 protocol,
00124 reuse_addr) == -1)
00125 ACE_ERROR ((LM_ERROR,
00126 ACE_TEXT ("%p\n"),
00127 ACE_TEXT ("ACE_SOCK::ACE_SOCK")));
00128 }
00129
00130 int
00131 ACE_SOCK::open (int type,
00132 int protocol_family,
00133 int protocol,
00134 ACE_Protocol_Info *protocolinfo,
00135 ACE_SOCK_GROUP g,
00136 u_long flags,
00137 int reuse_addr)
00138 {
00139 ACE_TRACE ("ACE_SOCK::open");
00140
00141 this->set_handle (ACE_OS::socket (protocol_family,
00142 type,
00143 protocol,
00144 protocolinfo,
00145 g,
00146 flags));
00147 int one = 1;
00148
00149 if (this->get_handle () == ACE_INVALID_HANDLE)
00150 return -1;
00151 else if (reuse_addr
00152 && this->set_option (SOL_SOCKET,
00153 SO_REUSEADDR,
00154 &one,
00155 sizeof one) == -1)
00156 {
00157 this->close ();
00158 return -1;
00159 }
00160 else
00161 return 0;
00162 }
00163
00164 ACE_SOCK::ACE_SOCK (int type,
00165 int protocol_family,
00166 int protocol,
00167 ACE_Protocol_Info *protocolinfo,
00168 ACE_SOCK_GROUP g,
00169 u_long flags,
00170 int reuse_addr)
00171 {
00172
00173 if (this->open (type,
00174 protocol_family,
00175 protocol,
00176 protocolinfo,
00177 g,
00178 flags,
00179 reuse_addr) == -1)
00180 ACE_ERROR ((LM_ERROR,
00181 ACE_TEXT ("%p\n"),
00182 ACE_TEXT ("ACE_SOCK::ACE_SOCK")));
00183 }
00184
00185 ACE_END_VERSIONED_NAMESPACE_DECL