Qt 4.8
qabstractsocket.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QABSTRACTSOCKET_H
43 #define QABSTRACTSOCKET_H
44 
45 #include <QtCore/qiodevice.h>
46 #include <QtCore/qobject.h>
47 #ifndef QT_NO_DEBUG_STREAM
48 #include <QtCore/qdebug.h>
49 #endif
50 
52 
54 
55 QT_MODULE(Network)
56 
57 class QHostAddress;
58 #ifndef QT_NO_NETWORKPROXY
59 class QNetworkProxy;
60 #endif
62 class QAuthenticator;
63 
65 {
66  Q_OBJECT
68 public:
69  enum SocketType {
72  UnknownSocketType = -1
73  };
77  UnknownNetworkLayerProtocol = -1
78  };
79  enum SocketError {
99 
100  UnknownSocketError = -1
101  };
102  enum SocketState {
109  ClosingState
110 #ifdef QT3_SUPPORT
111  ,
112  Idle = UnconnectedState,
113  HostLookup = HostLookupState,
114  Connecting = ConnectingState,
115  Connected = ConnectedState,
116  Closing = ClosingState,
117  Connection = ConnectedState
118 #endif
119  };
120  enum SocketOption {
121  LowDelayOption, // TCP_NODELAY
122  KeepAliveOption, // SO_KEEPALIVE
123  MulticastTtlOption, // IP_MULTICAST_TTL
124  MulticastLoopbackOption // IP_MULTICAST_LOOPBACK
125  };
126 
127  QAbstractSocket(SocketType socketType, QObject *parent);
128  virtual ~QAbstractSocket();
129 
130  // ### Qt 5: Make connectToHost() and disconnectFromHost() virtual.
131  void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite);
132  void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite);
133  void disconnectFromHost();
134 
135  bool isValid() const;
136 
137  qint64 bytesAvailable() const;
138  qint64 bytesToWrite() const;
139 
140  bool canReadLine() const;
141 
142  quint16 localPort() const;
143  QHostAddress localAddress() const;
144  quint16 peerPort() const;
145  QHostAddress peerAddress() const;
146  QString peerName() const;
147 
148  // ### Qt 5: Make setReadBufferSize() virtual
149  qint64 readBufferSize() const;
150  void setReadBufferSize(qint64 size);
151 
152  void abort();
153 
154  // ### Qt 5: Make socketDescriptor() and setSocketDescriptor() virtual.
155  int socketDescriptor() const;
156  bool setSocketDescriptor(int socketDescriptor, SocketState state = ConnectedState,
157  OpenMode openMode = ReadWrite);
158 
159  // ### Qt 5: Make virtual?
160  void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
161  QVariant socketOption(QAbstractSocket::SocketOption option);
162 
163  SocketType socketType() const;
164  SocketState state() const;
165  SocketError error() const;
166 
167  // from QIODevice
168  void close();
169  bool isSequential() const;
170  bool atEnd() const;
171  bool flush();
172 
173  // for synchronous access
174  // ### Qt 5: Make waitForConnected() and waitForDisconnected() virtual.
175  bool waitForConnected(int msecs = 30000);
176  bool waitForReadyRead(int msecs = 30000);
177  bool waitForBytesWritten(int msecs = 30000);
178  bool waitForDisconnected(int msecs = 30000);
179 
180 #ifndef QT_NO_NETWORKPROXY
181  void setProxy(const QNetworkProxy &networkProxy);
182  QNetworkProxy proxy() const;
183 #endif
184 
185 Q_SIGNALS:
186  void hostFound();
187  void connected();
188  void disconnected();
189  void stateChanged(QAbstractSocket::SocketState);
191 #ifndef QT_NO_NETWORKPROXY
192  void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
193 #endif
194 
195 protected Q_SLOTS:
196  void connectToHostImplementation(const QString &hostName, quint16 port, OpenMode mode = ReadWrite);
197  void disconnectFromHostImplementation();
198 
199 protected:
200  qint64 readData(char *data, qint64 maxlen);
201  qint64 readLineData(char *data, qint64 maxlen);
202  qint64 writeData(const char *data, qint64 len);
203 
204  void setSocketState(SocketState state);
205  void setSocketError(SocketError socketError);
206  void setLocalPort(quint16 port);
207  void setLocalAddress(const QHostAddress &address);
208  void setPeerPort(quint16 port);
209  void setPeerAddress(const QHostAddress &address);
210  void setPeerName(const QString &name);
211 
212  QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = 0);
213 
214 private:
217 
218  Q_PRIVATE_SLOT(d_func(), void _q_connectToNextAddress())
219  Q_PRIVATE_SLOT(d_func(), void _q_startConnecting(const QHostInfo &))
220  Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt())
221  Q_PRIVATE_SLOT(d_func(), void _q_testConnection())
222  Q_PRIVATE_SLOT(d_func(), void _q_forceDisconnect())
223 
224 public:
225 #ifdef QT3_SUPPORT
226  enum Error {
227  ErrConnectionRefused = ConnectionRefusedError,
228  ErrHostNotFound = HostNotFoundError,
229  ErrSocketRead = UnknownSocketError
230  };
231  inline QT3_SUPPORT int socket() const { return socketDescriptor(); }
232  inline QT3_SUPPORT void setSocket(int socket) { setSocketDescriptor(socket); }
233  inline QT3_SUPPORT qulonglong waitForMore(int msecs, bool *timeout = 0) const
234  {
235  QAbstractSocket *that = const_cast<QAbstractSocket *>(this);
236  if (that->waitForReadyRead(msecs))
237  return qulonglong(bytesAvailable());
238  if (error() == SocketTimeoutError && timeout)
239  *timeout = true;
240  return 0;
241  }
242  typedef SocketState State;
243 Q_SIGNALS:
244  QT_MOC_COMPAT void connectionClosed(); // same as disconnected()
245  QT_MOC_COMPAT void delayedCloseFinished(); // same as disconnected()
246 
247 
248 #endif
249 };
250 
251 #ifndef QT_NO_DEBUG_STREAM
254 #endif
255 
257 
259 
260 #endif // QABSTRACTSOCKET_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
#define QT_MOC_COMPAT
Definition: qglobal.h:1128
Error
Definition: qaudio.h:58
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
SocketType
This enum describes the transport layer protocol.
bool waitForReadyRead(int msecs=30000)
This function blocks until new data is available for reading and the QIODevice::readyRead() signal ha...
#define QT_MODULE(x)
Definition: qglobal.h:2783
virtual bool waitForReadyRead(int msecs)
Blocks until new data is available for reading and the readyRead() signal has been emitted...
Definition: qiodevice.cpp:1616
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define error(msg)
virtual qint64 writeData(const char *data, qint64 len)=0
Writes up to maxSize bytes from data to the device.
SocketOption
This enum represents the options that can be set on a socket.
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
#define Q_SLOTS
Definition: qobjectdefs.h:71
NetworkLayerProtocol
This enum describes the network layer protocol values used in Qt.
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_SIGNALS
Definition: qobjectdefs.h:72
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
Definition: qiodevice.cpp:711
virtual qint64 readLineData(char *data, qint64 maxlen)
Reads up to maxSize characters into data and returns the number of characters read.
Definition: qiodevice.cpp:1285
#define Q_ENUMS(x)
Definition: qobjectdefs.h:84
SocketState
This enum describes the different states in which a socket can be.
The QNetworkProxy class provides a network layer proxy.
virtual qint64 bytesToWrite() const
For buffered devices, this function returns the number of bytes waiting to be written.
Definition: qiodevice.cpp:767
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static QIntfbScreen * connected
const char * name
SocketError
This enum describes the socket errors that can occur.
unsigned short quint16
Definition: qglobal.h:936
static const char * data(const QByteArray &arr)
The QHostInfo class provides static functions for host name lookups.
Definition: qhostinfo.h:58
__int64 qint64
Definition: qglobal.h:942
#define Q_OBJECT
Definition: qobjectdefs.h:157
The QAuthenticator class provides an authentication object.
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
Definition: qiodevice.cpp:454
virtual qint64 readData(char *data, qint64 maxlen)=0
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an...
State
Definition: qaudio.h:59
#define Q_NETWORK_EXPORT
Definition: qglobal.h:1452
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
Definition: qiodevice.cpp:752
virtual bool waitForBytesWritten(int msecs)
For buffered devices, this function waits until a payload of buffered written data has been written t...
Definition: qiodevice.cpp:1648
Q_CORE_EXPORT QTextStream & flush(QTextStream &s)
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
quint64 qulonglong
Definition: qglobal.h:952
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false...
Definition: qiodevice.cpp:1330
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
#define QT_END_HEADER
Definition: qglobal.h:137
The QAbstractSocket class provides the base functionality common to all socket types.
Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError)