1 // $Id: MEM_Acceptor.cpp 80826 2008-03-04 14:51:23Z wotte $
3 #include "ace/MEM_Acceptor.h"
5 #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1)
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/OS_NS_sys_socket.h"
10 #include "ace/OS_NS_unistd.h"
12 #if !defined (__ACE_INLINE__)
13 #include "ace/MEM_Acceptor.inl"
14 #endif /* __ACE_INLINE__ */
16 ACE_RCSID(ace
, MEM_Acceptor
, "$Id: MEM_Acceptor.cpp 80826 2008-03-04 14:51:23Z wotte $")
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 ACE_ALLOC_HOOK_DEFINE(ACE_MEM_Acceptor
)
23 ACE_MEM_Acceptor::dump (void) const
25 #if defined (ACE_HAS_DUMP)
26 ACE_TRACE ("ACE_MEM_Acceptor::dump");
27 #endif /* ACE_HAS_DUMP */
30 // Do nothing routine for constructor.
32 ACE_MEM_Acceptor::ACE_MEM_Acceptor (void)
34 malloc_options_ (ACE_DEFAULT_BASE_ADDR
, 0),
35 preferred_strategy_ (ACE_MEM_IO::Reactive
)
37 ACE_TRACE ("ACE_MEM_Acceptor::ACE_MEM_Acceptor");
40 ACE_MEM_Acceptor::~ACE_MEM_Acceptor (void)
42 ACE_TRACE ("ACE_MEM_Acceptor::~ACE_MEM_Acceptor");
43 delete[] this->mmap_prefix_
;
46 // General purpose routine for performing server ACE_SOCK creation.
48 ACE_MEM_Acceptor::ACE_MEM_Acceptor (const ACE_MEM_Addr
&remote_sap
,
53 malloc_options_ (ACE_DEFAULT_BASE_ADDR
, 0),
54 preferred_strategy_ (ACE_MEM_IO::Reactive
)
56 ACE_TRACE ("ACE_MEM_Acceptor::ACE_MEM_Acceptor");
57 if (this->open (remote_sap
,
62 ACE_TEXT ("ACE_MEM_Acceptor::ACE_MEM_Acceptor")));
66 ACE_MEM_Acceptor::open (const ACE_MEM_Addr
&remote_sap
,
71 ACE_TRACE ("ACE_MEM_Acceptor::open");
72 return this->ACE_SOCK_Acceptor::open (remote_sap
.get_local_addr (),
79 // General purpose routine for accepting new connections.
82 ACE_MEM_Acceptor::accept (ACE_MEM_Stream
&new_stream
,
83 ACE_MEM_Addr
*remote_sap
,
84 ACE_Time_Value
*timeout
,
88 ACE_TRACE ("ACE_MEM_Acceptor::accept");
90 int in_blocking_mode
= 1;
91 if (this->shared_accept_start (timeout
,
93 in_blocking_mode
) == -1)
98 struct sockaddr_in inet_addr
;
104 addr
= reinterpret_cast<sockaddr
*> (&inet_addr
);
105 len
= sizeof (inet_addr
);
110 // On Win32 the third parameter to <accept> must be a NULL
111 // pointer if to ignore the client's address.
112 new_stream
.set_handle (ACE_OS::accept (this->get_handle (),
115 while (new_stream
.get_handle () == ACE_INVALID_HANDLE
122 ACE_INET_Addr
temp (&inet_addr
, len
);
123 remote_sap
->set_port_number (temp
.get_port_number ());
127 if (this->shared_accept_finish (new_stream
,
129 reset_new_handle
) == -1)
132 // Allocate 2 * MAXPATHLEN so we can accomodate the unique
133 // name that gets appended later
134 ACE_TCHAR buf
[2 * MAXPATHLEN
+ 1];
136 ACE_INET_Addr local_addr
;
137 if (new_stream
.get_local_addr (local_addr
) == -1)
140 if (this->mmap_prefix_
!= 0)
142 ACE_OS::sprintf (buf
,
145 local_addr
.get_port_number ());
150 // - 24 is so we can append name to the end.
151 if (ACE::get_temp_dir (buf
, MAXPATHLEN
- 24) == -1)
153 ACE_ERROR ((LM_ERROR
,
154 ACE_TEXT ("Temporary path too long, ")
155 ACE_TEXT ("defaulting to current directory\n")));
159 ACE_OS::sprintf (name
,
160 ACE_TEXT ("MEM_Acceptor_%d_"),
161 local_addr
.get_port_number ());
162 ACE_OS::strcat (buf
, name
);
164 ACE_TCHAR unique
[MAXPATHLEN
];
165 ACE_OS::unique_name (&new_stream
, unique
, MAXPATHLEN
);
167 ACE_OS::strcat (buf
, unique
);
169 // Make sure we have a fresh start.
170 ACE_OS::unlink (buf
);
172 new_stream
.disable (ACE_NONBLOCK
);
173 ACE_HANDLE new_handle
= new_stream
.get_handle ();
175 // Protocol negociation:
176 // Tell the client side what level of signaling strategy
178 ACE_MEM_IO::Signal_Strategy client_signaling
=
179 #if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM)
180 this->preferred_strategy_
;
182 // We don't support MT.
183 ACE_MEM_IO::Reactive
;
184 #endif /* ACE_WIN32 || !_ACE_USE_SV_SEM */
185 if (ACE::send (new_handle
, &client_signaling
,
186 sizeof (ACE_INT16
)) == -1)
187 ACE_ERROR_RETURN ((LM_DEBUG
,
188 ACE_TEXT ("ACE_MEM_Acceptor::accept error sending strategy\n")),
191 // Now we get the signaling strategy the client support.
192 if (ACE::recv (new_handle
, &client_signaling
,
193 sizeof (ACE_INT16
)) == -1)
194 ACE_ERROR_RETURN ((LM_DEBUG
,
195 ACE_TEXT ("ACE_MEM_Acceptor::%p error receiving strategy\n"), ACE_TEXT ("accept")),
198 // Ensure minimum buffer size
199 if (this->malloc_options_
.minimum_bytes_
< ACE_MEM_STREAM_MIN_BUFFER
)
200 this->malloc_options_
.minimum_bytes_
= ACE_MEM_STREAM_MIN_BUFFER
;
202 // Client will decide what signaling strategy to use.
204 // Now set up the shared memory malloc pool.
205 if (new_stream
.init (buf
,
206 static_cast<ACE_MEM_IO::Signal_Strategy
> (client_signaling
),
207 &this->malloc_options_
) == -1)
210 // @@ Need to handle timeout here.
211 ACE_UINT16 buf_len
= static_cast<ACE_UINT16
> ((ACE_OS::strlen (buf
) + 1) *
214 // No need to worry about byte-order because both parties should always
215 // be on the same machine.
216 if (ACE::send (new_handle
, &buf_len
, sizeof (ACE_UINT16
)) == -1)
219 // Now send the pathname of the mmap file.
220 if (ACE::send (new_handle
, buf
, buf_len
) == -1)
226 ACE_MEM_Acceptor::shared_accept_finish (ACE_MEM_Stream new_stream
,
227 int in_blocking_mode
,
228 int reset_new_handle
) const
230 ACE_TRACE ("ACE_MEM_Acceptor::shared_accept_finish ()");
232 ACE_HANDLE new_handle
= new_stream
.get_handle ();
234 // Check to see if we were originally in blocking mode, and if so,
235 // set the <new_stream>'s handle and <this> handle to be in blocking
237 if (in_blocking_mode
)
239 // Save/restore errno.
240 ACE_Errno_Guard
error (errno
);
242 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
244 ACE::clr_flags (this->get_handle (),
246 ACE::clr_flags (new_handle
,
250 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
251 if (reset_new_handle
)
252 // Reset the event association inherited by the new handle.
253 ::WSAEventSelect ((SOCKET
) new_handle
, 0, 0);
255 ACE_UNUSED_ARG (reset_new_handle
);
256 #endif /* ACE_WIN32 */
257 if (new_handle
== ACE_INVALID_HANDLE
)
263 ACE_END_VERSIONED_NAMESPACE_DECL
265 #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */