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/22 22:51:35 UTC

svn commit: r498821 - in /incubator/activemq/activemq-cpp/trunk/activemq-cpp: src/main/ src/main/activemq/network/ vs2005-build/

Author: nmittler
Date: Mon Jan 22 13:51:30 2007
New Revision: 498821

URL: http://svn.apache.org/viewvc?view=rev&rev=498821
Log:
[AMQCPP-44] - adding appropriate platform logging of socket errors

Added:
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.h
Modified:
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/ServerSocket.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketInputStream.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketOutputStream.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/TcpSocket.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/vs2005-build/vs2005-activemq.vcproj

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am?view=diff&rev=498821&r1=498820&r2=498821
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am Mon Jan 22 13:51:30 2007
@@ -62,6 +62,7 @@
     activemq/network/BufferedSocket.cpp \
     activemq/network/SocketOutputStream.cpp \
     activemq/network/SocketFactory.cpp \
+    activemq/network/SocketError.cpp \
     activemq/transport/IOTransportFactory.cpp \
     activemq/transport/ResponseCorrelator.cpp \
     activemq/transport/TcpTransport.cpp \
@@ -208,6 +209,7 @@
     activemq/network/BufferedSocket.h \
     activemq/network/SocketOutputStream.h \
     activemq/network/SocketFactory.h \
+    activemq/network/SocketError.h \
     activemq/transport/IOTransportFactory.h \
     activemq/transport/Transport.h \
     activemq/transport/TransportFactory.h \

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/ServerSocket.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/ServerSocket.cpp?view=diff&rev=498821&r1=498820&r2=498821
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/ServerSocket.cpp (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/ServerSocket.cpp Mon Jan 22 13:51:30 2007
@@ -16,16 +16,13 @@
  */
 
 #include "ServerSocket.h"
+#include "SocketError.h"
 
 #ifdef HAVE_WINSOCK2_H
     #include <Winsock2.h>
     #include <Ws2tcpip.h> 
     #include <sys/stat.h>
     #define stat _stat
-    #ifdef errno
-    #undef errno
-    #endif
-    int errno;
 #else
     #include <unistd.h>
     #include <netdb.h>
@@ -35,17 +32,14 @@
     #include <netinet/in.h>
     #include <arpa/inet.h>
     #include <string.h>
-    extern int errno;
 #endif
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <ctype.h>
-#include <errno.h>
 #include <sys/types.h>
 #include <assert.h>
-#include <errno.h>
 #include <string>
 
 using namespace activemq::network;
@@ -113,7 +107,7 @@
     socketHandle = ::socket(AF_INET, SOCK_STREAM, 0 );
     if( socketHandle < 0) {
         socketHandle = Socket::INVALID_SOCKET_HANDLE;
-            throw SocketException( __FILE__, __LINE__, ::strerror( errno ));
+            throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str());
     }
    
     // Verify the port value.
@@ -137,7 +131,7 @@
     struct addrinfo *res_ptr = NULL;
     status = ::getaddrinfo(host, NULL, &hints, &res_ptr);
     if( status != 0 || res_ptr == NULL) {
-        throw SocketException( __FILE__, __LINE__, ::strerror( errno ) );
+        throw SocketException( __FILE__, __LINE__, 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.
@@ -163,12 +157,12 @@
     if( status < 0 ){
         close();
         throw SocketException ( __FILE__, __LINE__, 
-            "ServerSocket::bind - %s", ::strerror( errno ) );
+            "ServerSocket::bind - %s", SocketError::getErrorString().c_str() );
     }
     status = ::listen( socketHandle, backlog );
     if( status < 0 ) {
         close();
-        throw SocketException( __FILE__, __LINE__, ::strerror( errno ) );
+        throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str() );
     }
 }
 
@@ -207,7 +201,7 @@
         ::accept( socketHandle, reinterpret_cast<struct sockaddr*>(&temp), &temp_len );
     if( ss_socket_handle < 0 ) {
         throw SocketException( __FILE__, __LINE__, 
-            "ServerSocket::accept- %s", ::strerror( errno ) );
+            "ServerSocket::accept- %s", SocketError::getErrorString().c_str() );
     }
     
     return new TcpSocket( ss_socket_handle );

Added: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.cpp?view=auto&rev=498821
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.cpp (added)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.cpp Mon Jan 22 13:51:30 2007
@@ -0,0 +1,47 @@
+#include "SocketError.h"
+#include <activemq/util/Config.h>
+
+#if defined(HAVE_WINSOCK2_H)
+    #include <Winsock2.h>
+#else
+    #include <errno.h>    
+    extern int errno;
+#endif
+
+using namespace activemq;
+using namespace activemq::network;
+
+////////////////////////////////////////////////////////////////////////////////
+std::string SocketError::getErrorString() {
+    
+    std::string returnValue;
+    
+    #if defined(HAVE_WINSOCK2_H)
+    
+        // If the socket was temporarily unavailable - just try again.
+        int errorCode = ::WSAGetLastError();
+  
+        // Create the error string.
+        static const int errorStringSize = 512;
+        char errorString[errorStringSize];
+        memset( errorString, 0, errorStringSize );
+        ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+           0,
+           errorCode,
+           0,
+           errorString,
+           errorStringSize - 1,
+           NULL);
+           
+        returnValue = errorString;
+        
+    #else
+         
+        // Create the error string.
+        returnValue = ::strerror(errno);
+        
+    #endif
+    
+    return returnValue;
+}
+

Added: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.h?view=auto&rev=498821
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.h (added)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketError.h Mon Jan 22 13:51:30 2007
@@ -0,0 +1,16 @@
+#ifndef ACTIVEMQ_NETWORK_SOCKETERROR_H_
+#define ACTIVEMQ_NETWORK_SOCKETERROR_H_
+
+#include <string>
+
+namespace activemq{
+namespace network{
+    
+    class SocketError {  
+    public:
+    
+        static std::string getErrorString();
+    };
+}}
+
+#endif /*ACTIVEMQ_NETWORK_SOCKETERROR_H_*/

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketInputStream.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketInputStream.cpp?view=diff&rev=498821&r1=498820&r2=498821
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketInputStream.cpp (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketInputStream.cpp Mon Jan 22 13:51:30 2007
@@ -18,11 +18,8 @@
 #include <activemq/util/Config.h>
 
 #if !defined(HAVE_WINSOCK2_H) 
-    //#include <sys/poll.h>
     #include <sys/select.h>
     #include <sys/socket.h>
-    #include <errno.h>    
-    extern int errno;
 #else
     #include <Winsock2.h>
 #endif
@@ -38,6 +35,7 @@
 #endif
 
 #include <activemq/network/SocketInputStream.h>
+#include <activemq/network/SocketError.h>
 #include <activemq/io/IOException.h>
 #include <activemq/exceptions/UnsupportedOperationException.h>
 #include <stdlib.h>
@@ -102,7 +100,7 @@
         tv.tv_usec = 0;
         int returnCode = ::select(socket+1, &rd, NULL, NULL, &tv);
         if(returnCode == -1){
-            throw IOException(__FILE__, __LINE__, ::strerror(errno));
+            throw IOException(__FILE__, __LINE__, SocketError::getErrorString().c_str() );
         }
         return (returnCode == 0)? 0 : 1;
         
@@ -143,33 +141,9 @@
     // Check for error.
     if( len == -1 ){
         
-        #if !defined(HAVE_WINSOCK2_H) 
-         
-            // Create the error string.
-            char* errorString = ::strerror(errno);
-         
-        #else
-        
-            // If the socket was temporarily unavailable - just try again.
-            int errorCode = ::WSAGetLastError();
-      
-            // Create the error string.
-            static const int errorStringSize = 512;
-            char errorString[errorStringSize];
-            memset( errorString, 0, errorStringSize );
-            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
-               0,
-               errorCode,
-               0,
-               errorString,
-               errorStringSize - 1,
-               NULL);
-              
-        #endif
-        
         // Otherwise, this was a bad error - throw an exception.
         throw IOException( __FILE__, __LINE__, 
-                "activemq::io::SocketInputStream::read - %s", errorString );
+                "activemq::io::SocketInputStream::read - %s", SocketError::getErrorString().c_str() );
     }
     
     if( debug ){

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketOutputStream.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketOutputStream.cpp?view=diff&rev=498821&r1=498820&r2=498821
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketOutputStream.cpp (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/network/SocketOutputStream.cpp Mon Jan 22 13:51:30 2007
@@ -17,12 +17,12 @@
  
 #include "SocketOutputStream.h"
 #include <activemq/util/Config.h>
+#include "SocketError.h"
 
 #ifdef HAVE_WINSOCK2_H
     #include <Winsock2.h>
 #else
     #include <sys/socket.h>
-    extern int errno;
 #endif
 
 #include <errno.h>
@@ -87,7 +87,7 @@
         int length = ::send( socket, (const char*)buffer, remaining, sendOpts );      	
         if( length < 0 ){
             throw IOException( __FILE__, __LINE__, 
-                "activemq::io::SocketOutputStream::write - %s", ::strerror(errno) );
+                "activemq::io::SocketOutputStream::write - %s", SocketError::getErrorString().c_str() );
         }
          
         buffer+=length;

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=498821&r1=498820&r2=498821
==============================================================================
--- 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 Mon Jan 22 13:51:30 2007
@@ -30,7 +30,6 @@
     #include <netinet/in.h>
     #include <arpa/inet.h>
     #include <string.h>
-    extern int errno;
 #endif
 
 #ifndef SHUT_RDWR 
@@ -41,13 +40,12 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <ctype.h>
-#include <errno.h>
 #include <sys/types.h>
 
 #include "TcpSocket.h"
 #include "SocketInputStream.h"
 #include "SocketOutputStream.h"
-#include <errno.h>
+#include "SocketError.h"
 
 using namespace activemq::network;
 using namespace activemq::io;
@@ -127,7 +125,7 @@
     socketHandle = ::socket(AF_INET, SOCK_STREAM, 0);
     if( socketHandle < 0 ) {
         socketHandle = INVALID_SOCKET_HANDLE;
-            throw SocketException( __FILE__, __LINE__, ::strerror( errno ) );
+            throw SocketException( __FILE__, __LINE__, SocketError::getErrorString().c_str() );
     }
    
     // Check port value.
@@ -155,7 +153,7 @@
     status = ::getaddrinfo( host, NULL, &hints, &res_ptr );
     if( status != 0 || res_ptr == NULL){      
         throw SocketException( __FILE__, __LINE__, 
-            "Socket::connect - %s", ::strerror( errno ) );        
+            "Socket::connect - %s", SocketError::getErrorString().c_str() );        
     }
      
     assert(res_ptr->ai_addr->sa_family == AF_INET);
@@ -180,7 +178,7 @@
     if( status < 0 ){
         close();
         throw SocketException( __FILE__, __LINE__, 
-            "Socket::connect - %s", ::strerror( errno ) );
+            "Socket::connect - %s", SocketError::getErrorString().c_str() );
     }
    
     // Create an input/output stream for this socket.

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/vs2005-build/vs2005-activemq.vcproj
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/vs2005-build/vs2005-activemq.vcproj?view=diff&rev=498821&r1=498820&r2=498821
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/vs2005-build/vs2005-activemq.vcproj (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/vs2005-build/vs2005-activemq.vcproj Mon Jan 22 13:51:30 2007
@@ -1036,6 +1036,14 @@
 					>
 				</File>
 				<File
+					RelativePath="..\src\main\activemq\network\SocketError.cpp"
+					>
+				</File>
+				<File
+					RelativePath="..\src\main\activemq\network\SocketError.h"
+					>
+				</File>
+				<File
 					RelativePath="..\src\main\activemq\network\SocketException.h"
 					>
 				</File>