2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
#ifndef CBUFFEREDOUTPUTSTREAM_H
|
|
|
|
#define CBUFFEREDOUTPUTSTREAM_H
|
|
|
|
|
|
|
|
#include "IOutputStream.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CStreamBuffer.h"
|
2002-06-02 22:57:50 +00:00
|
|
|
#include "CCondVar.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
class CMutex;
|
|
|
|
class IJob;
|
|
|
|
|
2002-07-28 17:25:13 +00:00
|
|
|
//! Memory buffer output stream
|
|
|
|
/*!
|
|
|
|
This class provides an output stream that writes to a memory buffer.
|
|
|
|
It also provides a means for the owner to ensure thread safe access.
|
|
|
|
Typically, an owner object will make this object visible to clients
|
|
|
|
that need access to an IOutputStream while using the CBufferedOutputStream
|
|
|
|
methods to read the data written to the stream.
|
|
|
|
*/
|
2001-10-06 14:13:28 +00:00
|
|
|
class CBufferedOutputStream : public IOutputStream {
|
2002-04-29 14:40:01 +00:00
|
|
|
public:
|
2002-07-28 17:25:13 +00:00
|
|
|
/*!
|
|
|
|
The \c mutex must not be NULL and will be used to ensure thread
|
|
|
|
safe access. If \c adoptedCloseCB is not NULL it will be called
|
|
|
|
when close() is called, allowing the creator to detect the close.
|
|
|
|
*/
|
|
|
|
CBufferedOutputStream(CMutex* mutex, IJob* adoptedCloseCB);
|
2001-10-06 14:13:28 +00:00
|
|
|
~CBufferedOutputStream();
|
|
|
|
|
2002-07-28 17:25:13 +00:00
|
|
|
//! @name manipulators
|
|
|
|
//@{
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-28 17:25:13 +00:00
|
|
|
//! Read data without removing from buffer
|
|
|
|
/*!
|
|
|
|
Returns a buffer of \c n bytes (which must be <= getSize()). The
|
|
|
|
caller must not modify the buffer nor delete it. The mutex must
|
|
|
|
be locked before calling this.
|
|
|
|
*/
|
2001-10-14 16:58:01 +00:00
|
|
|
const void* peek(UInt32 n);
|
2002-07-28 17:25:13 +00:00
|
|
|
|
|
|
|
//! Discard data
|
|
|
|
/*!
|
|
|
|
Discards the next \c n bytes. If \c n >= getSize() then the buffer
|
|
|
|
is cleared. The mutex must be locked before calling this.
|
|
|
|
*/
|
2001-10-14 16:58:01 +00:00
|
|
|
void pop(UInt32 n);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-28 17:25:13 +00:00
|
|
|
//@}
|
|
|
|
//! @name accessors
|
|
|
|
//@{
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-28 17:25:13 +00:00
|
|
|
//! Get size of buffer
|
|
|
|
/*!
|
|
|
|
Returns the number of bytes in the buffer. The mutex must be locked
|
|
|
|
before calling this.
|
|
|
|
*/
|
2001-10-14 16:58:01 +00:00
|
|
|
UInt32 getSize() const;
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-28 17:25:13 +00:00
|
|
|
//@}
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
// IOutputStream overrides
|
|
|
|
// these all lock the mutex for their duration
|
2001-10-14 16:58:01 +00:00
|
|
|
virtual void close();
|
2002-07-28 17:25:13 +00:00
|
|
|
virtual UInt32 write(const void*, UInt32 n);
|
2001-10-14 16:58:01 +00:00
|
|
|
virtual void flush();
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
private:
|
2001-10-06 14:13:28 +00:00
|
|
|
CMutex* m_mutex;
|
|
|
|
IJob* m_closeCB;
|
2002-06-02 22:57:50 +00:00
|
|
|
CCondVar<bool> m_empty;
|
2001-10-06 14:13:28 +00:00
|
|
|
CStreamBuffer m_buffer;
|
|
|
|
bool m_closed;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|