1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/* -*- Mode: C++ -*-
* Worldvisions Tunnel Vision Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*
* An stream wrapper for encoders.
*/
#ifndef __WVENCODERSTREAM_H
#define __WVENCODERSTREAM_H
#include "wvstream.h"
#include "wvstreamclone.h"
#include "wvencoder.h"
/**
* WvEncoderStream chains a series of encoders on the input and
* output ports of the underlying stream to effect on-the-fly data
* transformations.
*
* Notice that the value of WvEncoderStream's auto_flush flag becomes
* important when working with encoders that treat explicit buffer
* flushing in a special manner. For instance, on flush() the Gzip
* encoder synchronizes its output. Were auto_flush to remain true,
* each incremental write to the stream would cause the Gzip encoder
* to flush its dictionary thereby resulting in poor compression.
*
* @see WvStream::auto_flush(bool)
*/
class WvEncoderStream : public WvStreamClone
{
bool is_closing;
bool is_eof;
WvDynBuf readinbuf;
WvDynBuf readoutbuf;
WvDynBuf writeinbuf;
WvDynBuf writeoutbuf;
public:
/** Encoder chain through which input data is passed. */
WvEncoderChain readchain;
/** Encoder chain through which output data is passed. */
WvEncoderChain writechain;
/**
* Controls the minimum number of unencoded bytes the encoder
* should try to read at once from the underlying stream,
* to optimize performance of block-oriented protocols.
* This is not the same as queuemin() which guarantees how much
* encoded input must be generated before select() returns true.
* if 0, the encoder will only whatever is specified in uread()
*/
size_t min_readsize;
/**
* Creates an encoder stream.
*
* "cloned" is the stream to wrap
*/
WvEncoderStream(WvStream *cloned);
virtual ~WvEncoderStream();
/**
* Safely shuts down the stream.
*
* Does the following in sequence:
*
* - Flushes and finishes the read chain
* - Flushes and finishes the write chain
* - Flushes the stream output buffers
* - Closes the underlying stream
*/
virtual void close();
/**
* Flushes the read chain through to the stream's input buffer.
*
* The regular stream flush() only operates on the write chain.
*
* Returns: true if the encoder chain returned true
*/
bool flush_read();
/**
* Flushes the write chain through to the stream's output buffer.
*
* The regular stream flush() invokes this, then attempts to
* synchronously push the buffered data to the stream, which
* may not always be desirable since it can be quite costly.
*
* To simply cause the write chain to be flushed but allow
* WvStreams to drain the encoded output buffer at its leisure,
* use this function.
*
* Returns: true if the encoder chain returned true
*/
bool flush_write();
/**
* Calls flush() then finish() on the read chain of encoders.
*
* Returns: true if the encoder chain returned true
*/
bool finish_read();
/**
* Calls flush() then finish() on the write chain of encoders.
*
* Does not flush() the stream.
*
* Returns: true if the encoder chain returned true.
*/
bool finish_write();
/**
* Defines isok() semantics for encoders.
*
* Returns false on error or after all data has been read from
* the internal buffers and readchain.isfinished() or
* ! writechain.isok().
*
* Returns: true if it is still possible to read and write data
*/
virtual bool isok() const;
protected:
bool pre_select(SelectInfo &si);
virtual size_t uread(void *buf, size_t size);
virtual size_t uwrite(const void *buf, size_t size);
virtual bool flush_internal(time_t msec_timeout);
private:
void checkreadisok();
void checkwriteisok();
// pulls a chunk of specified size from the underlying stream
void pull(size_t size);
// pushes a chunk to the underlying stream
bool push(bool flush, bool finish);
};
#endif // __WVENCODERSTREAM_H
|