[Box Backup-dev] COMMIT r582 - in box/chris/general: lib/server lib/win32 test/basicserver test/basicserver/testfiles

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Tue, 23 May 2006 00:32:43 +0000 (GMT)


Author: chris
Date: 2006-05-23 00:31:58 +0000 (Tue, 23 May 2006)
New Revision: 582

Modified:
   box/chris/general/lib/server/SocketStream.cpp
   box/chris/general/lib/server/SocketStream.h
   box/chris/general/lib/server/SocketStreamTLS.cpp
   box/chris/general/lib/win32/emu.cpp
   box/chris/general/test/basicserver/testbasicserver.cpp
   box/chris/general/test/basicserver/testfiles/srv3.conf
   box/chris/general/test/basicserver/testfiles/srv4.conf
Log:
* test/basicserver/testfiles/srv3.conf
* test/basicserver/testfiles/srv4.conf
- Fixed for Windows (breaks Unix)

* test/basicserver/testbasicserver.cpp
- Wakey wakey: don't sleep 1000 times too long
- Fixed test servers 3 and 4 command lines and PID file path on Windows
- Don't hup servers 3 and 4 on Windows, or check for memory leaks
- Don't try to use Unix sockets on Windows, it doesn't work and annoys the beast

* lib/win32/emu.cpp
- Better implementation of poll()

* lib/server/SocketStream.h
* lib/server/SocketStream.cpp
- Fixed invalid handle values (signed vs unsigned comparison)

* lib/server/SocketStreamTLS.cpp
- Fixed non-blocking sockets on Windows


Modified: box/chris/general/lib/server/SocketStream.cpp
===================================================================
--- box/chris/general/lib/server/SocketStream.cpp	2006-05-22 13:03:59 UTC (rev 581)
+++ box/chris/general/lib/server/SocketStream.cpp	2006-05-23 00:31:58 UTC (rev 582)
@@ -36,7 +36,7 @@
 //
 // --------------------------------------------------------------------------
 SocketStream::SocketStream()
-	: mSocketHandle(-1),
+	: mSocketHandle(mInvalidHandle),
 	  mReadClosed(false),
 	  mWriteClosed(false),
 	  mBytesRead(0),
@@ -85,7 +85,7 @@
 	{
 		THROW_EXCEPTION(ServerException, BadSocketHandle);
 	}
-	if(mSocketHandle == -1)
+	if(mSocketHandle == mInvalidHandle)
 	{
 		THROW_EXCEPTION(ServerException, DupError);
 	}
@@ -101,7 +101,7 @@
 // --------------------------------------------------------------------------
 SocketStream::~SocketStream()
 {
-	if(mSocketHandle != -1)
+	if(mSocketHandle != mInvalidHandle)
 	{
 		Close();
 	}
@@ -117,7 +117,10 @@
 // --------------------------------------------------------------------------
 void SocketStream::Attach(int socket)
 {
-	if(mSocketHandle != -1) {THROW_EXCEPTION(ServerException, SocketAlreadyOpen)}
+	if(mSocketHandle != mInvalidHandle) 
+	{
+		THROW_EXCEPTION(ServerException, SocketAlreadyOpen)
+	}
 
 	mSocketHandle = socket;
 	ResetCounters();
@@ -134,7 +137,10 @@
 // --------------------------------------------------------------------------
 void SocketStream::Open(int Type, const char *Name, int Port)
 {
-	if(mSocketHandle != -1) {THROW_EXCEPTION(ServerException, SocketAlreadyOpen)}
+	if(mSocketHandle != mInvalidHandle) 
+	{
+		THROW_EXCEPTION(ServerException, SocketAlreadyOpen)
+	}
 	
 	// Setup parameters based on type, looking up names if required
 	int sockDomain = 0;
@@ -144,7 +150,7 @@
 
 	// Create the socket
 	mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */);
-	if(mSocketHandle == -1)
+	if(mSocketHandle == mInvalidHandle)
 	{
 		THROW_EXCEPTION(ServerException, SocketOpenError)
 	}
@@ -158,7 +164,7 @@
 #else
 		::close(mSocketHandle);
 #endif
-		mSocketHandle = -1;
+		mSocketHandle = mInvalidHandle;
 		THROW_EXCEPTION(ConnectionException, Conn_SocketConnectError)
 	}
 	ResetCounters();
@@ -174,7 +180,10 @@
 // --------------------------------------------------------------------------
 int SocketStream::Read(void *pBuffer, int NBytes, int Timeout)
 {
-	if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)}
+	if(mSocketHandle == mInvalidHandle) 
+	{
+		THROW_EXCEPTION(ServerException, BadSocketHandle)
+	}
 
 	if(Timeout != IOStream::TimeOutInfinite)
 	{
@@ -247,7 +256,10 @@
 // --------------------------------------------------------------------------
 void SocketStream::Write(const void *pBuffer, int NBytes)
 {
-	if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)}
+	if(mSocketHandle == mInvalidHandle) 
+	{
+		THROW_EXCEPTION(ServerException, BadSocketHandle)
+	}
 	
 	// Buffer in byte sized type.
 	ASSERT(sizeof(char) == 1);
@@ -311,7 +323,10 @@
 // --------------------------------------------------------------------------
 void SocketStream::Close()
 {
-	if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)}
+	if(mSocketHandle == mInvalidHandle) 
+	{
+		THROW_EXCEPTION(ServerException, BadSocketHandle)
+	}
 #ifdef WIN32
 	if(::closesocket(mSocketHandle) == -1)
 #else
@@ -333,7 +348,10 @@
 // --------------------------------------------------------------------------
 void SocketStream::Shutdown(bool Read, bool Write)
 {
-	if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)}
+	if(mSocketHandle == mInvalidHandle) 
+	{
+		THROW_EXCEPTION(ServerException, BadSocketHandle)
+	}
 	
 	// Do anything?
 	if(!Read && !Write) return;
@@ -388,7 +406,10 @@
 // --------------------------------------------------------------------------
 tOSSocketHandle SocketStream::GetSocketHandle()
 {
-	if(mSocketHandle == -1) {THROW_EXCEPTION(ServerException, BadSocketHandle)}
+	if(mSocketHandle == mInvalidHandle) 
+	{
+		THROW_EXCEPTION(ServerException, BadSocketHandle)
+	}
 	return mSocketHandle;
 }
 

Modified: box/chris/general/lib/server/SocketStream.h
===================================================================
--- box/chris/general/lib/server/SocketStream.h	2006-05-22 13:03:59 UTC (rev 581)
+++ box/chris/general/lib/server/SocketStream.h	2006-05-23 00:31:58 UTC (rev 582)
@@ -56,6 +56,7 @@
 	tOSSocketHandle mSocketHandle;
 	bool mReadClosed;
 	bool mWriteClosed;
+	static const tOSSocketHandle mInvalidHandle = -1;
 
 protected:
 	off_t mBytesRead;

Modified: box/chris/general/lib/server/SocketStreamTLS.cpp
===================================================================
--- box/chris/general/lib/server/SocketStreamTLS.cpp	2006-05-22 13:03:59 UTC (rev 581)
+++ box/chris/general/lib/server/SocketStreamTLS.cpp	2006-05-23 00:31:58 UTC (rev 582)
@@ -137,8 +137,12 @@
 		THROW_EXCEPTION(ServerException, TLSAllocationFailed)
 	}
 
-#ifndef WIN32
 	// Make the socket non-blocking so timeouts on Read work
+
+#ifdef WIN32
+	u_long nonblocking = 1;
+	ioctlsocket(socket, FIONBIO, &nonblocking);
+#else // !WIN32
 	// This is more portable than using ioctl with FIONBIO
 	int statusFlags = 0;
 	if(::fcntl(socket, F_GETFL, &statusFlags) < 0
@@ -309,7 +313,7 @@
 
 		case SSL_ERROR_WANT_READ:
 		case SSL_ERROR_WANT_WRITE:
-			// wait for the requried data
+			// wait for the required data
 			// Will only get once around this loop, so don't need to calculate timeout values
 			if(WaitWhenRetryRequired(se, Timeout) == false)
 			{

Modified: box/chris/general/lib/win32/emu.cpp
===================================================================
--- box/chris/general/lib/win32/emu.cpp	2006-05-22 13:03:59 UTC (rev 581)
+++ box/chris/general/lib/win32/emu.cpp	2006-05-23 00:31:58 UTC (rev 582)
@@ -952,10 +952,10 @@
 		fd_set readfd;
 		fd_set writefd;
 
-		readfd.fd_count = 0;
-		writefd.fd_count = 0;
+		FD_ZERO(&readfd);
+		FD_ZERO(&writefd);
 
-		struct pollfd *ufdsTmp = ufds;
+		// struct pollfd *ufdsTmp = ufds;
 
 		timeval timOut;
 		timeval *tmpptr; 
@@ -968,41 +968,61 @@
 		timOut.tv_sec  = timeout / 1000;
 		timOut.tv_usec = timeout * 1000;
 
-		if (ufds->events & POLLIN)
+		for (int i = 0; i < nfds; i++)
 		{
-			for (unsigned long i = 0; i < nfds; i++)
+			struct pollfd* ufd = &(ufds[i]);
+
+			if (ufd->events & POLLIN)
 			{
-				readfd.fd_array[i] = ufdsTmp->fd;
-				readfd.fd_count++;
+				FD_SET(ufd->fd, &readfd);
 			}
-		}
 
-		if (ufds->events & POLLOUT)
-		{
-			for (unsigned long i = 0; i < nfds; i++)
+			if (ufd->events & POLLOUT)
 			{
+				FD_SET(ufd->fd, &writefd);
+			}
 
-				writefd.fd_array[i]=ufdsTmp->fd;
-				writefd.fd_count++;
+			if (ufd->events & ~(POLLIN | POLLOUT))
+			{
+				printf("Unsupported poll bits %d",
+					ufd->events);
+				return -1;
 			}
 		}	
 
-		int noffds = select(0, &readfd, &writefd, 0, tmpptr);
+		int nready = select(0, &readfd, &writefd, 0, tmpptr);
 
-		if (noffds == SOCKET_ERROR)
+		if (nready == SOCKET_ERROR)
 		{
 			// int errval = WSAGetLastError();
 
-			ufdsTmp = ufds;
+			struct pollfd* pufd = ufds;
 			for (unsigned long i = 0; i < nfds; i++)
 			{
-				ufdsTmp->revents = POLLERR;
-				ufdsTmp++;
+				pufd->revents = POLLERR;
+				pufd++;
 			}
 			return (-1);
 		}
+		else if (nready > 0)
+		{
+			for (int i = 0; i < nfds; i++)
+			{
+				struct pollfd *ufd = &(ufds[i]);
 
-		return noffds;
+				if (FD_ISSET(ufd->fd, &readfd))
+				{
+					ufd->revents |= POLLIN;
+				}
+
+				if (FD_ISSET(ufd->fd, &writefd))
+				{
+					ufd->revents |= POLLOUT;
+				}
+			}
+		}
+
+		return nready;
 	}
 	catch (...)
 	{

Modified: box/chris/general/test/basicserver/testbasicserver.cpp
===================================================================
--- box/chris/general/test/basicserver/testbasicserver.cpp	2006-05-22 13:03:59 UTC (rev 581)
+++ box/chris/general/test/basicserver/testbasicserver.cpp	2006-05-23 00:31:58 UTC (rev 582)
@@ -63,7 +63,7 @@
 void testservers_pause_before_reply()
 {
 #ifdef WIN32
-	Sleep(COMMS_SERVER_WAIT_BEFORE_REPLYING * 1000);
+	Sleep(COMMS_SERVER_WAIT_BEFORE_REPLYING);
 #else
 	struct timespec t;
 	t.tv_sec = 0;
@@ -545,15 +545,24 @@
 
 	// Launch a test SSL server
 	{
+#ifdef WIN32
+		int pid = LaunchServer("test srv3 testfiles\\srv3.conf", 
+			"testfiles\\srv3.pid");
+#else
 		int pid = LaunchServer("./test srv3 testfiles/srv3.conf", "testfiles/srv3.pid");
+#endif
 		TEST_THAT(pid != -1 && pid != 0);
 		if(pid > 0)
 		{
 			// Will it restart?
 			TEST_THAT(ServerIsAlive(pid));
+
+#ifndef WIN32
 			TEST_THAT(HUPServer(pid));
 			::sleep(1);
 			TEST_THAT(ServerIsAlive(pid));
+#endif
+
 			// Make some connections
 			{
 				// SSL library
@@ -568,36 +577,50 @@
 
 				SocketStreamTLS conn1;
 				conn1.Open(context, Socket::TypeINET, "localhost", 2003);
+#ifndef WIN32
 				SocketStreamTLS conn2;
 				conn2.Open(context, Socket::TypeUNIX, "testfiles/srv3.sock");
 				SocketStreamTLS conn3;
 				conn3.Open(context, Socket::TypeINET, "localhost", 2003);
+#endif
 				// Quick check that reconnections fail
 				TEST_CHECK_THROWS(conn1.Open(context, Socket::TypeUNIX, "testfiles/srv3.sock");, ServerException, SocketAlreadyOpen);
 				// Stuff some data around
 				std::vector<IOStream *> conns;
 				conns.push_back(&conn1);
+#ifndef WIN32
 				conns.push_back(&conn2);
 				conns.push_back(&conn3);
+#endif
 				Srv2TestConversations(conns);
 				// Implicit close
 			}
+#ifndef WIN32
 			// HUP again
 			TEST_THAT(HUPServer(pid));
 			::sleep(1);
 			TEST_THAT(ServerIsAlive(pid));
+#endif
 			// Kill it
 			TEST_THAT(KillServer(pid));
 			::sleep(1);
 			TEST_THAT(!ServerIsAlive(pid));
+#ifndef WIN32
 			TestRemoteProcessMemLeaks("test-srv3.memleaks");
+#endif
 		}
 	}
 	
 //protocolserver:
 	// Launch a test protocol handling server
 	{
-		int pid = LaunchServer("./test srv4 testfiles/srv4.conf", "testfiles/srv4.pid");
+#ifdef WIN32
+		int pid = LaunchServer("test srv4 testfiles\\srv4.conf", 
+			"testfiles\\srv4.pid");
+#else
+		int pid = LaunchServer("./test srv4 testfiles/srv4.conf", 
+			"testfiles/srv4.pid");
+#endif
 		TEST_THAT(pid != -1 && pid != 0);
 		if(pid > 0)
 		{
@@ -606,7 +629,11 @@
 
 			// Open a connection to it		
 			SocketStream conn;
+#ifdef WIN32
+			conn.Open(Socket::TypeINET, "localhost", 2003);
+#else
 			conn.Open(Socket::TypeUNIX, "testfiles/srv4.sock");
+#endif
 			
 			// Create a protocol
 			TestProtocolClient protocol(conn);
@@ -669,7 +696,9 @@
 			TEST_THAT(KillServer(pid));
 			::sleep(1);
 			TEST_THAT(!ServerIsAlive(pid));
+#ifndef WIN32
 			TestRemoteProcessMemLeaks("test-srv4.memleaks");
+#endif
 		}
 	}
 

Modified: box/chris/general/test/basicserver/testfiles/srv3.conf
===================================================================
--- box/chris/general/test/basicserver/testfiles/srv3.conf	2006-05-22 13:03:59 UTC (rev 581)
+++ box/chris/general/test/basicserver/testfiles/srv3.conf	2006-05-23 00:31:58 UTC (rev 582)
@@ -1,9 +1,9 @@
 Server
 {
-	PidFile = testfiles/srv3.pid
-	ListenAddresses = inet:localhost,unix:testfiles/srv3.sock
-	CertificateFile = testfiles/serverCerts.pem
-	PrivateKeyFile = testfiles/serverPrivKey.pem
-	TrustedCAsFile = testfiles/serverTrustedCAs.pem
+	PidFile = testfiles\srv3.pid
+	ListenAddresses = inet:localhost
+	CertificateFile = testfiles\serverCerts.pem
+	PrivateKeyFile = testfiles\serverPrivKey.pem
+	TrustedCAsFile = testfiles\serverTrustedCAs.pem
 }
 

Modified: box/chris/general/test/basicserver/testfiles/srv4.conf
===================================================================
--- box/chris/general/test/basicserver/testfiles/srv4.conf	2006-05-22 13:03:59 UTC (rev 581)
+++ box/chris/general/test/basicserver/testfiles/srv4.conf	2006-05-23 00:31:58 UTC (rev 582)
@@ -1,6 +1,6 @@
 Server
 {
-	PidFile = testfiles/srv4.pid
-	ListenAddresses = unix:testfiles/srv4.sock
+	PidFile = testfiles\srv4.pid
+	ListenAddresses = inet:localhost
 }