diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2014-02-01 14:18:36 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-03-21 12:59:25 +0100 |
commit | 4d948685996df0e5e6c9242c12276d4d584bc974 (patch) | |
tree | f23f9fcbc8836b7cfd97a2579b0805454c578d16 /src | |
parent | 2e6607507719a396c941a13e2429647901f60917 (diff) | |
download | qtwebchannel-4d948685996df0e5e6c9242c12276d4d584bc974.tar.gz |
Merge QWebChannelSocket and QWebSocketTransport.
The former was just the private implementation of the latter. This way,
the code structure is more understandable to newcomers and follows
existing best-practices.
Change-Id: I07cf64370553f4c2de349b5f01e90b31112fee58
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/webchannel/qwebchannelsocket.cpp | 150 | ||||
-rw-r--r-- | src/webchannel/qwebsockettransport.cpp | 115 | ||||
-rw-r--r-- | src/webchannel/qwebsockettransport.h | 4 | ||||
-rw-r--r-- | src/webchannel/qwebsockettransport_p.h (renamed from src/webchannel/qwebchannelsocket_p.h) | 6 | ||||
-rw-r--r-- | src/webchannel/webchannel.pro | 3 |
5 files changed, 116 insertions, 162 deletions
diff --git a/src/webchannel/qwebchannelsocket.cpp b/src/webchannel/qwebchannelsocket.cpp deleted file mode 100644 index e6a58c8..0000000 --- a/src/webchannel/qwebchannelsocket.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebChannel module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qwebchannelsocket_p.h" - -#include <QUuid> -#include <QDebug> - -#include <QtWebSockets/QWebSocket> - -QT_BEGIN_NAMESPACE - -QWebChannelSocket::QWebChannelSocket(QObject *parent) - : QWebSocketServer(QStringLiteral("QWebChannel Server"), NonSecureMode, parent) - , m_messageHandler(Q_NULLPTR) - , m_useSecret(true) - , m_starting(false) -{ - connect(this, SIGNAL(acceptError(QAbstractSocket::SocketError)), - SLOT(socketError())); - connect(this, SIGNAL(newConnection()), - SLOT(validateNewConnection())); -} - -QWebChannelSocket::~QWebChannelSocket() -{ - close(); - qDeleteAll(m_clients); -} - -void QWebChannelSocket::initLater() -{ - if (m_starting) - return; - metaObject()->invokeMethod(this, "init", Qt::QueuedConnection); - m_starting = true; -} - -void QWebChannelSocket::sendMessage(const QString &message) -{ - foreach (QWebSocket *client, m_clients) { - client->sendTextMessage(message); - } -} - -void QWebChannelSocket::validateNewConnection() -{ - QWebSocket *client = nextPendingConnection(); - // FIXME: client->protocol() != QStringLiteral("QWebChannel") - // protocols are not supported in QtWebSockets yet... - if (m_useSecret && client->requestUrl().path() != m_secret) - { - client->close(QWebSocketProtocol::CloseCodeBadOperation); - client->deleteLater(); - } else { - connect(client, SIGNAL(textMessageReceived(QString)), - SLOT(messageReceived(QString))); - connect(client, SIGNAL(disconnected()), - SLOT(clientDisconnected())); - m_clients << client; - } -} - -void QWebChannelSocket::init() -{ - close(); - - m_starting = false; - if (m_useSecret) { - m_secret = QUuid::createUuid().toString(); - // replace { by / - m_secret[0] = QLatin1Char('/'); - // chop of trailing } - m_secret.chop(1); - } - - if (!listen(QHostAddress::LocalHost)) { - emit failed(errorString()); - return; - } - - m_baseUrl = QStringLiteral("127.0.0.1:%1%2").arg(serverPort()).arg(m_secret); - emit initialized(); - emit baseUrlChanged(m_baseUrl); -} - -void QWebChannelSocket::socketError() -{ - emit failed(errorString()); -} - -void QWebChannelSocket::messageReceived(const QString &message) -{ - if (m_messageHandler) { - m_messageHandler->handleMessage(message); - } - emit textDataReceived(message); -} - -void QWebChannelSocket::clientDisconnected() -{ - QWebSocket *client = qobject_cast<QWebSocket*>(sender()); - if (!client) { - return; - } - const int idx = m_clients.indexOf(client); - Q_ASSERT(idx != -1); - m_clients.remove(idx); - client->deleteLater(); -} - -QT_END_NAMESPACE diff --git a/src/webchannel/qwebsockettransport.cpp b/src/webchannel/qwebsockettransport.cpp index 9c9ef97..9a432fe 100644 --- a/src/webchannel/qwebsockettransport.cpp +++ b/src/webchannel/qwebsockettransport.cpp @@ -40,13 +40,120 @@ ****************************************************************************/ #include "qwebsockettransport.h" -#include "qwebchannelsocket_p.h" +#include "qwebsockettransport_p.h" -QT_BEGIN_NAMESPACE +#include <QUuid> + +#include <QtWebSockets/QWebSocket> + +QT_USE_NAMESPACE + +//BEGIN QWebSocketTransportPrivate + +QWebSocketTransportPrivate::QWebSocketTransportPrivate(QObject *parent) + : QWebSocketServer(QStringLiteral("QWebChannel Server"), NonSecureMode, parent) + , m_messageHandler(Q_NULLPTR) + , m_useSecret(true) + , m_starting(false) +{ + connect(this, SIGNAL(acceptError(QAbstractSocket::SocketError)), + SLOT(socketError())); + connect(this, SIGNAL(newConnection()), + SLOT(validateNewConnection())); +} + +QWebSocketTransportPrivate::~QWebSocketTransportPrivate() +{ + close(); + qDeleteAll(m_clients); +} + +void QWebSocketTransportPrivate::initLater() +{ + if (m_starting) + return; + metaObject()->invokeMethod(this, "init", Qt::QueuedConnection); + m_starting = true; +} + +void QWebSocketTransportPrivate::sendMessage(const QString &message) +{ + foreach (QWebSocket *client, m_clients) { + client->sendTextMessage(message); + } +} + +void QWebSocketTransportPrivate::validateNewConnection() +{ + QWebSocket *client = nextPendingConnection(); + // FIXME: client->protocol() != QStringLiteral("QWebChannel") + // protocols are not supported in QtWebSockets yet... + if (m_useSecret && client->requestUrl().path() != m_secret) + { + client->close(QWebSocketProtocol::CloseCodeBadOperation); + client->deleteLater(); + } else { + connect(client, SIGNAL(textMessageReceived(QString)), + SLOT(messageReceived(QString))); + connect(client, SIGNAL(disconnected()), + SLOT(clientDisconnected())); + m_clients << client; + } +} + +void QWebSocketTransportPrivate::init() +{ + close(); + + m_starting = false; + if (m_useSecret) { + m_secret = QUuid::createUuid().toString(); + // replace { by / + m_secret[0] = QLatin1Char('/'); + // chop of trailing } + m_secret.chop(1); + } + + if (!listen(QHostAddress::LocalHost)) { + emit failed(errorString()); + return; + } + + m_baseUrl = QStringLiteral("127.0.0.1:%1%2").arg(serverPort()).arg(m_secret); + emit initialized(); + emit baseUrlChanged(m_baseUrl); +} + +void QWebSocketTransportPrivate::socketError() +{ + emit failed(errorString()); +} + +void QWebSocketTransportPrivate::messageReceived(const QString &message) +{ + if (m_messageHandler) { + m_messageHandler->handleMessage(message); + } + emit textDataReceived(message); +} + +void QWebSocketTransportPrivate::clientDisconnected() +{ + QWebSocket *client = qobject_cast<QWebSocket*>(sender()); + if (!client) { + return; + } + const int idx = m_clients.indexOf(client); + Q_ASSERT(idx != -1); + m_clients.remove(idx); + client->deleteLater(); +} + +//END QWebSocketTransportPrivate QWebSocketTransport::QWebSocketTransport(QObject *parent) : QObject(parent) - , d(new QWebChannelSocket) + , d(new QWebSocketTransportPrivate) { connect(d.data(), SIGNAL(textDataReceived(QString)), SIGNAL(messageReceived(QString))); @@ -97,5 +204,3 @@ bool QWebSocketTransport::useSecret() const { return d->m_useSecret; } - -QT_END_NAMESPACE diff --git a/src/webchannel/qwebsockettransport.h b/src/webchannel/qwebsockettransport.h index d8d164a..6b1cb13 100644 --- a/src/webchannel/qwebsockettransport.h +++ b/src/webchannel/qwebsockettransport.h @@ -46,7 +46,7 @@ QT_BEGIN_NAMESPACE -class QWebChannelSocket; +class QWebSocketTransportPrivate; class Q_WEBCHANNEL_EXPORT QWebSocketTransport : public QObject, public QWebChannelTransportInterface { Q_OBJECT @@ -73,7 +73,7 @@ Q_SIGNALS: void messageReceived(const QString &message); private: - QScopedPointer<QWebChannelSocket> d; + QScopedPointer<QWebSocketTransportPrivate> d; }; QT_END_NAMESPACE diff --git a/src/webchannel/qwebchannelsocket_p.h b/src/webchannel/qwebsockettransport_p.h index 74f5c48..1a95787 100644 --- a/src/webchannel/qwebchannelsocket_p.h +++ b/src/webchannel/qwebsockettransport_p.h @@ -48,7 +48,7 @@ QT_BEGIN_NAMESPACE -class QWebChannelSocket : public QWebSocketServer +class QWebSocketTransportPrivate : public QWebSocketServer { Q_OBJECT public: @@ -59,8 +59,8 @@ public: bool m_useSecret; bool m_starting; - explicit QWebChannelSocket(QObject *parent = 0); - virtual ~QWebChannelSocket(); + explicit QWebSocketTransportPrivate(QObject *parent = 0); + virtual ~QWebSocketTransportPrivate(); void initLater(); void sendMessage(const QString &message); diff --git a/src/webchannel/webchannel.pro b/src/webchannel/webchannel.pro index f04e3eb..c6e6d69 100644 --- a/src/webchannel/webchannel.pro +++ b/src/webchannel/webchannel.pro @@ -18,14 +18,13 @@ PUBLIC_HEADERS += \ PRIVATE_HEADERS += \ qwebchannel_p.h \ qmetaobjectpublisher_p.h \ - qwebchannelsocket_p.h \ + qwebsockettransport_p.h \ variantargument_p.h \ signalhandler_p.h SOURCES += \ qwebchannel.cpp \ qmetaobjectpublisher.cpp \ - qwebchannelsocket.cpp \ qwebsockettransport.cpp qtHaveModule(qml) { |