From 16c6f08e2de6d54accf3465712cc790b281698a0 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Sat, 16 Nov 2019 06:51:53 -0800 Subject: [PATCH] fix android build + proxy work --- CMakeLists.txt | 2 +- DOCKER_VERSION | 2 +- ixwebsocket/IXWebSocketVersion.h | 2 +- test/IXSentryClientTest.cpp | 10 +- ws/ws_cobra_metrics_to_redis.cpp | 28 ++--- ws/ws_cobra_to_sentry.cpp | 2 +- ws/ws_proxy_server.cpp | 206 +++++++++++++++++-------------- 7 files changed, 138 insertions(+), 114 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f8b8776a..84ff3269 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,7 +107,7 @@ elseif (WIN32) list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/windows/IXSetThreadName_windows.cpp) elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/freebsd/IXSetThreadName_freebsd.cpp) -elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux") +else() list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/linux/IXSetThreadName_linux.cpp) list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSelectInterruptEventFd.cpp) list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSelectInterruptEventFd.h) diff --git a/DOCKER_VERSION b/DOCKER_VERSION index 15020207..643916c0 100644 --- a/DOCKER_VERSION +++ b/DOCKER_VERSION @@ -1 +1 @@ -7.3.0 +7.3.1 diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index ac4e4944..9e8f6893 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "7.3.0" +#define IX_WEBSOCKET_VERSION "7.3.1" diff --git a/test/IXSentryClientTest.cpp b/test/IXSentryClientTest.cpp index 48675125..f2e154a8 100644 --- a/test/IXSentryClientTest.cpp +++ b/test/IXSentryClientTest.cpp @@ -8,9 +8,8 @@ #include "catch.hpp" #include -#include - #include +#include using namespace ix; @@ -21,7 +20,9 @@ namespace ix SECTION("Attempt to index nil") { SentryClient sentryClient(""); - std::string stack = "Attempt to index nil[overlay]!\nstack traceback:\n\tfoo.lua:2661: in function 'getFoo'\n\tfoo.lua:1666: in function 'onUpdate'\n\tfoo.lua:1751: in function "; + std::string stack = "Attempt to index nil[overlay]!\nstack traceback:\n\tfoo.lua:2661: " + "in function 'getFoo'\n\tfoo.lua:1666: in function " + "'onUpdate'\n\tfoo.lua:1751: in function "; auto frames = sentryClient.parseLuaStackTrace(stack); REQUIRE(frames.size() == 3); @@ -30,7 +31,8 @@ namespace ix SECTION("Attempt to perform nil") { SentryClient sentryClient(""); - std::string stack = "Attempt to perform nil - 1572111278.299\nstack traceback:\n\tfoo.lua:57: in function "; + std::string stack = "Attempt to perform nil - 1572111278.299\nstack " + "traceback:\n\tfoo.lua:57: in function "; auto frames = sentryClient.parseLuaStackTrace(stack); REQUIRE(frames.size() == 1); diff --git a/ws/ws_cobra_metrics_to_redis.cpp b/ws/ws_cobra_metrics_to_redis.cpp index 0f9e49ac..7c07a6cd 100644 --- a/ws/ws_cobra_metrics_to_redis.cpp +++ b/ws/ws_cobra_metrics_to_redis.cpp @@ -128,23 +128,21 @@ namespace ix { spdlog::info("Subscriber authenticated"); - conn.subscribe(channel, - filter, - [&msgPerSeconds, - &msgCount, - &conditionVariableMutex, - &condition, - &queue](const Json::Value& msg) { - { - std::unique_lock lock(conditionVariableMutex); - queue.push(msg); - } + conn.subscribe( + channel, + filter, + [&msgPerSeconds, &msgCount, &conditionVariableMutex, &condition, &queue]( + const Json::Value& msg) { + { + std::unique_lock lock(conditionVariableMutex); + queue.push(msg); + } - condition.notify_one(); + condition.notify_one(); - msgPerSeconds++; - msgCount++; - }); + msgPerSeconds++; + msgCount++; + }); } else if (eventType == ix::CobraConnection_EventType_Subscribed) { diff --git a/ws/ws_cobra_to_sentry.cpp b/ws/ws_cobra_to_sentry.cpp index a0917373..3759a0b9 100644 --- a/ws/ws_cobra_to_sentry.cpp +++ b/ws/ws_cobra_to_sentry.cpp @@ -4,12 +4,12 @@ * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. */ -#include #include #include #include #include #include +#include #include #include #include diff --git a/ws/ws_proxy_server.cpp b/ws/ws_proxy_server.cpp index e44bff21..ed0289ca 100644 --- a/ws/ws_proxy_server.cpp +++ b/ws/ws_proxy_server.cpp @@ -13,13 +13,29 @@ namespace ix class ProxyConnectionState : public ix::ConnectionState { public: + ProxyConnectionState() + : _connected(false) + { + } + ix::WebSocket& webSocket() { return _serverWebSocket; } + bool isConnected() + { + return _connected; + } + + void setConnected() + { + _connected = true; + } + private: ix::WebSocket _serverWebSocket; + bool _connected; }; int ws_proxy_server_main(int port, @@ -38,101 +54,109 @@ namespace ix }; server.setConnectionStateFactory(factory); - server.setOnConnectionCallback( - [remoteUrl, verbose](std::shared_ptr webSocket, - std::shared_ptr connectionState) { - auto state = std::dynamic_pointer_cast(connectionState); + server.setOnConnectionCallback([remoteUrl, + verbose](std::shared_ptr webSocket, + std::shared_ptr connectionState) { + auto state = std::dynamic_pointer_cast(connectionState); - // Server connection - state->webSocket().setOnMessageCallback( - [webSocket, state, verbose](const WebSocketMessagePtr& msg) { - if (msg->type == ix::WebSocketMessageType::Open) - { - std::cerr << "New connection" << std::endl; - std::cerr << "id: " << state->getId() << std::endl; - std::cerr << "Uri: " << msg->openInfo.uri << std::endl; - std::cerr << "Headers:" << std::endl; - for (auto it : msg->openInfo.headers) - { - std::cerr << it.first << ": " << it.second << std::endl; - } - } - else if (msg->type == ix::WebSocketMessageType::Close) - { - std::cerr << "Closed connection" - << " code " << msg->closeInfo.code << " reason " - << msg->closeInfo.reason << std::endl; - } - else if (msg->type == ix::WebSocketMessageType::Error) - { - std::stringstream ss; - ss << "Connection error: " << msg->errorInfo.reason << std::endl; - ss << "#retries: " << msg->errorInfo.retries << std::endl; - ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; - ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str(); - webSocket->close(msg->closeInfo.code, msg->closeInfo.reason); - } - else if (msg->type == ix::WebSocketMessageType::Message) - { - std::cerr << "Received " << msg->wireSize << " bytes from server" << std::endl; - if (verbose) - { - std::cerr << "payload " << msg->str << std::endl; - } + // Server connection + state->webSocket().setOnMessageCallback([webSocket, state, verbose]( + const WebSocketMessagePtr& msg) { + if (msg->type == ix::WebSocketMessageType::Open) + { + std::cerr << "New connection" << std::endl; + std::cerr << "id: " << state->getId() << std::endl; + std::cerr << "Uri: " << msg->openInfo.uri << std::endl; + std::cerr << "Headers:" << std::endl; + for (auto it : msg->openInfo.headers) + { + std::cerr << it.first << ": " << it.second << std::endl; + } + state->setConnected(); + } + else if (msg->type == ix::WebSocketMessageType::Close) + { + std::cerr << "Closed connection" + << " code " << msg->closeInfo.code << " reason " + << msg->closeInfo.reason << std::endl; + } + else if (msg->type == ix::WebSocketMessageType::Error) + { + std::stringstream ss; + ss << "Connection error: " << msg->errorInfo.reason << std::endl; + ss << "#retries: " << msg->errorInfo.retries << std::endl; + ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; + ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; + std::cerr << ss.str(); + webSocket->close(msg->closeInfo.code, msg->closeInfo.reason); + } + else if (msg->type == ix::WebSocketMessageType::Message) + { + std::cerr << "Received " << msg->wireSize << " bytes from server" << std::endl; + if (verbose) + { + std::cerr << "payload " << msg->str << std::endl; + } - webSocket->send(msg->str, msg->binary); - } - }); - - // Client connection - webSocket->setOnMessageCallback( - [state, remoteUrl, verbose](const WebSocketMessagePtr& msg) { - if (msg->type == ix::WebSocketMessageType::Open) - { - std::cerr << "New connection" << std::endl; - std::cerr << "id: " << state->getId() << std::endl; - std::cerr << "Uri: " << msg->openInfo.uri << std::endl; - std::cerr << "Headers:" << std::endl; - for (auto it : msg->openInfo.headers) - { - std::cerr << it.first << ": " << it.second << std::endl; - } - - // Connect to the 'real' server - std::string url(remoteUrl); - url += msg->openInfo.uri; - state->webSocket().setUrl(url); - state->webSocket().start(); - } - else if (msg->type == ix::WebSocketMessageType::Close) - { - std::cerr << "Closed connection" - << " code " << msg->closeInfo.code << " reason " - << msg->closeInfo.reason << std::endl; - state->webSocket().close(msg->closeInfo.code, msg->closeInfo.reason); - } - else if (msg->type == ix::WebSocketMessageType::Error) - { - std::stringstream ss; - ss << "Connection error: " << msg->errorInfo.reason << std::endl; - ss << "#retries: " << msg->errorInfo.retries << std::endl; - ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; - ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str(); - } - else if (msg->type == ix::WebSocketMessageType::Message) - { - std::cerr << "Received " << msg->wireSize << " bytes from client" << std::endl; - if (verbose) - { - std::cerr << "payload " << msg->str << std::endl; - } - state->webSocket().send(msg->str, msg->binary); - } - }); + webSocket->send(msg->str, msg->binary); + } }); + // Client connection + webSocket->setOnMessageCallback([state, remoteUrl, verbose]( + const WebSocketMessagePtr& msg) { + if (msg->type == ix::WebSocketMessageType::Open) + { + std::cerr << "New connection" << std::endl; + std::cerr << "id: " << state->getId() << std::endl; + std::cerr << "Uri: " << msg->openInfo.uri << std::endl; + std::cerr << "Headers:" << std::endl; + for (auto it : msg->openInfo.headers) + { + std::cerr << it.first << ": " << it.second << std::endl; + } + + // Connect to the 'real' server + std::string url(remoteUrl); + url += msg->openInfo.uri; + state->webSocket().setUrl(url); + state->webSocket().start(); + + // we should sleep here for a bit until we've established the + // connection with the remote server + while (!state->isConnected()) + { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + } + else if (msg->type == ix::WebSocketMessageType::Close) + { + std::cerr << "Closed connection" + << " code " << msg->closeInfo.code << " reason " + << msg->closeInfo.reason << std::endl; + state->webSocket().close(msg->closeInfo.code, msg->closeInfo.reason); + } + else if (msg->type == ix::WebSocketMessageType::Error) + { + std::stringstream ss; + ss << "Connection error: " << msg->errorInfo.reason << std::endl; + ss << "#retries: " << msg->errorInfo.retries << std::endl; + ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; + ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; + std::cerr << ss.str(); + } + else if (msg->type == ix::WebSocketMessageType::Message) + { + std::cerr << "Received " << msg->wireSize << " bytes from client" << std::endl; + if (verbose) + { + std::cerr << "payload " << msg->str << std::endl; + } + state->webSocket().send(msg->str, msg->binary); + } + }); + }); + auto res = server.listen(); if (!res.first) {