diff options
Diffstat (limited to '')
-rw-r--r-- | src/stable/clientbuf.h | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/src/stable/clientbuf.h b/src/stable/clientbuf.h new file mode 100644 index 0000000..7781b6a --- /dev/null +++ b/src/stable/clientbuf.h | |||
@@ -0,0 +1,179 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2019 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_CLIENT_BUF_H | ||
9 | #define BU_CLIENT_BUF_H | ||
10 | |||
11 | #include "bu/stream.h" | ||
12 | #include "bu/streamstack.h" | ||
13 | #include "bu/queuebuf.h" | ||
14 | #include "bu/mutex.h" | ||
15 | |||
16 | namespace Bu | ||
17 | { | ||
18 | /** | ||
19 | * ClientBuf represents two buffered I/O channels, input and output. Each | ||
20 | * is stored in its own Bu::QueueBuf. One side is client oriented and can | ||
21 | * have filters applied ala Bu::StreamStack. The other is server oriented | ||
22 | * and is accesesed raw. Both must be accessed via accessor objects. | ||
23 | * | ||
24 | * Direct access to the buffers isn't allowed, instead you must select a | ||
25 | * perspective. client() provides an accessor where reading consumes data | ||
26 | * coming from the socket and writing provides data to write to the socket. | ||
27 | * server() works in reverse, writing is writing data from the socket that | ||
28 | * we have just read, reading is reading data that should be written to the | ||
29 | * socket. | ||
30 | * | ||
31 | * client() provides a filtered access view of the data, while server() is | ||
32 | * unfiltered. | ||
33 | */ | ||
34 | class ClientBuf | ||
35 | { | ||
36 | public: | ||
37 | class ClientAccess : public Bu::Stream | ||
38 | { | ||
39 | friend class Bu::ClientBuf; | ||
40 | private: | ||
41 | ClientAccess( Bu::ClientBuf &rBuf ); | ||
42 | |||
43 | public: | ||
44 | virtual ~ClientAccess(); | ||
45 | |||
46 | virtual void close(); | ||
47 | virtual size read( void *pBuf, size iBytes ); | ||
48 | virtual size peek( void *pData, int iBytes, int iOffset=0 ); | ||
49 | virtual size write( const void *pBuf, size iBytes ); | ||
50 | using Stream::write; | ||
51 | virtual size tell(); | ||
52 | virtual void seek( size offset ); | ||
53 | virtual void setPos( size pos ); | ||
54 | virtual void setPosEnd( size pos ); | ||
55 | virtual bool isEos(); | ||
56 | virtual bool isOpen(); | ||
57 | virtual void flush(); | ||
58 | virtual bool canRead(); | ||
59 | virtual bool canWrite(); | ||
60 | virtual bool isReadable(); | ||
61 | virtual bool isWritable(); | ||
62 | virtual bool isSeekable(); | ||
63 | virtual bool isBlocking(); | ||
64 | virtual void setBlocking( bool bBlocking=true ); | ||
65 | virtual void setSize( size iSize ); | ||
66 | virtual size getSize() const; | ||
67 | virtual size getBlockSize() const; | ||
68 | virtual Bu::String getLocation() const; | ||
69 | |||
70 | private: | ||
71 | Bu::ClientBuf &rBuf; | ||
72 | Bu::QueueBuf qbPeek; | ||
73 | mutable Bu::Mutex mAccess; | ||
74 | }; | ||
75 | |||
76 | class ServerAccess : public Bu::Stream | ||
77 | { | ||
78 | friend class Bu::ClientBuf; | ||
79 | private: | ||
80 | ServerAccess( Bu::ClientBuf &rBuf ); | ||
81 | |||
82 | public: | ||
83 | virtual ~ServerAccess(); | ||
84 | |||
85 | virtual void close(); | ||
86 | virtual size read( void *pBuf, size iBytes ); | ||
87 | virtual size peek( void *pData, int iBytes, int iOffset=0 ); | ||
88 | virtual size write( const void *pBuf, size iBytes ); | ||
89 | using Stream::write; | ||
90 | virtual size tell(); | ||
91 | virtual void seek( size offset ); | ||
92 | virtual void setPos( size pos ); | ||
93 | virtual void setPosEnd( size pos ); | ||
94 | virtual bool isEos(); | ||
95 | virtual bool isOpen(); | ||
96 | virtual void flush(); | ||
97 | virtual bool canRead(); | ||
98 | virtual bool canWrite(); | ||
99 | virtual bool isReadable(); | ||
100 | virtual bool isWritable(); | ||
101 | virtual bool isSeekable(); | ||
102 | virtual bool isBlocking(); | ||
103 | virtual void setBlocking( bool bBlocking=true ); | ||
104 | virtual void setSize( size iSize ); | ||
105 | virtual size getSize() const; | ||
106 | virtual size getBlockSize() const; | ||
107 | virtual Bu::String getLocation() const; | ||
108 | |||
109 | private: | ||
110 | Bu::ClientBuf &rBuf; | ||
111 | }; | ||
112 | |||
113 | private: | ||
114 | class ClientAccessRaw : public Bu::Stream | ||
115 | { | ||
116 | friend class Bu::ClientBuf; | ||
117 | private: | ||
118 | ClientAccessRaw( Bu::ClientBuf &rBuf ); | ||
119 | |||
120 | public: | ||
121 | virtual ~ClientAccessRaw(); | ||
122 | |||
123 | virtual void close(); | ||
124 | virtual size read( void *pBuf, size iBytes ); | ||
125 | virtual size write( const void *pBuf, size iBytes ); | ||
126 | using Stream::write; | ||
127 | virtual size tell(); | ||
128 | virtual void seek( size offset ); | ||
129 | virtual void setPos( size pos ); | ||
130 | virtual void setPosEnd( size pos ); | ||
131 | virtual bool isEos(); | ||
132 | virtual bool isOpen(); | ||
133 | virtual void flush(); | ||
134 | virtual bool canRead(); | ||
135 | virtual bool canWrite(); | ||
136 | virtual bool isReadable(); | ||
137 | virtual bool isWritable(); | ||
138 | virtual bool isSeekable(); | ||
139 | virtual bool isBlocking(); | ||
140 | virtual void setBlocking( bool bBlocking=true ); | ||
141 | virtual void setSize( size iSize ); | ||
142 | virtual size getSize() const; | ||
143 | virtual size getBlockSize() const; | ||
144 | virtual Bu::String getLocation() const; | ||
145 | |||
146 | private: | ||
147 | Bu::ClientBuf &rBuf; | ||
148 | }; | ||
149 | |||
150 | public: | ||
151 | ClientBuf(); | ||
152 | virtual ~ClientBuf(); | ||
153 | |||
154 | /** | ||
155 | * Access the client access side. Use directly, do NOT copy or store. | ||
156 | */ | ||
157 | ClientAccess &client(); | ||
158 | |||
159 | /** | ||
160 | * Access the server access side. Use directly, do NOT copy or store. | ||
161 | */ | ||
162 | ServerAccess &server(); | ||
163 | |||
164 | private: | ||
165 | ClientAccessRaw accClientRaw; | ||
166 | ServerAccess accServer; | ||
167 | Bu::StreamStack accClientFiltered; | ||
168 | ClientAccess accClient; | ||
169 | Bu::QueueBuf qbOutput; | ||
170 | Bu::QueueBuf qbInput; | ||
171 | Bu::Mutex mOutput; | ||
172 | Bu::Mutex mInput; | ||
173 | friend class Bu::ClientBuf::ClientAccess; | ||
174 | friend class Bu::ClientBuf::ClientAccessRaw; | ||
175 | friend class Bu::ClientBuf::ServerAccess; | ||
176 | }; | ||
177 | } | ||
178 | |||
179 | #endif | ||