src/network/socket/qabstractsocketengine.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qabstractsocketengine_p.h"
       
    43 #include "qnativesocketengine_p.h"
       
    44 #include "qmutex.h"
       
    45 #include "qnetworkproxy.h"
       
    46 
       
    47 QT_BEGIN_NAMESPACE
       
    48 
       
    49 class QSocketEngineHandlerList : public QList<QSocketEngineHandler*>
       
    50 {
       
    51 public:
       
    52     QMutex mutex;
       
    53 };
       
    54 
       
    55 Q_GLOBAL_STATIC(QSocketEngineHandlerList, socketHandlers)
       
    56 
       
    57 QSocketEngineHandler::QSocketEngineHandler()
       
    58 {
       
    59     if (!socketHandlers())
       
    60         return;
       
    61     QMutexLocker locker(&socketHandlers()->mutex);
       
    62     socketHandlers()->prepend(this);
       
    63 }
       
    64 
       
    65 QSocketEngineHandler::~QSocketEngineHandler()
       
    66 {
       
    67     if (!socketHandlers())
       
    68         return;
       
    69     QMutexLocker locker(&socketHandlers()->mutex);
       
    70     socketHandlers()->removeAll(this);
       
    71 }
       
    72 
       
    73 QAbstractSocketEnginePrivate::QAbstractSocketEnginePrivate()
       
    74     : socketError(QAbstractSocket::UnknownSocketError)
       
    75     , hasSetSocketError(false)
       
    76     , socketErrorString(QLatin1String(QT_TRANSLATE_NOOP(QSocketLayer, "Unknown error")))
       
    77     , socketState(QAbstractSocket::UnconnectedState)
       
    78     , socketType(QAbstractSocket::UnknownSocketType)
       
    79     , socketProtocol(QAbstractSocket::UnknownNetworkLayerProtocol)
       
    80     , localPort(0)
       
    81     , peerPort(0)
       
    82     , receiver(0)
       
    83 {
       
    84 }
       
    85 
       
    86 QAbstractSocketEngine::QAbstractSocketEngine(QObject *parent)
       
    87     : QObject(*new QAbstractSocketEnginePrivate(), parent)
       
    88 {
       
    89 }
       
    90 
       
    91 QAbstractSocketEngine::QAbstractSocketEngine(QAbstractSocketEnginePrivate &dd, QObject* parent)
       
    92     : QObject(dd, parent)
       
    93 {
       
    94 }
       
    95 
       
    96 QAbstractSocketEngine *QAbstractSocketEngine::createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &proxy, QObject *parent)
       
    97 {
       
    98 #ifndef QT_NO_NETWORKPROXY
       
    99     // proxy type must have been resolved by now
       
   100     if (proxy.type() == QNetworkProxy::DefaultProxy)
       
   101         return 0;
       
   102 #endif
       
   103 
       
   104     QMutexLocker locker(&socketHandlers()->mutex);
       
   105     for (int i = 0; i < socketHandlers()->size(); i++) {
       
   106         if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketType, proxy, parent))
       
   107             return ret;
       
   108     }
       
   109 
       
   110 #ifndef QT_NO_NETWORKPROXY
       
   111     // only NoProxy can have reached here
       
   112     if (proxy.type() != QNetworkProxy::NoProxy)
       
   113         return 0;
       
   114 #endif
       
   115 
       
   116     return new QNativeSocketEngine(parent);
       
   117 }
       
   118 
       
   119 QAbstractSocketEngine *QAbstractSocketEngine::createSocketEngine(int socketDescripter, QObject *parent)
       
   120 {
       
   121     QMutexLocker locker(&socketHandlers()->mutex);
       
   122     for (int i = 0; i < socketHandlers()->size(); i++) {
       
   123         if (QAbstractSocketEngine *ret = socketHandlers()->at(i)->createSocketEngine(socketDescripter, parent))
       
   124             return ret;
       
   125     }
       
   126     return new QNativeSocketEngine(parent);
       
   127 }
       
   128 
       
   129 QAbstractSocket::SocketError QAbstractSocketEngine::error() const
       
   130 {
       
   131     return d_func()->socketError;
       
   132 }
       
   133 
       
   134 QString QAbstractSocketEngine::errorString() const
       
   135 {
       
   136     return d_func()->socketErrorString;
       
   137 }
       
   138 
       
   139 void QAbstractSocketEngine::setError(QAbstractSocket::SocketError error, const QString &errorString) const
       
   140 {
       
   141     Q_D(const QAbstractSocketEngine);
       
   142     d->socketError = error;
       
   143     d->socketErrorString = errorString;
       
   144 }
       
   145 
       
   146 void QAbstractSocketEngine::setReceiver(QAbstractSocketEngineReceiver *receiver)
       
   147 {
       
   148     d_func()->receiver = receiver;
       
   149 }
       
   150 
       
   151 void QAbstractSocketEngine::readNotification()
       
   152 {
       
   153     if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
       
   154         receiver->readNotification();
       
   155 }
       
   156 
       
   157 void QAbstractSocketEngine::writeNotification()
       
   158 {
       
   159     if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
       
   160         receiver->writeNotification();
       
   161 }
       
   162 
       
   163 void QAbstractSocketEngine::exceptionNotification()
       
   164 {
       
   165     if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
       
   166         receiver->exceptionNotification();
       
   167 }
       
   168 
       
   169 void QAbstractSocketEngine::connectionNotification()
       
   170 {
       
   171     if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
       
   172         receiver->connectionNotification();
       
   173 }
       
   174 
       
   175 #ifndef QT_NO_NETWORKPROXY
       
   176 void QAbstractSocketEngine::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
       
   177 {
       
   178     if (QAbstractSocketEngineReceiver *receiver = d_func()->receiver)
       
   179         receiver->proxyAuthenticationRequired(proxy, authenticator);
       
   180 }
       
   181 #endif
       
   182 
       
   183 
       
   184 QAbstractSocket::SocketState QAbstractSocketEngine::state() const
       
   185 {
       
   186     return d_func()->socketState;
       
   187 }
       
   188 
       
   189 void QAbstractSocketEngine::setState(QAbstractSocket::SocketState state)
       
   190 {
       
   191     d_func()->socketState = state;
       
   192 }
       
   193 
       
   194 QAbstractSocket::SocketType QAbstractSocketEngine::socketType() const
       
   195 {
       
   196     return d_func()->socketType;
       
   197 }
       
   198 
       
   199 void QAbstractSocketEngine::setSocketType(QAbstractSocket::SocketType socketType)
       
   200 {
       
   201     d_func()->socketType = socketType;
       
   202 }
       
   203 
       
   204 QAbstractSocket::NetworkLayerProtocol QAbstractSocketEngine::protocol() const
       
   205 {
       
   206     return d_func()->socketProtocol;
       
   207 }
       
   208 
       
   209 void QAbstractSocketEngine::setProtocol(QAbstractSocket::NetworkLayerProtocol protocol)
       
   210 {
       
   211     d_func()->socketProtocol = protocol;
       
   212 }
       
   213 
       
   214 QHostAddress QAbstractSocketEngine::localAddress() const
       
   215 {
       
   216     return d_func()->localAddress;
       
   217 }
       
   218 
       
   219 void QAbstractSocketEngine::setLocalAddress(const QHostAddress &address)
       
   220 {
       
   221     d_func()->localAddress = address;
       
   222 }
       
   223 
       
   224 quint16 QAbstractSocketEngine::localPort() const
       
   225 {
       
   226     return d_func()->localPort;
       
   227 }
       
   228 
       
   229 void QAbstractSocketEngine::setLocalPort(quint16 port)
       
   230 {
       
   231     d_func()->localPort = port;
       
   232 }
       
   233 
       
   234 QHostAddress QAbstractSocketEngine::peerAddress() const
       
   235 {
       
   236     return d_func()->peerAddress;
       
   237 }
       
   238 
       
   239 void QAbstractSocketEngine::setPeerAddress(const QHostAddress &address)
       
   240 {
       
   241    d_func()->peerAddress = address;
       
   242 }
       
   243 
       
   244 quint16 QAbstractSocketEngine::peerPort() const
       
   245 {
       
   246     return d_func()->peerPort;
       
   247 }
       
   248 
       
   249 void QAbstractSocketEngine::setPeerPort(quint16 port)
       
   250 {
       
   251     d_func()->peerPort = port;
       
   252 }
       
   253 
       
   254 QT_END_NAMESPACE