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 2007/01/27 17:25:52 UTC

svn commit: r500558 - in /incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq: network/ transport/

Author: nmittler
Date: Sat Jan 27 08:25:51 2007
New Revision: 500558

URL: http://svn.apache.org/viewvc?view=rev&rev=500558
Log:
[AMQCPP-58] Various cleanup to free resources during socket error conditions

Modified:
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketFactory.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.h

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.cpp?view=diff&rev=500558&r1=500557&r2=500558
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.cpp (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.cpp Sat Jan 27 08:25:51 2007
@@ -28,7 +28,13 @@
 BufferedSocket::BufferedSocket( Socket* socket,
                                 int inputBufferSize,
                                 int outputBufferSize,
-                                bool own )
+                                bool own ) :
+    socket(NULL),
+    own(false),
+    inputStream(NULL),
+    outputStream(NULL),
+    inputBufferSize(0),
+    outputBufferSize(0)
 {
     if(inputBufferSize < 0 || outputBufferSize < 0 )
     {
@@ -58,27 +64,10 @@
 {
     try
     {
-        if( outputStream )
-        {
-            // Ensure all data is written
-            outputStream->flush();
-        }
-
-        // Close the socket      
-        socket->close();
-         
-        // if we own it, delete it.
-        if( own )
-        {
-            delete socket;
-        }
-      
-        // Clean up our streams.
-        delete inputStream;
-        delete outputStream;
+        close();       
     }
     AMQ_CATCH_NOTHROW( ActiveMQException )
-    AMQ_CATCHALL_NOTHROW( )
+    AMQ_CATCHALL_NOTHROW()
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -112,18 +101,32 @@
 {
     try
     {
-        // Ensure all data writen
-        outputStream->flush();
-      
-        // Close the Socket
-        socket->close();
-      
-        // Remove old stream, recreate if reconnected
-        delete inputStream;
-        delete outputStream;
-      
-        inputStream = NULL;
-        outputStream = NULL;
+        if( outputStream != NULL )
+        {
+            // Ensure all data is written
+            outputStream->flush();
+            
+            delete outputStream;
+            outputStream = NULL;
+        }
+        
+        if( inputStream != NULL ){
+            delete inputStream;
+            inputStream = NULL;
+        }
+
+        if( socket != NULL ){
+            // Close the socket
+            try{   
+                socket->close();
+            } catch( cms::CMSException& ex ){ /* Absorb */ }
+             
+            // if we own it, delete it.
+            if( own ) {
+                delete socket;                
+            }
+            socket = NULL;
+        }
     }
     AMQ_CATCH_RETHROW( SocketException )
     AMQ_CATCH_EXCEPTION_CONVERT( ActiveMQException, SocketException )

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.h?view=diff&rev=500558&r1=500557&r2=500558
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/BufferedSocket.h Sat Jan 27 08:25:51 2007
@@ -87,7 +87,11 @@
          * Indicates whether or not this socket is connected to a destination.
          * @return true if connected
          */
-        virtual bool isConnected() const{
+        virtual bool isConnected() const{            
+            if( socket == NULL ){
+                return false;
+            }
+            
             return socket->isConnected();
         }
 
@@ -113,6 +117,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual int getSoLinger() const throw( SocketException ){
+            checkSocket();
             return socket->getSoLinger();
         }
       
@@ -122,6 +127,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual void setSoLinger( int linger ) throw( SocketException ){
+            checkSocket();
             socket->setSoLinger( linger );
         }
       
@@ -131,6 +137,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual bool getKeepAlive() const throw( SocketException ){
+            checkSocket();
             return socket->getKeepAlive();
         }
       
@@ -140,6 +147,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual void setKeepAlive( bool keepAlive ) throw( SocketException ){
+            checkSocket();
             socket->setKeepAlive( keepAlive );
         }
       
@@ -149,6 +157,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual int getReceiveBufferSize() const throw( SocketException ){
+            checkSocket();
             return socket->getReceiveBufferSize();
         }
       
@@ -158,6 +167,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual void setReceiveBufferSize( int size ) throw( SocketException ){
+            checkSocket();
             socket->setReceiveBufferSize( size );
         }
       
@@ -167,6 +177,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual bool getReuseAddress() const throw( SocketException ){
+            checkSocket();
             return socket->getReuseAddress();
         }
       
@@ -176,6 +187,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual void setReuseAddress( bool reuse ) throw( SocketException ){
+            checkSocket();
             socket->setReuseAddress( reuse );
         }
       
@@ -185,6 +197,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual int getSendBufferSize() const throw( SocketException ){
+            checkSocket();
             return socket->getSendBufferSize();
         }
       
@@ -194,6 +207,7 @@
          * @throws SocketException if the operation fails.
          */
         virtual void setSendBufferSize( int size ) throw( SocketException ){
+            checkSocket();
             socket->setSendBufferSize( size );
         }
       
@@ -203,6 +217,7 @@
          * @throws SocketException Thrown if unable to retrieve the information.
          */
         virtual int getSoTimeout() const throw( SocketException ){
+            checkSocket();
             return socket->getSoTimeout();
         }
       
@@ -212,7 +227,16 @@
          * @throws SocketException Thrown if unable to set the information.
          */
         virtual void setSoTimeout( int timeout ) throw( SocketException ){
+            checkSocket();
             socket->setSoTimeout( timeout );
+        }
+        
+    private:
+    
+        void checkSocket() const throw ( SocketException ) {
+            if( socket == NULL ) {
+                throw SocketException( __FILE__, __LINE__, "socket is NULL" );
+            }
         }
 
     };

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketFactory.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketFactory.cpp?view=diff&rev=500558&r1=500557&r2=500558
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketFactory.cpp (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketFactory.cpp Sat Jan 27 08:25:51 2007
@@ -77,13 +77,13 @@
             properties.getProperty( "soKeepAlive", "false" ) == "true";   
       
         // Get the socket receive buffer size.
-        int soReceiveBufferSize = 2000000;
-        dummy = properties.getProperty( "soReceiveBufferSize", "2000000" );  
+        int soReceiveBufferSize = -1;
+        dummy = properties.getProperty( "soReceiveBufferSize", "-1" );  
         sscanf( dummy.c_str(), "%d", &soReceiveBufferSize );
       
         // Get the socket send buffer size.
-        int soSendBufferSize = 2000000;
-        dummy = properties.getProperty( "soSendBufferSize", "2000000" );  
+        int soSendBufferSize = -1;
+        dummy = properties.getProperty( "soSendBufferSize", "-1" );  
         sscanf( dummy.c_str(), "%d", &soSendBufferSize );
       
         // Now that we have all the elements that we wanted - let's do it!
@@ -92,19 +92,37 @@
         // The buffered socket will own the TcpSocket instance, and will
         // clean it up when it is cleaned up.
         TcpSocket* tcpSocket = new TcpSocket();
-        BufferedSocket* socket = 
+        BufferedSocket* bufferedSocket = 
             new BufferedSocket(tcpSocket, inputBufferSize, outputBufferSize);
-      
-        // Connect the socket.
-        socket->connect( host.c_str(), port );
-      
-        // Set the socket options.
-        socket->setSoLinger( soLinger );
-        socket->setKeepAlive( soKeepAlive );
-        socket->setReceiveBufferSize( soReceiveBufferSize );
-        socket->setSendBufferSize( soSendBufferSize );
 
-        return socket;
+        try
+        {
+            // Connect the socket.
+            bufferedSocket->connect( host.c_str(), port );
+
+            // Set the socket options.
+            bufferedSocket->setSoLinger( soLinger );
+            bufferedSocket->setKeepAlive( soKeepAlive );
+
+            if( soReceiveBufferSize > 0 ){
+                bufferedSocket->setReceiveBufferSize( soReceiveBufferSize );
+            }
+
+            if( soSendBufferSize > 0 ){
+                bufferedSocket->setSendBufferSize( soSendBufferSize );
+            }
+        }
+        catch ( SocketException& ex )
+        {
+            ex.setMark( __FILE__, __LINE__ );
+            try{
+                delete bufferedSocket;
+            } catch( SocketException& ex2 ){ /* Absorb */ }
+            
+            throw ex;
+        }
+
+        return bufferedSocket;
     }
     AMQ_CATCH_RETHROW( SocketException )
     AMQ_CATCH_EXCEPTION_CONVERT( ActiveMQException, SocketException )

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.cpp?view=diff&rev=500558&r1=500557&r2=500558
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.cpp (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.cpp Sat Jan 27 08:25:51 2007
@@ -75,25 +75,46 @@
 #endif
 
 ////////////////////////////////////////////////////////////////////////////////
-TcpSocket::TcpSocket() {
-   
-    socketHandle = INVALID_SOCKET_HANDLE;
-    inputStream = NULL;
-    outputStream = NULL;
-   
-	#if defined(HAVE_WINSOCK2_H)
+TcpSocket::TcpSocket() throw (SocketException) 
+:
+    socketHandle( INVALID_SOCKET_HANDLE ),
+    inputStream( NULL ),
+    outputStream( NULL )
+{
+
+    try {
+       
+#if defined(HAVE_WINSOCK2_H)
         if( staticSocketInitializer.getSocketInitError() != NULL ) {
             throw *staticSocketInitializer.getSocketInitError();
         }
-    #endif
+#endif
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-TcpSocket::TcpSocket( SocketHandle socketHandle ){
-    this->socketHandle = socketHandle;
-   
-    inputStream = new SocketInputStream( socketHandle );
-    outputStream = new SocketOutputStream( socketHandle );
+TcpSocket::TcpSocket( SocketHandle socketHandle )
+:
+    socketHandle( INVALID_SOCKET_HANDLE ),
+    inputStream( NULL ),
+    outputStream( NULL )
+{
+    try {
+       
+#if defined(HAVE_WINSOCK2_H)
+        if( staticSocketInitializer.getSocketInitError() != NULL ) {
+            throw *staticSocketInitializer.getSocketInitError();
+        }
+#endif
+        
+        this->socketHandle = socketHandle;
+        this->inputStream = new SocketInputStream( socketHandle );
+        this->outputStream = new SocketOutputStream( socketHandle );
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException )    
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -116,89 +137,78 @@
 ////////////////////////////////////////////////////////////////////////////////
 void TcpSocket::connect(const char* host, int port) throw ( SocketException )
 {
-    if( isConnected() ) {
-        throw SocketException( __FILE__, __LINE__, 
-            "Socket::connect - Socket already connected.  host: %s, port: %d", host, port );
-    }
-    
-    // Create the socket.
-    socketHandle = ::socket(AF_INET, SOCK_STREAM, 0);
-    if( socketHandle < 0 ) {
-        socketHandle = INVALID_SOCKET_HANDLE;
-            throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str() );
-    }
-   
-    // Check port value.
-    if (port <= 0 || port > 65535) {
-        close();
-        throw SocketException ( __FILE__, __LINE__, 
-            "Socket::connect- Port out of range: %d", port );
-    }
-    
+    try{
+        
+        if( isConnected() ) {
+            throw SocketException( __FILE__, __LINE__, 
+                "Socket::connect - Socket already connected.  host: %s, port: %d", host, port );
+        }
+        
+        // Create the socket.
+        checkResult( socketHandle = ::socket(AF_INET, SOCK_STREAM, 0) );
+       
+        // Check port value.
+        if (port <= 0 || port > 65535) {
+            close();
+            throw SocketException ( __FILE__, __LINE__, 
+                "Socket::connect- Port out of range: %d", port );
+        }
+        
 #ifdef SO_NOSIGPIPE // Don't want to get a SIGPIPE on FreeBSD and Mac OS X
-
-    int optval = 1;
-    if( ::setsockopt( socketHandle, 
-                      SOL_SOCKET, SO_NOSIGPIPE, 
-                      (char*)&optval, 
-                      sizeof(optval)) < 0 )
-    {
-        close();
-        throw SocketException ( __FILE__, __LINE__, 
-            "Socket::connect- Failed setting SO_NOSIGPIPE: %s", SocketError::getErrorString().c_str() );
-    }
     
+        int optval = 1;
+        checkResult( ::setsockopt( socketHandle, SOL_SOCKET, SO_NOSIGPIPE, (char*)&optval, sizeof(optval)) );
+        
 #endif
-    
-    sockaddr_in target_addr;
-    target_addr.sin_family = AF_INET;
-    target_addr.sin_port = htons( ( short ) port );
-    target_addr.sin_addr.s_addr = 0; // To be set later down...
-    memset( &target_addr.sin_zero, 0, sizeof( target_addr.sin_zero ) );
-
-	int status;
-	
-    // Resolve name
+        
+        sockaddr_in target_addr;
+        target_addr.sin_family = AF_INET;
+        target_addr.sin_port = htons( ( short ) port );
+        target_addr.sin_addr.s_addr = 0; // To be set later down...
+        memset( &target_addr.sin_zero, 0, sizeof( target_addr.sin_zero ) );
+    	
+        // Resolve name
 #if defined(HAVE_STRUCT_ADDRINFO)    
-    addrinfo hints;
-    memset(&hints, 0, sizeof(addrinfo));
-    hints.ai_family = PF_INET;
-    struct addrinfo *res_ptr = NULL;
-    
-    status = ::getaddrinfo( host, NULL, &hints, &res_ptr );
-    if( status != 0 || res_ptr == NULL){      
-        throw SocketException( __FILE__, __LINE__, 
-            "Socket::connect - %s", SocketError::getErrorString().c_str() );        
-    }
-     
-    assert(res_ptr->ai_addr->sa_family == AF_INET);
-    // Porting: On both 32bit and 64 bit systems that we compile to soo far, sin_addr 
-    // is a 32 bit value, not an unsigned long.
-    assert( sizeof( ( ( sockaddr_in* )res_ptr->ai_addr )->sin_addr.s_addr ) == 4 );
-    target_addr.sin_addr.s_addr = ( ( sockaddr_in* )res_ptr->ai_addr )->sin_addr.s_addr;
-    freeaddrinfo( res_ptr );
+        addrinfo hints;
+        memset(&hints, 0, sizeof(addrinfo));
+        hints.ai_family = PF_INET;
+        struct addrinfo *res_ptr = NULL;
+        
+        checkResult( ::getaddrinfo( host, NULL, &hints, &res_ptr ) );
+         
+        assert(res_ptr->ai_addr->sa_family == AF_INET);
+        // Porting: On both 32bit and 64 bit systems that we compile to soo far, sin_addr 
+        // is a 32 bit value, not an unsigned long.
+        assert( sizeof( ( ( sockaddr_in* )res_ptr->ai_addr )->sin_addr.s_addr ) == 4 );
+        target_addr.sin_addr.s_addr = ( ( sockaddr_in* )res_ptr->ai_addr )->sin_addr.s_addr;
+        freeaddrinfo( res_ptr );
 #else
-	struct ::hostent *he = ::gethostbyname(host);
-	if( he == NULL ) {
-        throw SocketException( __FILE__, __LINE__, "Failed to resolve hostname" );
-	}
-	target_addr.sin_addr.s_addr = *((in_addr_t *)he->h_addr);
+    	struct ::hostent *he = ::gethostbyname(host);
+    	if( he == NULL ) {
+            throw SocketException( __FILE__, __LINE__, "Failed to resolve hostname" );
+    	}
+    	target_addr.sin_addr.s_addr = *((in_addr_t *)he->h_addr);
 #endif
-   
-    // Attempt the connection to the server.
-    status = ::connect( socketHandle, 
-                        ( const sockaddr * )&target_addr, 
-                        sizeof( target_addr ) );
-                      
-    if( status < 0 ){
-        close();
-        throw SocketException( __FILE__, __LINE__, 
-            "Socket::connect - %s", SocketError::getErrorString().c_str() );
+       
+        // Attempt the connection to the server.
+        checkResult( ::connect( socketHandle, 
+                            ( const sockaddr * )&target_addr, 
+                            sizeof( target_addr ) ) );
+       
+        // Create an input/output stream for this socket.
+        inputStream = new SocketInputStream( socketHandle );
+        outputStream = new SocketOutputStream( socketHandle );
+        
+    }
+    catch( SocketException& ex ) {
+        ex.setMark( __FILE__, __LINE__);
+        try{ close(); } catch( cms::CMSException& cx){ /* Absorb */ }
+        throw ex;
+    }
+    catch( ... ){
+        try{ close(); } catch( cms::CMSException& cx){ /* Absorb */ }
+        throw SocketException( __FILE__, __LINE__, "connect() caught unknown exception");
     }
-   
-    // Create an input/output stream for this socket.
-    inputStream = new SocketInputStream( socketHandle );
-    outputStream = new SocketOutputStream( socketHandle );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -233,118 +243,180 @@
 ////////////////////////////////////////////////////////////////////////////////
 int TcpSocket::getSoLinger() const throw( SocketException ){
    
-    linger value;
-    socklen_t length = sizeof( value );
-    ::getsockopt( socketHandle, SOL_SOCKET, SO_LINGER, (char*)&value, &length );
-   
-    return value.l_onoff? value.l_linger : 0;
+   try{
+        linger value;
+        socklen_t length = sizeof( value );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_LINGER, (char*)&value, &length ));
+       
+        return value.l_onoff? value.l_linger : 0;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////    
 void TcpSocket::setSoLinger( int dolinger ) throw( SocketException ){
-   
-    linger value;
-    value.l_onoff = dolinger != 0;
-    value.l_linger = dolinger;
-    ::setsockopt( socketHandle, SOL_SOCKET, SO_LINGER, (char*)&value, sizeof(value) );
+
+    try{
+        linger value;
+        value.l_onoff = dolinger != 0;
+        value.l_linger = dolinger;
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_LINGER, (char*)&value, sizeof(value) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 bool TcpSocket::getKeepAlive() const throw( SocketException ){
    
-    int value;
-    socklen_t length = sizeof( int );
-    ::getsockopt( socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, &length );
-    return value != 0;
+    try{
+        int value;
+        socklen_t length = sizeof( int );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, &length ));
+        return value != 0;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void TcpSocket::setKeepAlive( const bool keepAlive ) throw( SocketException ){
    
-    int value = keepAlive? 1 : 0;
-    ::setsockopt(socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, sizeof(int) );
+    try{
+        int value = keepAlive? 1 : 0;
+        checkResult(::setsockopt(socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, sizeof(int)) );
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 int TcpSocket::getReceiveBufferSize() const throw( SocketException ){
    
-    int value;
-    socklen_t length = sizeof( value );
-    ::getsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&value, &length );
-    return value;
+    try{
+        int value;
+        socklen_t length = sizeof( value );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&value, &length ));
+        return value;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void TcpSocket::setReceiveBufferSize( int size ) throw( SocketException ){
    
-    ::setsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&size, sizeof(size) );
+    try{
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_RCVBUF, (char*)&size, sizeof(size) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 bool TcpSocket::getReuseAddress() const throw( SocketException ){
    
-    int value;
-    socklen_t length = sizeof( int );
-    ::getsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, &length );
-    return value != 0;
+    try{
+        int value;
+        socklen_t length = sizeof( int );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, &length ));
+        return value != 0;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void TcpSocket::setReuseAddress( bool reuse ) throw( SocketException ){
    
-    int value = reuse? 1 : 0;
-    ::setsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(int) );
+    try{
+        int value = reuse? 1 : 0;
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(int) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 int TcpSocket::getSendBufferSize() const throw( SocketException ){
    
-    int value;
-    socklen_t length = sizeof( value );
-    ::getsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, (char*)&value, &length );
-    return value;
+    try{
+        int value;
+        socklen_t length = sizeof( value );
+        checkResult(::getsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, (char*)&value, &length ));
+        return value;
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void TcpSocket::setSendBufferSize( int size ) throw( SocketException ){
    
-    ::setsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, ( char* )&size, sizeof( size ) );
+    try{
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_SNDBUF, (char*)&size, sizeof(size) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void TcpSocket::setSoTimeout ( const int millisecs ) throw ( SocketException )
 {
-	#if !defined(HAVE_WINSOCK2_H)
+    try{
+        
+#if !defined(HAVE_WINSOCK2_H)
         timeval timot;
         timot.tv_sec = millisecs / 1000;
         timot.tv_usec = (millisecs % 1000) * 1000;
-    #else
+#else
         int timot = millisecs;
-    #endif
-
-    ::setsockopt( socketHandle, SOL_SOCKET, SO_RCVTIMEO, (const char*) &timot, sizeof (timot) );
-    ::setsockopt( socketHandle, SOL_SOCKET, SO_SNDTIMEO, (const char*) &timot, sizeof (timot) );
+#endif
+    
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_RCVTIMEO, (const char*) &timot, sizeof (timot) ));
+        checkResult(::setsockopt( socketHandle, SOL_SOCKET, SO_SNDTIMEO, (const char*) &timot, sizeof (timot) ));
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 int TcpSocket::getSoTimeout() const throw( SocketException )
 {
-	#if !defined(HAVE_WINSOCK2_H)
+    try{
+        
+#if !defined(HAVE_WINSOCK2_H)
         timeval timot;
         timot.tv_sec = 0;
         timot.tv_usec = 0;
         socklen_t size = sizeof(timot);
-    #else
+#else
         int timot = 0;
         int size = sizeof(timot);
-    #endif
-  
-    ::getsockopt(socketHandle, SOL_SOCKET, SO_RCVTIMEO, (char*) &timot, &size);
-  
-	#if !defined(HAVE_WINSOCK2_H)
+#endif
+      
+        checkResult(::getsockopt(socketHandle, SOL_SOCKET, SO_RCVTIMEO, (char*) &timot, &size));
+      
+#if !defined(HAVE_WINSOCK2_H)
         return (timot.tv_sec * 1000) + (timot.tv_usec / 1000);
-    #else
+#else
         return timot;
-    #endif
+#endif
+    
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCHALL_THROW( SocketException ) 
 
 }
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::checkResult( int value ) const throw (SocketException) {
+    
+    if( value < 0 ){
+        throw SocketException( __FILE__, __LINE__, 
+            SocketError::getErrorString().c_str() );
+    }
+}
+
+

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.h?view=diff&rev=500558&r1=500557&r2=500558
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.h Sat Jan 27 08:25:51 2007
@@ -57,8 +57,10 @@
    
         /** 
          * Construct a non-connected socket.
+         * @throws SocketException thrown one windows if the static initialization
+         * call to WSAStartup was not successful.
          */
-        TcpSocket();
+        TcpSocket() throw (SocketException);
  
         /** 
          * Construct a connected or bound socket based on given
@@ -231,6 +233,8 @@
           
             static StaticSocketInitializer staticSocketInitializer;
         #endif
+        
+        void checkResult( int value ) const throw (SocketException);
    
     };
 

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.cpp?view=diff&rev=500558&r1=500557&r2=500558
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.cpp (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.cpp Sat Jan 27 08:25:51 2007
@@ -60,14 +60,40 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-TcpTransport::~TcpTransport(void)
+TcpTransport::~TcpTransport()
 {
     try
     {
-        socket->close();
-        delete socket;
+        try{
+            close();
+        } catch( cms::CMSException& ex ){ /* Absorb */ }
+        
+        if( socket != NULL ) {
+            delete socket;
+            socket = NULL;
+        }
     }
     AMQ_CATCH_NOTHROW( ActiveMQException )
-    AMQ_CATCHALL_NOTHROW( )
+    AMQ_CATCHALL_NOTHROW()
 }
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpTransport::close() throw( cms::CMSException ) {
+    
+    try
+    {
+        // Invoke the paren't close first.
+        TransportFilter::close();
+        
+        // Close the socket.
+        if( socket != NULL ) {
+            socket->close();
+        }
+    }
+    AMQ_CATCH_RETHROW( SocketException )
+    AMQ_CATCH_EXCEPTION_CONVERT( ActiveMQException, SocketException )
+    AMQ_CATCHALL_THROW( SocketException )
+}
+
+
 

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.h?view=diff&rev=500558&r1=500557&r2=500558
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/TcpTransport.h Sat Jan 27 08:25:51 2007
@@ -52,7 +52,13 @@
                       Transport* next, 
                       const bool own = true );
                       
-    	virtual ~TcpTransport(void);
+    	virtual ~TcpTransport();
+        
+        /**
+         * Delegates to the superclass and then closes the socket.
+         * @throws CMSException if errors occur.
+         */
+        virtual void close() throw( cms::CMSException );
 
     };