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 |
|
---|
42 | namespace 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 | */
|
---|
55 | class BlockingStream : public Failable
|
---|
56 | {
|
---|
57 | private:
|
---|
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 |
|
---|
78 | public:
|
---|
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 | };
|
---|
122 |
|
---|
123 | //------------------------------------------------------------------------------
|
---|
124 | // Inline definitions
|
---|
125 | //------------------------------------------------------------------------------
|
---|
126 |
|
---|
127 | inline BlockingStream::BlockingStream(BufferedStream& stream) :
|
---|
128 | stream(stream),
|
---|
129 | event(stream.getWaiter()),
|
---|
130 | interrupted(false),
|
---|
131 | readingOffset(0)
|
---|
132 | {
|
---|
133 | }
|
---|
134 |
|
---|
135 | //------------------------------------------------------------------------------
|
---|
136 |
|
---|
137 | inline void BlockingStream::interrupt()
|
---|
138 | {
|
---|
139 | event.fire();
|
---|
140 | }
|
---|
141 |
|
---|
142 | //------------------------------------------------------------------------------
|
---|
143 |
|
---|
144 | } /* namespace xplcommon */
|
---|
145 |
|
---|
146 | //------------------------------------------------------------------------------
|
---|
147 | #endif // XPLCOMMON_BLOCKINGSTREAM_H
|
---|
148 |
|
---|
149 | // Local Variables:
|
---|
150 | // mode: C++
|
---|
151 | // c-basic-offset: 4
|
---|
152 | // indent-tabs-mode: nil
|
---|
153 | // End:
|
---|