1/* -*- C++ -*-
2 * Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org>
3 *
4 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25#ifndef KBUFFEREDSOCKET_H
26#define KBUFFEREDSOCKET_H
27
28#include <kdecore_export.h>
29#include "k3streamsocket.h"
30
31#include <QtCore/QObject>
32#include <QtCore/QByteArray>
33#include <QtCore/QList>
34
35namespace KNetwork {
36
37class KBufferedSocketPrivate;
38/** @class KBufferedSocket k3bufferedsocket.h k3bufferedsocket.h
39 * @brief Buffered stream sockets.
40 *
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.
45 *
46 * Objects of this type operate only in non-blocking mode. A call to
47 * setBlocking(true) will result in an error.
48 *
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.
53 *
54 * @see KNetwork::KStreamSocket, KNetwork::KServerSocket
55 * @author Thiago Macieira <thiago@kde.org>
56 * @deprecated Use KSocketFactory or KLocalSocket instead
57 */
58class KDECORE_EXPORT_DEPRECATED KBufferedSocket: public KStreamSocket
59{
60 Q_OBJECT
61public:
62 /**
63 * Default constructor.
64 *
65 * @param node destination host
66 * @param service destination service to connect to
67 * @param parent the parent object for this object
68 */
69 explicit KBufferedSocket(const QString& node = QString(), const QString& service = QString(),
70 QObject* parent = 0L);
71
72 /**
73 * Destructor.
74 */
75 virtual ~KBufferedSocket();
76
77 /**
78 * Be sure to catch new devices.
79 */
80 virtual void setSocketDevice(KSocketDevice* device);
81
82protected:
83 /**
84 * Buffered sockets can only operate in non-blocking mode.
85 */
86 virtual bool setSocketOptions(int opts);
87
88public:
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.
92 *
93 * @sa closeNow
94 */
95 virtual void close();
96
97 /**
98 * Make use of the buffers.
99 */
100 virtual qint64 bytesAvailable() const;
101
102 /**
103 * Make use of buffers.
104 */
105 virtual qint64 waitForMore(int msecs, bool *timeout = 0L);
106
107 /**
108 * Catch changes.
109 */
110 virtual void enableRead(bool enable);
111
112 /**
113 * Catch changes.
114 */
115 virtual void enableWrite(bool enable);
116
117 /**
118 * Sets the use of input buffering.
119 */
120 void setInputBuffering(bool enable);
121
122 /**
123 * Sets the use of output buffering.
124 */
125 void setOutputBuffering(bool enable);
126
127 /**
128 * Returns the length of the output buffer.
129 */
130 virtual qint64 bytesToWrite() const;
131
132 /**
133 * Closes the socket and discards any output data that had been buffered
134 * with writeData() but that had not yet been written.
135 *
136 * @sa close
137 */
138 virtual void closeNow();
139
140 /**
141 * Returns true if a line can be read with readLine()
142 */
143 virtual bool canReadLine() const;
144
145 // KDE4: make virtual, add timeout to match the Qt4 signature
146 // and move to another class up the hierarchy
147 /**
148 * Blocks until the connection is either established, or completely
149 * failed.
150 */
151 void waitForConnect();
152
153protected:
154 /**
155 * Reads data from a socket.
156 *
157 * The @p from parameter is always set to peerAddress()
158 */
159 virtual qint64 readData(char *data, qint64 maxlen, KSocketAddress *from);
160
161 /**
162 * Peeks data from the socket.
163 *
164 * The @p from parameter is always set to peerAddress()
165 */
166 virtual qint64 peekData(char *data, qint64 maxlen, KSocketAddress *from);
167
168 /**
169 * Writes data to the socket.
170 *
171 * The @p to parameter is discarded.
172 */
173 virtual qint64 writeData(const char *data, qint64 len, const KSocketAddress* to);
174
175 /**
176 * Improve the readLine performance
177 */
178 virtual qint64 readLineData(char *data, qint64 maxSize);
179
180 /**
181 * Catch connection to clear the buffers
182 */
183 virtual void stateChanging(SocketState newState);
184
185protected Q_SLOTS:
186 /**
187 * Slot called when there's read activity.
188 */
189 virtual void slotReadActivity();
190
191 /**
192 * Slot called when there's write activity.
193 */
194 virtual void slotWriteActivity();
195
196#if 0
197 // Already present in QIODevice
198Q_SIGNALS:
199 /**
200 * This signal is emitted whenever data is written.
201 */
202 void bytesWritten(int bytes);
203#endif
204
205private:
206 KBufferedSocket(const KBufferedSocket&);
207 KBufferedSocket& operator=(const KBufferedSocket&);
208
209 KBufferedSocketPrivate* const d;
210};
211
212} // namespace KNetwork
213
214#endif
215