[Box Backup-dev] COMMIT r419 - box/chris/win32/vc2005-compile-fixes/lib/win32

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Sat, 11 Feb 2006 13:06:51 +0000 (GMT)


Author: chris
Date: 2006-02-11 13:06:49 +0000 (Sat, 11 Feb 2006)
New Revision: 419

Modified:
   box/chris/win32/vc2005-compile-fixes/lib/win32/WinNamedPipeStream.cpp
Log:
* WinNamedPipeStream.cpp
- Use Close() method to properly close sockets if ConnectNamedPipe fails
- Detect and fix mIsConnected with no socket handle
- Null the socket handle and set mIsConnected to FALSE even if CloseHandle
  fails
- Use upper-case constants (TRUE, FALSE) instead of lower case


Modified: box/chris/win32/vc2005-compile-fixes/lib/win32/WinNamedPipeStream.cpp
===================================================================
--- box/chris/win32/vc2005-compile-fixes/lib/win32/WinNamedPipeStream.cpp	2006-02-11 13:04:45 UTC (rev 418)
+++ box/chris/win32/vc2005-compile-fixes/lib/win32/WinNamedPipeStream.cpp	2006-02-11 13:06:49 UTC (rev 419)
@@ -100,8 +100,7 @@
 	{
 		::syslog(LOG_ERR, "ConnectNamedPipe failed: %d", 
 			GetLastError());
-		CloseHandle(mSocketHandle);
-		mSocketHandle = NULL;
+		Close();
 		THROW_EXCEPTION(ServerException, SocketOpenError)
 	}
 	
@@ -184,7 +183,7 @@
 	// Closed for reading at EOF?
 	if (NumBytesRead == 0)
 	{
-		mReadClosed = true;
+		mReadClosed = TRUE;
 	}
 	
 	return NumBytesRead;
@@ -224,7 +223,7 @@
 
 		if (!Success)
 		{
-			mWriteClosed = true;	// assume can't write again
+			mWriteClosed = TRUE;	// assume can't write again
 			THROW_EXCEPTION(ConnectionException, 
 				Conn_SocketWriteError)
 		}
@@ -243,8 +242,15 @@
 // --------------------------------------------------------------------------
 void WinNamedPipeStream::Close()
 {
-	if (mSocketHandle == NULL || !mIsConnected) 
+	if (mSocketHandle == NULL && mIsConnected)
 	{
+		fprintf(stderr, "Inconsistent connected state\n");
+		::syslog(LOG_ERR, "Inconsistent connected state");
+		mIsConnected = FALSE;
+	}
+
+	if (mSocketHandle == NULL) 
+	{
 		THROW_EXCEPTION(ServerException, BadSocketHandle)
 	}
 
@@ -265,14 +271,16 @@
 		mIsServer = false;
 	}
 
-	if (!CloseHandle(mSocketHandle))
+	bool result = CloseHandle(mSocketHandle);
+
+	mSocketHandle = NULL;
+	mIsConnected = FALSE;
+
+	if (!result) 
 	{
 		::syslog(LOG_ERR, "CloseHandle failed: %d", GetLastError());
 		THROW_EXCEPTION(ServerException, SocketCloseError)
 	}
-	
-	mSocketHandle = NULL;
-	mIsConnected = FALSE;
 }
 
 // --------------------------------------------------------------------------