Use override/default for RTPortableServer
[ACE_TAO.git] / ACE / ace / Name_Proxy.cpp
blobb4d5f702b959caf2766abbee340b69b73bcfceab
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
7 void
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 ();
15 this->peer_.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.
27 int
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,
50 ACE_TEXT ("%p\n"),
51 ACE_TEXT ("ACE_Name_Proxy::ACE_Name_Proxy")));
54 /// Obtain underlying handle.
55 ACE_HANDLE
56 ACE_Name_Proxy::get_handle () const
58 ACE_TRACE ("ACE_Name_Proxy::get_handle");
59 return this->peer_.get_handle ();
62 int
63 ACE_Name_Proxy::request_reply (ACE_Name_Request &request)
65 ACE_TRACE ("ACE_Name_Proxy::request_reply");
66 void *buffer {};
67 ssize_t const length = request.encode (buffer);
69 if (length == -1)
70 ACELIB_ERROR_RETURN ((LM_ERROR,
71 ACE_TEXT ("%p\n"),
72 ACE_TEXT ("encode failed")),
73 -1);
75 // Transmit request via a blocking send.
76 if (this->peer_.send_n (buffer, length) != length)
77 ACELIB_ERROR_RETURN ((LM_ERROR,
78 ACE_TEXT ("%p\n"),
79 ACE_TEXT ("send_n failed")),
80 -1);
81 else
83 ACE_Name_Reply reply;
85 // Receive reply via blocking read.
86 if (this->peer_.recv_n (&reply,
87 sizeof reply) == -1)
88 ACELIB_ERROR_RETURN ((LM_ERROR,
89 ACE_TEXT ("%p\n"),
90 ACE_TEXT ("recv failed")),
91 -1);
92 else if (reply.decode () == -1)
93 ACELIB_ERROR_RETURN ((LM_ERROR,
94 ACE_TEXT ("%p\n"),
95 ACE_TEXT ("decode failed")),
96 -1);
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");
106 void *buffer {};
107 ssize_t const length = request.encode (buffer);
109 if (length == -1)
110 ACELIB_ERROR_RETURN ((LM_ERROR,
111 ACE_TEXT ("%p\n"),
112 ACE_TEXT ("encode failed")),
113 -1);
115 // Transmit request via a blocking send.
116 else if (this->peer_.send_n (buffer, length) != length)
117 ACELIB_ERROR_RETURN ((LM_ERROR,
118 ACE_TEXT ("%p\n"),
119 ACE_TEXT ("send_n failed")),
120 -1);
121 return 0;
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
130 // the message.
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.
136 if (n == -1)
138 ACELIB_DEBUG ((LM_DEBUG,
139 ACE_TEXT ("****************** recv_reply returned -1\n")));
141 else if (n != 0)
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
151 return -1;
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"),
168 length,
169 n));
170 return -1;
173 // Decode the request into host byte order.
174 if (reply.decode () == -1)
176 ACELIB_ERROR ((LM_ERROR,
177 ACE_TEXT ("%p\n"),
178 ACE_TEXT ("decode failed")));
179 return -1;
181 return 0;
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