source: xplcommon/src/xplcommon/win32/BufferedStream.h@ 21:eb59943050c9

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

Added the implementation of the local sockets for Win32 and it seems to work

File size: 5.0 KB
Line 
1// Copyright (c) 2012 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_WIN32_BUFFEREDSTREAM_H
31#define XPLCOMMON_WIN32_BUFFEREDSTREAM_H
32//------------------------------------------------------------------------------
33
34#include "Overlappable.h"
35
36//------------------------------------------------------------------------------
37
38namespace xplcommon { namespace win32 {
39
40//------------------------------------------------------------------------------
41
42class ReadingBuffer;
43class WritingBuffer;
44class Overlapped;
45
46//------------------------------------------------------------------------------
47
48/**
49 * A buffered stream.
50 *
51 * It wraps a HANDLE which can be read and/or written with ReadFile or
52 * WriteFile, respectively.
53 */
54class BufferedStream : public Overlappable
55{
56public:
57 /**
58 * The default capacity.
59 */
60 static const size_t DEFAULT_CAPACITY = 4096;
61
62private:
63 /**
64 * The reading buffer. It may be 0, if no reading buffer is
65 * associated with this waitable.
66 */
67 ReadingBuffer* readingBuffer;
68
69 /**
70 * The writing buffer. It may be 0, if no writing buffer is
71 * associated with this waitable.
72 */
73 WritingBuffer* writingBuffer;
74
75protected:
76 /**
77 * Construct the buffered stream.
78 */
79 BufferedStream(Waiter* waiter = 0, HANDLE handle = 0,
80 size_t readingCapacity = DEFAULT_CAPACITY,
81 size_t writingCapacity = DEFAULT_CAPACITY);
82
83public:
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
101protected:
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
130inline ReadingBuffer& BufferedStream::getReadingBuffer()
131{
132 return *readingBuffer;
133}
134
135//------------------------------------------------------------------------------
136
137inline WritingBuffer& BufferedStream::getWritingBuffer()
138{
139 return *writingBuffer;
140}
141
142//------------------------------------------------------------------------------
143
144} /* namespace xplcommon::win32 */ } /* namespace xplcommon */
145
146//------------------------------------------------------------------------------
147#endif // XPLCOMMON_WIN32_BUFFEREDSTREAM_H
148
149// Local Variables:
150// mode: C++
151// c-basic-offset: 4
152// indent-tabs-mode: nil
153// End:
Note: See TracBrowser for help on using the repository browser.