File indexing completed on 2024-03-03 15:03:11

0001 /*  -*- C++ -*-
0002  *  Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org>
0003  *
0004  *
0005  *  Permission is hereby granted, free of charge, to any person obtaining
0006  *  a copy of this software and associated documentation files (the
0007  *  "Software"), to deal in the Software without restriction, including
0008  *  without limitation the rights to use, copy, modify, merge, publish,
0009  *  distribute, sublicense, and/or sell copies of the Software, and to
0010  *  permit persons to whom the Software is furnished to do so, subject to
0011  *  the following conditions:
0012  *
0013  *  The above copyright notice and this permission notice shall be included
0014  *  in all copies or substantial portions of the Software.
0015  *
0016  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0020  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0021  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0022  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023  */
0024 
0025 #ifndef KSOCKETBUFFER_P_H
0026 #define KSOCKETBUFFER_P_H
0027 
0028 #include <QMutex>
0029 #include <QByteArray>
0030 #include <QList>
0031 
0032 namespace KNetwork
0033 {
0034 
0035 class KActiveSocketBase;
0036 
0037 namespace Internal
0038 {
0039 
0040 /**
0041  * @internal
0042  * @class KSocketBuffer k3socketbuffer_p.h k3socketbuffer_p.h
0043  * @brief generic socket buffering code
0044  *
0045  * This class implements generic buffering used by KBufferedSocket.
0046  *
0047  * @author Thiago Macieira <thiago@kde.org>
0048  * @deprecated Use KSocketFactory or KLocalSocket instead
0049  */
0050 class KSocketBuffer
0051 {
0052 public:
0053     /**
0054      * Default constructor.
0055      *
0056      * @param size    the maximum size of the buffer
0057      */
0058     KSocketBuffer(qint64 size = -1);
0059 
0060     /**
0061      * Copy constructor.
0062      */
0063     KSocketBuffer(const KSocketBuffer &other);
0064 
0065     /**
0066      * Virtual destructor. Frees the buffer and discards its contents.
0067      */
0068     ~KSocketBuffer();
0069 
0070     /**
0071      * Assignment operator.
0072      */
0073     KSocketBuffer &operator=(const KSocketBuffer &other);
0074 
0075     /**
0076      * Returns true if a line can be read from the buffer.
0077      */
0078     bool canReadLine() const;
0079 
0080     /**
0081      * Reads a line from the buffer and discard it from the buffer.
0082      */
0083     qint64 readLine(char *data, qint64 maxSize);
0084 
0085     /**
0086      * Returns the number of bytes in the buffer. Note that this is not
0087      * the size of the buffer.
0088      *
0089      * @sa size
0090      */
0091     qint64 length() const;
0092 
0093     /**
0094      * Returns true if the buffer is empty of data.
0095      */
0096     inline bool isEmpty() const
0097     {
0098         return length() == 0;
0099     }
0100 
0101     /**
0102      * Retrieves the buffer size. The value of -1 indicates that
0103      * the buffer has no defined upper limit.
0104      *
0105      * @sa length for the length of the data stored
0106      */
0107     qint64 size() const;
0108 
0109     /**
0110      * Sets the size of the buffer, if allowed.
0111      *
0112      * @param size    the maximum size, use -1 for unlimited.
0113      * @returns true on success, false if an error occurred.
0114      * @note if the new size is less than length(), the buffer will be truncated
0115      */
0116     bool setSize(qint64 size);
0117 
0118     /**
0119      * Returns true if the buffer is full (i.e., cannot receive more data)
0120      */
0121     inline bool isFull() const
0122     {
0123         return size() != -1 && size() == length();
0124     }
0125 
0126     /**
0127      * Adds data to the end of the buffer.
0128      *
0129      * @param data    the data to be added
0130      * @param len     the data length, in bytes
0131      * @returns the number of bytes added to the end of the buffer.
0132      */
0133     qint64 feedBuffer(const char *data, qint64 len);
0134 
0135     /**
0136      * Clears the buffer.
0137      */
0138     void clear();
0139 
0140     /**
0141      * Consumes data from the beginning of the buffer.
0142      *
0143      * @param data    where to copy the data to
0144      * @param maxlen  the maximum length to copy, in bytes
0145      * @param discard if true, the bytes copied will be discarded
0146      * @returns the number of bytes copied from the buffer
0147      */
0148     qint64 consumeBuffer(char *data, qint64 maxlen, bool discard = true);
0149 
0150     /**
0151      * Sends at most @p len bytes of data to the I/O Device.
0152      *
0153      * @param device  the device to which to send data
0154      * @param len     the amount of data to send; -1 to send everything
0155      * @returns the number of bytes sent and discarded from the buffer, -1
0156      *          indicates an error.
0157      */
0158     qint64 sendTo(KActiveSocketBase *device, qint64 len = -1);
0159 
0160     /**
0161      * Tries to receive @p len bytes of data from the I/O device.
0162      *
0163      * @param device  the device to receive from
0164      * @param len     the number of bytes to receive; -1 to read as much
0165      *                    as possible
0166      * @returns the number of bytes received and copied into the buffer,
0167      *          -1 indicates an error.
0168      */
0169     qint64 receiveFrom(KActiveSocketBase *device, qint64 len = -1);
0170 
0171 protected:
0172     mutable QMutex m_mutex;
0173     QList<QByteArray> m_list;
0174     qint64 m_offset;  ///< offset of the start of data in the first element
0175 
0176     qint64 m_size;        ///< the maximum length of the buffer
0177     mutable qint64 m_length;
0178 };
0179 
0180 }
0181 }         // namespace KNetwork::Internal
0182 
0183 #endif