// Copyright (c) 2013 by István Váradi // This file is part of VSCPL, a simple cross-platform utility library // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. #ifndef HU_VARADIISTVAN_SCPL_IO_WIN32_BUFFEREDSTREAM_H #define HU_VARADIISTVAN_SCPL_IO_WIN32_BUFFEREDSTREAM_H //------------------------------------------------------------------------------ #include "Overlappable.h" //------------------------------------------------------------------------------ namespace hu { namespace varadiistvan { namespace scpl { namespace io { //------------------------------------------------------------------------------ class ReadingBuffer; class WritingBuffer; class Overlapped; //------------------------------------------------------------------------------ /** * A buffered stream. * * It wraps a HANDLE which can be read and/or written with ReadFile or * WriteFile, respectively. */ class BufferedStream : public Overlappable { public: /** * The default capacity. */ static const size_t DEFAULT_CAPACITY = 4096; private: /** * The reading buffer. It may be 0, if no reading buffer is * associated with this waitable. */ ReadingBuffer* readingBuffer; /** * The writing buffer. It may be 0, if no writing buffer is * associated with this waitable. */ WritingBuffer* writingBuffer; protected: /** * Construct the buffered stream. */ BufferedStream(Waiter* waiter = 0, HANDLE handle = 0, size_t readingCapacity = DEFAULT_CAPACITY, size_t writingCapacity = DEFAULT_CAPACITY); public: /** * Destroy the stream. */ virtual ~BufferedStream(); /** * Get the reference of the reading buffer. Note, that it can be a * 0 reference, if the stream has no reading buffer. */ ReadingBuffer& getReadingBuffer(); /** * Get the reference of the writing buffer. Note, that it can be a * 0 reference, if the stream has no writing buffer. */ WritingBuffer& getWritingBuffer(); protected: /** * Read using the handle at most the given number of bytes into * the given buffer. * * @return the number of bytes read. If 0, it indicates the end of * the file or connection. If negative, it means either a failure, * or the fact that the operation cannot be completed at this time. */ ssize_t read(void* dest, size_t length, Overlapped* overlapped); /** * Write using the handle the given number of bytes from the given * buffer. * * @return whether the writing succeeded. If false, it is either a * failure, or an inidication of the fact that the operation could * not be completed. */ bool write(const void* src, size_t length, Overlapped* overlapped); friend class ReadingBuffer; friend class WritingBuffer; }; //------------------------------------------------------------------------------ // Inline definitions //------------------------------------------------------------------------------ inline ReadingBuffer& BufferedStream::getReadingBuffer() { return *readingBuffer; } //------------------------------------------------------------------------------ inline WritingBuffer& BufferedStream::getWritingBuffer() { return *writingBuffer; } //------------------------------------------------------------------------------ } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */ //------------------------------------------------------------------------------ #endif // HU_VARADIISTVAN_SCPL_IO_WIN32_BUFFEREDSTREAM_H // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: