You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by nm...@apache.org on 2008/04/12 18:30:24 UTC
svn commit: r647467 - in
/activemq/activemq-cpp/decaf/trunk/src/main/decaf/net: SocketFactory.cpp
TcpSocket.cpp TcpSocket.h
Author: nmittler
Date: Sat Apr 12 09:30:21 2008
New Revision: 647467
URL: http://svn.apache.org/viewvc?rev=647467&view=rev
Log:
[AMQCPP-168] Applying patch for adding soConnectTimeout switch to decaf TcpSocket
Modified:
activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/SocketFactory.cpp
activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp
activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.h
Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/SocketFactory.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/SocketFactory.cpp?rev=647467&r1=647466&r2=647467&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/SocketFactory.cpp (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/SocketFactory.cpp Sat Apr 12 09:30:21 2008
@@ -90,6 +90,11 @@
bool tcpNoDelay =
properties.getProperty( "tcpNoDelay", "true" ) == "true";
+ // Get the socket connect timeout in microseconds.
+ int connectTimeout = -1;
+ dummy = properties.getProperty( "soConnectTimeout", "-1" );
+ sscanf( dummy.c_str(), "%d", &connectTimeout );
+
// Now that we have all the elements that we wanted - let's do it!
// Create a TCP Socket and then Wrap it in a buffered socket
// so that users get the benefit of buffered reads and writes.
@@ -100,7 +105,7 @@
try {
// Connect the socket.
- tcpSocket->connect( host.c_str(), port );
+ tcpSocket->connect( host.c_str(), port, connectTimeout );
// Set the socket options.
tcpSocket->setSoLinger( soLinger );
Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp?rev=647467&r1=647466&r2=647467&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.cpp Sat Apr 12 09:30:21 2008
@@ -84,7 +84,7 @@
}
////////////////////////////////////////////////////////////////////////////////
-void TcpSocket::connect(const char* host, int port) throw ( SocketException ) {
+void TcpSocket::connect(const char* host, int port, int timeout) throw ( SocketException ) {
try{
@@ -101,20 +101,25 @@
checkResult( apr_socket_create(
&socketHandle, socketAddress->family, SOCK_STREAM, APR_PROTO_TCP, apr_pool.getAprPool() ) );
- // it is a good idea to specify socket options explicitly. in this
- // case, we make a blocking socket with system timeout, this should allow us
- // the time needed to connect to the socket before returning, but not
- // block us forever waiting if there isn't anyone there. Some poeple
- // say that you shouldn't use this mode as its an OS timeout that is not
- // controllable, if this becomes an issue, we should decide on what a
- // reasonable timeout should be and set it here.
- apr_socket_opt_set( socketHandle, APR_SO_NONBLOCK, 0 );
- apr_socket_timeout_set( socketHandle, -1 );
+ // To make blocking-with-timeout sockets, we have to set it to
+ // 'APR_SO_NONBLOCK==1(on) and timeout>0'. On Unix, we have no
+ // problem to specify 'APR_SO_NONBLOCK==0(off) and timeout>0'.
+ // Unfortunatelly, we have a problem on Windows. Setting the
+ // mode to 'APR_SO_NONBLOCK==0(off) and timeout>0' causes
+ // blocking-with-system-timeout sockets on Windows.
+ //
+ // http://dev.ariel-networks.com/apr/apr-tutorial/html/apr-tutorial-13.html
+
+ // If we have a connection timeout specified, temporarily set the socket to
+ // non-blocking so that we can timeout the connect operation. We'll restore
+ // to blocking mode right after we connect.
+ apr_socket_opt_set( socketHandle, APR_SO_NONBLOCK, (timeout>0)?1:0 );
+ apr_socket_timeout_set( socketHandle, timeout );
+
+ // Connect to the broker.
+ checkResult(apr_socket_connect( socketHandle, socketAddress ));
- checkResult( apr_socket_connect( socketHandle, socketAddress ) );
-
- // Now that we are connected, we want to set ourself up as a blocking
- // socket by default.
+ // Now that we are connected, we want to go back to blocking.
apr_socket_opt_set( socketHandle, APR_SO_NONBLOCK, 0 );
apr_socket_timeout_set( socketHandle, -1 );
Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.h?rev=647467&r1=647466&r2=647467&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.h (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/net/TcpSocket.h Sat Apr 12 09:30:21 2008
@@ -98,9 +98,22 @@
* connected to another destination.
* @param host The host of the server to connect to.
* @param port The port of the server to connect to.
+ * @param timeout of socket in microseconds
* @throws IOException Thrown if a failure occurred in the connect.
*/
- virtual void connect( const char* host, int port ) throw( SocketException );
+ void connect( const char* host, int port, int timeout) throw( SocketException );
+
+ /**
+ * Connects to the specified destination. Closes this socket if
+ * connected to another destination.
+ * @param host The host of the server to connect to.
+ * @param port The port of the server to connect to.
+ * @throws IOException Thrown if a failure occurred in the connect.
+ */
+ virtual void connect( const char* host, int port ) throw(SocketException)
+ {
+ connect(host,port,-1);
+ }
/**
* Indicates whether or not this socket is connected to a destination.