// 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_POSIX_BUFFEREDSTREAM_H #define HU_VARADIISTVAN_SCPL_IO_POSIX_BUFFEREDSTREAM_H //------------------------------------------------------------------------------ #include "Waitable.h" #include //------------------------------------------------------------------------------ namespace hu { namespace varadiistvan { namespace scpl { namespace io { //------------------------------------------------------------------------------ class ReadingBuffer; class WritingBuffer; //------------------------------------------------------------------------------ /** * A waitable that has a read and/or a write buffer associated with * it. */ class BufferedStream : public Waitable { public: /** * The default buffer 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 waitable with the given capacities of * the reading and writing buffers. If any of the capacities is 0, * the corresponding buffer will not be created. */ BufferedStream(Waiter* waiter = 0, int fd = -1, size_t readingCapacity = DEFAULT_CAPACITY, size_t writingCapacity = DEFAULT_CAPACITY, short events = 0); public: /** * Destroy the buffered waitable. */ virtual ~BufferedStream(); /** * Get the reference of the reading buffer. Note, that it can be a * 0 reference, if the waitable has no reading buffer. */ ReadingBuffer& getReadingBuffer(); /** * Get the reference of the writing buffer. Note, that it can be a * 0 reference, if the waitable has no writing buffer. */ WritingBuffer& getWritingBuffer(); protected: /** * Read using the file descriptor at most the given number of * bytes into the given buffer. * * This default implementation uses the read(2) function for * reading. * * @return the number of bytes read (0 in case of end-of-file or * when the connection is closed), -1 on error. In case of error, * the error code will be set up. */ virtual ssize_t read(void* dest, size_t length); /** * Write using the file descriptor the given number of bytes from * the given buffer. * * This default implementation uses the write(2) function for * writing. * * @return the number of bytes written, or -1 on error. In case of error, * the error code will be set up. */ virtual ssize_t write(const void* src, size_t length); /** * Check if the waitable is ready. */ virtual bool isReady(); /** * Handle any events on the file descriptor. */ virtual void handleEvents(short events); 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_POSIX_BUFFEREDSTREAM_H // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: