From 140a21c8b330249df5fe56fefaf924d5ccf4c32d Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Wed, 26 Feb 2020 11:23:36 -0800 Subject: [PATCH] (ws_connect) display sent/received bytes statistics on exit --- docs/CHANGELOG.md | 4 ++++ ixwebsocket/IXWebSocketVersion.h | 2 +- ws/ws_connect.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index c60e0f33..d05d8824 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All changes to this project will be documented in this file. +## [8.1.6] - 2020-02-26 + +(ws_connect) display sent/received bytes statistics on exit + ## [8.1.5] - 2020-02-23 (server) give thread name to some usual worker threads / unittest is broken !! diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index cf1e6d28..2d3a1883 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "8.1.4" +#define IX_WEBSOCKET_VERSION "8.1.6" diff --git a/ws/ws_connect.cpp b/ws/ws_connect.cpp index 9cf21b99..ca0d399d 100644 --- a/ws/ws_connect.cpp +++ b/ws/ws_connect.cpp @@ -30,6 +30,9 @@ namespace ix void start(); void stop(); + int getSentBytes() { return _sentBytes; } + int getReceivedBytes() { return _receivedBytes; } + void sendMessage(const std::string& text); private: @@ -38,6 +41,8 @@ namespace ix ix::WebSocket _webSocket; bool _disablePerMessageDeflate; bool _binaryMode; + std::atomic _receivedBytes; + std::atomic _sentBytes; void log(const std::string& msg); WebSocketHttpHeaders parseHeaders(const std::string& data); @@ -54,6 +59,8 @@ namespace ix : _url(url) , _disablePerMessageDeflate(disablePerMessageDeflate) , _binaryMode(binaryMode) + , _receivedBytes(0) + , _sentBytes(0) { if (disableAutomaticReconnection) { @@ -68,6 +75,20 @@ namespace ix { _webSocket.addSubProtocol(subprotocol); } + + WebSocket::setTrafficTrackerCallback( + [this](int size, bool incoming) + { + if (incoming) + { + _receivedBytes += size; + } + else + { + _sentBytes += size; + } + } + ); } void WebSocketConnect::log(const std::string& msg) @@ -246,6 +267,9 @@ namespace ix spdlog::info(""); webSocketChat.stop(); + spdlog::info("Received {} bytes", webSocketChat.getReceivedBytes()); + spdlog::info("Sent {} bytes", webSocketChat.getSentBytes()); + return 0; } } // namespace ix