[6916] Fixed typos in spell checking code.
[getmangos.git] / dep / ACE_wrappers / ace / MEM_Acceptor.cpp
blob920c3ca9c485a11ba8b91d2303691753b9369e08
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)
22 void
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)
33 : mmap_prefix_ (0),
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,
49 int reuse_addr,
50 int backlog,
51 int protocol)
52 : mmap_prefix_ (0),
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,
58 reuse_addr,
59 backlog,
60 protocol) == -1)
61 ACE_ERROR ((LM_ERROR,
62 ACE_TEXT ("ACE_MEM_Acceptor::ACE_MEM_Acceptor")));
65 int
66 ACE_MEM_Acceptor::open (const ACE_MEM_Addr &remote_sap,
67 int reuse_addr,
68 int back_log,
69 int protocol)
71 ACE_TRACE ("ACE_MEM_Acceptor::open");
72 return this->ACE_SOCK_Acceptor::open (remote_sap.get_local_addr (),
73 reuse_addr,
74 PF_INET,
75 back_log,
76 protocol);
79 // General purpose routine for accepting new connections.
81 int
82 ACE_MEM_Acceptor::accept (ACE_MEM_Stream &new_stream,
83 ACE_MEM_Addr *remote_sap,
84 ACE_Time_Value *timeout,
85 int restart,
86 int reset_new_handle)
88 ACE_TRACE ("ACE_MEM_Acceptor::accept");
90 int in_blocking_mode = 1;
91 if (this->shared_accept_start (timeout,
92 restart,
93 in_blocking_mode) == -1)
94 return -1;
95 else
97 sockaddr *addr = 0;
98 struct sockaddr_in inet_addr;
99 int *len_ptr = 0;
100 int len = 0;
102 if (remote_sap != 0)
104 addr = reinterpret_cast<sockaddr *> (&inet_addr);
105 len = sizeof (inet_addr);
106 len_ptr = &len;
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 (),
113 addr,
114 len_ptr));
115 while (new_stream.get_handle () == ACE_INVALID_HANDLE
116 && restart != 0
117 && errno == EINTR
118 && timeout == 0);
120 if (remote_sap != 0)
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,
128 in_blocking_mode,
129 reset_new_handle) == -1)
130 return -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)
138 return -1;
140 if (this->mmap_prefix_ != 0)
142 ACE_OS::sprintf (buf,
143 ACE_TEXT ("%s_%d_"),
144 this->mmap_prefix_,
145 local_addr.get_port_number ());
147 else
149 ACE_TCHAR name[25];
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")));
156 buf[0] = 0;
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
177 // we support.
178 ACE_MEM_IO::Signal_Strategy client_signaling =
179 #if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM)
180 this->preferred_strategy_;
181 #else
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")),
189 -1);
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")),
196 -1);
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)
208 return -1;
210 // @@ Need to handle timeout here.
211 ACE_UINT16 buf_len = static_cast<ACE_UINT16> ((ACE_OS::strlen (buf) + 1) *
212 sizeof (ACE_TCHAR));
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)
217 return -1;
219 // Now send the pathname of the mmap file.
220 if (ACE::send (new_handle, buf, buf_len) == -1)
221 return -1;
222 return 0;
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
236 // mode.
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
243 // originally.
244 ACE::clr_flags (this->get_handle (),
245 ACE_NONBLOCK);
246 ACE::clr_flags (new_handle,
247 ACE_NONBLOCK);
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);
254 #else
255 ACE_UNUSED_ARG (reset_new_handle);
256 #endif /* ACE_WIN32 */
257 if (new_handle == ACE_INVALID_HANDLE)
258 return -1;
260 return 0;
263 ACE_END_VERSIONED_NAMESPACE_DECL
265 #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */