KDELibs4Support

k3bufferedsocket.h
1 /* -*- C++ -*-
2  * Copyright (C) 2003,2005 Thiago Macieira <[email protected]>
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 <kdelibs4support_export.h>
29 #include "k3streamsocket.h"
30 
31 #include <QObject>
32 #include <QByteArray>
33 #include <QList>
34 
35 namespace KNetwork
36 {
37 
38 class KBufferedSocketPrivate;
39 /** @class KBufferedSocket k3bufferedsocket.h k3bufferedsocket.h
40  * @brief Buffered stream sockets.
41  *
42  * This class allows the user to create and operate buffered stream sockets
43  * such as those used in most Internet connections. This class is
44  * also the one that resembles the most to the old QSocket
45  * implementation.
46  *
47  * Objects of this type operate only in non-blocking mode. A call to
48  * setBlocking(true) will result in an error.
49  *
50  * @note Buffered sockets only make sense if you're using them from
51  * the main (event-loop) thread. This is actually a restriction
52  * imposed by Qt's QSocketNotifier. If you want to use a socket
53  * in an auxiliary thread, please use KStreamSocket.
54  *
55  * @see KNetwork::KStreamSocket, KNetwork::KServerSocket
56  * @author Thiago Macieira <[email protected]>
57  * @deprecated Use KSocketFactory or KLocalSocket instead
58  */
59 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KBufferedSocket: public KStreamSocket
60 {
61  Q_OBJECT
62 public:
63  /**
64  * Default constructor.
65  *
66  * @param node destination host
67  * @param service destination service to connect to
68  * @param parent the parent object for this object
69  */
70  KDELIBS4SUPPORT_DEPRECATED explicit KBufferedSocket(const QString &node = QString(), const QString &service = QString(),
71  QObject *parent = nullptr);
72 
73  /**
74  * Destructor.
75  */
76  ~KBufferedSocket() override;
77 
78  /**
79  * Be sure to catch new devices.
80  */
81  void setSocketDevice(KSocketDevice *device) override;
82 
83 protected:
84  /**
85  * Buffered sockets can only operate in non-blocking mode.
86  */
87  bool setSocketOptions(int opts) override;
88 
89 public:
90  /**
91  * Closes the socket for new data, but allow data that had been buffered
92  * for output with writeData() to be still be written.
93  *
94  * @sa closeNow
95  */
96  void close() override;
97 
98  /**
99  * Make use of the buffers.
100  */
101  qint64 bytesAvailable() const override;
102 
103  /**
104  * Make use of buffers.
105  */
106  qint64 waitForMore(int msecs, bool *timeout = nullptr) override;
107 
108  /**
109  * Catch changes.
110  */
111  void enableRead(bool enable) override;
112 
113  /**
114  * Catch changes.
115  */
116  void enableWrite(bool enable) override;
117 
118  /**
119  * Sets the use of input buffering.
120  */
121  void setInputBuffering(bool enable);
122 
123  /**
124  * Sets the use of output buffering.
125  */
126  void setOutputBuffering(bool enable);
127 
128  /**
129  * Returns the length of the output buffer.
130  */
131  qint64 bytesToWrite() const override;
132 
133  /**
134  * Closes the socket and discards any output data that had been buffered
135  * with writeData() but that had not yet been written.
136  *
137  * @sa close
138  */
139  virtual void closeNow();
140 
141  /**
142  * Returns true if a line can be read with readLine()
143  */
144  bool canReadLine() const override;
145 
146  // KDE4: make virtual, add timeout to match the Qt4 signature
147  // and move to another class up the hierarchy
148  /**
149  * Blocks until the connection is either established, or completely
150  * failed.
151  */
152  void waitForConnect();
153 
154 protected:
155  /**
156  * Reads data from a socket.
157  *
158  * The @p from parameter is always set to peerAddress()
159  */
160  qint64 readData(char *data, qint64 maxlen, KSocketAddress *from) override;
161 
162  /**
163  * Peeks data from the socket.
164  *
165  * The @p from parameter is always set to peerAddress()
166  */
167  qint64 peekData(char *data, qint64 maxlen, KSocketAddress *from) override;
168 
169  /**
170  * Writes data to the socket.
171  *
172  * The @p to parameter is discarded.
173  */
174  qint64 writeData(const char *data, qint64 len, const KSocketAddress *to) override;
175 
176  /**
177  * Improve the readLine performance
178  */
179  qint64 readLineData(char *data, qint64 maxSize) override;
180 
181  /**
182  * Catch connection to clear the buffers
183  */
184  void stateChanging(SocketState newState) override;
185 
186 protected Q_SLOTS:
187  /**
188  * Slot called when there's read activity.
189  */
190  void slotReadActivity() override;
191 
192  /**
193  * Slot called when there's write activity.
194  */
195  void slotWriteActivity() override;
196 
197 #if 0
198  // Already present in QIODevice
199 Q_SIGNALS:
200  /**
201  * This signal is emitted whenever data is written.
202  */
203  void bytesWritten(int bytes);
204 #endif
205 
206 private:
207  KBufferedSocket(const KBufferedSocket &);
208  KBufferedSocket &operator=(const KBufferedSocket &);
209 
210  KBufferedSocketPrivate *const d;
211 };
212 
213 } // namespace KNetwork
214 
215 #endif
Simple stream socket.
A generic socket address.
A namespace to store all networking-related (socket) classes.
Buffered stream sockets.
Low-level socket functionality.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Mar 2 2021 23:56:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.