1 #include "ace/Name_Proxy.h"
2 #include "ace/Log_Category.h"
3 #include "ace/os_include/arpa/os_inet.h"
5 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
8 ACE_Name_Proxy::dump () const
10 #if defined (ACE_HAS_DUMP)
11 ACE_TRACE ("ACE_Name_Proxy::dump");
13 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
14 this->connector_
.dump ();
16 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
17 #endif /* ACE_HAS_DUMP */
20 /// Default constructor.
21 ACE_Name_Proxy::ACE_Name_Proxy ()
23 ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
26 /// Establish binding with the ACE_Name Server at remote_addr.
28 ACE_Name_Proxy::open (const ACE_INET_Addr
&remote_addr
,
29 ACE_Synch_Options
& options
)
31 ACE_TRACE ("ACE_Name_Proxy::open");
32 ACE_Time_Value
*timeout
{};
34 if (options
[ACE_Synch_Options::USE_TIMEOUT
])
35 timeout
= const_cast<ACE_Time_Value
*> (options
.time_value ());
37 // Initiate the connection.
38 return this->connector_
.connect (this->peer_
, remote_addr
, timeout
);
41 /// Establish binding with the ACE_Name Server at remote_addr.
42 ACE_Name_Proxy::ACE_Name_Proxy (
43 const ACE_INET_Addr
&remote_addr
,
44 ACE_Synch_Options
& options
)
46 ACE_TRACE ("ACE_Name_Proxy::ACE_Name_Proxy");
47 if (this->open (remote_addr
, options
) == -1
48 && options
[ACE_Synch_Options::USE_TIMEOUT
] && errno
!= EWOULDBLOCK
)
49 ACELIB_ERROR ((LM_ERROR
,
51 ACE_TEXT ("ACE_Name_Proxy::ACE_Name_Proxy")));
54 /// Obtain underlying handle.
56 ACE_Name_Proxy::get_handle () const
58 ACE_TRACE ("ACE_Name_Proxy::get_handle");
59 return this->peer_
.get_handle ();
63 ACE_Name_Proxy::request_reply (ACE_Name_Request
&request
)
65 ACE_TRACE ("ACE_Name_Proxy::request_reply");
67 ssize_t
const length
= request
.encode (buffer
);
70 ACELIB_ERROR_RETURN ((LM_ERROR
,
72 ACE_TEXT ("encode failed")),
75 // Transmit request via a blocking send.
76 if (this->peer_
.send_n (buffer
, length
) != length
)
77 ACELIB_ERROR_RETURN ((LM_ERROR
,
79 ACE_TEXT ("send_n failed")),
85 // Receive reply via blocking read.
86 if (this->peer_
.recv_n (&reply
,
88 ACELIB_ERROR_RETURN ((LM_ERROR
,
90 ACE_TEXT ("recv failed")),
92 else if (reply
.decode () == -1)
93 ACELIB_ERROR_RETURN ((LM_ERROR
,
95 ACE_TEXT ("decode failed")),
97 errno
= int (reply
.errnum ());
98 return reply
.status ();
103 ACE_Name_Proxy::send_request (ACE_Name_Request
&request
)
105 ACE_TRACE ("ACE_Name_Proxy::send_request");
107 ssize_t
const length
= request
.encode (buffer
);
110 ACELIB_ERROR_RETURN ((LM_ERROR
,
112 ACE_TEXT ("encode failed")),
115 // Transmit request via a blocking send.
116 else if (this->peer_
.send_n (buffer
, length
) != length
)
117 ACELIB_ERROR_RETURN ((LM_ERROR
,
119 ACE_TEXT ("send_n failed")),
125 ACE_Name_Proxy::recv_reply (ACE_Name_Request
&reply
)
127 ACE_TRACE ("ACE_Name_Proxy::recv_reply");
128 // Read the first 4 bytes to get the length of the message This
129 // implementation assumes that the first 4 bytes are the length of
131 ssize_t n
= this->peer_
.recv ((void *) &reply
, sizeof (ACE_UINT32
));
132 if (n
!= sizeof (ACE_UINT32
))
134 // Not the correct number of bytes. Sort out just what kind of fail,
135 // but this is wrong and will end up failing the call.
138 ACELIB_DEBUG ((LM_DEBUG
,
139 ACE_TEXT ("****************** recv_reply returned -1\n")));
143 ACELIB_ERROR ((LM_ERROR
,
144 ACE_TEXT ("%p got %d bytes, expected %d bytes\n"),
145 ACE_TEXT ("recv failed"),
147 sizeof (ACE_UINT32
)));
149 // If 0, no diagnostic - the peer shut it down.
150 // We've shutdown unexpectedly
154 // Transform the length into host byte order.
155 ssize_t
const length
= ACE_NTOHL (reply
.length ());
157 // Receive the rest of the request message.
158 // @@ beware of blocking read!!!.
159 n
= this->peer_
.recv ((void *) (((char *) &reply
) + sizeof (ACE_UINT32
)),
160 length
- sizeof (ACE_UINT32
));
162 // Subtract off the size of the part we skipped over...
163 if (n
!= ssize_t (length
- sizeof (ACE_UINT32
)))
165 ACELIB_ERROR ((LM_ERROR
,
166 ACE_TEXT ("%p expected %d, got %d\n"),
167 ACE_TEXT ("invalid length"),
173 // Decode the request into host byte order.
174 if (reply
.decode () == -1)
176 ACELIB_ERROR ((LM_ERROR
,
178 ACE_TEXT ("decode failed")));
184 /// Close down the connection to the server.
185 ACE_Name_Proxy::~ACE_Name_Proxy ()
187 ACE_TRACE ("ACE_Name_Proxy::~ACE_Name_Proxy");
188 this->peer_
.close ();
191 ACE_END_VERSIONED_NAMESPACE_DECL