xerces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amass...@apache.org
Subject svn commit: r231012 - in /xerces/c/trunk/src/xercesc/util/NetAccessors: Socket/UnixHTTPURLInputStream.cpp WinSock/BinHTTPURLInputStream.cpp
Date Tue, 09 Aug 2005 08:09:21 GMT
Author: amassari
Date: Tue Aug  9 01:08:59 2005
New Revision: 231012

URL: http://svn.apache.org/viewcvs?rev=231012&view=rev
Log:
Close the socket even in case of exceptions being thrown (jira#1467)

Modified:
    xerces/c/trunk/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp
    xerces/c/trunk/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp

Modified: xerces/c/trunk/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp
URL: http://svn.apache.org/viewcvs/xerces/c/trunk/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp?rev=231012&r1=231011&r2=231012&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp (original)
+++ xerces/c/trunk/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp Tue Aug
 9 01:08:59 2005
@@ -45,6 +45,38 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+class SocketJanitor
+{
+public:
+    // -----------------------------------------------------------------------
+    //  Constructors and Destructor
+    // -----------------------------------------------------------------------
+    SocketJanitor(int* toDelete) : fData(toDelete) {}
+    ~SocketJanitor() { reset(); }
+
+    int* get() const { return fData; }
+    int* release() { int* p = fData; fData = 0; return p; }
+
+    void reset(int* p = 0) { if(fData) close(*fData); fData=p; }
+    bool isDataNull() { return (fData == 0); }
+
+private :
+    // -----------------------------------------------------------------------
+    //  Unimplemented constructors and operators
+    // -----------------------------------------------------------------------
+    SocketJanitor();
+    SocketJanitor(const SocketJanitor&);
+    SocketJanitor& operator=(const SocketJanitor&);
+
+    // -----------------------------------------------------------------------
+    //  Private data members
+    //
+    //  fData
+    //      This is the pointer to the socket that must be closed when 
+    //      this object is destroyed.
+    // -----------------------------------------------------------------------
+    int*  fData;
+};
 
 UnixHTTPURLInputStream::UnixHTTPURLInputStream(const XMLURL& urlSource, const XMLNetHTTPInfo*
httpInfo/*=0*/)
       : fSocket(0)
@@ -248,6 +280,7 @@
         ThrowXMLwithMemMgr1(NetAccessorException,
                  XMLExcepts::NetAcc_CreateSocket, urlSource.getURLText(), fMemoryManager);
     }
+    SocketJanitor janSock(&s);
 
     if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
     {
@@ -407,7 +440,7 @@
         ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlSource.getURLText(),
fMemoryManager);
     }
 
-    fSocket = s;
+    fSocket = *janSock.release();
 
 }
 

Modified: xerces/c/trunk/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp
URL: http://svn.apache.org/viewcvs/xerces/c/trunk/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp?rev=231012&r1=231011&r2=231012&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp (original)
+++ xerces/c/trunk/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp Tue Aug
 9 01:08:59 2005
@@ -37,6 +37,39 @@
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+class SocketJanitor
+{
+public:
+    // -----------------------------------------------------------------------
+    //  Constructors and Destructor
+    // -----------------------------------------------------------------------
+    SocketJanitor(SOCKET* toDelete) : fData(toDelete) {}
+    ~SocketJanitor() { reset(); }
+
+    SOCKET* get() const { return fData; }
+    SOCKET* release() {	SOCKET* p = fData; fData = 0; return p; }
+
+    void reset(SOCKET* p = 0) { if(fData) closesocket(*fData); fData=p; }
+    bool isDataNull() { return (fData == 0); }
+
+private :
+    // -----------------------------------------------------------------------
+    //  Unimplemented constructors and operators
+    // -----------------------------------------------------------------------
+    SocketJanitor();
+    SocketJanitor(const SocketJanitor&);
+    SocketJanitor& operator=(const SocketJanitor&);
+
+    // -----------------------------------------------------------------------
+    //  Private data members
+    //
+    //  fData
+    //      This is the pointer to the socket that must be closed when 
+    //      this object is destroyed.
+    // -----------------------------------------------------------------------
+    SOCKET*  fData;
+};
+
 typedef struct hostent *(WSAAPI * LPFN_GETHOSTBYNAME)(const char* name);
 typedef unsigned long(WSAAPI * LPFN_INET_ADDR)(const char* cp);
 typedef struct hostent *(WSAAPI * LPFN_GETHOSTBYADDR)(const char* addr, int len, int type);
@@ -285,6 +318,7 @@
         ThrowXMLwithMemMgr1(NetAccessorException,
                  XMLExcepts::NetAcc_CreateSocket, urlSource.getURLText(), fMemoryManager);
     }
+    SocketJanitor janSock(&s);
 
     if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) == SOCKET_ERROR)
     {
@@ -469,7 +503,7 @@
         ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlSource.getURLText(),
fMemoryManager);
     }
 
-    fSocketHandle = (unsigned int) s;
+    fSocketHandle = (unsigned int) *janSock.release();
 }
 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org


Mime
View raw message