fix logic
[personal-kdelibs.git] / kdecore / network / k3bufferedsocket.h
blobb212842015f239a559232ccf533803b2e2ddcbe9
1 /* -*- C++ -*-
2 * Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org>
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #ifndef KBUFFEREDSOCKET_H
26 #define KBUFFEREDSOCKET_H
28 #include <kdecore_export.h>
29 #include "k3streamsocket.h"
31 #include <QtCore/QObject>
32 #include <QtCore/QByteArray>
33 #include <QtCore/QList>
35 namespace KNetwork {
37 class KBufferedSocketPrivate;
38 /** @class KBufferedSocket k3bufferedsocket.h k3bufferedsocket.h
39 * @brief Buffered stream sockets.
41 * This class allows the user to create and operate buffered stream sockets
42 * such as those used in most Internet connections. This class is
43 * also the one that resembles the most to the old QSocket
44 * implementation.
46 * Objects of this type operate only in non-blocking mode. A call to
47 * setBlocking(true) will result in an error.
49 * @note Buffered sockets only make sense if you're using them from
50 * the main (event-loop) thread. This is actually a restriction
51 * imposed by Qt's QSocketNotifier. If you want to use a socket
52 * in an auxiliary thread, please use KStreamSocket.
54 * @see KNetwork::KStreamSocket, KNetwork::KServerSocket
55 * @author Thiago Macieira <thiago@kde.org>
56 * @deprecated Use KSocketFactory or KLocalSocket instead
58 class KDECORE_EXPORT KBufferedSocket: public KStreamSocket
60 Q_OBJECT
61 public:
62 /**
63 * Default constructor.
65 * @param node destination host
66 * @param service destination service to connect to
67 * @param parent the parent object for this object
69 explicit KBufferedSocket(const QString& node = QString(), const QString& service = QString(),
70 QObject* parent = 0L);
72 /**
73 * Destructor.
75 virtual ~KBufferedSocket();
77 /**
78 * Be sure to catch new devices.
80 virtual void setSocketDevice(KSocketDevice* device);
82 protected:
83 /**
84 * Buffered sockets can only operate in non-blocking mode.
86 virtual bool setSocketOptions(int opts);
88 public:
89 /**
90 * Closes the socket for new data, but allow data that had been buffered
91 * for output with writeData() to be still be written.
93 * @sa closeNow
95 virtual void close();
97 /**
98 * Make use of the buffers.
100 virtual qint64 bytesAvailable() const;
103 * Make use of buffers.
105 virtual qint64 waitForMore(int msecs, bool *timeout = 0L);
108 * Catch changes.
110 virtual void enableRead(bool enable);
113 * Catch changes.
115 virtual void enableWrite(bool enable);
118 * Sets the use of input buffering.
120 void setInputBuffering(bool enable);
123 * Sets the use of output buffering.
125 void setOutputBuffering(bool enable);
128 * Returns the length of the output buffer.
130 virtual qint64 bytesToWrite() const;
133 * Closes the socket and discards any output data that had been buffered
134 * with writeData() but that had not yet been written.
136 * @sa close
138 virtual void closeNow();
141 * Returns true if a line can be read with readLine()
143 virtual bool canReadLine() const;
145 // KDE4: make virtual, add timeout to match the Qt4 signature
146 // and move to another class up the hierarchy
148 * Blocks until the connection is either established, or completely
149 * failed.
151 void waitForConnect();
153 protected:
155 * Reads data from a socket.
157 * The @p from parameter is always set to peerAddress()
159 virtual qint64 readData(char *data, qint64 maxlen, KSocketAddress *from);
162 * Peeks data from the socket.
164 * The @p from parameter is always set to peerAddress()
166 virtual qint64 peekData(char *data, qint64 maxlen, KSocketAddress *from);
169 * Writes data to the socket.
171 * The @p to parameter is discarded.
173 virtual qint64 writeData(const char *data, qint64 len, const KSocketAddress* to);
176 * Improve the readLine performance
178 virtual qint64 readLineData(char *data, qint64 maxSize);
181 * Catch connection to clear the buffers
183 virtual void stateChanging(SocketState newState);
185 protected Q_SLOTS:
187 * Slot called when there's read activity.
189 virtual void slotReadActivity();
192 * Slot called when there's write activity.
194 virtual void slotWriteActivity();
196 #if 0
197 // Already present in QIODevice
198 Q_SIGNALS:
200 * This signal is emitted whenever data is written.
202 void bytesWritten(int bytes);
203 #endif
205 private:
206 KBufferedSocket(const KBufferedSocket&);
207 KBufferedSocket& operator=(const KBufferedSocket&);
209 KBufferedSocketPrivate* const d;
212 } // namespace KNetwork
214 #endif