Changeset - 4353dbef025d
[Not reviewed]
0 3 0
Jan Kaluza - 9 years ago 2016-02-01 09:25:48
jkaluza@redhat.com
Libtransport: Better WebSocketClient logging
3 files changed with 13 insertions and 21 deletions:
0 comments (0 inline, 0 general)
include/transport/WebSocketClient.h
Show inline comments
 
@@ -47,13 +47,13 @@
 
namespace Transport {
 

	
 
class Component;
 

	
 
class WebSocketClient {
 
	public:
 
		WebSocketClient(Component *component);
 
		WebSocketClient(Component *component, const std::string &user);
 

	
 
		virtual ~WebSocketClient();
 

	
 
		void connectServer(const std::string &u);
 

	
 
		void write(const std::string &data);
 
@@ -79,9 +79,10 @@ class WebSocketClient {
 
		Swift::PlatformTLSFactories *m_tlsFactory;
 
		std::string m_host;
 
		std::string m_path;
 
		std::string m_buffer;
 
		bool m_upgraded;
 
		Swift::Timer::ref m_reconnectTimer;
 
		std::string m_user;
 
};
 

	
 
}
libtransport/WebSocketClient.cpp
Show inline comments
 
@@ -31,15 +31,16 @@
 
#include <iterator>
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "WebSocketClient");
 

	
 
WebSocketClient::WebSocketClient(Component *component) {
 
WebSocketClient::WebSocketClient(Component *component, const std::string &user) {
 
	m_component = component;
 
	m_upgraded = false;
 
	m_user = user;
 

	
 
#if HAVE_SWIFTEN_3
 
	Swift::TLSOptions o;
 
#endif
 
	m_tlsFactory = new Swift::PlatformTLSFactories();
 
#if HAVE_SWIFTEN_3
 
@@ -60,13 +61,13 @@ WebSocketClient::~WebSocketClient() {
 

	
 
	delete m_tlsFactory;
 
	delete m_tlsConnectionFactory;
 
}
 

	
 
void WebSocketClient::connectServer() {
 
	LOG4CXX_INFO(logger, "Starting DNS query for " << m_host << " " << m_path);
 
	LOG4CXX_INFO(logger, m_user << ": Starting DNS query for " << m_host << " " << m_path);
 

	
 
	m_upgraded = false;
 
	m_buffer.clear();
 

	
 
	m_dnsQuery = m_component->getNetworkFactories()->getDomainNameResolver()->createAddressQuery(m_host);
 
	m_dnsQuery->onResult.connect(boost::bind(&WebSocketClient::handleDNSResult, this, _1, _2));
 
@@ -85,13 +86,12 @@ void WebSocketClient::write(const std::string &data) {
 
	if (!m_conn) {
 
		return;
 
	}
 

	
 
	uint8_t opcode = 129; // UTF8
 
	if (data.empty()) {
 
		LOG4CXX_INFO(logger, "pong");
 
		opcode = 138; // PONG
 
	}
 

	
 
	// Mask the payload
 
	char mask_bits[4] = {0x11, 0x22, 0x33, 0x44};
 
	std::string payload = data;
 
@@ -114,13 +114,13 @@ void WebSocketClient::write(const std::string &data) {
 
			+ std::string((char *) &size7, 1)
 
			+ std::string((char *) &size16, 2)
 
			+ std::string((char *) &mask_bits[0], 4)
 
			+ payload));
 
	}
 

	
 
	LOG4CXX_INFO(logger, "> " << data);
 
	LOG4CXX_INFO(logger, m_user << ": > " << data);
 
}
 

	
 
void WebSocketClient::handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) {
 
	std::string d = Swift::safeByteArrayToString(*data);
 
	m_buffer += d;
 

	
 
@@ -134,22 +134,17 @@ void WebSocketClient::handleDataRead(boost::shared_ptr<Swift::SafeByteArray> dat
 
			return;
 
		}
 
	}
 

	
 
	while (m_buffer.size() > 0) {
 
		if (m_buffer.size() >= 2) {
 
			LOG4CXX_INFO(logger, "BUFFER: '" << m_buffer << "'");
 
			LOG4CXX_INFO(logger, "BUFFER: '" << Swift::Hexify::hexify(Swift::createByteArray(m_buffer)) << "'");
 
			uint8_t opcode = *((uint8_t *) &m_buffer[0]) & 0xf;
 
			uint8_t size7 = *((uint8_t *) &m_buffer[1]) & 127;
 
			bool mask = *((uint8_t *) &m_buffer[1]) & 128;
 
			uint16_t size16 = 0;
 
			int header_size = 2;
 
			LOG4CXX_INFO(logger, "OPCODE: " << (int) opcode);
 
			LOG4CXX_INFO(logger, "SIZE7: " << (int) size7);
 
			LOG4CXX_INFO(logger, "MASK: " << (int) mask);
 
			if (size7 == 126) {
 
				if (m_buffer.size() >= 4) {
 
					size16 = *((uint16_t *) &m_buffer[2]);
 
					size16 = ntohs(size16);
 
					header_size += 2;
 
				}
 
@@ -158,25 +153,21 @@ void WebSocketClient::handleDataRead(boost::shared_ptr<Swift::SafeByteArray> dat
 
				}
 
			}
 

	
 
			// This seems to be Slack bug... sometimes we receive 0x89 followed by 0x81
 
			// For now, in that case we will just ignore the 0x89 and skip it...
 
			if (opcode == 9 && mask && size7 == 1) {
 
				LOG4CXX_WARN(logger, "Applying Slack workaround because of partial data received from server");
 
				LOG4CXX_WARN(logger, m_user << ": Applying Slack workaround because of partial data received from server");
 
				m_buffer.erase(0, 1);
 
				continue;
 
			}
 

	
 
// 			if (opcode == 9) {
 
// 				write("");
 
// 			}
 

	
 
			unsigned int size = (size16 == 0 ? size7 : size16);
 
			if (m_buffer.size() >= size + header_size) {
 
				std::string payload = m_buffer.substr(header_size, size);
 
				LOG4CXX_INFO(logger, "< " << payload);
 
				LOG4CXX_INFO(logger, m_user << ": < " << payload);
 
				onPayloadReceived(payload);
 
				m_buffer.erase(0, size + header_size);
 
				
 
			}
 
			else if (size == 0) {
 
				m_buffer.erase(0, header_size);
 
@@ -195,13 +186,13 @@ void WebSocketClient::handleConnected(bool error) {
 
	if (error) {
 
		LOG4CXX_ERROR(logger, "Connection to " << m_host << " failed. Will reconnect in 1 second.");
 
		m_reconnectTimer->start();
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, "Connected to " << m_host);
 
	LOG4CXX_INFO(logger, m_user << ": Connected to " << m_host);
 

	
 
	std::string req = "";
 
	req += "GET " + m_path + " HTTP/1.1\r\n";
 
	req += "Host: " + m_host + ":443\r\n";
 
	req += "Upgrade: websocket\r\n";
 
	req += "Connection: Upgrade\r\n";
 
@@ -214,26 +205,26 @@ void WebSocketClient::handleConnected(bool error) {
 

	
 
void WebSocketClient::handleDisconnected(const boost::optional<Swift::Connection::Error> &error) {
 
	if (!error) {
 
		return;
 
	}
 

	
 
	LOG4CXX_ERROR(logger, "Disconected from " << m_host << ". Will reconnect in 1 second.");
 
	LOG4CXX_ERROR(logger, m_user << ": Disconected from " << m_host << ". Will reconnect in 1 second.");
 
	onWebSocketDisconnected(error);
 
	m_reconnectTimer->start();
 
}
 

	
 
void WebSocketClient::handleDNSResult(const std::vector<Swift::HostAddress> &addrs, boost::optional<Swift::DomainNameResolveError> error) {
 
	if (error) {
 
		LOG4CXX_ERROR(logger, "DNS resolving error. Will try again in 1 second.");
 
		LOG4CXX_ERROR(logger, m_user << ": DNS resolving error. Will try again in 1 second.");
 
		m_reconnectTimer->start();
 
		return;
 
	}
 

	
 
	if (addrs.empty()) {
 
		LOG4CXX_ERROR(logger, "DNS name cannot be resolved. Will try again in 1 second.");
 
		LOG4CXX_ERROR(logger, m_user << ": DNS name cannot be resolved. Will try again in 1 second.");
 
		m_reconnectTimer->start();
 
		return;
 
	}
 

	
 
	m_conn = m_tlsConnectionFactory->createConnection();
 
	m_conn->onDataRead.connect(boost::bind(&WebSocketClient::handleDataRead, this, _1));
spectrum/src/frontends/slack/SlackRTM.cpp
Show inline comments
 
@@ -42,13 +42,13 @@ DEFINE_LOGGER(logger, "SlackRTM");
 
SlackRTM::SlackRTM(Component *component, StorageBackend *storageBackend, SlackIdManager *idManager, UserInfo uinfo) : m_uinfo(uinfo) {
 
	m_component = component;
 
	m_storageBackend = storageBackend;
 
	m_counter = 0;
 
	m_started = false;
 
	m_idManager = idManager;
 
	m_client = new WebSocketClient(component);
 
	m_client = new WebSocketClient(component, m_uinfo.jid);
 
	m_client->onPayloadReceived.connect(boost::bind(&SlackRTM::handlePayloadReceived, this, _1));
 
	m_client->onWebSocketConnected.connect(boost::bind(&SlackRTM::handleWebSocketConnected, this));
 

	
 
	m_pingTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(20000);
 
	m_pingTimer->onTick.connect(boost::bind(&SlackRTM::sendPing, this));
 

	
0 comments (0 inline, 0 general)