source: xplcommon/src/xplcommon/BlockingStream.h@ 31:bbd688924703

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

Implemented the data stream

File size: 5.6 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of libxplcommon, a common utility library for
4// development related to X-Plane
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// The views and conclusions contained in the software and documentation are those
27// of the authors and should not be interpreted as representing official policies,
28// either expressed or implied, of the FreeBSD Project.
29
30#ifndef XPLCOMMON_BLOCKINGSTREAM_H
31#define XPLCOMMON_BLOCKINGSTREAM_H
32//------------------------------------------------------------------------------
33
34#include "Failable.h"
35
36#include "BufferedStream.h"
37
38#include "Waiter.h"
39#include "WaitableEvent.h"
40
41//------------------------------------------------------------------------------
42
43namespace xplcommon {
44
45//------------------------------------------------------------------------------
46
47/**
48 * Wrapper for a buffered stream, which an event and provides
49 * blocking, but interruptible read() and write() operations.
50 *
51 * The class is designed in a way, that it expects its constructed
52 * instances to be called only from a single thread at a time, except
53 * for the interrupt() function, which may be called from another
54 * thread anytime.
55 */
56class BlockingStream : public Failable
57{
58private:
59 /**
60 * The buffered stream used.
61 */
62 BufferedStream& stream;
63
64 /**
65 * The waitable event.
66 */
67 WaitableEvent event;
68
69 /**
70 * Indicate if we are interrupted.
71 */
72 bool interrupted;
73
74 /**
75 * The offset in the buffer of the reading stream.
76 */
77 size_t readingOffset;
78
79 /**
80 * Indicate if the end-of-file has been reached while reading.
81 */
82 bool eof;
83
84public:
85 /**
86 * Construct the blocking stream for the given buffered stream.
87 *
88 * The stream must have a waiter which is used for waiting.
89 */
90 BlockingStream(BufferedStream& stream);
91
92 /**
93 * Determine if the stream has neither failed nor been
94 * interrupted.
95 */
96 operator bool() const;
97
98 /**
99 * Interrupt the stream. Pending or future read and write calls
100 * will return indicating error. The isInterrupted() function can
101 * be used to check if the stream was interrupted or not.
102 */
103 void interrupt();
104
105 /**
106 * Indicate if the stream is interrupted or not.
107 */
108 bool isInterrupted();
109
110 /**
111 * Read the given number of bytes into the given buffer.
112 *
113 * @return if the reading has succeeded
114 */
115 bool read(void* dest, size_t length);
116
117 /**
118 * Skip the given number of bytes.
119 */
120 bool skip(size_t length);
121
122 /**
123 * Write the given number of bytes from the given buffer. The data
124 * may not be written to the underlying stream fully, so if you
125 * want to make sure that it is sent, call flush() afterwards.
126 *
127 * @return true on success, false on error.
128 */
129 bool write(const void* src, size_t length);
130
131 /**
132 * Flush anything written so far into the buffer, but not to the
133 * underlying stream.
134 *
135 * @return whether the operation has succeeded.
136 */
137 bool flush();
138
139private:
140 /**
141 * Fill the buffer with data from the stream.
142 */
143 bool fill();
144
145 /**
146 * Check for the stream being interrupted.
147 *
148 * @return if the stream is interrupted, false otherwise
149 */
150 bool checkInterrupted();
151};
152
153//------------------------------------------------------------------------------
154// Inline definitions
155//------------------------------------------------------------------------------
156
157inline BlockingStream::BlockingStream(BufferedStream& stream) :
158 stream(stream),
159 event(stream.getWaiter()),
160 interrupted(false),
161 readingOffset(0),
162 eof(false)
163{
164}
165
166//------------------------------------------------------------------------------
167
168inline BlockingStream::operator bool() const
169{
170 return !failed() && !interrupted && !eof;
171}
172
173//------------------------------------------------------------------------------
174
175inline void BlockingStream::interrupt()
176{
177 event.fire();
178}
179
180//------------------------------------------------------------------------------
181
182inline bool BlockingStream::isInterrupted()
183{
184 return interrupted;
185}
186
187//------------------------------------------------------------------------------
188
189} /* namespace xplcommon */
190
191//------------------------------------------------------------------------------
192#endif // XPLCOMMON_BLOCKINGSTREAM_H
193
194// Local Variables:
195// mode: C++
196// c-basic-offset: 4
197// indent-tabs-mode: nil
198// End:
Note: See TracBrowser for help on using the repository browser.