source: xplcommon/src/xplcommon/BlockingStream.h@ 30:1dde7e03353f

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

A little cleanup

File size: 4.9 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#include "Waiter.h"
38#include "WaitableEvent.h"
39
40//------------------------------------------------------------------------------
41
42namespace xplcommon {
43
44//------------------------------------------------------------------------------
45
46/**
47 * Wrapper for a buffered stream, which an event and provides
48 * blocking, but interruptible read() and write() operations.
49 *
50 * The class is designed in a way, that it expects its constructed
51 * instances to be called only from a single thread at a time, except
52 * for the interrupt() function, which may be called from another
53 * thread anytime.
54 */
55class BlockingStream : public Failable
56{
57private:
58 /**
59 * The buffered stream used.
60 */
61 BufferedStream& stream;
62
63 /**
64 * The waitable event.
65 */
66 WaitableEvent event;
67
68 /**
69 * Indicate if we are interrupted.
70 */
71 bool interrupted;
72
73 /**
74 * The offset in the buffer of the reading stream.
75 */
76 size_t readingOffset;
77
78public:
79 /**
80 * Construct the blocking stream for the given buffered stream.
81 *
82 * The stream must have a waiter which is used for waiting.
83 */
84 BlockingStream(BufferedStream& stream);
85
86 /**
87 * Interrupt the stream. Pending or future read and write calls
88 * will return indicating error. The isInterrupted() function can
89 * be used to check if the stream was interrupted or not.
90 */
91 void interrupt();
92
93 /**
94 * Indicate if the stream is interrupted or not.
95 */
96 bool isInterrupted();
97
98 /**
99 * Read the given number of bytes into the given buffer.
100 *
101 * @return if the reading has succeeded
102 */
103 bool read(void* dest, size_t length);
104
105 /**
106 * Write the given number of bytes from the given buffer. The data
107 * may not be written to the underlying stream fully, so if you
108 * want to make sure that it is sent, call flush() afterwards.
109 *
110 * @return true on success, false on error.
111 */
112 bool write(const void* src, size_t length);
113
114 /**
115 * Flush anything written so far into the buffer, but not to the
116 * underlying stream.
117 *
118 * @return whether the operation has succeeded.
119 */
120 bool flush();
121
122private:
123 /**
124 * Fill the buffer with data from the stream.
125 */
126 bool fill();
127};
128
129//------------------------------------------------------------------------------
130// Inline definitions
131//------------------------------------------------------------------------------
132
133inline BlockingStream::BlockingStream(BufferedStream& stream) :
134 stream(stream),
135 event(stream.getWaiter()),
136 interrupted(false),
137 readingOffset(0)
138{
139}
140
141//------------------------------------------------------------------------------
142
143inline void BlockingStream::interrupt()
144{
145 event.fire();
146}
147
148//------------------------------------------------------------------------------
149
150} /* namespace xplcommon */
151
152//------------------------------------------------------------------------------
153#endif // XPLCOMMON_BLOCKINGSTREAM_H
154
155// Local Variables:
156// mode: C++
157// c-basic-offset: 4
158// indent-tabs-mode: nil
159// End:
Note: See TracBrowser for help on using the repository browser.