source: psi/vendor/affinix/current/cutestuff/util/bytestream.cpp@ 2

Last change on this file since 2 was 2, checked in by dmik, 19 years ago

Imported original Psi 0.10 sources from Affinix

File size: 7.3 KB
Line 
1/*
2 * bytestream.cpp - base class for bytestreams
3 * Copyright (C) 2003 Justin Karneges
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include"bytestream.h"
22
23// CS_NAMESPACE_BEGIN
24
25//! \class ByteStream bytestream.h
26//! \brief Base class for "bytestreams"
27//!
28//! This class provides a basic framework for a "bytestream", here defined
29//! as a bi-directional, asynchronous pipe of data. It can be used to create
30//! several different kinds of bytestream-applications, such as a console or
31//! TCP connection, or something more abstract like a security layer or tunnel,
32//! all with the same interface. The provided functions make creating such
33//! classes simpler. ByteStream is a pure-virtual class, so you do not use it
34//! on its own, but instead through a subclass such as \a BSocket.
35//!
36//! The signals connectionClosed(), delayedCloseFinished(), readyRead(),
37//! bytesWritten(), and error() serve the exact same function as those from
38//! <A HREF="http://doc.trolltech.com/3.1/qsocket.html">QSocket</A>.
39//!
40//! The simplest way to create a ByteStream is to reimplement isOpen(), close(),
41//! and tryWrite(). Call appendRead() whenever you want to make data available for
42//! reading. ByteStream will take care of the buffers with regards to the caller,
43//! and will call tryWrite() when the write buffer gains data. It will be your
44//! job to call tryWrite() whenever it is acceptable to write more data to
45//! the underlying system.
46//!
47//! If you need more advanced control, reimplement read(), write(), bytesAvailable(),
48//! and/or bytesToWrite() as necessary.
49//!
50//! Use appendRead(), appendWrite(), takeRead(), and takeWrite() to modify the
51//! buffers. If you have more advanced requirements, the buffers can be accessed
52//! directly with readBuf() and writeBuf().
53//!
54//! Also available are the static convenience functions ByteStream::appendArray()
55//! and ByteStream::takeArray(), which make dealing with byte queues very easy.
56
57class ByteStream::Private
58{
59public:
60 Private() {}
61
62 QByteArray readBuf, writeBuf;
63};
64
65//!
66//! Constructs a ByteStream object with parent \a parent.
67ByteStream::ByteStream(QObject *parent)
68:QObject(parent)
69{
70 d = new Private;
71}
72
73//!
74//! Destroys the object and frees allocated resources.
75ByteStream::~ByteStream()
76{
77 delete d;
78}
79
80//!
81//! Returns TRUE if the stream is open, meaning that you can write to it.
82bool ByteStream::isOpen() const
83{
84 return false;
85}
86
87//!
88//! Closes the stream. If there is data in the write buffer then it will be
89//! written before actually closing the stream. Once all data has been written,
90//! the delayedCloseFinished() signal will be emitted.
91//! \sa delayedCloseFinished()
92void ByteStream::close()
93{
94}
95
96//!
97//! Writes array \a a to the stream.
98void ByteStream::write(const QByteArray &a)
99{
100 if(!isOpen())
101 return;
102
103 bool doWrite = bytesToWrite() == 0 ? true: false;
104 appendWrite(a);
105 if(doWrite)
106 tryWrite();
107}
108
109//!
110//! Reads bytes \a bytes of data from the stream and returns them as an array. If \a bytes is 0, then
111//! \a read will return all available data.
112QByteArray ByteStream::read(int bytes)
113{
114 return takeRead(bytes);
115}
116
117//!
118//! Returns the number of bytes available for reading.
119int ByteStream::bytesAvailable() const
120{
121 return d->readBuf.size();
122}
123
124//!
125//! Returns the number of bytes that are waiting to be written.
126int ByteStream::bytesToWrite() const
127{
128 return d->writeBuf.size();
129}
130
131//!
132//! Writes string \a cs to the stream.
133void ByteStream::write(const QCString &cs)
134{
135 QByteArray block(cs.length());
136 memcpy(block.data(), cs.data(), block.size());
137 write(block);
138}
139
140//!
141//! Clears the read buffer.
142void ByteStream::clearReadBuffer()
143{
144 d->readBuf.resize(0);
145}
146
147//!
148//! Clears the write buffer.
149void ByteStream::clearWriteBuffer()
150{
151 d->writeBuf.resize(0);
152}
153
154//!
155//! Appends \a block to the end of the read buffer.
156void ByteStream::appendRead(const QByteArray &block)
157{
158 appendArray(&d->readBuf, block);
159}
160
161//!
162//! Appends \a block to the end of the write buffer.
163void ByteStream::appendWrite(const QByteArray &block)
164{
165 appendArray(&d->writeBuf, block);
166}
167
168//!
169//! Returns \a size bytes from the start of the read buffer.
170//! If \a size is 0, then all available data will be returned.
171//! If \a del is TRUE, then the bytes are also removed.
172QByteArray ByteStream::takeRead(int size, bool del)
173{
174 return takeArray(&d->readBuf, size, del);
175}
176
177//!
178//! Returns \a size bytes from the start of the write buffer.
179//! If \a size is 0, then all available data will be returned.
180//! If \a del is TRUE, then the bytes are also removed.
181QByteArray ByteStream::takeWrite(int size, bool del)
182{
183 return takeArray(&d->writeBuf, size, del);
184}
185
186//!
187//! Returns a reference to the read buffer.
188QByteArray & ByteStream::readBuf()
189{
190 return d->readBuf;
191}
192
193//!
194//! Returns a reference to the write buffer.
195QByteArray & ByteStream::writeBuf()
196{
197 return d->writeBuf;
198}
199
200//!
201//! Attempts to try and write some bytes from the write buffer, and returns the number
202//! successfully written or -1 on error. The default implementation returns -1.
203int ByteStream::tryWrite()
204{
205 return -1;
206}
207
208//!
209//! Append array \a b to the end of the array pointed to by \a a.
210void ByteStream::appendArray(QByteArray *a, const QByteArray &b)
211{
212 int oldsize = a->size();
213 a->resize(oldsize + b.size());
214 memcpy(a->data() + oldsize, b.data(), b.size());
215}
216
217//!
218//! Returns \a size bytes from the start of the array pointed to by \a from.
219//! If \a size is 0, then all available data will be returned.
220//! If \a del is TRUE, then the bytes are also removed.
221QByteArray ByteStream::takeArray(QByteArray *from, int size, bool del)
222{
223 QByteArray a;
224 if(size == 0) {
225 a = from->copy();
226 if(del)
227 from->resize(0);
228 }
229 else {
230 if(size > (int)from->size())
231 size = from->size();
232 a.resize(size);
233 char *r = from->data();
234 memcpy(a.data(), r, size);
235 if(del) {
236 int newsize = from->size()-size;
237 memmove(r, r+size, newsize);
238 from->resize(newsize);
239 }
240 }
241 return a;
242}
243 void connectionClosed();
244 void delayedCloseFinished();
245 void readyRead();
246 void bytesWritten(int);
247 void error(int);
248
249//! \fn void ByteStream::connectionClosed()
250//! This signal is emitted when the remote end of the stream closes.
251
252//! \fn void ByteStream::delayedCloseFinished()
253//! This signal is emitted when all pending data has been written to the stream
254//! after an attempt to close.
255
256//! \fn void ByteStream::readyRead()
257//! This signal is emitted when data is available to be read.
258
259//! \fn void ByteStream::bytesWritten(int x);
260//! This signal is emitted when data has been successfully written to the stream.
261//! \a x is the number of bytes written.
262
263//! \fn void ByteStream::error(int code)
264//! This signal is emitted when an error occurs in the stream. The reason for
265//! error is indicated by \a code.
266
267// CS_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.