Qt 4.8
qabstractsocketengine.cpp
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 
43 
44 #ifdef Q_OS_SYMBIAN
45 #include "qsymbiansocketengine_p.h"
46 #else
47 #include "qnativesocketengine_p.h"
48 #endif
49 
50 #include "qmutex.h"
51 #include "qnetworkproxy.h"
52 
54 
55 class QSocketEngineHandlerList : public QList<QSocketEngineHandler*>
56 {
57 public:
59 };
60 
62 
64 {
65  if (!socketHandlers())
66  return;
67  QMutexLocker locker(&socketHandlers()->mutex);
68  socketHandlers()->prepend(this);
69 }
70 
72 {
73  if (!socketHandlers())
74  return;
75  QMutexLocker locker(&socketHandlers()->mutex);
76  socketHandlers()->removeAll(this);
77 }
78 
80  : socketError(QAbstractSocket::UnknownSocketError)
81  , hasSetSocketError(false)
82  , socketErrorString(QLatin1String(QT_TRANSLATE_NOOP(QSocketLayer, "Unknown error")))
83  , socketState(QAbstractSocket::UnconnectedState)
84  , socketType(QAbstractSocket::UnknownSocketType)
85  , socketProtocol(QAbstractSocket::UnknownNetworkLayerProtocol)
86  , localPort(0)
87  , peerPort(0)
88  , receiver(0)
89 {
90 }
91 
93  : QObject(*new QAbstractSocketEnginePrivate(), parent)
94 {
95 }
96 
98  : QObject(dd, parent)
99 {
100 }
101 
103 {
104 #ifndef QT_NO_NETWORKPROXY
105  // proxy type must have been resolved by now
106  if (proxy.type() == QNetworkProxy::DefaultProxy)
107  return 0;
108 #endif
109 
110  QMutexLocker locker(&socketHandlers()->mutex);
111  for (int i = 0; i < socketHandlers()->size(); i++) {
112  if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketType, proxy, parent))
113  return ret;
114  }
115 
116 #ifndef QT_NO_NETWORKPROXY
117  // only NoProxy can have reached here
118  if (proxy.type() != QNetworkProxy::NoProxy)
119  return 0;
120 #endif
121 
122 #ifdef Q_OS_SYMBIAN
123  return new QSymbianSocketEngine(parent);
124 #else
125  return new QNativeSocketEngine(parent);
126 #endif
127 }
128 
130 {
131  QMutexLocker locker(&socketHandlers()->mutex);
132  for (int i = 0; i < socketHandlers()->size(); i++) {
133  if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketDescripter, parent))
134  return ret;
135  }
136 #ifdef Q_OS_SYMBIAN
137  return new QSymbianSocketEngine(parent);
138 #else
139  return new QNativeSocketEngine(parent);
140 #endif
141 }
142 
144 {
145  return d_func()->socketError;
146 }
147 
149 {
150  return d_func()->socketErrorString;
151 }
152 
154 {
155  Q_D(const QAbstractSocketEngine);
156  d->socketError = error;
157  d->socketErrorString = errorString;
158 }
159 
161 {
162  d_func()->receiver = receiver;
163 }
164 
166 {
167  if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
168  receiver->readNotification();
169 }
170 
172 {
173  if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
174  receiver->writeNotification();
175 }
176 
178 {
179  if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
180  receiver->exceptionNotification();
181 }
182 
184 {
185  if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
186  receiver->connectionNotification();
187 }
188 
189 #ifndef QT_NO_NETWORKPROXY
191 {
192  if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
193  receiver->proxyAuthenticationRequired(proxy, authenticator);
194 }
195 #endif
196 
197 
199 {
200  return d_func()->socketState;
201 }
202 
204 {
205  d_func()->socketState = state;
206 }
207 
209 {
210  return d_func()->socketType;
211 }
212 
214 {
215  d_func()->socketType = socketType;
216 }
217 
219 {
220  return d_func()->socketProtocol;
221 }
222 
224 {
225  d_func()->socketProtocol = protocol;
226 }
227 
229 {
230  return d_func()->localAddress;
231 }
232 
234 {
235  d_func()->localAddress = address;
236 }
237 
239 {
240  return d_func()->localPort;
241 }
242 
244 {
245  d_func()->localPort = port;
246 }
247 
249 {
250  return d_func()->peerAddress;
251 }
252 
254 {
255  d_func()->peerAddress = address;
256 }
257 
259 {
260  return d_func()->peerPort;
261 }
262 
264 {
265  d_func()->peerPort = port;
266 }
267 
QNetworkProxy::ProxyType type() const
Returns the proxy type for this instance.
double d
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setProtocol(QAbstractSocket::NetworkLayerProtocol protocol)
SocketType
This enum describes the transport layer protocol.
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
static QAbstractSocketEngine * createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent)
void setPeerAddress(const QHostAddress &address)
#define QT_TRANSLATE_NOOP(scope, x)
Marks the string literal sourceText for dynamic translation in the given context; i...
Definition: qglobal.h:2487
void setState(QAbstractSocket::SocketState state)
void setLocalPort(quint16 port)
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_D(Class)
Definition: qglobal.h:2482
The QNativeSocketEngine class provides low level access to a socket.
SocketState
This enum describes the different states in which a socket can be.
The QNetworkProxy class provides a network layer proxy.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setError(QAbstractSocket::SocketError error, const QString &errorString) const
QHostAddress peerAddress() const
void setPeerPort(quint16 port)
void setLocalAddress(const QHostAddress &address)
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
SocketError
This enum describes the socket errors that can occur.
QAbstractSocketEngine(QObject *parent=0)
unsigned short quint16
Definition: qglobal.h:936
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QHostAddress localAddress() const
void setReceiver(QAbstractSocketEngineReceiver *receiver)
The QAuthenticator class provides an authentication object.
void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
QAbstractSocket::SocketError error() const
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QAbstractSocket::NetworkLayerProtocol protocol() const
void setSocketType(QAbstractSocket::SocketType socketType)
QObject * parent
Definition: qobject.h:92
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
QAbstractSocket::SocketState state() const
QAbstractSocket::SocketType socketType() const
The QAbstractSocket class provides the base functionality common to all socket types.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62