[Box Backup-dev] COMMIT r608 - box/chris/general/bin/bbackupd

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Tue, 30 May 2006 20:03:56 +0000 (GMT)


Author: chris
Date: 2006-05-30 20:03:47 +0000 (Tue, 30 May 2006)
New Revision: 608

Modified:
   box/chris/general/bin/bbackupd/BackupDaemon.cpp
   box/chris/general/bin/bbackupd/BackupDaemon.h
Log:
* BackupDaemon.cpp, BackupDaemon.h
- Changed to use an Event instead of Sleep+Poll to detect command socket
  activity in the main thread. May also solve inaccurate sleep after error.


Modified: box/chris/general/bin/bbackupd/BackupDaemon.cpp
===================================================================
--- box/chris/general/bin/bbackupd/BackupDaemon.cpp	2006-05-30 20:01:08 UTC (rev 607)
+++ box/chris/general/bin/bbackupd/BackupDaemon.cpp	2006-05-30 20:03:47 UTC (rev 608)
@@ -125,8 +125,8 @@
 	}
 
 #ifdef WIN32
-	// Create the event object to signal when new messages are
-	// queued to be sent.
+	// Create the event object to signal from main thread to worker
+	// when new messages are queued to be sent to the command socket.
 	mhMessageToSendEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
 	if (mhMessageToSendEvent == INVALID_HANDLE_VALUE)
 	{
@@ -135,6 +135,16 @@
 		exit(1);
 	}
 
+	// Create the event object to signal from worker to main thread
+	// when a command has been received on the command socket.
+	mhCommandReceivedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+	if (mhCommandReceivedEvent == INVALID_HANDLE_VALUE)
+	{
+		syslog(LOG_ERR, "Failed to create event object: error %d",
+			GetLastError);
+		exit(1);
+	}
+
 	// Create the critical section to protect the message queue
 	InitializeCriticalSection(&mMessageQueueLock);
 
@@ -276,7 +286,7 @@
 void BackupDaemon::RunHelperThread(void)
 {
 	mpCommandSocketInfo = new CommandSocketInfo;
-	this->mReceivedCommandConn = false;
+	// this->mReceivedCommandConn = false;
 	WinNamedPipeStream& rSocket(mpCommandSocketInfo->mListeningSocket);
 
 	// loop until the parent process exits
@@ -426,7 +436,7 @@
 					break;
 				}
 
-				this->mReceivedCommandConn = true;
+				// this->mReceivedCommandConn = true;
 			}
 
 			rSocket.Close();
@@ -985,25 +995,27 @@
 void BackupDaemon::WaitOnCommandSocket(box_time_t RequiredDelay, bool &DoSyncFlagOut, bool &SyncIsForcedOut)
 {
 #ifdef WIN32
-	// Really could use some interprocess protection, mutex etc
-	// any side effect should be too bad???? :)
-	DWORD timeout = (DWORD)BoxTimeToMilliSeconds(RequiredDelay);
+	DWORD requiredDelayMs = BoxTimeToMilliSeconds(RequiredDelay);
 
-	while ( this->mReceivedCommandConn == false )
+	DWORD result = WaitForSingleObject(mhCommandReceivedEvent, 
+		(DWORD)requiredDelayMs);
+
+	if (result == WAIT_OBJECT_0)
 	{
-		Sleep(1);
-
-		if ( timeout == 0 )
-		{
-			DoSyncFlagOut = false;
-			SyncIsForcedOut = false;
-			return;
-		}
-		timeout--;
+		DoSyncFlagOut = this->mDoSyncFlagOut;
+		SyncIsForcedOut = this->mSyncIsForcedOut;
+		ResetEvent(mhCommandReceivedEvent);
 	}
-	this->mReceivedCommandConn = false;
-	DoSyncFlagOut = this->mDoSyncFlagOut;
-	SyncIsForcedOut = this->mSyncIsForcedOut;
+	else if (result == WAIT_TIMEOUT)
+	{
+		DoSyncFlagOut = false;
+		SyncIsForcedOut = false;
+	}
+	else
+	{
+		::syslog(LOG_ERR, "Unexpected result from "
+			"WaitForSingleObject: error %d", GetLastError());
+	}
 
 	return;
 #else // ! WIN32
@@ -1106,8 +1118,10 @@
 		while(mpCommandSocketInfo->mpGetLine != 0 && !mpCommandSocketInfo->mpGetLine->IsEOF()
 			&& mpCommandSocketInfo->mpGetLine->GetLine(command, false /* no preprocessing */, timeout))
 		{
-			TRACE1("Receiving command '%s' over command socket\n", command.c_str());
-			
+			TRACE1("Receiving command '%s' over command socket\n", 
+				command.c_str());
+			SetEvent(mhCommandReceivedEvent);
+
 			bool sendOK = false;
 			bool sendResponse = true;
 		

Modified: box/chris/general/bin/bbackupd/BackupDaemon.h
===================================================================
--- box/chris/general/bin/bbackupd/BackupDaemon.h	2006-05-30 20:01:08 UTC (rev 607)
+++ box/chris/general/bin/bbackupd/BackupDaemon.h	2006-05-30 20:03:47 UTC (rev 608)
@@ -185,8 +185,8 @@
 	void RunHelperThread(void);
 
 	private:
-	bool mDoSyncFlagOut, mSyncIsForcedOut, mReceivedCommandConn;
-	HANDLE mhMessageToSendEvent;
+	bool mDoSyncFlagOut, mSyncIsForcedOut;
+	HANDLE mhMessageToSendEvent, mhCommandReceivedEvent;
 	CRITICAL_SECTION mMessageQueueLock;
 	std::vector<std::string> mMessageList;
 #endif