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(std::unique_ptr<EventFactory> eventFactory,
|
---|
79 | Waiter* waiter = 0, HANDLE handle = 0,
|
---|
80 | size_t readingCapacity = DEFAULT_CAPACITY,
|
---|
81 | size_t writingCapacity = DEFAULT_CAPACITY);
|
---|
82 |
|
---|
83 | public:
|
---|
84 | /**
|
---|
85 | * Destroy the stream.
|
---|
86 | */
|
---|
87 | virtual ~BufferedStream();
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Get the reference of the reading buffer. Note, that it can be a
|
---|
91 | * 0 reference, if the stream has no reading buffer.
|
---|
92 | */
|
---|
93 | ReadingBuffer& getReadingBuffer();
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Get the reference of the writing buffer. Note, that it can be a
|
---|
97 | * 0 reference, if the stream has no writing buffer.
|
---|
98 | */
|
---|
99 | WritingBuffer& getWritingBuffer();
|
---|
100 |
|
---|
101 | protected:
|
---|
102 | /**
|
---|
103 | * Read using the handle at most the given number of bytes into
|
---|
104 | * the given buffer.
|
---|
105 | *
|
---|
106 | * @return the number of bytes read. If 0, it indicates the end of
|
---|
107 | * the file or connection. If negative, it means either a failure,
|
---|
108 | * or the fact that the operation cannot be completed at this time.
|
---|
109 | */
|
---|
110 | ssize_t read(void* dest, size_t length, Overlapped* overlapped);
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Write using the handle the given number of bytes from the given
|
---|
114 | * buffer.
|
---|
115 | *
|
---|
116 | * @return whether the writing succeeded. If false, it is either a
|
---|
117 | * failure, or an inidication of the fact that the operation could
|
---|
118 | * not be completed.
|
---|
119 | */
|
---|
120 | bool write(const void* src, size_t length, Overlapped* overlapped);
|
---|
121 |
|
---|
122 | friend class ReadingBuffer;
|
---|
123 | friend class WritingBuffer;
|
---|
124 | };
|
---|
125 |
|
---|
126 | //------------------------------------------------------------------------------
|
---|
127 | // Inline definitions
|
---|
128 | //------------------------------------------------------------------------------
|
---|
129 |
|
---|
130 | inline ReadingBuffer& BufferedStream::getReadingBuffer()
|
---|
131 | {
|
---|
132 | return *readingBuffer;
|
---|
133 | }
|
---|
134 |
|
---|
135 | //------------------------------------------------------------------------------
|
---|
136 |
|
---|
137 | inline WritingBuffer& BufferedStream::getWritingBuffer()
|
---|
138 | {
|
---|
139 | return *writingBuffer;
|
---|
140 | }
|
---|
141 |
|
---|
142 | //------------------------------------------------------------------------------
|
---|
143 |
|
---|
144 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
145 |
|
---|
146 | //------------------------------------------------------------------------------
|
---|
147 | #endif // HU_VARADIISTVAN_SCPL_IO_WIN32_BUFFEREDSTREAM_H
|
---|
148 |
|
---|
149 | // Local Variables:
|
---|
150 | // mode: C++
|
---|
151 | // c-basic-offset: 4
|
---|
152 | // indent-tabs-mode: nil
|
---|
153 | // End:
|
---|