diff options
Diffstat (limited to 'src/network/socket')
-rw-r--r-- | src/network/socket/qabstractsocket.cpp | 12 | ||||
-rw-r--r-- | src/network/socket/qabstractsocket.h | 4 | ||||
-rw-r--r-- | src/network/socket/qlocalserver.h | 4 | ||||
-rw-r--r-- | src/network/socket/qlocalserver_unix.cpp | 4 | ||||
-rw-r--r-- | src/network/socket/qlocalsocket.cpp | 75 | ||||
-rw-r--r-- | src/network/socket/qlocalsocket.h | 7 | ||||
-rw-r--r-- | src/network/socket/qlocalsocket_tcp.cpp | 22 | ||||
-rw-r--r-- | src/network/socket/qlocalsocket_unix.cpp | 15 | ||||
-rw-r--r-- | src/network/socket/qlocalsocket_win.cpp | 18 | ||||
-rw-r--r-- | src/network/socket/qnativesocketengine.cpp | 16 | ||||
-rw-r--r-- | src/network/socket/qnativesocketengine_unix.cpp | 8 | ||||
-rw-r--r-- | src/network/socket/qnativesocketengine_win.cpp | 2 | ||||
-rw-r--r-- | src/network/socket/qtcpserver.h | 4 | ||||
-rw-r--r-- | src/network/socket/qtcpsocket.cpp | 2 | ||||
-rw-r--r-- | src/network/socket/qtcpsocket.h | 4 | ||||
-rw-r--r-- | src/network/socket/qudpsocket.h | 4 |
16 files changed, 118 insertions, 83 deletions
diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index c19238d768..71d6166840 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -643,7 +643,7 @@ bool QAbstractSocketPrivate::initSocketLayer(QAbstractSocket::NetworkLayerProtoc return false; } - if (threadData->eventDispatcher) + if (threadData->hasEventDispatcher()) socketEngine->setReceiver(this); #if defined (QABSTRACTSOCKET_DEBUG) @@ -1134,7 +1134,7 @@ void QAbstractSocketPrivate::_q_connectToNextAddress() } // Start the connect timer. - if (threadData->eventDispatcher) { + if (threadData->hasEventDispatcher()) { if (!connectTimer) { connectTimer = new QTimer(q); QObject::connect(connectTimer, SIGNAL(timeout()), @@ -1159,7 +1159,7 @@ void QAbstractSocketPrivate::_q_connectToNextAddress() void QAbstractSocketPrivate::_q_testConnection() { if (socketEngine) { - if (threadData->eventDispatcher) { + if (threadData->hasEventDispatcher()) { if (connectTimer) connectTimer->stop(); } @@ -1180,7 +1180,7 @@ void QAbstractSocketPrivate::_q_testConnection() addresses.clear(); } - if (threadData->eventDispatcher) { + if (threadData->hasEventDispatcher()) { if (connectTimer) connectTimer->stop(); } @@ -1640,7 +1640,7 @@ void QAbstractSocket::connectToHost(const QString &hostName, quint16 port, return; #endif } else { - if (d->threadData->eventDispatcher) { + if (d->threadData->hasEventDispatcher()) { // this internal API for QHostInfo either immediately gives us the desired // QHostInfo from cache or later calls the _q_startConnecting slot. bool immediateResultValid = false; @@ -1846,7 +1846,7 @@ bool QAbstractSocket::setSocketDescriptor(qintptr socketDescriptor, SocketState return false; } - if (d->threadData->eventDispatcher) + if (d->threadData->hasEventDispatcher()) d->socketEngine->setReceiver(d); QIODevice::open(openMode); diff --git a/src/network/socket/qabstractsocket.h b/src/network/socket/qabstractsocket.h index b7e3f2853a..46114abf73 100644 --- a/src/network/socket/qabstractsocket.h +++ b/src/network/socket/qabstractsocket.h @@ -48,8 +48,6 @@ #include <QtCore/qdebug.h> #endif -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -242,6 +240,4 @@ QT_END_NAMESPACE Q_DECLARE_METATYPE(QAbstractSocket::SocketState) Q_DECLARE_METATYPE(QAbstractSocket::SocketError) -QT_END_HEADER - #endif // QABSTRACTSOCKET_H diff --git a/src/network/socket/qlocalserver.h b/src/network/socket/qlocalserver.h index d7d6b16e10..b5791db653 100644 --- a/src/network/socket/qlocalserver.h +++ b/src/network/socket/qlocalserver.h @@ -44,8 +44,6 @@ #include <QtNetwork/qabstractsocket.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -109,7 +107,5 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(QLocalServer::SocketOptions) QT_END_NAMESPACE -QT_END_HEADER - #endif // QLOCALSERVER_H diff --git a/src/network/socket/qlocalserver_unix.cpp b/src/network/socket/qlocalserver_unix.cpp index 2bcf1ac83e..51a33a4b35 100644 --- a/src/network/socket/qlocalserver_unix.cpp +++ b/src/network/socket/qlocalserver_unix.cpp @@ -297,9 +297,9 @@ void QLocalServerPrivate::waitForNewConnection(int msec, bool *timedOut) FD_ZERO(&readfds); FD_SET(listenSocket, &readfds); - timeval timeout; + struct timespec timeout; timeout.tv_sec = msec / 1000; - timeout.tv_usec = (msec % 1000) * 1000; + timeout.tv_nsec = (msec % 1000) * 1000 * 1000; int result = -1; result = qt_safe_select(listenSocket + 1, &readfds, 0, 0, (msec == -1) ? 0 : &timeout); diff --git a/src/network/socket/qlocalsocket.cpp b/src/network/socket/qlocalsocket.cpp index bd2c4258b5..1ce6568364 100644 --- a/src/network/socket/qlocalsocket.cpp +++ b/src/network/socket/qlocalsocket.cpp @@ -71,18 +71,17 @@ QT_BEGIN_NAMESPACE */ /*! - \fn void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) + \fn void QLocalSocket::open(OpenMode openMode) - Attempts to make a connection to \a name. + Equivalent to connectToServer(OpenMode mode). + The socket is opened in the given \a openMode to the server defined by setServerName(). - The socket is opened in the given \a openMode and first enters ConnectingState. - It then attempts to connect to the address or addresses returned by the lookup. - Finally, if a connection is established, QLocalSocket enters ConnectedState - and emits connected(). - - At any point, the socket can emit error() to signal that an error occurred. + Note that unlike in most other QIODevice subclasses, open() may not open the device directly. + The function return false if the socket was already connected or if the server to connect + to was not defined and true in any other case. The connected() or error() signals will be + emitted once the device is actualy open (or the connection failed). - See also state(), serverName(), and waitForConnected(). + See connectToServer() for more details. */ /*! @@ -354,8 +353,62 @@ QLocalSocket::~QLocalSocket() } /*! - Returns the name of the peer as specified by connectToServer(), or an - empty QString if connectToServer() has not been called or it failed. + \since 5.1 + + Attempts to make a connection to serverName(). + setServerName() must be called before you open the connection. + Alternatively you can use connectToServer(const QString &name, OpenMode openMode); + + The socket is opened in the given \a openMode and first enters ConnectingState. + If a connection is established, QLocalSocket enters ConnectedState and emits connected(). + + After calling this function, the socket can emit error() to signal that an error occurred. + + \sa state(), serverName(), waitForConnected() +*/ +void QLocalSocket::connectToServer(OpenMode openMode) +{ + open(openMode); +} + +/*! \overload + + Set the server \a name and attempts to make a connection to it. + + The socket is opened in the given \a openMode and first enters ConnectingState. + If a connection is established, QLocalSocket enters ConnectedState and emits connected(). + + After calling this function, the socket can emit error() to signal that an error occurred. + + \sa state(), serverName(), waitForConnected() +*/ +void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) +{ + setServerName(name); + open(openMode); +} + +/*! + \since 5.1 + + Set the \a name of the peer to connect to. + On Windows name is the name of a named pipe; on Unix name is the name of a local domain socket. + + This function must be called when the socket is not connected. +*/ +void QLocalSocket::setServerName(const QString & name) +{ + Q_D(QLocalSocket); + if (d->state != UnconnectedState) { + qWarning("QLocalSocket::setServerName() called while not in unconnected state"); + return; + } + d->serverName = name; +} + +/*! + Returns the name of the peer as specified by setServerName(), or an + empty QString if setServerName() has not been called or connectToServer() failed. \sa connectToServer(), fullServerName() diff --git a/src/network/socket/qlocalsocket.h b/src/network/socket/qlocalsocket.h index e1ee174608..427dab1def 100644 --- a/src/network/socket/qlocalsocket.h +++ b/src/network/socket/qlocalsocket.h @@ -45,8 +45,6 @@ #include <QtCore/qiodevice.h> #include <QtNetwork/qabstractsocket.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -86,9 +84,11 @@ public: QLocalSocket(QObject *parent = 0); ~QLocalSocket(); + void connectToServer(OpenMode openMode = ReadWrite); void connectToServer(const QString &name, OpenMode openMode = ReadWrite); void disconnectFromServer(); + void setServerName(const QString &name); QString serverName() const; QString fullServerName() const; @@ -97,6 +97,7 @@ public: virtual qint64 bytesAvailable() const; virtual qint64 bytesToWrite() const; virtual bool canReadLine() const; + virtual bool open(OpenMode openMode = ReadWrite) Q_DECL_OVERRIDE; virtual void close(); LocalSocketError error() const; bool flush(); @@ -152,6 +153,4 @@ Q_NETWORK_EXPORT QDebug operator<<(QDebug, QLocalSocket::LocalSocketState); QT_END_NAMESPACE -QT_END_HEADER - #endif // QLOCALSOCKET_H diff --git a/src/network/socket/qlocalsocket_tcp.cpp b/src/network/socket/qlocalsocket_tcp.cpp index cbae8d58ff..edde5a4687 100644 --- a/src/network/socket/qlocalsocket_tcp.cpp +++ b/src/network/socket/qlocalsocket_tcp.cpp @@ -214,29 +214,30 @@ void QLocalSocketPrivate::errorOccurred(QLocalSocket::LocalSocketError error, co q->emit stateChanged(state); } -void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) +bool QLocalSocket::open(OpenMode openMode) { Q_D(QLocalSocket); - if (state() == ConnectedState - || state() == ConnectingState) - return; + if (state() == ConnectedState || state() == ConnectingState) { + setErrorString(tr("Trying to connect while connection is in progress")); + emit error(QLocalSocket::OperationError); + return false; + } d->errorString.clear(); d->state = ConnectingState; emit stateChanged(d->state); - if (name.isEmpty()) { + if (d->serverName.isEmpty()) { d->errorOccurred(ServerNotFoundError, QLatin1String("QLocalSocket::connectToServer")); - return; + return false; } - d->serverName = name; const QLatin1String prefix("QLocalServer/"); if (name.startsWith(prefix)) - d->fullServerName = name; + d->fullServerName = d->serverName; else - d->fullServerName = prefix + name; + d->fullServerName = prefix + d->serverName; QSettings settings(QLatin1String("QtProject"), QLatin1String("Qt")); bool ok; @@ -244,10 +245,11 @@ void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) if (!ok) { d->errorOccurred(ServerNotFoundError, QLatin1String("QLocalSocket::connectToServer")); - return; + return false; } d->tcpSocket->connectToHost(QHostAddress::LocalHost, port, openMode); QIODevice::open(openMode); + return true; } bool QLocalSocket::setSocketDescriptor(qintptr socketDescriptor, diff --git a/src/network/socket/qlocalsocket_unix.cpp b/src/network/socket/qlocalsocket_unix.cpp index e846e43e73..67182e57b0 100644 --- a/src/network/socket/qlocalsocket_unix.cpp +++ b/src/network/socket/qlocalsocket_unix.cpp @@ -221,14 +221,14 @@ void QLocalSocketPrivate::errorOccurred(QLocalSocket::LocalSocketError error, co q->emit stateChanged(state); } -void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) +bool QLocalSocket::open(OpenMode openMode) { Q_D(QLocalSocket); if (state() == ConnectedState || state() == ConnectingState) { QString errorString = d->generateErrorString(QLocalSocket::OperationError, QLatin1String("QLocalSocket::connectToserver")); setErrorString(errorString); emit error(QLocalSocket::OperationError); - return; + return false; } d->errorString.clear(); @@ -236,17 +236,17 @@ void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) d->state = ConnectingState; emit stateChanged(d->state); - if (name.isEmpty()) { + if (d->serverName.isEmpty()) { d->errorOccurred(ServerNotFoundError, QLatin1String("QLocalSocket::connectToServer")); - return; + return false; } // create the socket if (-1 == (d->connectingSocket = qt_safe_socket(PF_UNIX, SOCK_STREAM, 0))) { d->errorOccurred(UnsupportedSocketOperationError, QLatin1String("QLocalSocket::connectToServer")); - return; + return false; } // set non blocking so we can try to connect and it won't wait int flags = fcntl(d->connectingSocket, F_GETFL, 0); @@ -254,13 +254,14 @@ void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) || -1 == (fcntl(d->connectingSocket, F_SETFL, flags | O_NONBLOCK))) { d->errorOccurred(UnknownSocketError, QLatin1String("QLocalSocket::connectToServer")); - return; + return false; } // _q_connectToSocket does the actual connecting - d->connectingName = name; + d->connectingName = d->serverName; d->connectingOpenMode = openMode; d->_q_connectToSocket(); + return true; } /*! diff --git a/src/network/socket/qlocalsocket_win.cpp b/src/network/socket/qlocalsocket_win.cpp index 660e0901f8..cdfa18377d 100644 --- a/src/network/socket/qlocalsocket_win.cpp +++ b/src/network/socket/qlocalsocket_win.cpp @@ -128,33 +128,33 @@ void QLocalSocketPrivate::destroyPipeHandles() } } -void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) +bool QLocalSocket::open(OpenMode openMode) { Q_D(QLocalSocket); if (state() == ConnectedState || state() == ConnectingState) { setErrorString(tr("Trying to connect while connection is in progress")); emit error(QLocalSocket::OperationError); - return; + return false; } d->error = QLocalSocket::UnknownSocketError; d->errorString = QString(); d->state = ConnectingState; emit stateChanged(d->state); - if (name.isEmpty()) { + if (d->serverName.isEmpty()) { d->error = QLocalSocket::ServerNotFoundError; setErrorString(QLocalSocket::tr("%1: Invalid name").arg(QLatin1String("QLocalSocket::connectToServer"))); d->state = UnconnectedState; emit error(d->error); emit stateChanged(d->state); - return; + return false; } QString pipePath = QLatin1String("\\\\.\\pipe\\"); - if (name.startsWith(pipePath)) - d->fullServerName = name; + if (d->serverName.startsWith(pipePath)) + d->fullServerName = d->serverName; else - d->fullServerName = pipePath + name; + d->fullServerName = pipePath + d->serverName; // Try to open a named pipe HANDLE localSocket; forever { @@ -184,15 +184,15 @@ void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) if (localSocket == INVALID_HANDLE_VALUE) { d->setErrorString(QLatin1String("QLocalSocket::connectToServer")); d->fullServerName = QString(); - return; + return false; } // we have a valid handle - d->serverName = name; if (setSocketDescriptor((qintptr)localSocket, ConnectedState, openMode)) { d->handle = localSocket; emit connected(); } + return true; } // This is reading from the buffer diff --git a/src/network/socket/qnativesocketengine.cpp b/src/network/socket/qnativesocketengine.cpp index 6dbc25db4c..536bc63ed0 100644 --- a/src/network/socket/qnativesocketengine.cpp +++ b/src/network/socket/qnativesocketengine.cpp @@ -623,8 +623,8 @@ int QNativeSocketEngine::accept() { Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::accept(), -1); - Q_CHECK_STATE(QNativeSocketEngine::accept(), QAbstractSocket::ListeningState, false); - Q_CHECK_TYPE(QNativeSocketEngine::accept(), QAbstractSocket::TcpSocket, false); + Q_CHECK_STATE(QNativeSocketEngine::accept(), QAbstractSocket::ListeningState, -1); + Q_CHECK_TYPE(QNativeSocketEngine::accept(), QAbstractSocket::TcpSocket, -1); return d->nativeAccept(); } @@ -702,7 +702,7 @@ qint64 QNativeSocketEngine::bytesAvailable() const { Q_D(const QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::bytesAvailable(), -1); - Q_CHECK_NOT_STATE(QNativeSocketEngine::bytesAvailable(), QAbstractSocket::UnconnectedState, false); + Q_CHECK_NOT_STATE(QNativeSocketEngine::bytesAvailable(), QAbstractSocket::UnconnectedState, -1); return d->nativeBytesAvailable(); } @@ -732,7 +732,7 @@ qint64 QNativeSocketEngine::pendingDatagramSize() const { Q_D(const QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::pendingDatagramSize(), -1); - Q_CHECK_TYPE(QNativeSocketEngine::pendingDatagramSize(), QAbstractSocket::UdpSocket, false); + Q_CHECK_TYPE(QNativeSocketEngine::pendingDatagramSize(), QAbstractSocket::UdpSocket, -1); return d->nativePendingDatagramSize(); } @@ -757,7 +757,7 @@ qint64 QNativeSocketEngine::readDatagram(char *data, qint64 maxSize, QHostAddres { Q_D(QNativeSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QNativeSocketEngine::readDatagram(), -1); - Q_CHECK_TYPE(QNativeSocketEngine::readDatagram(), QAbstractSocket::UdpSocket, false); + Q_CHECK_TYPE(QNativeSocketEngine::readDatagram(), QAbstractSocket::UdpSocket, -1); return d->nativeReceiveDatagram(data, maxSize, address, port); } @@ -1214,7 +1214,7 @@ void QNativeSocketEngine::setReadNotificationEnabled(bool enable) Q_D(QNativeSocketEngine); if (d->readNotifier) { d->readNotifier->setEnabled(enable); - } else if (enable && d->threadData->eventDispatcher) { + } else if (enable && d->threadData->hasEventDispatcher()) { d->readNotifier = new QReadNotifier(d->socketDescriptor, this); d->readNotifier->setEnabled(true); } @@ -1231,7 +1231,7 @@ void QNativeSocketEngine::setWriteNotificationEnabled(bool enable) Q_D(QNativeSocketEngine); if (d->writeNotifier) { d->writeNotifier->setEnabled(enable); - } else if (enable && d->threadData->eventDispatcher) { + } else if (enable && d->threadData->hasEventDispatcher()) { d->writeNotifier = new QWriteNotifier(d->socketDescriptor, this); d->writeNotifier->setEnabled(true); } @@ -1248,7 +1248,7 @@ void QNativeSocketEngine::setExceptionNotificationEnabled(bool enable) Q_D(QNativeSocketEngine); if (d->exceptNotifier) { d->exceptNotifier->setEnabled(enable); - } else if (enable && d->threadData->eventDispatcher) { + } else if (enable && d->threadData->hasEventDispatcher()) { d->exceptNotifier = new QExceptionNotifier(d->socketDescriptor, this); d->exceptNotifier->setEnabled(true); } diff --git a/src/network/socket/qnativesocketengine_unix.cpp b/src/network/socket/qnativesocketengine_unix.cpp index 4f3408b067..4c94c4dbb9 100644 --- a/src/network/socket/qnativesocketengine_unix.cpp +++ b/src/network/socket/qnativesocketengine_unix.cpp @@ -1126,9 +1126,9 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) co FD_ZERO(&fds); FD_SET(socketDescriptor, &fds); - struct timeval tv; + struct timespec tv; tv.tv_sec = timeout / 1000; - tv.tv_usec = (timeout % 1000) * 1000; + tv.tv_nsec = (timeout % 1000) * 1000 * 1000; int retval; if (selectForRead) @@ -1152,9 +1152,9 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool c if (checkWrite) FD_SET(socketDescriptor, &fdwrite); - struct timeval tv; + struct timespec tv; tv.tv_sec = timeout / 1000; - tv.tv_usec = (timeout % 1000) * 1000; + tv.tv_nsec = (timeout % 1000) * 1000 * 1000; int ret; ret = qt_safe_select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv); diff --git a/src/network/socket/qnativesocketengine_win.cpp b/src/network/socket/qnativesocketengine_win.cpp index a135abd5e3..3027b77161 100644 --- a/src/network/socket/qnativesocketengine_win.cpp +++ b/src/network/socket/qnativesocketengine_win.cpp @@ -1450,7 +1450,7 @@ int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) co bool readEnabled = selectForRead && readNotifier && readNotifier->isEnabled(); if (readEnabled) readNotifier->setEnabled(false); - + fd_set fds; int ret = 0; diff --git a/src/network/socket/qtcpserver.h b/src/network/socket/qtcpserver.h index dfabfa1be7..8e25aa5a29 100644 --- a/src/network/socket/qtcpserver.h +++ b/src/network/socket/qtcpserver.h @@ -46,8 +46,6 @@ #include <QtNetwork/qabstractsocket.h> #include <QtNetwork/qhostaddress.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -108,6 +106,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QTCPSERVER_H diff --git a/src/network/socket/qtcpsocket.cpp b/src/network/socket/qtcpsocket.cpp index fb5d46a9f7..ce37401a89 100644 --- a/src/network/socket/qtcpsocket.cpp +++ b/src/network/socket/qtcpsocket.cpp @@ -41,7 +41,7 @@ //#define QTCPSOCKET_DEBUG -/*! +/*! \class QTcpSocket \brief The QTcpSocket class provides a TCP socket. diff --git a/src/network/socket/qtcpsocket.h b/src/network/socket/qtcpsocket.h index 44fb68a207..ed5ce4aeed 100644 --- a/src/network/socket/qtcpsocket.h +++ b/src/network/socket/qtcpsocket.h @@ -45,8 +45,6 @@ #include <QtNetwork/qabstractsocket.h> #include <QtCore/qvariant.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -69,6 +67,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QTCPSOCKET_H diff --git a/src/network/socket/qudpsocket.h b/src/network/socket/qudpsocket.h index 7a064a3432..da6306a430 100644 --- a/src/network/socket/qudpsocket.h +++ b/src/network/socket/qudpsocket.h @@ -45,8 +45,6 @@ #include <QtNetwork/qabstractsocket.h> #include <QtNetwork/qhostaddress.h> -QT_BEGIN_HEADER - QT_BEGIN_NAMESPACE @@ -90,6 +88,4 @@ private: QT_END_NAMESPACE -QT_END_HEADER - #endif // QUDPSOCKET_H |