diff options
author | Kevron Rees <kevron.m.rees@intel.com> | 2014-01-02 16:18:47 -0800 |
---|---|---|
committer | Kevron Rees <kevron.m.rees@intel.com> | 2014-01-02 16:18:47 -0800 |
commit | ff5290afca78664da71444582b9d8bff9965f185 (patch) | |
tree | 109da86642e5bee132adbb7c2becc70dce196d82 /plugins/websocket/websocketsink.cpp | |
parent | 2884fe08bf62b414d4c74727fc282e2a82f94fb9 (diff) | |
download | automotive-message-broker-binarywebsockets.tar.gz |
add option to use json or binary protocolbinarywebsockets
Diffstat (limited to 'plugins/websocket/websocketsink.cpp')
-rw-r--r-- | plugins/websocket/websocketsink.cpp | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/plugins/websocket/websocketsink.cpp b/plugins/websocket/websocketsink.cpp index 4a7ff288..37f88e39 100644 --- a/plugins/websocket/websocketsink.cpp +++ b/plugins/websocket/websocketsink.cpp @@ -33,15 +33,27 @@ #include <QJsonDocument> #include <QVariantMap> +bool WebSocketSink::doBinary = false; + +// libwebsocket_write helper function static int lwsWrite(struct libwebsocket *lws, const char* strToWrite, int len) { - /*std::unique_ptr<char[]> buffer(new char[LWS_SEND_BUFFER_PRE_PADDING + strToWrite.length() + LWS_SEND_BUFFER_POST_PADDING]); - - char *buf = buffer.get() + LWS_SEND_BUFFER_PRE_PADDING; - strcpy(buf, strToWrite.c_str()); -*/ - //NOTE: delete[] on buffer is not needed since std::unique_ptr<char[]> is used - return libwebsocket_write(lws, (unsigned char*)strToWrite, len, LWS_WRITE_BINARY); + int retval = -1; + + if(WebSocketSink::doBinary) + { + retval = libwebsocket_write(lws, (unsigned char*)strToWrite, len, LWS_WRITE_BINARY); + } + else + { + std::unique_ptr<char[]> buffer(new char[LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING]); + char *buf = buffer.get() + LWS_SEND_BUFFER_PRE_PADDING; + strcpy(buf, strToWrite); + + retval = libwebsocket_write(lws, (unsigned char*)buf, len, LWS_WRITE_TEXT); + } + + return retval; } @@ -75,7 +87,11 @@ void WebSocketSink::propertyChanged(AbstractPropertyType *value) reply["name"]=property.c_str(); reply["transactionid"]=m_uuid.c_str(); - QByteArray replystr = QJsonDocument::fromVariant(reply).toBinaryData(); + QByteArray replystr; + if(WebSocketSink::doBinary) + replystr = QJsonDocument::fromVariant(reply).toBinaryData(); + else + replystr = QJsonDocument::fromVariant(reply).toJson(); lwsWrite(m_wsi, replystr.data(),replystr.length()); } @@ -91,3 +107,5 @@ PropertyList WebSocketSink::subscriptions() return PropertyList(); } +/// 6% and 4% cpu with json +/// 5% and 4% with binary |