summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2008-08-21 11:05:38 +0000
committerChris Wilson <chris+github@qwirx.com>2008-08-21 11:05:38 +0000
commit67279841cd455a04470515dd9e4db0cd7af46651 (patch)
tree42cf7f37a2ab5d13208a7ef362d60fa506f17f50 /lib
parent96c337af8c1b79911fe0268db9337970f794094c (diff)
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/common/IOStream.cpp20
-rw-r--r--lib/common/IOStream.h1
-rw-r--r--lib/common/SelfFlushingStream.h71
3 files changed, 92 insertions, 0 deletions
diff --git a/lib/common/IOStream.cpp b/lib/common/IOStream.cpp
index 3c7be561..76fb7fed 100644
--- a/lib/common/IOStream.cpp
+++ b/lib/common/IOStream.cpp
@@ -238,4 +238,24 @@ bool IOStream::CopyStreamTo(IOStream &rCopyTo, int Timeout, int BufferSize)
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);
+ }
+}
+
diff --git a/lib/common/IOStream.h b/lib/common/IOStream.h
index 042ccca4..fc9f8a97 100644
--- a/lib/common/IOStream.h
+++ b/lib/common/IOStream.h
@@ -57,6 +57,7 @@ public:
// 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);
};
diff --git a/lib/common/SelfFlushingStream.h b/lib/common/SelfFlushingStream.h
new file mode 100644
index 00000000..36e9a4d3
--- /dev/null
+++ b/lib/common/SelfFlushingStream.h
@@ -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
+