diff options
author | michel_j <michel_j@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 2005-09-02 20:12:05 +0000 |
---|---|---|
committer | michel_j <michel_j@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 2005-09-02 20:12:05 +0000 |
commit | 08a5f50bc7f0e82f9b517d7b0936d175fef8d3b0 (patch) | |
tree | c42cf36f9e5efaa3631af811902d4c18526042e6 | |
parent | 2143b11e65a3932c1d2113b90d51e838d47e8744 (diff) | |
download | ATCD-08a5f50bc7f0e82f9b517d7b0936d175fef8d3b0.tar.gz |
Fri Sep 2 15:08:27 2005 Justin Michel <michel_j@ociweb.com>
-rw-r--r-- | ace/ACE.cpp | 124 | ||||
-rw-r--r-- | ace/SOCK_Connector.cpp | 21 | ||||
-rw-r--r-- | ace/config-win32-common.h | 4 | ||||
-rw-r--r-- | tests/MT_SOCK_Test.cpp | 140 |
4 files changed, 177 insertions, 112 deletions
diff --git a/ace/ACE.cpp b/ace/ACE.cpp index 67dbc2277e9..b739eb26d64 100644 --- a/ace/ACE.cpp +++ b/ace/ACE.cpp @@ -2595,40 +2595,33 @@ ACE::handle_timed_complete (ACE_HANDLE h, #else ACE_Handle_Set rd_handles; ACE_Handle_Set wr_handles; - - rd_handles.set_bit (h); - wr_handles.set_bit (h); -#endif /* !ACE_WIN32 && ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */ - -#if defined (ACE_WIN32) // Winsock is different - it sets the exception bit for failed connect, // unlike other platforms, where the read bit is set. + // !!! Some Windows machines with some network cards will set the + // read bit instead of the exception bit. In fact, sometimes only the + // write bit will be set, and you won't know the connection failed + // until you call send/recv. ACE_Handle_Set ex_handles; - ex_handles.set_bit (h); -#endif /* ACE_WIN32 */ - int need_to_check = 0; - int known_failure = 0; + rd_handles.set_bit (h); + wr_handles.set_bit (h); + ex_handles.set_bit (h); +#endif /* !ACE_WIN32 && ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */ -#if defined (ACE_WIN32) - int n = ACE_OS::select (0, // Ignored on Windows: int (h) + 1, - 0, - wr_handles, - ex_handles, - timeout); -#else -# if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT) + bool need_to_check = false; + bool known_failure = false; +#if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT) int n = ACE_OS::poll (&fds, 1, timeout); - -# else - int n = ACE_OS::select (int (h) + 1, +#else + // Use C-style cast because the type of h varies by platform + int select_width = int(h) + 1; + int n = ACE_OS::select (select_width, // Ignored on windows rd_handles, wr_handles, - 0, + ex_handles, // Ignored on unix timeout); -# endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */ -#endif /* ACE_WIN32 */ +#endif /* defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT) */ // If we failed to connect within the time period allocated by the // caller, then we fail (e.g., the remote host might have been too @@ -2653,78 +2646,93 @@ ACE::handle_timed_complete (ACE_HANDLE h, // what getsockopt says about the error. if (ex_handles.is_set (h)) { - need_to_check = 1; - known_failure = 1; + known_failure = true; } -#elif defined (VXWORKS) + else if (rd_handles.is_set(h)) + { + need_to_check = true; + } + else + { + ACE_ASSERT(wr_handles.is_set(h) != 0); + // Depending on the network card or driver, sometimes you can't trust + // the select bits. The easiest, most reliable, and most portable thing + // is to always check the connection. + need_to_check = true; + } +#elif defined (VXWORKS) || defined (AIX) ACE_UNUSED_ARG (is_tli); // Force the check on VxWorks. The read handle for "h" is not set, // so "need_to_check" is false at this point. The write handle is // set, for what it's worth. - need_to_check = 1; + need_to_check = true; #else if (is_tli) - + { # if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT) need_to_check = (fds.revents & POLLIN) && !(fds.revents & POLLOUT); # else need_to_check = rd_handles.is_set (h) && !wr_handles.is_set (h); # endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */ - + } else -#if defined(AIX) - // AIX is broken... both success and failed connect will set the - // write handle only, so always check. - need_to_check = 1; -#else + { # if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT) - need_to_check = (fds.revents & POLLIN); + need_to_check = (fds.revents & POLLIN) != 0; # else - need_to_check = rd_handles.is_set (h); + need_to_check = rd_handles.is_set (h) != 0; # endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */ -#endif /* AIX */ + } #endif /* ACE_WIN32 */ - if (need_to_check) + if (need_to_check || known_failure) { -#if defined (SOL_SOCKET) && defined (SO_ERROR) int sock_err = 0; +#if defined (SOL_SOCKET) && defined (SO_ERROR) int sock_err_len = sizeof (sock_err); int sockopt_ret = ACE_OS::getsockopt (h, SOL_SOCKET, SO_ERROR, (char *)&sock_err, &sock_err_len); - if (sockopt_ret < 0) + if (sockopt_ret >= 0 && sock_err != 0) { h = ACE_INVALID_HANDLE; + errno = sock_err; } - - if (sock_err != 0 || known_failure) + else +#endif + if (known_failure) { h = ACE_INVALID_HANDLE; - errno = sock_err; + errno = ECONNREFUSED; } -#else - char dummy; + else + { - // The following recv() won't block provided that the - // ACE_NONBLOCK flag has not been turned off . - n = ACE::recv (h, &dummy, 1, MSG_PEEK); + // The following recv() won't block provided that the + // ACE_NONBLOCK flag has not been turned off . + char dummy; + n = ACE::recv (h, &dummy, 1, MSG_PEEK); - // If no data was read/peeked at, check to see if it's because - // of a non-connected socket (and therefore an error) or there's - // just no data yet. - if (n <= 0) - { + // If no data was read/peeked at, check to see if it's because + // of a non-connected socket (and therefore an error) or there's + // just no data yet. if (n == 0) { errno = ECONNREFUSED; h = ACE_INVALID_HANDLE; } - else if (errno != EWOULDBLOCK && errno != EAGAIN) - h = ACE_INVALID_HANDLE; + else if (n < 0) + { + sock_err = errno; // errno is a function on win32 + if (sock_err != EWOULDBLOCK && sock_err != EAGAIN) + { + // Ignore the error from recv(), the real error is ... + errno = ECONNREFUSED; + h = ACE_INVALID_HANDLE; + } + } } -#endif - } + } // 1. The HANDLE is ready for writing and doesn't need to be checked or // 2. recv() returned an indication of the state of the socket - if there is diff --git a/ace/SOCK_Connector.cpp b/ace/SOCK_Connector.cpp index 86c09b92f1d..013ca22b73f 100644 --- a/ace/SOCK_Connector.cpp +++ b/ace/SOCK_Connector.cpp @@ -222,30 +222,31 @@ ACE_SOCK_Connector::complete (ACE_SOCK_Stream &new_stream, const ACE_Time_Value *tv) { ACE_TRACE ("ACE_SOCK_Connector::complete"); - ACE_HANDLE h = ACE::handle_timed_complete (new_stream.get_handle (), - tv); + ACE_HANDLE h = ACE::handle_timed_complete (new_stream.get_handle (), tv); // We failed to get connected. if (h == ACE_INVALID_HANDLE) { -#if defined (ACE_WIN32) +#if defined (ACE_HAS_NON_BLOCKING_BUG) // Win32 has a timing problem - if you check to see if the // connection has completed too fast, it will fail - so wait // <ACE_NON_BLOCKING_BUG_DELAY> microseconds to let it catch up // then retry to see if it's a real failure. - ACE_Time_Value time (0, ACE_NON_BLOCKING_BUG_DELAY); - ACE_OS::sleep (time); - h = ACE::handle_timed_complete (new_stream.get_handle (), - tv); + if (errno != ETIME) + { + ACE_Time_Value time (0, ACE_NON_BLOCKING_BUG_DELAY); + ACE_OS::sleep (time); + h = ACE::handle_timed_complete (new_stream.get_handle (), tv); + } if (h == ACE_INVALID_HANDLE) { -#endif /* ACE_WIN32 */ +#endif /* ACE_HAS_NON_BLOCKING_BUG */ // Save/restore errno. ACE_Errno_Guard error (errno); new_stream.close (); return -1; -#if defined (ACE_WIN32) +#if defined (ACE_HAS_NON_BLOCKING_BUG) } -#endif /* ACE_WIN32 */ +#endif /* ACE_HAS_NON_BLOCKING_BUG */ } if (remote_sap != 0) diff --git a/ace/config-win32-common.h b/ace/config-win32-common.h index ff791a5046a..463d5550da4 100644 --- a/ace/config-win32-common.h +++ b/ace/config-win32-common.h @@ -587,5 +587,9 @@ # endif # endif /* !ACE_HAS_WINCE */ +#if !defined (ACE_DEFAULT_BACKLOG) +# define ACE_DEFAULT_BACKLOG SOMAXCONN +#endif /* ACE_DEFAULT_BACKLOG */ + #include /**/ "ace/post.h" #endif /* ACE_CONFIG_WIN32_COMMON_H */ diff --git a/tests/MT_SOCK_Test.cpp b/tests/MT_SOCK_Test.cpp index 0f310d55190..618713c9b52 100644 --- a/tests/MT_SOCK_Test.cpp +++ b/tests/MT_SOCK_Test.cpp @@ -38,6 +38,13 @@ ACE_RCSID(tests, MT_SOCK_Test, "$Id$") static const char ACE_ALPHABET[] = "abcdefghijklmnopqrstuvwxyz"; +// Normally the test will have BACKLOG < NUM_CLIENTS to force some +// of the connections to fail. +// Do NOT use ACE_DEFAULT_BACKLOG here, because that will likely +// be set to some other value. (i.e. Win32 = SOMAXCONN) +static const int BACKLOG = 5; +static const int NUM_CLIENTS = 30; + #if !defined (ACE_LACKS_FORK) || defined (ACE_HAS_THREADS) static void * @@ -57,7 +64,7 @@ client (void *arg) #endif /* ACE_HAS_BROKEN_NON_BLOCKING_CONNECTS */ ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) starting timed connect\n"))); + ACE_TEXT ("(%P|%t) client: Connecting...\n"))); // Initiate timed connection with server. // Attempt a timed connect to the server. @@ -67,57 +74,67 @@ client (void *arg) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("connection failed"))); + ACE_TEXT ("client: Connection timed out."))); return 0; } if (cli_stream.get_local_addr (client_addr) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("get_local_addr")), + ACE_TEXT ("client: get_local_addr")), 0); ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) connected client at %d\n"), + ACE_TEXT ("(%P|%t) client: Connected at %d\n"), client_addr.get_port_number ())); if (cli_stream.disable (ACE_NONBLOCK) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("disable"))); + ACE_TEXT ("client: disable"))); // Send data to server (correctly handles "incomplete writes"). + ACE_DEBUG ((LM_DEBUG, "(%P|%t) client: Sending data...\n")); + for (const char *c = ACE_ALPHABET; *c != '\0'; c++) if (cli_stream.send_n (c, 1) == -1) + { ACE_ERROR ((LM_ERROR, - ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("send_n"))); + ACE_TEXT ("(%P|%t) (%d) %p\n"), errno, + ACE_TEXT ("client: send_n"))); + ACE_ERROR ((LM_ERROR, "client: Closing stream.\n")); + cli_stream.close(); + return 0; + } ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) closing writer\n"))); + ACE_TEXT ("(%P|%t) client: Closing writer...\n"))); // Explicitly close the writer-side of the connection. if (cli_stream.close_writer () == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("close_writer"))); + ACE_TEXT ("client: close_writer"))); char buf[1]; + ACE_DEBUG ((LM_DEBUG, + ACE_TEXT ("(%P|%t) client: Waiting for server handshake...\n"))); + // Wait for handshake with server. if (cli_stream.recv_n (buf, 1) != 1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("recv_n"))); + ACE_TEXT ("client: recv_n"))); ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) received handshake from server\n"))); + ACE_TEXT ("(%P|%t) client: Handshake received. Closing stream.\n"))); // Close the connection completely. if (cli_stream.close () == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("close"))); + ACE_TEXT ("client: close"))); return 0; } @@ -125,12 +142,12 @@ static void * server (void *arg) { ACE_SOCK_Acceptor *peer_acceptor = - static_cast<ACE_SOCK_Acceptor *> (arg); + ACE_static_cast (ACE_SOCK_Acceptor *, arg); if (peer_acceptor->enable (ACE_NONBLOCK) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("enable"))); + ACE_TEXT ("server: enable acceptor"))); // Keep these objects out here to prevent excessive constructor // calls... @@ -140,8 +157,15 @@ server (void *arg) const ACE_Time_Value def_timeout (ACE_DEFAULT_TIMEOUT); ACE_Time_Value tv (def_timeout); - // Performs the iterative server activities. + // We want some of the clients to get connection failures, but on + // a really fast machine with a good network card and multiple + // processors this may never happen. + // Add a sleep() to allow the client threads to complete. + ACE_OS::sleep(def_timeout); + int num_clients_connected = 0; + + // Performs the iterative server activities. for (;;) { char buf[BUFSIZ]; @@ -149,6 +173,8 @@ server (void *arg) handle_set.reset (); handle_set.set_bit (peer_acceptor->get_handle ()); + ACE_DEBUG((LM_DEBUG, "(%P|%t) server: Waiting for connection...\n")); + int select_width; # if defined (ACE_WIN64) // This arg is ignored on Windows and causes pointer truncation @@ -157,20 +183,26 @@ server (void *arg) # else select_width = int (peer_acceptor->get_handle ()) + 1; # endif /* ACE_WIN64 */ - int result = ACE_OS::select (select_width, - handle_set, - 0, 0, &tv); + int result = ACE_OS::select (select_width, handle_set, 0, 0, &tv); ACE_ASSERT (tv == def_timeout); if (result == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("select")), + ACE_TEXT ("server: select acceptor")), 0); else if (result == 0) { ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) select timed out, shutting down\n"))); + ACE_TEXT ("(%P|%t) server: Test finished.\n"))); + // The meaning of the backlog parameter for listen() varies by platform. For + // some reason lost to history, the specified value is typically + // backlog * 1.5, backlog * 1.5 + 1, or event taken literally as on Windows. + // We'll accept any number less than backlog * 2 as valid. + if (num_clients_connected >= BACKLOG * 2) + ACE_ERROR((LM_ERROR, + "(%P|%t) server: Incorrect # client connections. Expected:%d-%d Actual:%d\n", + BACKLOG, BACKLOG * 2, num_clients_connected)); return 0; } @@ -182,8 +214,10 @@ server (void *arg) { const char *t = ACE_ALPHABET; + ++num_clients_connected; + ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) client %s connected from %d\n"), + ACE_TEXT ("(%P|%t) server: Client %s connected from %d\n"), ACE_TEXT_CHAR_TO_TCHAR(cli_addr.get_host_name ()), cli_addr.get_port_number ())); @@ -191,17 +225,17 @@ server (void *arg) if (new_stream.enable (ACE_NONBLOCK) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("enable")), + ACE_TEXT ("server: enable non blocking i/o")), 0); handle_set.reset (); handle_set.set_bit (new_stream.get_handle ()); // Read data from client (terminate on error). + ACE_DEBUG ((LM_DEBUG, + ACE_TEXT ("(%P|%t) server: Waiting for data...\n"))); for (ssize_t r_bytes; ;) { - ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) waiting in select\n"))); int select_width; # if defined (ACE_WIN64) // This arg is ignored on Windows and causes pointer truncation @@ -217,39 +251,48 @@ server (void *arg) ACE_TEXT ("(%P|%t) %p\n"), ACE_TEXT ("select")), 0); + + ACE_DEBUG((LM_DEBUG, "(%P|%t) server: Receiving data...\n")); + while ((r_bytes = new_stream.recv (buf, 1)) > 0) { ACE_ASSERT (*t == buf[0]); t++; } + ACE_DEBUG((LM_DEBUG, "(%P|%t) server: Received data.\n")); + if (r_bytes == 0) { // Handshake back with client. ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) reached end of input, connection closed by client\n"))); + ACE_TEXT ("(%P|%t) server: Connection closed by client.\n"))); + + ACE_DEBUG ((LM_DEBUG, "(%P|%t) server: Sending handshake.\n")); if (new_stream.send_n ("", 1) != 1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("send_n"))); + ACE_TEXT ("server: send_n"))); + + ACE_DEBUG((LM_DEBUG, "(%P|%t) server: Closing stream.\n")); // Close endpoint. if (new_stream.close () == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("close"))); + ACE_TEXT ("server: close"))); break; } else if (r_bytes == -1) { if (errno == EWOULDBLOCK || errno == EAGAIN) ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) no input available, going back to reading\n"))); + ACE_TEXT ("(%P|%t) server: (EWOULDBLOCK) Waiting for more data...\n"))); else ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("recv_n")), + ACE_TEXT ("server: recv_n")), 0); } } @@ -258,11 +301,11 @@ server (void *arg) { if (errno == EWOULDBLOCK) ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) no connections available, going back to accepting\n"))); + ACE_TEXT ("(%P|%t) server: No more connections pending.\n"))); else ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("accept"))); + ACE_TEXT ("server: accept"))); } } ACE_NOTREACHED (return 0); @@ -271,7 +314,7 @@ server (void *arg) #endif /* !ACE_LACKS_FORK || ACE_HAS_THREADS */ static void -spawn (void) +spawn (int num_clients) { // Acceptor ACE_SOCK_Acceptor peer_acceptor; @@ -280,11 +323,11 @@ spawn (void) ACE_INET_Addr server_addr; // Bind listener to any port and then find out what the port was. - if (peer_acceptor.open (ACE_Addr::sap_any) == -1 + if (peer_acceptor.open (ACE_Addr::sap_any, 0, PF_UNSPEC, BACKLOG) == -1 || peer_acceptor.get_local_addr (server_addr) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), - ACE_TEXT ("open"))); + ACE_TEXT ("spawn: open"))); else { ACE_DEBUG ((LM_DEBUG, @@ -292,14 +335,14 @@ spawn (void) server_addr.get_port_number ())); #if !defined (ACE_LACKS_FORK) - for (size_t i = 0; i < ACE_MAX_CLIENTS; i++) + for (int i = 0; i < num_clients; i++) { switch (ACE_OS::fork (ACE_TEXT ("child"))) { case -1: ACE_ERROR ((LM_ERROR, - "(%P|%t) %p\n", "fork failed")); - i = ACE_MAX_CLIENTS; + "(%P|%t) %p\n", "spawn: fork failed")); + i = num_clients; // Break out of 'for' loop. break; case 0: @@ -318,37 +361,46 @@ spawn (void) // Reap the child pids. for (pid_t pid; (pid = ACE_OS::wait ()) != -1; ) ACE_DEBUG ((LM_DEBUG, - "(%P|%t) reaping pid %d\n", - pid)); + "(%P|%t) spawn: reaping pid %d\n", pid)); #elif defined (ACE_HAS_THREADS) + + ACE_DEBUG((LM_DEBUG, "Spawning server...\n")); + if (ACE_Thread_Manager::instance ()->spawn (ACE_THR_FUNC (server), (void *) &peer_acceptor, THR_BOUND | THR_DETACHED) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n%a"), - ACE_TEXT ("spawn failed"), + ACE_TEXT ("spawn: failed"), 1)); + ACE_DEBUG((LM_DEBUG, "Spawning %d clients...\n", num_clients)); + if (ACE_Thread_Manager::instance ()->spawn_n - (ACE_MAX_CLIENTS, + (num_clients, ACE_THR_FUNC (client), (void *) &server_addr, THR_BOUND | THR_DETACHED) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n%a"), - ACE_TEXT ("spawn failed"), + ACE_TEXT ("spawn: failed 2"), 1)); + ACE_DEBUG((LM_DEBUG, "Waiting for threads to finish...\n")); + // Wait for the threads to exit. ACE_Thread_Manager::instance ()->wait (); #else ACE_ERROR ((LM_INFO, ACE_TEXT ("(%P|%t) ") - ACE_TEXT ("only one thread may be run") + ACE_TEXT ("spawn: only one thread may be run") ACE_TEXT (" in a process on this platform\n"))); #endif /* !ACE_LACKS_FORK */ + + ACE_DEBUG((LM_DEBUG, "Threads complete. Closing Acceptor.\n")); + peer_acceptor.close (); } } @@ -358,7 +410,7 @@ run_main (int, ACE_TCHAR *[]) { ACE_START_TEST (ACE_TEXT ("MT_SOCK_Test")); - spawn (); + spawn (NUM_CLIENTS); ACE_END_TEST; return 0; |