/src/network/socket/qabstractsocketengine.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 268 lines · 185 code · 41 blank · 42 comment · 15 complexity · 5208d11f8287a110fd044bf838b76943 MD5 · raw file

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