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_WIN32_BUFFEREDSTREAM_H
|
---|
30 | #define HU_VARADIISTVAN_SCPL_IO_WIN32_BUFFEREDSTREAM_H
|
---|
31 | //------------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | #include "Overlappable.h"
|
---|
34 |
|
---|
35 | //------------------------------------------------------------------------------
|
---|
36 |
|
---|
37 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
38 |
|
---|
39 | //------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | class ReadingBuffer;
|
---|
42 | class WritingBuffer;
|
---|
43 | class Overlapped;
|
---|
44 |
|
---|
45 | //------------------------------------------------------------------------------
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * A buffered stream.
|
---|
49 | *
|
---|
50 | * It wraps a HANDLE which can be read and/or written with ReadFile or
|
---|
51 | * WriteFile, respectively.
|
---|
52 | */
|
---|
53 | class BufferedStream : public Overlappable
|
---|
54 | {
|
---|
55 | public:
|
---|
56 | /**
|
---|
57 | * The default capacity.
|
---|
58 | */
|
---|
59 | static const size_t DEFAULT_CAPACITY = 4096;
|
---|
60 |
|
---|
61 | private:
|
---|
62 | /**
|
---|
63 | * The reading buffer. It may be 0, if no reading buffer is
|
---|
64 | * associated with this waitable.
|
---|
65 | */
|
---|
66 | ReadingBuffer* readingBuffer;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * The writing buffer. It may be 0, if no writing buffer is
|
---|
70 | * associated with this waitable.
|
---|
71 | */
|
---|
72 | WritingBuffer* writingBuffer;
|
---|
73 |
|
---|
74 | protected:
|
---|
75 | /**
|
---|
76 | * Construct the buffered stream.
|
---|
77 | */
|
---|
78 | BufferedStream(Waiter* waiter = 0, HANDLE handle = 0,
|
---|
79 | size_t readingCapacity = DEFAULT_CAPACITY,
|
---|
80 | size_t writingCapacity = DEFAULT_CAPACITY);
|
---|
81 |
|
---|
82 | public:
|
---|
83 | /**
|
---|
84 | * Destroy the stream.
|
---|
85 | */
|
---|
86 | virtual ~BufferedStream();
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Get the reference of the reading buffer. Note, that it can be a
|
---|
90 | * 0 reference, if the stream has no reading buffer.
|
---|
91 | */
|
---|
92 | ReadingBuffer& getReadingBuffer();
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Get the reference of the writing buffer. Note, that it can be a
|
---|
96 | * 0 reference, if the stream has no writing buffer.
|
---|
97 | */
|
---|
98 | WritingBuffer& getWritingBuffer();
|
---|
99 |
|
---|
100 | protected:
|
---|
101 | /**
|
---|
102 | * Read using the handle at most the given number of bytes into
|
---|
103 | * the given buffer.
|
---|
104 | *
|
---|
105 | * @return the number of bytes read. If 0, it indicates the end of
|
---|
106 | * the file or connection. If negative, it means either a failure,
|
---|
107 | * or the fact that the operation cannot be completed at this time.
|
---|
108 | */
|
---|
109 | ssize_t read(void* dest, size_t length, Overlapped* overlapped);
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Write using the handle the given number of bytes from the given
|
---|
113 | * buffer.
|
---|
114 | *
|
---|
115 | * @return whether the writing succeeded. If false, it is either a
|
---|
116 | * failure, or an inidication of the fact that the operation could
|
---|
117 | * not be completed.
|
---|
118 | */
|
---|
119 | bool write(const void* src, size_t length, Overlapped* overlapped);
|
---|
120 |
|
---|
121 | friend class ReadingBuffer;
|
---|
122 | friend class WritingBuffer;
|
---|
123 | };
|
---|
124 |
|
---|
125 | //------------------------------------------------------------------------------
|
---|
126 | // Inline definitions
|
---|
127 | //------------------------------------------------------------------------------
|
---|
128 |
|
---|
129 | inline ReadingBuffer& BufferedStream::getReadingBuffer()
|
---|
130 | {
|
---|
131 | return *readingBuffer;
|
---|
132 | }
|
---|
133 |
|
---|
134 | //------------------------------------------------------------------------------
|
---|
135 |
|
---|
136 | inline WritingBuffer& BufferedStream::getWritingBuffer()
|
---|
137 | {
|
---|
138 | return *writingBuffer;
|
---|
139 | }
|
---|
140 |
|
---|
141 | //------------------------------------------------------------------------------
|
---|
142 |
|
---|
143 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
144 |
|
---|
145 | //------------------------------------------------------------------------------
|
---|
146 | #endif // HU_VARADIISTVAN_SCPL_IO_WIN32_BUFFEREDSTREAM_H
|
---|
147 |
|
---|
148 | // Local Variables:
|
---|
149 | // mode: C++
|
---|
150 | // c-basic-offset: 4
|
---|
151 | // indent-tabs-mode: nil
|
---|
152 | // End:
|
---|