[Box Backup-commit] COMMIT r2254 - box/trunk/lib/common

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Thu, 21 Aug 2008 12:05:39 +0100 (BST)


Author: chris
Date: 2008-08-21 12:05:38 +0100 (Thu, 21 Aug 2008)
New Revision: 2254

Added:
   box/trunk/lib/common/SelfFlushingStream.h
Modified:
   box/trunk/lib/common/IOStream.cpp
   box/trunk/lib/common/IOStream.h
Log:
Add a Flush() method to IOStream to read and discard all remaining data, 
and a SelfFlushingStream class which can be used to ensure that protocol 
streams are always flushed, to avoid breaking protocol.


Modified: box/trunk/lib/common/IOStream.cpp
===================================================================
--- box/trunk/lib/common/IOStream.cpp	2008-08-21 11:04:21 UTC (rev 2253)
+++ box/trunk/lib/common/IOStream.cpp	2008-08-21 11:05:38 UTC (rev 2254)
@@ -238,4 +238,24 @@
 	return true;	// completed
 }
 
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    IOStream::Flush(int Timeout)
+//		Purpose: Read and discard all remaining data in stream.
+//			 Useful for protocol streams which must be flushed
+//			 to avoid breaking the protocol.
+//		Created: 2008/08/20
+//
+// --------------------------------------------------------------------------
+void IOStream::Flush(int Timeout)
+{
+	char buffer[4096];
 
+	while(StreamDataLeft())
+	{
+		Read(buffer, sizeof(buffer), Timeout);
+	}
+}
+
+

Modified: box/trunk/lib/common/IOStream.h
===================================================================
--- box/trunk/lib/common/IOStream.h	2008-08-21 11:04:21 UTC (rev 2253)
+++ box/trunk/lib/common/IOStream.h	2008-08-21 11:05:38 UTC (rev 2254)
@@ -57,6 +57,7 @@
 	// Utility functions
 	bool ReadFullBuffer(void *pBuffer, int NBytes, int *pNBytesRead, int Timeout = IOStream::TimeOutInfinite);
 	bool CopyStreamTo(IOStream &rCopyTo, int Timeout = IOStream::TimeOutInfinite, int BufferSize = 1024);
+	void Flush(int Timeout = IOStream::TimeOutInfinite);
 	
 	static int ConvertSeekTypeToOSWhence(int SeekType);
 };

Added: box/trunk/lib/common/SelfFlushingStream.h
===================================================================
--- box/trunk/lib/common/SelfFlushingStream.h	                        (rev 0)
+++ box/trunk/lib/common/SelfFlushingStream.h	2008-08-21 11:05:38 UTC (rev 2254)
@@ -0,0 +1,71 @@
+// --------------------------------------------------------------------------
+//
+// File
+//		Name:    SelfFlushingStream.h
+//		Purpose: A stream wrapper that always flushes the underlying
+//			 stream, to ensure protocol safety.
+//		Created: 2008/08/20
+//
+// --------------------------------------------------------------------------
+
+#ifndef SELFFLUSHINGSTREAM__H
+#define SELFFLUSHINGSTREAM__H
+
+#include "IOStream.h"
+
+// --------------------------------------------------------------------------
+//
+// Class
+//		Name:    SelfFlushingStream
+//		Purpose: A stream wrapper that always flushes the underlying
+//			 stream, to ensure protocol safety.
+//		Created: 2008/08/20
+//
+// --------------------------------------------------------------------------
+class SelfFlushingStream : public IOStream
+{
+public:
+	SelfFlushingStream(IOStream &rSource)
+	: mrSource(rSource) { }
+
+	SelfFlushingStream(const SelfFlushingStream &rToCopy)
+	: mrSource(rToCopy.mrSource) { }
+	
+	~SelfFlushingStream()
+	{
+		Flush();
+	}
+
+private:
+	// no copying from IOStream allowed
+	SelfFlushingStream(const IOStream& rToCopy);
+	
+public:
+	virtual int Read(void *pBuffer, int NBytes,
+		int Timeout = IOStream::TimeOutInfinite)
+	{
+		return mrSource.Read(pBuffer, NBytes, Timeout);
+	}
+	virtual pos_type BytesLeftToRead()
+	{
+		return mrSource.BytesLeftToRead();
+	}
+	virtual void Write(const void *pBuffer, int NBytes)
+	{
+		mrSource.Write(pBuffer, NBytes);
+	}
+	virtual bool StreamDataLeft()
+	{
+		return mrSource.StreamDataLeft();
+	}
+	virtual bool StreamClosed()
+	{
+		return mrSource.StreamClosed();
+	}
+
+private:
+	IOStream &mrSource;
+};
+
+#endif // SELFFLUSHINGSTREAM__H
+


Property changes on: box/trunk/lib/common/SelfFlushingStream.h
___________________________________________________________________
Name: svn:mime-type
   + text/x-chdr
Name: svn:eol-style
   + native