3 //=============================================================================
5 * @file MT_SOCK_Test.cpp
7 * This is a multi-threaded torture test of the
8 * <ACE_SOCK_Acceptor> and <ACE_SOCK_Connector> classes. The test
9 * forks 30 processes or spawns 30 threads (depending upon the
10 * platform) and then executes client and server allowing them to
11 * connect and exchange data. Note that most of the connections
12 * will fail since we're overrunning the size of the listen queue
13 * for the acceptor-mode socket.
15 * @author Doug Schmidt <d.schmidt@vanderbilt.edu>
17 //=============================================================================
20 #include "test_config.h"
21 #include "ace/OS_NS_sys_wait.h"
22 #include "ace/OS_NS_unistd.h"
23 #include "ace/Thread.h"
24 #include "ace/Thread_Manager.h"
25 #include "ace/SOCK_Connector.h"
26 #include "ace/SOCK_Acceptor.h"
27 #include "ace/Handle_Set.h"
28 #include "ace/Time_Value.h"
30 static const char ACE_ALPHABET
[] = "abcdefghijklmnopqrstuvwxyz";
32 // Normally the test will have BACKLOG < NUM_CLIENTS to force some
33 // of the connections to fail.
34 // Do NOT use ACE_DEFAULT_BACKLOG here, because that will likely
35 // be set to some other value. (i.e. Win32 = SOMAXCONN)
36 static const int BACKLOG
= 5;
37 static const int NUM_CLIENTS
= 30;
39 #if !defined (ACE_LACKS_FORK) || defined (ACE_HAS_THREADS)
44 ACE_INET_Addr
*remote_addr
= (ACE_INET_Addr
*) arg
;
45 ACE_INET_Addr
server_addr (remote_addr
->get_port_number (),
46 ACE_DEFAULT_SERVER_HOST
);
47 ACE_INET_Addr client_addr
;
48 ACE_SOCK_Stream cli_stream
;
49 ACE_SOCK_Connector con
;
50 ACE_Time_Value
tv (ACE_DEFAULT_TIMEOUT
);
51 ACE_Time_Value
*timeout
= &tv
;
53 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%P|%t) client: Connecting...\n")));
54 // Initiate timed connection with server.
56 // Attempt a timed connect to the server.
57 if (con
.connect (cli_stream
,
62 ACE_TEXT ("(%P|%t) %p\n"),
63 ACE_TEXT ("client: Connection timed out.")));
67 if (cli_stream
.get_local_addr (client_addr
) == -1)
68 ACE_ERROR_RETURN ((LM_ERROR
,
69 ACE_TEXT ("(%P|%t) %p\n"),
70 ACE_TEXT ("client: get_local_addr")),
74 ACE_TEXT ("(%P|%t) client: Connected at %d\n"),
75 client_addr
.get_port_number ()));
77 if (cli_stream
.disable (ACE_NONBLOCK
) == -1)
79 ACE_TEXT ("(%P|%t) %p\n"),
80 ACE_TEXT ("client: disable")));
82 // Send data to server (correctly handles "incomplete writes").
84 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) client: Sending data...\n"));
86 for (const char *c
= ACE_ALPHABET
; *c
!= '\0'; c
++)
87 if (cli_stream
.send_n (c
, 1) == -1)
89 // This is, I believe, more of an issue with WinXP-64 _server_
90 // side, but we can trap it here since we know we're connecting
91 // to localhost. Some Windows versions will appear to accept
92 // connections at the TCP level past the listen backlog but if
93 // data arrives before the actual application-level accept() occurs,
94 // the connection is reset. This is caused when we trip the Windows
95 // SYN attack prevention (http://technet2.microsoft.com/WindowsServer/
96 // en/library/910c8482-e5e5-4e2c-9ea4-11301ddfc4661033.mspx?mfr=true)
97 // So, if we get a reset on the first send, don't flag the error -
98 // just note it and act like the connection was refused.
99 if (c
== ACE_ALPHABET
&& errno
== ECONNRESET
) // First byte sent
103 ACE_TEXT ("(%P|%t) client: Connection refused (delayed)\n")));
108 ACE_ERROR ((LM_ERROR
,
109 ACE_TEXT ("(%P|%t) (errno %d) %p\n"), ACE_ERRNO_GET
,
110 ACE_TEXT ("client: send_n")));
111 ACE_ERROR ((LM_ERROR
, "client: Closing stream.\n"));
116 ACE_DEBUG ((LM_DEBUG
,
117 ACE_TEXT ("(%P|%t) client: Closing writer...\n")));
119 // Explicitly close the writer-side of the connection.
120 if (cli_stream
.close_writer () == -1)
121 ACE_ERROR ((LM_ERROR
,
122 ACE_TEXT ("(%P|%t) %p\n"),
123 ACE_TEXT ("client: close_writer")));
126 ACE_DEBUG ((LM_DEBUG
,
127 ACE_TEXT ("(%P|%t) client: Waiting for server handshake...\n")));
129 // Wait for handshake with server.
130 if (cli_stream
.recv_n (buf
, 1) != 1)
131 ACE_ERROR ((LM_ERROR
,
132 ACE_TEXT ("(%P|%t) %p\n"),
133 ACE_TEXT ("client: recv_n")));
137 ACE_TEXT ("(%P|%t) client: Handshake received. Closing stream.\n")));
139 // Close the connection completely.
140 if (cli_stream
.close () == -1)
141 ACE_ERROR ((LM_ERROR
,
142 ACE_TEXT ("(%P|%t) %p\n"),
143 ACE_TEXT ("client: close")));
150 ACE_SOCK_Acceptor
*peer_acceptor
=
151 static_cast<ACE_SOCK_Acceptor
*> (arg
);
153 if (peer_acceptor
->enable (ACE_NONBLOCK
) == -1)
154 ACE_ERROR ((LM_ERROR
,
155 ACE_TEXT ("(%P|%t) %p\n"),
156 ACE_TEXT ("server: enable acceptor")));
158 // Keep these objects out here to prevent excessive constructor
160 ACE_SOCK_Stream new_stream
;
161 ACE_INET_Addr cli_addr
;
162 const ACE_Time_Value
def_timeout (ACE_DEFAULT_TIMEOUT
);
163 ACE_Time_Value
tv (def_timeout
);
165 // We want some of the clients to get connection failures, but on
166 // a really fast machine with a good network card and multiple
167 // processors this may never happen.
168 // Add a sleep() to allow the client threads to complete.
169 ACE_OS::sleep(def_timeout
);
171 int num_clients_connected
= 0;
173 // Performs the iterative server activities.
178 ACE_DEBUG((LM_DEBUG
, "(%P|%t) server: Waiting for connection...\n"));
180 int result
= ACE::handle_read_ready (peer_acceptor
->get_handle (), &tv
);
181 ACE_TEST_ASSERT (tv
== def_timeout
);
187 ACE_DEBUG ((LM_DEBUG
,
188 ACE_TEXT ("(%P|%t) server: Test finished.\n")));
189 // The meaning of the backlog parameter for listen() varies by
190 // platform. For some reason lost to history, the specified value
191 // is typically backlog * 1.5, backlog * 1.5 + 1, or even taken
192 // literally as on Windows. We'll accept any number less than
193 // backlog * 2 as valid.
194 if (num_clients_connected
> BACKLOG
* 2)
195 ACE_ERROR ((LM_ERROR
,
196 ACE_TEXT ("(%P|%t) server: Incorrect # client ")
197 ACE_TEXT ("connections. Expected:%d-%d Actual:%d\n"),
198 BACKLOG
, BACKLOG
* 2, num_clients_connected
));
202 ACE_ERROR_RETURN ((LM_ERROR
,
203 ACE_TEXT ("(%P|%t) %p\n"),
204 ACE_TEXT ("server: handle_read_ready acceptor")),
208 // Create a new ACE_SOCK_Stream endpoint (note automatic restart
209 // if errno == EINTR).
211 while ((result
= peer_acceptor
->accept (new_stream
,
214 const char *t
= ACE_ALPHABET
;
216 ++num_clients_connected
;
218 ACE_DEBUG ((LM_DEBUG
,
219 ACE_TEXT ("(%P|%t) server: Client %C connected from %d\n"),
220 cli_addr
.get_host_name (),
221 cli_addr
.get_port_number ()));
223 // Enable non-blocking I/O.
224 if (new_stream
.enable (ACE_NONBLOCK
) == -1)
225 ACE_ERROR_RETURN ((LM_ERROR
,
226 ACE_TEXT ("(%P|%t) %p\n"),
227 ACE_TEXT ("server: enable non blocking i/o")),
229 // Read data from client (terminate on error).
230 ACE_DEBUG ((LM_DEBUG
,
231 ACE_TEXT ("(%P|%t) server: Waiting for data...\n")));
233 for (ssize_t r_bytes
; ;)
235 if (ACE::handle_read_ready (new_stream
.get_handle (), 0) == -1)
236 ACE_ERROR_RETURN ((LM_ERROR
,
237 ACE_TEXT ("(%P|%t) %p\n"),
238 ACE_TEXT ("stream handle_read_ready")),
241 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) server: Receiving data...\n"));
243 while ((r_bytes
= new_stream
.recv (buf
, 1)) > 0)
245 ACE_TEST_ASSERT (*t
== buf
[0]);
249 ACE_DEBUG((LM_DEBUG
, "(%P|%t) server: Received data.\n"));
253 // Handshake back with client.
256 ACE_TEXT ("(%P|%t) server: Connection closed by client.\n")));
260 ACE_TEXT ("(%P|%t) server: Sending handshake.\n")));
262 if (new_stream
.send_n ("", 1) != 1)
263 ACE_ERROR ((LM_ERROR
,
264 ACE_TEXT ("(%P|%t) %p\n"),
265 ACE_TEXT ("server: send_n")));
267 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) server: Closing stream.\n"));
270 if (new_stream
.close () == -1)
271 ACE_ERROR ((LM_ERROR
,
272 ACE_TEXT ("(%P|%t) %p\n"),
273 ACE_TEXT ("server: close")));
276 else if (r_bytes
== -1)
278 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
279 ACE_DEBUG ((LM_DEBUG
,
280 ACE_TEXT ("(%P|%t) server: (EWOULDBLOCK) Waiting for more data...\n")));
282 ACE_ERROR_RETURN ((LM_ERROR
,
283 ACE_TEXT ("(%P|%t) %p\n"),
284 ACE_TEXT ("server: recv_n")),
291 if (errno
== EWOULDBLOCK
)
292 ACE_DEBUG ((LM_DEBUG
,
293 ACE_TEXT ("(%P|%t) server: No more connections pending.\n")));
295 ACE_ERROR ((LM_ERROR
,
296 ACE_TEXT ("(%P|%t) %p\n"),
297 ACE_TEXT ("server: accept")));
300 ACE_NOTREACHED (return 0);
303 #endif /* !ACE_LACKS_FORK || ACE_HAS_THREADS */
306 spawn (int num_clients
)
309 ACE_SOCK_Acceptor peer_acceptor
;
311 // Create a server address.
312 ACE_INET_Addr server_addr
;
314 // Bind listener to any port and then find out what the port was.
315 if (peer_acceptor
.open (ACE_Addr::sap_any
, 0, PF_UNSPEC
, BACKLOG
) == -1
316 || peer_acceptor
.get_local_addr (server_addr
) == -1)
317 ACE_ERROR ((LM_ERROR
,
318 ACE_TEXT ("(%P|%t) %p\n"),
319 ACE_TEXT ("spawn: open")));
322 ACE_DEBUG ((LM_DEBUG
,
323 ACE_TEXT ("(%P|%t) starting server at port %d\n"),
324 server_addr
.get_port_number ()));
326 #if !defined (ACE_LACKS_FORK)
327 for (int i
= 0; i
< num_clients
; i
++)
329 switch (ACE_OS::fork (ACE_TEXT ("child")))
332 ACE_ERROR ((LM_ERROR
,
333 ACE_TEXT ("(%P|%t) %p\n"), "spawn: fork failed"));
335 // Break out of 'for' loop.
338 ACE_LOG_MSG
->sync (ACE_TEXT ("MT_SOCK_Test-child"));
339 client (&server_addr
);
347 server ((void *) &peer_acceptor
);
349 peer_acceptor
.close();
351 // Reap the child pids.
352 for (pid_t pid
; (pid
= ACE_OS::waitpid (0, 0, 0)) != -1; )
353 ACE_DEBUG ((LM_DEBUG
,
354 ACE_TEXT ("(%P|%t) spawn: reaping pid %d\n"), pid
));
356 #elif defined (ACE_HAS_THREADS)
358 ACE_DEBUG((LM_DEBUG
, "Spawning server...\n"));
360 if (ACE_Thread_Manager::instance ()->spawn
361 (ACE_THR_FUNC (server
),
362 (void *) &peer_acceptor
,
363 THR_BOUND
| THR_DETACHED
) == -1)
364 ACE_ERROR ((LM_ERROR
,
365 ACE_TEXT ("(%P|%t) %p\n%a"),
366 ACE_TEXT ("spawn: failed"),
369 ACE_DEBUG((LM_DEBUG
, "Spawning %d clients...\n", num_clients
));
371 if (ACE_Thread_Manager::instance ()->spawn_n
373 ACE_THR_FUNC (client
),
374 (void *) &server_addr
,
375 THR_BOUND
| THR_DETACHED
) == -1)
376 ACE_ERROR ((LM_ERROR
,
377 ACE_TEXT ("(%P|%t) %p\n%a"),
378 ACE_TEXT ("spawn: failed 2"),
381 ACE_DEBUG((LM_DEBUG
, "Waiting for threads to finish...\n"));
383 // Wait for the threads to exit.
384 ACE_Thread_Manager::instance ()->wait ();
387 ACE_TEXT ("(%P|%t) ")
388 ACE_TEXT ("spawn: only one thread may be run")
389 ACE_TEXT (" in a process on this platform\n")));
390 #endif /* !ACE_LACKS_FORK */
392 ACE_DEBUG ((LM_DEBUG
,
393 ACE_TEXT ("Threads complete. Closing Acceptor.\n")));
395 peer_acceptor
.close ();
400 run_main (int, ACE_TCHAR
*[])
402 ACE_START_TEST (ACE_TEXT ("MT_SOCK_Test"));
404 #ifndef ACE_LACKS_ACCEPT