File indexing completed on 2024-03-24 15:27:06

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 KBUFFEREDSOCKET_H
0026 #define KBUFFEREDSOCKET_H
0027 
0028 #include <kdelibs4support_export.h>
0029 #include "k3streamsocket.h"
0030 
0031 #include <QObject>
0032 #include <QByteArray>
0033 #include <QList>
0034 
0035 namespace KNetwork
0036 {
0037 
0038 class KBufferedSocketPrivate;
0039 /** @class KBufferedSocket k3bufferedsocket.h k3bufferedsocket.h
0040  *  @brief Buffered stream sockets.
0041  *
0042  * This class allows the user to create and operate buffered stream sockets
0043  * such as those used in most Internet connections. This class is
0044  * also the one that resembles the most to the old QSocket
0045  * implementation.
0046  *
0047  * Objects of this type operate only in non-blocking mode. A call to
0048  * setBlocking(true) will result in an error.
0049  *
0050  * @note Buffered sockets only make sense if you're using them from
0051  *       the main (event-loop) thread. This is actually a restriction
0052  *       imposed by Qt's QSocketNotifier. If you want to use a socket
0053  *       in an auxiliary thread, please use KStreamSocket.
0054  *
0055  * @see KNetwork::KStreamSocket, KNetwork::KServerSocket
0056  * @author Thiago Macieira <thiago@kde.org>
0057  * @deprecated Use KSocketFactory or KLocalSocket instead
0058  */
0059 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KBufferedSocket: public KStreamSocket
0060 {
0061     Q_OBJECT
0062 public:
0063     /**
0064      * Default constructor.
0065      *
0066      * @param node    destination host
0067      * @param service destination service to connect to
0068      * @param parent      the parent object for this object
0069      */
0070     KDELIBS4SUPPORT_DEPRECATED explicit KBufferedSocket(const QString &node = QString(), const QString &service = QString(),
0071                              QObject *parent = nullptr);
0072 
0073     /**
0074      * Destructor.
0075      */
0076     ~KBufferedSocket() override;
0077 
0078     /**
0079      * Be sure to catch new devices.
0080      */
0081     void setSocketDevice(KSocketDevice *device) override;
0082 
0083 protected:
0084     /**
0085      * Buffered sockets can only operate in non-blocking mode.
0086      */
0087     bool setSocketOptions(int opts) override;
0088 
0089 public:
0090     /**
0091      * Closes the socket for new data, but allow data that had been buffered
0092      * for output with writeData() to be still be written.
0093      *
0094      * @sa closeNow
0095      */
0096     void close() override;
0097 
0098     /**
0099      * Make use of the buffers.
0100      */
0101     qint64 bytesAvailable() const override;
0102 
0103     /**
0104      * Make use of buffers.
0105      */
0106     qint64 waitForMore(int msecs, bool *timeout = nullptr) override;
0107 
0108     /**
0109      * Catch changes.
0110      */
0111     void enableRead(bool enable) override;
0112 
0113     /**
0114      * Catch changes.
0115      */
0116     void enableWrite(bool enable) override;
0117 
0118     /**
0119      * Sets the use of input buffering.
0120      */
0121     void setInputBuffering(bool enable);
0122 
0123     /**
0124      * Sets the use of output buffering.
0125      */
0126     void setOutputBuffering(bool enable);
0127 
0128     /**
0129      * Returns the length of the output buffer.
0130      */
0131     qint64 bytesToWrite() const override;
0132 
0133     /**
0134      * Closes the socket and discards any output data that had been buffered
0135      * with writeData() but that had not yet been written.
0136      *
0137      * @sa close
0138      */
0139     virtual void closeNow();
0140 
0141     /**
0142      * Returns true if a line can be read with readLine()
0143      */
0144     bool canReadLine() const override;
0145 
0146     // KDE4: make virtual, add timeout to match the Qt4 signature
0147     //       and move to another class up the hierarchy
0148     /**
0149      * Blocks until the connection is either established, or completely
0150      * failed.
0151      */
0152     void waitForConnect();
0153 
0154 protected:
0155     /**
0156      * Reads data from a socket.
0157      *
0158      * The @p from parameter is always set to peerAddress()
0159      */
0160     qint64 readData(char *data, qint64 maxlen, KSocketAddress *from) override;
0161 
0162     /**
0163      * Peeks data from the socket.
0164      *
0165      * The @p from parameter is always set to peerAddress()
0166      */
0167     qint64 peekData(char *data, qint64 maxlen, KSocketAddress *from) override;
0168 
0169     /**
0170      * Writes data to the socket.
0171      *
0172      * The @p to parameter is discarded.
0173      */
0174     qint64 writeData(const char *data, qint64 len, const KSocketAddress *to) override;
0175 
0176     /**
0177      * Improve the readLine performance
0178      */
0179     qint64 readLineData(char *data, qint64 maxSize) override;
0180 
0181     /**
0182      * Catch connection to clear the buffers
0183      */
0184     void stateChanging(SocketState newState) override;
0185 
0186 protected Q_SLOTS:
0187     /**
0188      * Slot called when there's read activity.
0189      */
0190     void slotReadActivity() override;
0191 
0192     /**
0193      * Slot called when there's write activity.
0194      */
0195     void slotWriteActivity() override;
0196 
0197 #if 0
0198     // Already present in QIODevice
0199 Q_SIGNALS:
0200     /**
0201      * This signal is emitted whenever data is written.
0202      */
0203     void bytesWritten(int bytes);
0204 #endif
0205 
0206 private:
0207     KBufferedSocket(const KBufferedSocket &);
0208     KBufferedSocket &operator=(const KBufferedSocket &);
0209 
0210     KBufferedSocketPrivate *const d;
0211 };
0212 
0213 }               // namespace KNetwork
0214 
0215 #endif