~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to iris/src/irisnet/noncore/cutestuff/bytestream.cpp

  • Committer: Package Import Robot
  • Author(s): Jan Niehusmann
  • Date: 2014-07-01 21:49:34 UTC
  • mfrom: (6.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140701214934-gt4dkgm94byi4vnn
Tags: 0.15-1
* New upstream version
* set debhelper compat level to 9
* set Standards-Version to 3.9.5 (no further changes)
* add lintian override regarding license-problem-non-free-RFC
* use qconf to regenerate configure script
* implement hardening using buildflags instead of hardening-wrapper

Show diffs side-by-side

added added

removed removed

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