From 23ccec8ce246634799b06f55b85478b3fbbb356d Mon Sep 17 00:00:00 2001 From: Kurt Pattyn Date: Sun, 22 Dec 2013 18:24:21 +0100 Subject: Add optimizations Change-Id: Icd293f832e2d7a6272d4953c1994065d16222375 Reviewed-by: Kurt Pattyn --- src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp | 4 ++++ src/imports/qmlwebsockets/qqmlwebsocket.cpp | 28 +++++++++++++--------- src/imports/qmlwebsockets/qqmlwebsocket.h | 7 ++++-- 3 files changed, 26 insertions(+), 13 deletions(-) (limited to 'src/imports') diff --git a/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp b/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp index c8d2cd6..6664e59 100644 --- a/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp +++ b/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp @@ -43,6 +43,8 @@ #include +QT_BEGIN_NAMESPACE + void QmlWebsocket_plugin::registerTypes(const char *uri) { Q_ASSERT(uri == QLatin1String("Qt.WebSockets")); @@ -50,3 +52,5 @@ void QmlWebsocket_plugin::registerTypes(const char *uri) // @uri Qt.WebSockets qmlRegisterType(uri, 1 /*major*/, 0 /*minor*/, "WebSocket"); } + +QT_END_NAMESPACE diff --git a/src/imports/qmlwebsockets/qqmlwebsocket.cpp b/src/imports/qmlwebsockets/qqmlwebsocket.cpp index 9d2ce7f..3483d75 100644 --- a/src/imports/qmlwebsockets/qqmlwebsocket.cpp +++ b/src/imports/qmlwebsockets/qqmlwebsocket.cpp @@ -59,6 +59,8 @@ #include "qqmlwebsocket.h" #include +QT_BEGIN_NAMESPACE + QQmlWebSocket::QQmlWebSocket(QObject *parent) : QObject(parent), m_webSocket(), @@ -74,14 +76,14 @@ QQmlWebSocket::~QQmlWebSocket() { } -void QQmlWebSocket::sendTextMessage(const QString &message) +qint64 QQmlWebSocket::sendTextMessage(const QString &message) { if (m_status != Open) { setErrorString(tr("Messages can only be send when the socket has Open status.")); setStatus(Error); - return; + return 0; } - m_webSocket->write(message); + return m_webSocket->write(message); } QUrl QQmlWebSocket::url() const @@ -123,14 +125,16 @@ void QQmlWebSocket::classBegin() void QQmlWebSocket::componentComplete() { - m_webSocket.reset(new QWebSocket()); - connect(m_webSocket.data(), SIGNAL(textMessageReceived(QString)), this, SIGNAL(textMessageReceived(QString))); - connect(m_webSocket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError))); - connect(m_webSocket.data(), SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onStateChanged(QAbstractSocket::SocketState))); + m_webSocket.reset(new (std::nothrow) QWebSocket()); + if (Q_LIKELY(m_webSocket)) { + connect(m_webSocket.data(), SIGNAL(textMessageReceived(QString)), this, SIGNAL(textMessageReceived(QString))); + connect(m_webSocket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError))); + connect(m_webSocket.data(), SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onStateChanged(QAbstractSocket::SocketState))); - m_componentCompleted = true; + m_componentCompleted = true; - open(); + open(); + } } void QQmlWebSocket::onError(QAbstractSocket::SocketError error) @@ -210,14 +214,14 @@ bool QQmlWebSocket::isActive() const void QQmlWebSocket::open() { - if (m_componentCompleted && m_isActive && m_url.isValid() && m_webSocket) { + if (m_componentCompleted && m_isActive && m_url.isValid() && Q_LIKELY(m_webSocket)) { m_webSocket->open(m_url); } } void QQmlWebSocket::close() { - if (m_componentCompleted && m_webSocket) { + if (m_componentCompleted && Q_LIKELY(m_webSocket)) { m_webSocket->close(); } } @@ -230,3 +234,5 @@ void QQmlWebSocket::setErrorString(QString errorString) m_errorString = errorString; Q_EMIT errorStringChanged(m_errorString); } + +QT_END_NAMESPACE diff --git a/src/imports/qmlwebsockets/qqmlwebsocket.h b/src/imports/qmlwebsockets/qqmlwebsocket.h index 47cf6fa..b9d7769 100644 --- a/src/imports/qmlwebsockets/qqmlwebsocket.h +++ b/src/imports/qmlwebsockets/qqmlwebsocket.h @@ -48,6 +48,8 @@ #include #include +QT_BEGIN_NAMESPACE + class QQmlWebSocket : public QObject, public QQmlParserStatus { Q_OBJECT @@ -81,8 +83,7 @@ public: void setActive(bool active); bool isActive() const; -public Q_SLOTS: - void sendTextMessage(const QString &message); + Q_INVOKABLE qint64 sendTextMessage(const QString &message); Q_SIGNALS: @@ -114,4 +115,6 @@ private: void setErrorString(QString errorString = QString()); }; +QT_END_NAMESPACE + #endif // QQMLWEBSOCKET_H -- cgit v1.2.1