source: vscpl/src/hu/varadiistvan/scpl/io/_posix/BufferedStream.h@ 6:53519b47b166

Last change on this file since 6:53519b47b166 was 6:53519b47b166, checked in by István Váradi <ivaradi@…>, 11 years ago

Imported some further stuff from the IO code

File size: 5.5 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of VSCPL, a simple cross-platform utility library
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29#ifndef HU_VARADIISTVAN_SCPL_IO_POSIX_BUFFEREDSTREAM_H
30#define HU_VARADIISTVAN_SCPL_IO_POSIX_BUFFEREDSTREAM_H
31//------------------------------------------------------------------------------
32
33#include "Waitable.h"
34
35#include <cstdlib>
36
37//------------------------------------------------------------------------------
38
39namespace hu { namespace varadiistvan { namespace scpl { namespace io {
40
41//------------------------------------------------------------------------------
42
43class ReadingBuffer;
44class WritingBuffer;
45
46//------------------------------------------------------------------------------
47
48/**
49 * A waitable that has a read and/or a write buffer associated with
50 * it.
51 */
52class BufferedStream : public Waitable
53{
54public:
55 /**
56 * The default buffer capacity.
57 */
58 static const size_t DEFAULT_CAPACITY = 4096;
59
60private:
61 /**
62 * The reading buffer. It may be 0, if no reading buffer is
63 * associated with this waitable.
64 */
65 ReadingBuffer* readingBuffer;
66
67 /**
68 * The writing buffer. It may be 0, if no writing buffer is
69 * associated with this waitable.
70 */
71 WritingBuffer* writingBuffer;
72
73protected:
74 /**
75 * Construct the buffered waitable with the given capacities of
76 * the reading and writing buffers. If any of the capacities is 0,
77 * the corresponding buffer will not be created.
78 */
79 BufferedStream(Waiter* waiter = 0, int fd = -1,
80 size_t readingCapacity = DEFAULT_CAPACITY,
81 size_t writingCapacity = DEFAULT_CAPACITY,
82 short events = 0);
83
84public:
85 /**
86 * Destroy the buffered waitable.
87 */
88 virtual ~BufferedStream();
89
90 /**
91 * Get the reference of the reading buffer. Note, that it can be a
92 * 0 reference, if the waitable has no reading buffer.
93 */
94 ReadingBuffer& getReadingBuffer();
95
96 /**
97 * Get the reference of the writing buffer. Note, that it can be a
98 * 0 reference, if the waitable has no writing buffer.
99 */
100 WritingBuffer& getWritingBuffer();
101
102protected:
103 /**
104 * Read using the file descriptor at most the given number of
105 * bytes into the given buffer.
106 *
107 * This default implementation uses the read(2) function for
108 * reading.
109 *
110 * @return the number of bytes read (0 in case of end-of-file or
111 * when the connection is closed), -1 on error. In case of error,
112 * the error code will be set up.
113 */
114 virtual ssize_t read(void* dest, size_t length);
115
116 /**
117 * Write using the file descriptor the given number of bytes from
118 * the given buffer.
119 *
120 * This default implementation uses the write(2) function for
121 * writing.
122 *
123 * @return the number of bytes written, or -1 on error. In case of error,
124 * the error code will be set up.
125 */
126 virtual ssize_t write(const void* src, size_t length);
127
128 /**
129 * Check if the waitable is ready.
130 */
131 virtual bool isReady();
132
133 /**
134 * Handle any events on the file descriptor.
135 */
136 virtual void handleEvents(short events);
137
138 friend class ReadingBuffer;
139 friend class WritingBuffer;
140};
141
142//------------------------------------------------------------------------------
143// Inline definitions
144//------------------------------------------------------------------------------
145
146inline ReadingBuffer& BufferedStream::getReadingBuffer()
147{
148 return *readingBuffer;
149}
150
151//------------------------------------------------------------------------------
152
153inline WritingBuffer& BufferedStream::getWritingBuffer()
154{
155 return *writingBuffer;
156}
157
158//------------------------------------------------------------------------------
159
160} /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
161
162//------------------------------------------------------------------------------
163#endif // HU_VARADIISTVAN_SCPL_IO_POSIX_BUFFEREDSTREAM_H
164
165// Local Variables:
166// mode: C++
167// c-basic-offset: 4
168// indent-tabs-mode: nil
169// End:
Note: See TracBrowser for help on using the repository browser.