Compare commits

..

1 Commits

Author SHA1 Message Date
bdee582203 add sentry native project 2020-03-06 11:53:36 -08:00
292 changed files with 8013 additions and 9835 deletions

View File

@ -1,21 +1,51 @@
name: unittest name: unittest
on:
push:
paths-ignore:
- 'docs/**'
on: [push]
# fake comment to trigger an action 1
jobs: jobs:
windows_mbedtls: linux:
runs-on: windows-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- uses: seanmiddleditch/gha-setup-vsdevenv@master - name: make test
- run: | run: make test
vcpkg install zlib:x64-windows
- run: | mac:
vcpkg install mbedtls:x64-windows runs-on: macOS-latest
- run: |
mkdir build steps:
cd build - uses: actions/checkout@v1
cmake -DCMAKE_TOOLCHAIN_FILE=c:/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_CXX_COMPILER=cl.exe -DUSE_MBED_TLS=1 -DUSE_TLS=1 -DUSE_WS=1 .. - name: make test
- run: cmake --build build run: make test
# We don't need to have redis running anymore, as we have our fake limited one
# - name: install redis
# run: brew install redis
#
# - name: start redis server
# run: brew services start redis
# # Windows does not work yet, I'm stuck at getting CMake to run + finding vcpkg
# win:
# runs-on: windows-2016
#
# steps:
# - uses: actions/checkout@v1
#
# - name: run cmake
# run: |
# "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
# mkdir build
# cd build
# cmake -DCMAKE_TOOLCHAIN_FILE=%VCPKG_INSTALLATION_ROOT%\scripts\buildsystems\vcpkg.cmake -DUSE_WS=1 -DUSE_TEST=1 -DUSE_TLS=1 -G"NMake Makefiles" ..
# - name: build
# run: |
# "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
# cd build
# nmake
# - name: run tests
# run:
# cd test
# ..\build\test\ixwebsocket_unittest.exe

View File

@ -1,25 +0,0 @@
name: mkdocs
on:
push:
paths:
- 'docs/**'
jobs:
linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install mkdocs
pip install mkdocs-material
pip install pygments
- name: Build doc
run: |
git pull
mkdocs gh-deploy

View File

@ -1,19 +0,0 @@
name: Mark stale issues and pull requests
on:
schedule:
- cron: "0 0 * * *"
jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'Stale issue message'
stale-pr-message: 'Stale pull request message'
stale-issue-label: 'no-issue-activity'
stale-pr-label: 'no-pr-activity'

59
.travis.yml Normal file
View File

@ -0,0 +1,59 @@
language: bash
# See https://github.com/amaiorano/vectrexy/blob/master/.travis.yml
# for ideas on installing vcpkg
matrix:
include:
# macOS
# - os: osx
# env:
# - HOMEBREW_NO_AUTO_UPDATE=1
# compiler: clang
# script:
# - brew install redis
# - brew services start redis
# - brew install mbedtls
# - python test/run.py
# - make ws
Linux
- os: linux
dist: bionic
before_install:
- sudo apt-get install -y libmbedtls-dev
- sudo apt-get install -y redis-server
script:
- python test/run.py
# - make ws
env:
- CC=gcc
- CXX=g++
# Clang + Linux disabled for now
# - os: linux
# dist: xenial
# script: python test/run.py
# env:
# - CC=clang
# - CXX=clang++
# Windows
# - os: windows
# env:
# - CMAKE_PATH="/c/Program Files/CMake/bin"
# script:
# - cd third_party/zlib
# - cmake .
# - cmake --build . --target install
# - cd ../..
# # - cd third_party/mbedtls
# # - cmake .
# # - cmake --build . --target install
# # - cd ../..
# - export PATH=$CMAKE_PATH:$PATH
# - cd test
# - cmake .
# - cmake --build --parallel .
# - ixwebsocket_unittest.exe
# # - python test/run.py

View File

@ -5,7 +5,7 @@ include(FindPackageHandleStandardArgs)
find_path(JSONCPP_INCLUDE_DIRS json/json.h) find_path(JSONCPP_INCLUDE_DIRS json/json.h)
find_library(JSONCPP_LIBRARY jsoncpp) find_library(JSONCPP_LIBRARY jsoncpp)
find_package_handle_standard_args(JsonCpp find_package_handle_standard_args(JSONCPP
FOUND_VAR FOUND_VAR
JSONCPP_FOUND JSONCPP_FOUND
REQUIRED_VARS REQUIRED_VARS

View File

@ -1,19 +0,0 @@
# Find package structure taken from libcurl
include(FindPackageHandleStandardArgs)
find_path(SPDLOG_INCLUDE_DIRS spdlog/spdlog.h)
find_library(JSONCPP_LIBRARY spdlog)
find_package_handle_standard_args(SPDLOG
FOUND_VAR
SPDLOG_FOUND
REQUIRED_VARS
SPDLOG_LIBRARY
SPDLOG_INCLUDE_DIRS
FAIL_MESSAGE
"Could NOT find spdlog"
)
set(SPDLOG_INCLUDE_DIRS ${SPDLOG_INCLUDE_DIRS})
set(SPDLOG_LIBRARIES ${SPDLOG_LIBRARY})

View File

@ -21,7 +21,6 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
endif() endif()
set( IXWEBSOCKET_SOURCES set( IXWEBSOCKET_SOURCES
ixwebsocket/IXBench.cpp
ixwebsocket/IXCancellationRequest.cpp ixwebsocket/IXCancellationRequest.cpp
ixwebsocket/IXConnectionState.cpp ixwebsocket/IXConnectionState.cpp
ixwebsocket/IXDNSLookup.cpp ixwebsocket/IXDNSLookup.cpp
@ -37,13 +36,13 @@ set( IXWEBSOCKET_SOURCES
ixwebsocket/IXSocketFactory.cpp ixwebsocket/IXSocketFactory.cpp
ixwebsocket/IXSocketServer.cpp ixwebsocket/IXSocketServer.cpp
ixwebsocket/IXSocketTLSOptions.cpp ixwebsocket/IXSocketTLSOptions.cpp
ixwebsocket/IXUdpSocket.cpp
ixwebsocket/IXUrlParser.cpp ixwebsocket/IXUrlParser.cpp
ixwebsocket/IXUserAgent.cpp ixwebsocket/IXUserAgent.cpp
ixwebsocket/IXWebSocket.cpp ixwebsocket/IXWebSocket.cpp
ixwebsocket/IXWebSocketCloseConstants.cpp ixwebsocket/IXWebSocketCloseConstants.cpp
ixwebsocket/IXWebSocketHandshake.cpp ixwebsocket/IXWebSocketHandshake.cpp
ixwebsocket/IXWebSocketHttpHeaders.cpp ixwebsocket/IXWebSocketHttpHeaders.cpp
ixwebsocket/IXWebSocketMessageQueue.cpp
ixwebsocket/IXWebSocketPerMessageDeflate.cpp ixwebsocket/IXWebSocketPerMessageDeflate.cpp
ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp
ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp
@ -53,7 +52,6 @@ set( IXWEBSOCKET_SOURCES
) )
set( IXWEBSOCKET_HEADERS set( IXWEBSOCKET_HEADERS
ixwebsocket/IXBench.h
ixwebsocket/IXCancellationRequest.h ixwebsocket/IXCancellationRequest.h
ixwebsocket/IXConnectionState.h ixwebsocket/IXConnectionState.h
ixwebsocket/IXDNSLookup.h ixwebsocket/IXDNSLookup.h
@ -71,7 +69,6 @@ set( IXWEBSOCKET_HEADERS
ixwebsocket/IXSocketFactory.h ixwebsocket/IXSocketFactory.h
ixwebsocket/IXSocketServer.h ixwebsocket/IXSocketServer.h
ixwebsocket/IXSocketTLSOptions.h ixwebsocket/IXSocketTLSOptions.h
ixwebsocket/IXUdpSocket.h
ixwebsocket/IXUrlParser.h ixwebsocket/IXUrlParser.h
ixwebsocket/IXUtf8Validator.h ixwebsocket/IXUtf8Validator.h
ixwebsocket/IXUserAgent.h ixwebsocket/IXUserAgent.h
@ -83,6 +80,7 @@ set( IXWEBSOCKET_HEADERS
ixwebsocket/IXWebSocketHttpHeaders.h ixwebsocket/IXWebSocketHttpHeaders.h
ixwebsocket/IXWebSocketInitResult.h ixwebsocket/IXWebSocketInitResult.h
ixwebsocket/IXWebSocketMessage.h ixwebsocket/IXWebSocketMessage.h
ixwebsocket/IXWebSocketMessageQueue.h
ixwebsocket/IXWebSocketMessageType.h ixwebsocket/IXWebSocketMessageType.h
ixwebsocket/IXWebSocketOpenInfo.h ixwebsocket/IXWebSocketOpenInfo.h
ixwebsocket/IXWebSocketPerMessageDeflate.h ixwebsocket/IXWebSocketPerMessageDeflate.h
@ -118,13 +116,12 @@ endif()
option(USE_TLS "Enable TLS support" FALSE) option(USE_TLS "Enable TLS support" FALSE)
if (USE_TLS) if (USE_TLS)
option(USE_MBED_TLS "Use Mbed TLS" OFF) if (WIN32)
option(USE_OPEN_SSL "Use OpenSSL" OFF) option(USE_MBED_TLS "Use Mbed TLS" ON)
else()
# default to mbedtls on windows if nothing is configured option(USE_MBED_TLS "Use Mbed TLS" OFF)
if (WIN32 AND NOT USE_OPEN_SSL AND NOT USE_MBED_TLS)
option(USE_MBED_TLS "Use Mbed TLS" ON)
endif() endif()
option(USE_OPEN_SSL "Use OpenSSL" OFF)
if (USE_MBED_TLS) if (USE_MBED_TLS)
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketMbedTLS.h) list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketMbedTLS.h)
@ -199,9 +196,7 @@ if (USE_TLS AND USE_MBED_TLS)
endif() endif()
endif() endif()
if (NOT ZLIB_FOUND) find_package(ZLIB)
find_package(ZLIB)
endif()
if (ZLIB_FOUND) if (ZLIB_FOUND)
include_directories(${ZLIB_INCLUDE_DIRS}) include_directories(${ZLIB_INCLUDE_DIRS})
target_link_libraries(ixwebsocket ${ZLIB_LIBRARIES}) target_link_libraries(ixwebsocket ${ZLIB_LIBRARIES})
@ -235,9 +230,9 @@ if (USE_WS OR USE_TEST)
add_subdirectory(ixcobra) add_subdirectory(ixcobra)
add_subdirectory(ixsnake) add_subdirectory(ixsnake)
add_subdirectory(ixsentry) add_subdirectory(ixsentry)
add_subdirectory(ixbots)
add_subdirectory(third_party/spdlog spdlog) add_subdirectory(third_party/spdlog spdlog)
add_subdirectory(third_party/sentry-native sentry-native)
if (USE_WS) if (USE_WS)
add_subdirectory(ws) add_subdirectory(ws)

View File

@ -1 +1 @@
docker/Dockerfile.alpine docker/Dockerfile.centos

View File

@ -1,13 +1,12 @@
FROM alpine:3.11 as build FROM alpine:3.11 as build
RUN apk add --no-cache \ RUN apk add --no-cache gcc g++ musl-dev linux-headers cmake openssl-dev
gcc g++ musl-dev linux-headers \ RUN apk add --no-cache make
cmake mbedtls-dev make zlib-dev RUN apk add --no-cache zlib-dev
RUN addgroup -S app && \ RUN addgroup -S app && adduser -S -G app app
adduser -S -G app app && \ RUN chown -R app:app /opt
chown -R app:app /opt && \ RUN chown -R app:app /usr/local
chown -R app:app /usr/local
# There is a bug in CMake where we cannot build from the root top folder # There is a bug in CMake where we cannot build from the root top folder
# So we build from /opt # So we build from /opt
@ -15,21 +14,22 @@ COPY --chown=app:app . /opt
WORKDIR /opt WORKDIR /opt
USER app USER app
RUN make ws_mbedtls_install && \ RUN [ "make", "ws_install" ]
sh tools/trim_repo_for_docker.sh RUN [ "rm", "-rf", "build" ]
FROM alpine:3.11 as runtime FROM alpine:3.11 as runtime
RUN apk add --no-cache libstdc++ mbedtls ca-certificates && \ RUN apk add --no-cache libstdc++
addgroup -S app && \ RUN apk add --no-cache strace
adduser -S -G app app RUN apk add --no-cache gdb
RUN addgroup -S app && adduser -S -G app app
COPY --chown=app:app --from=build /usr/local/bin/ws /usr/local/bin/ws COPY --chown=app:app --from=build /usr/local/bin/ws /usr/local/bin/ws
RUN chmod +x /usr/local/bin/ws
RUN ldd /usr/local/bin/ws
# COPY --chown=app:app --from=build /opt /opt # Copy source code for gcc
COPY --chown=app:app --from=build /opt /opt
RUN chmod +x /usr/local/bin/ws && \
ldd /usr/local/bin/ws
# Now run in usermode # Now run in usermode
USER app USER app

View File

@ -2,9 +2,6 @@ FROM centos:8 as build
RUN yum install -y gcc-c++ make cmake zlib-devel openssl-devel redhat-rpm-config RUN yum install -y gcc-c++ make cmake zlib-devel openssl-devel redhat-rpm-config
RUN yum install -y epel-release
RUN yum install -y mbedtls-devel
RUN groupadd app && useradd -g app app RUN groupadd app && useradd -g app app
RUN chown -R app:app /opt RUN chown -R app:app /opt
RUN chown -R app:app /usr/local RUN chown -R app:app /usr/local
@ -15,16 +12,13 @@ COPY --chown=app:app . /opt
WORKDIR /opt WORKDIR /opt
USER app USER app
RUN [ "make", "ws_mbedtls_install" ] RUN [ "make", "ws_install" ]
RUN [ "rm", "-rf", "build" ] RUN [ "rm", "-rf", "build" ]
FROM centos:8 as runtime FROM centos:8 as runtime
RUN yum install -y gdb strace RUN yum install -y gdb strace
RUN yum install -y epel-release
RUN yum install -y mbedtls
RUN groupadd app && useradd -g app app RUN groupadd app && useradd -g app app
COPY --chown=app:app --from=build /usr/local/bin/ws /usr/local/bin/ws COPY --chown=app:app --from=build /usr/local/bin/ws /usr/local/bin/ws
RUN chmod +x /usr/local/bin/ws RUN chmod +x /usr/local/bin/ws

View File

@ -1,204 +1,6 @@
# Changelog # Changelog
All changes to this project will be documented in this file. All changes to this project will be documented in this file.
## [9.3.3] - 2020-04-17
(ixbots) display sent/receive message, per seconds as accumulated
## [9.3.2] - 2020-04-17
(ws) add a --logfile option to configure all logs to go to a file
## [9.3.1] - 2020-04-16
(cobra bots) add a utility class to factor out the common bots features (heartbeat) and move all bots to used it + convert cobra_subscribe to be a bot and add a unittest for it
## [9.3.0] - 2020-04-15
(websocket) add a positive number to the heartbeat message sent, incremented each time the heartbeat is sent
## [9.2.9] - 2020-04-15
(ixcobra) change cobra event callback to use a struct instead of several objects, which is more flexible/extensible
## [9.2.8] - 2020-04-15
(ixcobra) make CobraConnection_EventType an enum class (CobraEventType)
## [9.2.7] - 2020-04-14
(ixsentry) add a library method to upload a payload directly to sentry
## [9.2.6] - 2020-04-14
(ixcobra) snake server / handle invalid incoming json messages + cobra subscriber in fluentd mode insert a created_at timestamp entry
## [9.2.5] - 2020-04-13
(websocket) WebSocketMessagePtr is a unique_ptr instead of a shared_ptr
## [9.2.4] - 2020-04-13
(websocket) use persistent member variable as temp variables to encode/decode zlib messages in order to reduce transient allocations
## [9.2.3] - 2020-04-13
(ws) add a --runtime option to ws cobra_subscribe to optionally limit how much time it will run
## [9.2.2] - 2020-04-04
(third_party deps) fix #177, update bundled spdlog to 1.6.0
## [9.2.1] - 2020-04-04
(windows) when using OpenSSL, the system store is used to populate the cacert. No need to ship a cacert.pem file with your app.
## [9.2.0] - 2020-04-04
(windows) ci: windows build with TLS (mbedtls) + verify that we can be build with OpenSSL
## [9.1.9] - 2020-03-30
(cobra to statsd bot) add ability to extract a numerical value and send a timer event to statsd, with the --timer option
## [9.1.8] - 2020-03-29
(cobra to statsd bot) bot init was missing + capture socket error
## [9.1.7] - 2020-03-29
(cobra to statsd bot) add ability to extract a numerical value and send a gauge event to statsd, with the --gauge option
## [9.1.6] - 2020-03-29
(ws cobra subscriber) use a Json::StreamWriter to write to std::cout, and save one std::string allocation for each message printed
## [9.1.5] - 2020-03-29
(docker) trim down docker image (300M -> 12M) / binary built without symbol and size optimization, and source code not copied over
## [9.1.4] - 2020-03-28
(jsoncpp) update bundled copy to version 1.9.3 (at sha 3beb37ea14aec1bdce1a6d542dc464d00f4a6cec)
## [9.1.3] - 2020-03-27
(docker) alpine docker build with release with debug info, and bundle ca-certificates
## [9.1.2] - 2020-03-26
(mac ssl) rename DarwinSSL -> SecureTransport (see this too -> https://github.com/curl/curl/issues/3733)
## [9.1.1] - 2020-03-26
(websocket) fix data race accessing _socket object without mutex protection when calling wakeUpFromPoll in WebSocketTransport.cpp
## [9.1.0] - 2020-03-26
(ixcobra) add explicit event types for handshake, authentication and subscription failure, and handle those by exiting in ws_cobra_subcribe and friends
## [9.0.3] - 2020-03-24
(ws connect) display statistics about how much time it takes to stop the connection
## [9.0.2] - 2020-03-24
(socket) works with unique_ptr<Socket> instead of shared_ptr<Socket> in many places
## [9.0.1] - 2020-03-24
(socket) selectInterrupt member is an unique_ptr instead of being a shared_ptr
## [9.0.0] - 2020-03-23
(websocket) reset per-message deflate codec everytime we connect to a server/client
## [8.3.4] - 2020-03-23
(websocket) fix #167, a long standing issue with sending empty messages with per-message deflate extension (and hopefully other zlib bug)
## [8.3.3] - 2020-03-22
(cobra to statsd) port to windows and add a unittest
## [8.3.2] - 2020-03-20
(websocket+tls) fix hang in tls handshake which could lead to ANR, discovered through unittesting.
## [8.3.1] - 2020-03-20
(cobra) CobraMetricsPublisher can be configure with an ix::CobraConfig + more unittest use SSL in server + client
## [8.3.0] - 2020-03-18
(websocket) Simplify ping/pong based heartbeat implementation
## [8.2.7] - 2020-03-17
(ws) ws connect gains a new option to set the interval at which to send pings
(ws) ws echo_server gains a new option (-p) to disable responding to pings with pongs
```
IXWebSocket$ ws connect --ping_interval 2 wss://echo.websocket.org
Type Ctrl-D to exit prompt...
Connecting to url: wss://echo.websocket.org
> ws_connect: connected
[2020-03-17 23:53:02.726] [info] Uri: /
[2020-03-17 23:53:02.726] [info] Headers:
[2020-03-17 23:53:02.727] [info] Connection: Upgrade
[2020-03-17 23:53:02.727] [info] Date: Wed, 18 Mar 2020 06:45:05 GMT
[2020-03-17 23:53:02.727] [info] Sec-WebSocket-Accept: 0gtqbxW0aVL/QI/ICpLFnRaiKgA=
[2020-03-17 23:53:02.727] [info] sec-websocket-extensions:
[2020-03-17 23:53:02.727] [info] Server: Kaazing Gateway
[2020-03-17 23:53:02.727] [info] Upgrade: websocket
[2020-03-17 23:53:04.894] [info] Received pong
[2020-03-17 23:53:06.859] [info] Received pong
[2020-03-17 23:53:08.881] [info] Received pong
[2020-03-17 23:53:10.848] [info] Received pong
[2020-03-17 23:53:12.898] [info] Received pong
[2020-03-17 23:53:14.865] [info] Received pong
[2020-03-17 23:53:16.890] [info] Received pong
[2020-03-17 23:53:18.853] [info] Received pong
[2020-03-17 23:53:19.388] [info]
ws_connect: connection closed: code 1000 reason Normal closure
[2020-03-17 23:53:19.502] [info] Received 208 bytes
[2020-03-17 23:53:19.502] [info] Sent 0 bytes
```
## [8.2.6] - 2020-03-16
(cobra to sentry bot + docker) default docker file uses mbedtls + ws cobra_to_sentry pass tls options to sentryClient.
## [8.2.5] - 2020-03-13
(cobra client) ws cobra subscribe resubscribe at latest position after being disconnected
## [8.2.4] - 2020-03-13
(cobra client) can subscribe with a position
## [8.2.3] - 2020-03-13
(cobra client) pass the message position to the subscription data callback
## [8.2.2] - 2020-03-12
(openssl tls backend) Fix a hand in OpenSSL when using TLS v1.3 ... by disabling TLS v1.3
## [8.2.1] - 2020-03-11
(cobra) IXCobraConfig struct has tlsOptions and per message deflate options
## [8.2.0] - 2020-03-11
(cobra) add IXCobraConfig struct to pass cobra config around
## [8.1.9] - 2020-03-09
(ws cobra_subscribe) add a --fluentd option to wrap a message in an enveloppe so that fluentd can recognize it
## [8.1.8] - 2020-03-02 ## [8.1.8] - 2020-03-02
(websocket server) fix regression with disabling zlib extension on the server side. If a client does not support this extension the server will handle it fine. We still need to figure out how to disable the option. (websocket server) fix regression with disabling zlib extension on the server side. If a client does not support this extension the server will handle it fine. We still need to figure out how to disable the option.

View File

@ -6,9 +6,7 @@ The per message deflate compression option is supported. It can lead to very nic
### TLS/SSL ### TLS/SSL
Connections can be optionally secured and encrypted with TLS/SSL when using a wss:// endpoint, or using normal un-encrypted socket with ws:// endpoints. AppleSSL is used on iOS and macOS, OpenSSL and mbedTLS can be used on Android, Linux and Windows. Connections can be optionally secured and encrypted with TLS/SSL when using a wss:// endpoint, or using normal un-encrypted socket with ws:// endpoints. AppleSSL is used on iOS and macOS, OpenSSL is used on Android and Linux, mbedTLS is used on Windows.
If you are using OpenSSL, try to be on a version higher than 1.1.x as there there are thread safety problems with 1.0.x.
### Polling and background thread work ### Polling and background thread work
@ -28,13 +26,7 @@ The library has an interactive tool which is handy for testing compatibility ith
The unittest tries to be comprehensive, and has been running on multiple platforms, with different sanitizers such as a thread sanitizer to catch data races or the undefined behavior sanitizer. The unittest tries to be comprehensive, and has been running on multiple platforms, with different sanitizers such as a thread sanitizer to catch data races or the undefined behavior sanitizer.
The regression test is running after each commit on github actions for multiple configurations. The regression test is running after each commit on travis.
* Linux
* macOS with thread sanitizer
* macOS, with OpenSSL, with thread sanitizer
* macOS, with MbedTLS, with thread sanitizer
* Windows, with MbedTLS (the unittest is not run yet)
## Limitations ## Limitations
@ -81,3 +73,5 @@ Here is a simplistic diagram which explains how the code is structured in term o
| | | |
+-----------------------+ +-----------------------+
``` ```

View File

@ -1,4 +1,4 @@
![Build status](https://github.com/machinezone/IXWebSocket/workflows/unittest/badge.svg) ![Alt text](https://travis-ci.org/machinezone/IXWebSocket.svg?branch=master)
## Introduction ## Introduction
@ -13,7 +13,7 @@
## Example code ## Example code
```c++ ```cpp
// Required on Windows // Required on Windows
ix::initNetSystem(); ix::initNetSystem();
@ -44,22 +44,7 @@ webSocket.send("hello world");
There are 2 main reasons that explain why IXWebSocket got written. First, we needed a C++ cross-platform client library, which should have few dependencies. What looked like the most solid one, [websocketpp](https://github.com/zaphoyd/websocketpp) did depend on boost and this was not an option for us. Secondly, there were other available libraries with fewer dependencies (C ones), but they required calling an explicit poll routine periodically to know if a client had received data from a server, which was not elegant. There are 2 main reasons that explain why IXWebSocket got written. First, we needed a C++ cross-platform client library, which should have few dependencies. What looked like the most solid one, [websocketpp](https://github.com/zaphoyd/websocketpp) did depend on boost and this was not an option for us. Secondly, there were other available libraries with fewer dependencies (C ones), but they required calling an explicit poll routine periodically to know if a client had received data from a server, which was not elegant.
We started by solving those 2 problems, then we added server websocket code, then an HTTP client, and finally a very simple HTTP server. IXWebSocket comes with a command line utility named ws which is quite handy, and is now packaged with alpine linux. You can install it with `apk add ws`. We started by solving those 2 problems, then we added server websocket code, then an HTTP client, and finally a very simple HTTP server.
* Few dependencies (only zlib)
* Simple to use ; uses std::string and std::function callbacks.
* Complete support of the websocket protocol, and basic http support.
* Client and Server
* TLS support
## Alternative libraries
There are plenty of great websocket libraries out there, which might work for you. Here are a couple of serious ones.
* [websocketpp](https://github.com/zaphoyd/websocketpp) - C++
* [beast](https://github.com/boostorg/beast) - C++
* [libwebsockets](https://libwebsockets.org/) - C
* [µWebSockets](https://github.com/uNetworking/uWebSockets) - C
## Contributing ## Contributing

View File

@ -1,94 +0,0 @@
Notes on how we can update the different packages for ixwebsocket.
## VCPKG
Visit the [releases](https://github.com/machinezone/IXWebSocket/releases) page on Github. A tag must have been made first.
Download the latest entry.
```
$ cd /tmp
/tmp$ curl -s -O -L https://github.com/machinezone/IXWebSocket/archive/v9.1.9.tar.gz
/tmp$
/tmp$ openssl sha512 v9.1.9.tar.gz
SHA512(v9.1.9.tar.gz)= f1fd731b5f6a9ce6d6d10bee22a5d9d9baaa8ea0564d6c4cd7eb91dcb88a45c49b2c7fdb75f8640a3589c1b30cee33ef5df8dcbb55920d013394d1e33ddd3c8e
```
Now go punch those values in the vcpkg ixwebsocket port config files. Here is what the diff look like.
```
vcpkg$ git diff
diff --git a/ports/ixwebsocket/CONTROL b/ports/ixwebsocket/CONTROL
index db9c2adc9..4acae5c3f 100644
--- a/ports/ixwebsocket/CONTROL
+++ b/ports/ixwebsocket/CONTROL
@@ -1,5 +1,5 @@
Source: ixwebsocket
-Version: 8.0.5
+Version: 9.1.9
Build-Depends: zlib
Homepage: https://github.com/machinezone/IXWebSocket
Description: Lightweight WebSocket Client and Server + HTTP Client and Server
diff --git a/ports/ixwebsocket/portfile.cmake b/ports/ixwebsocket/portfile.cmake
index de082aece..68e523a05 100644
--- a/ports/ixwebsocket/portfile.cmake
+++ b/ports/ixwebsocket/portfile.cmake
@@ -1,8 +1,8 @@
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO machinezone/IXWebSocket
- REF v8.0.5
- SHA512 9dcc20d9a0629b92c62a68a8bd7c8206f18dbd9e93289b0b687ec13c478ce9ad1f3563b38c399c8277b0d3812cc78ca725786ba1dedbc3445b9bdb9b689e8add
+ REF v9.1.9
+ SHA512 f1fd731b5f6a9ce6d6d10bee22a5d9d9baaa8ea0564d6c4cd7eb91dcb88a45c49b2c7fdb75f8640a3589c1b30cee33ef5df8dcbb55920d013394d1e33ddd3c8e
)
```
You will need a fork of the vcpkg repo to make a pull request.
```
git fetch upstream
git co master
git reset --hard upstream/master
git push origin master --force
```
Make the pull request (I use a new branch to do that).
```
vcpkg$ git co -b feature/ixwebsocket_9.1.9
M ports/ixwebsocket/CONTROL
M ports/ixwebsocket/portfile.cmake
Switched to a new branch 'feature/ixwebsocket_9.1.9'
vcpkg$
vcpkg$
vcpkg$ git commit -am 'ixwebsocket: update to 9.1.9'
[feature/ixwebsocket_9.1.9 8587a4881] ixwebsocket: update to 9.1.9
2 files changed, 3 insertions(+), 3 deletions(-)
vcpkg$
vcpkg$ git push
fatal: The current branch feature/ixwebsocket_9.1.9 has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/ixwebsocket_9.1.9
vcpkg$ git push --set-upstream origin feature/ixwebsocket_9.1.9
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 621 bytes | 621.00 KiB/s, done.
Total 6 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 4 local objects.
remote:
remote: Create a pull request for 'feature/ixwebsocket_9.1.9' on GitHub by visiting:
remote: https://github.com/bsergean/vcpkg/pull/new/feature/ixwebsocket_9.1.9
remote:
To https://github.com/bsergean/vcpkg.git
* [new branch] feature/ixwebsocket_9.1.9 -> feature/ixwebsocket_9.1.9
Branch 'feature/ixwebsocket_9.1.9' set up to track remote branch 'feature/ixwebsocket_9.1.9' from 'origin' by rebasing.
vcpkg$
```
Just visit this url, https://github.com/bsergean/vcpkg/pull/new/feature/ixwebsocket_9.1.9, printed on the console, to make the pull request.

View File

@ -35,7 +35,7 @@ webSocket.setUrl(url);
// Optional heart beat, sent every 45 seconds when there is not any traffic // Optional heart beat, sent every 45 seconds when there is not any traffic
// to make sure that load balancers do not kill an idle connection. // to make sure that load balancers do not kill an idle connection.
webSocket.setPingInterval(45); webSocket.setHeartBeatPeriod(45);
// Per message deflate connection is enabled by default. You can tweak its parameters or disable it // Per message deflate connection is enabled by default. You can tweak its parameters or disable it
webSocket.disablePerMessageDeflate(); webSocket.disablePerMessageDeflate();
@ -174,7 +174,7 @@ when there is no any traffic to make sure that load balancers do not kill an
idle connection. idle connection.
```cpp ```cpp
webSocket.setPingInterval(45); webSocket.setHeartBeatPeriod(45);
``` ```
### Supply extra HTTP headers. ### Supply extra HTTP headers.
@ -244,6 +244,39 @@ webSocket.setMaxWaitBetweenReconnectionRetries(5 * 1000); // 5000ms = 5s
uint32_t m = webSocket.getMaxWaitBetweenReconnectionRetries(); uint32_t m = webSocket.getMaxWaitBetweenReconnectionRetries();
``` ```
### TLS support and configuration
To leverage TLS features, the library must be compiled with the option `USE_TLS=1`.
Then, secure sockets are automatically used when connecting to a `wss://*` url.
Additional TLS options can be configured by passing a `ix::SocketTLSOptions` instance to the
`setTLSOptions` on `ix::WebSocket` (or `ix::WebSocketServer` or `ix::HttpServer`)
```cpp
webSocket.setTLSOptions({
.certFile = "path/to/cert/file.pem",
.keyFile = "path/to/key/file.pem",
.caFile = "path/to/trust/bundle/file.pem"
});
```
Specifying `certFile` and `keyFile` configures the certificate that will be used to communicate with TLS peers.
On a client, this is only necessary for connecting to servers that require a client certificate.
On a server, this is necessary for TLS support.
Specifying `caFile` configures the trusted roots bundle file (in PEM format) that will be used to verify peer certificates.
- The special value of `SYSTEM` (the default) indicates that the system-configured trust bundle should be used; this is generally what you want when connecting to any publicly exposed API/server.
- The special value of `NONE` can be used to disable peer verification; this is only recommended to rule out certificate verification when testing connectivity.
For a client, specifying `caFile` can be used if connecting to a server that uses a self-signed cert, or when using a custom CA in an internal environment.
For a server, specifying `caFile` implies that:
1. You require clients to present a certificate
1. It must be signed by one of the trusted roots in the file
## WebSocket server API ## WebSocket server API
```cpp ```cpp
@ -431,39 +464,3 @@ setOnConnectionCallback(
content); content);
} }
``` ```
## TLS support and configuration
To leverage TLS features, the library must be compiled with the option `USE_TLS=1`.
If you are using OpenSSL, try to be on a version higher than 1.1.x as there there are thread safety problems with 1.0.x.
Then, secure sockets are automatically used when connecting to a `wss://*` url.
Additional TLS options can be configured by passing a `ix::SocketTLSOptions` instance to the
`setTLSOptions` on `ix::WebSocket` (or `ix::WebSocketServer` or `ix::HttpServer`)
```cpp
webSocket.setTLSOptions({
.certFile = "path/to/cert/file.pem",
.keyFile = "path/to/key/file.pem",
.caFile = "path/to/trust/bundle/file.pem",
.tls = true // required in server mode
});
```
Specifying `certFile` and `keyFile` configures the certificate that will be used to communicate with TLS peers.
On a client, this is only necessary for connecting to servers that require a client certificate.
On a server, this is necessary for TLS support.
Specifying `caFile` configures the trusted roots bundle file (in PEM format) that will be used to verify peer certificates.
- The special value of `SYSTEM` (the default) indicates that the system-configured trust bundle should be used; this is generally what you want when connecting to any publicly exposed API/server.
- The special value of `NONE` can be used to disable peer verification; this is only recommended to rule out certificate verification when testing connectivity.
For a client, specifying `caFile` can be used if connecting to a server that uses a self-signed cert, or when using a custom CA in an internal environment.
For a server, specifying `caFile` implies that:
1. You require clients to present a certificate
1. It must be signed by one of the trusted roots in the file

View File

@ -1,49 +0,0 @@
#
# Author: Benjamin Sergeant
# Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
#
set (IXBOTS_SOURCES
ixbots/IXCobraBot.cpp
ixbots/IXCobraToSentryBot.cpp
ixbots/IXCobraToStatsdBot.cpp
ixbots/IXCobraToStdoutBot.cpp
ixbots/IXQueueManager.cpp
ixbots/IXStatsdClient.cpp
)
set (IXBOTS_HEADERS
ixbots/IXCobraBot.h
ixbots/IXCobraToSentryBot.h
ixbots/IXCobraToStatsdBot.h
ixbots/IXCobraToStdoutBot.h
ixbots/IXQueueManager.h
ixbots/IXStatsdClient.h
)
add_library(ixbots STATIC
${IXBOTS_SOURCES}
${IXBOTS_HEADERS}
)
find_package(JsonCpp)
if (NOT JSONCPP_FOUND)
set(JSONCPP_INCLUDE_DIRS ../third_party/jsoncpp)
endif()
find_package(SpdLog)
if (NOT SPDLOG_FOUND)
set(SPDLOG_INCLUDE_DIRS ../third_party/spdlog/include)
endif()
set(IXBOTS_INCLUDE_DIRS
.
..
../ixcore
../ixwebsocket
../ixcobra
../ixsentry
${JSONCPP_INCLUDE_DIRS}
${SPDLOG_INCLUDE_DIRS})
target_include_directories( ixbots PUBLIC ${IXBOTS_INCLUDE_DIRS} )

View File

@ -1,303 +0,0 @@
/*
* IXCobraBot.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#include "IXCobraBot.h"
#include "IXQueueManager.h"
#include <algorithm>
#include <chrono>
#include <ixcobra/IXCobraConnection.h>
#include <spdlog/spdlog.h>
#include <sstream>
#include <thread>
#include <vector>
namespace ix
{
int64_t CobraBot::run(const CobraConfig& config,
const std::string& channel,
const std::string& filter,
const std::string& position,
bool verbose,
size_t maxQueueSize,
bool useQueue,
bool enableHeartbeat,
int runtime)
{
ix::CobraConnection conn;
conn.configure(config);
conn.connect();
Json::FastWriter jsonWriter;
std::atomic<uint64_t> sentCount(0);
std::atomic<uint64_t> receivedCount(0);
std::atomic<uint64_t> sentCountTotal(0);
std::atomic<uint64_t> receivedCountTotal(0);
std::atomic<uint64_t> sentCountPerSecs(0);
std::atomic<uint64_t> receivedCountPerSecs(0);
std::atomic<bool> stop(false);
std::atomic<bool> throttled(false);
std::atomic<bool> fatalCobraError(false);
QueueManager queueManager(maxQueueSize);
auto timer = [&sentCount,
&receivedCount,
&sentCountTotal,
&receivedCountTotal,
&sentCountPerSecs,
&receivedCountPerSecs,
&stop] {
while (!stop)
{
//
// We cannot write to sentCount and receivedCount
// as those are used externally, so we need to introduce
// our own counters
//
spdlog::info("messages received {} {} sent {} {}",
receivedCountPerSecs,
receivedCountTotal,
sentCountPerSecs,
sentCountTotal);
receivedCountPerSecs = receivedCount - receivedCountTotal;
sentCountPerSecs = sentCount - receivedCountTotal;
receivedCountTotal += receivedCountPerSecs;
sentCountTotal += sentCountPerSecs;
auto duration = std::chrono::seconds(1);
std::this_thread::sleep_for(duration);
}
spdlog::info("timer thread done");
};
std::thread t1(timer);
auto heartbeat = [&sentCount, &receivedCount, &stop, &enableHeartbeat] {
std::string state("na");
if (!enableHeartbeat) return;
while (!stop)
{
std::stringstream ss;
ss << "messages received " << receivedCount;
ss << "messages sent " << sentCount;
std::string currentState = ss.str();
if (currentState == state)
{
spdlog::error("no messages received or sent for 1 minute, exiting");
exit(1);
}
state = currentState;
auto duration = std::chrono::minutes(1);
std::this_thread::sleep_for(duration);
}
spdlog::info("heartbeat thread done");
};
std::thread t2(heartbeat);
auto sender =
[this, &queueManager, verbose, &sentCount, &stop, &throttled, &fatalCobraError] {
while (true)
{
auto data = queueManager.pop();
Json::Value msg = data.first;
std::string position = data.second;
if (stop) break;
if (msg.isNull()) continue;
if (_onBotMessageCallback && _onBotMessageCallback(msg, position, verbose, throttled, fatalCobraError))
{
// That might be too noisy
if (verbose)
{
spdlog::info("cobra bot: sending succesfull");
}
++sentCount;
}
else
{
spdlog::error("cobra bot: error sending");
}
if (stop) break;
}
spdlog::info("sender thread done");
};
std::thread t3(sender);
std::string subscriptionPosition(position);
conn.setEventCallback([this,
&conn,
&channel,
&filter,
&subscriptionPosition,
&jsonWriter,
verbose,
&throttled,
&receivedCount,
&fatalCobraError,
&useQueue,
&queueManager,
&sentCount](const CobraEventPtr& event)
{
if (event->type == ix::CobraEventType::Open)
{
spdlog::info("Subscriber connected");
for (auto&& it : event->headers)
{
spdlog::info("{}: {}", it.first, it.second);
}
}
else if (event->type == ix::CobraEventType::Closed)
{
spdlog::info("Subscriber closed: {}", event->errMsg);
}
else if (event->type == ix::CobraEventType::Authenticated)
{
spdlog::info("Subscriber authenticated");
spdlog::info("Subscribing to {} at position {}", channel, subscriptionPosition);
spdlog::info("Using filter: {}", filter);
conn.subscribe(channel,
filter,
subscriptionPosition,
[this, &jsonWriter, verbose, &throttled, &receivedCount, &queueManager, &useQueue, &subscriptionPosition, &fatalCobraError, &sentCount](
const Json::Value& msg, const std::string& position) {
if (verbose)
{
spdlog::info("Subscriber received message {} -> {}", position, jsonWriter.write(msg));
}
subscriptionPosition = position;
// If we cannot send to sentry fast enough, drop the message
if (throttled)
{
return;
}
++receivedCount;
if (useQueue)
{
queueManager.add(msg, position);
}
else
{
if (_onBotMessageCallback && _onBotMessageCallback(msg, position, verbose, throttled, fatalCobraError))
{
// That might be too noisy
if (verbose)
{
spdlog::info("cobra bot: sending succesfull");
}
++sentCount;
}
else
{
spdlog::error("cobra bot: error sending");
}
}
});
}
else if (event->type == ix::CobraEventType::Subscribed)
{
spdlog::info("Subscriber: subscribed to channel {}", event->subscriptionId);
}
else if (event->type == ix::CobraEventType::UnSubscribed)
{
spdlog::info("Subscriber: unsubscribed from channel {}", event->subscriptionId);
}
else if (event->type == ix::CobraEventType::Error)
{
spdlog::error("Subscriber: error {}", event->errMsg);
}
else if (event->type == ix::CobraEventType::Published)
{
spdlog::error("Published message hacked: {}", event->msgId);
}
else if (event->type == ix::CobraEventType::Pong)
{
spdlog::info("Received websocket pong: {}", event->errMsg);
}
else if (event->type == ix::CobraEventType::HandshakeError)
{
spdlog::error("Subscriber: Handshake error: {}", event->errMsg);
fatalCobraError = true;
}
else if (event->type == ix::CobraEventType::AuthenticationError)
{
spdlog::error("Subscriber: Authentication error: {}", event->errMsg);
fatalCobraError = true;
}
else if (event->type == ix::CobraEventType::SubscriptionError)
{
spdlog::error("Subscriber: Subscription error: {}", event->errMsg);
fatalCobraError = true;
}
});
// Run forever
if (runtime == -1)
{
while (true)
{
auto duration = std::chrono::seconds(1);
std::this_thread::sleep_for(duration);
if (fatalCobraError) break;
}
}
// Run for a duration, used by unittesting now
else
{
for (int i = 0 ; i < runtime; ++i)
{
auto duration = std::chrono::seconds(1);
std::this_thread::sleep_for(duration);
if (fatalCobraError) break;
}
}
//
// Cleanup.
// join all the bg threads and stop them.
//
conn.disconnect();
stop = true;
// progress thread
t1.join();
// heartbeat thread
if (t2.joinable()) t2.join();
// sentry sender thread
t3.join();
return fatalCobraError ? -1 : (int64_t) sentCount;
}
void CobraBot::setOnBotMessageCallback(const OnBotMessageCallback& callback)
{
_onBotMessageCallback = callback;
}
}

View File

@ -1,43 +0,0 @@
/*
* IXCobraBot.h
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <ixcobra/IXCobraConfig.h>
#include <stddef.h>
#include <json/json.h>
#include <functional>
#include <atomic>
namespace ix
{
using OnBotMessageCallback = std::function<bool(const Json::Value&,
const std::string&,
const bool verbose,
std::atomic<bool>&,
std::atomic<bool>&)>;
class CobraBot
{
public:
CobraBot() = default;
int64_t run(const CobraConfig& config,
const std::string& channel,
const std::string& filter,
const std::string& position,
bool verbose,
size_t maxQueueSize,
bool useQueue,
bool enableHeartbeat,
int runtime);
void setOnBotMessageCallback(const OnBotMessageCallback& callback);
private:
OnBotMessageCallback _onBotMessageCallback;
};
}

View File

@ -1,117 +0,0 @@
/*
* IXCobraToSentryBot.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include "IXCobraToSentryBot.h"
#include "IXCobraBot.h"
#include "IXQueueManager.h"
#include <chrono>
#include <ixcobra/IXCobraConnection.h>
#include <spdlog/spdlog.h>
#include <sstream>
#include <vector>
namespace ix
{
int64_t cobra_to_sentry_bot(const CobraConfig& config,
const std::string& channel,
const std::string& filter,
const std::string& position,
SentryClient& sentryClient,
bool verbose,
size_t maxQueueSize,
bool enableHeartbeat,
int runtime)
{
CobraBot bot;
bot.setOnBotMessageCallback([&sentryClient](const Json::Value& msg,
const std::string& /*position*/,
const bool verbose,
std::atomic<bool>& throttled,
std::atomic<bool>& /*fatalCobraError*/) -> bool {
auto ret = sentryClient.send(msg, verbose);
HttpResponsePtr response = ret.first;
if (!response)
{
spdlog::warn("Null HTTP Response");
return false;
}
if (verbose)
{
for (auto it : response->headers)
{
spdlog::info("{}: {}", it.first, it.second);
}
spdlog::info("Upload size: {}", response->uploadSize);
spdlog::info("Download size: {}", response->downloadSize);
spdlog::info("Status: {}", response->statusCode);
if (response->errorCode != HttpErrorCode::Ok)
{
spdlog::info("error message: {}", response->errorMsg);
}
if (response->headers["Content-Type"] != "application/octet-stream")
{
spdlog::info("payload: {}", response->payload);
}
}
bool success = response->statusCode == 200;
if (!success)
{
spdlog::error("Error sending data to sentry: {}", response->statusCode);
spdlog::error("Body: {}", ret.second);
spdlog::error("Response: {}", response->payload);
// Error 429 Too Many Requests
if (response->statusCode == 429)
{
auto retryAfter = response->headers["Retry-After"];
std::stringstream ss;
ss << retryAfter;
int seconds;
ss >> seconds;
if (!ss.eof() || ss.fail())
{
seconds = 30;
spdlog::warn("Error parsing Retry-After header. "
"Using {} for the sleep duration",
seconds);
}
spdlog::warn("Error 429 - Too Many Requests. ws will sleep "
"and retry after {} seconds",
retryAfter);
throttled = true;
auto duration = std::chrono::seconds(seconds);
std::this_thread::sleep_for(duration);
throttled = false;
}
}
return success;
});
bool useQueue = true;
return bot.run(config,
channel,
filter,
position,
verbose,
maxQueueSize,
useQueue,
enableHeartbeat,
runtime);
}
} // namespace ix

View File

@ -1,24 +0,0 @@
/*
* IXCobraToSentryBot.h
* Author: Benjamin Sergeant
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <ixcobra/IXCobraConfig.h>
#include <ixsentry/IXSentryClient.h>
#include <string>
#include <cstdint>
namespace ix
{
int64_t cobra_to_sentry_bot(const CobraConfig& config,
const std::string& channel,
const std::string& filter,
const std::string& position,
SentryClient& sentryClient,
bool verbose,
size_t maxQueueSize,
bool enableHeartbeat,
int runtime);
} // namespace ix

View File

@ -1,157 +0,0 @@
/*
* IXCobraToStatsdBot.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include "IXCobraToStatsdBot.h"
#include "IXQueueManager.h"
#include "IXStatsdClient.h"
#include "IXCobraBot.h"
#include <chrono>
#include <ixcobra/IXCobraConnection.h>
#include <spdlog/spdlog.h>
#include <sstream>
#include <vector>
namespace ix
{
// fields are command line argument that can be specified multiple times
std::vector<std::string> parseFields(const std::string& fields)
{
std::vector<std::string> tokens;
// Split by \n
std::string token;
std::stringstream tokenStream(fields);
while (std::getline(tokenStream, token))
{
tokens.push_back(token);
}
return tokens;
}
//
// Extract an attribute from a Json Value.
// extractAttr("foo.bar", {"foo": {"bar": "baz"}}) => baz
//
Json::Value extractAttr(const std::string& attr, const Json::Value& jsonValue)
{
// Split by .
std::string token;
std::stringstream tokenStream(attr);
Json::Value val(jsonValue);
while (std::getline(tokenStream, token, '.'))
{
val = val[token];
}
return val;
}
int64_t cobra_to_statsd_bot(const ix::CobraConfig& config,
const std::string& channel,
const std::string& filter,
const std::string& position,
StatsdClient& statsdClient,
const std::string& fields,
const std::string& gauge,
const std::string& timer,
bool verbose,
size_t maxQueueSize,
bool enableHeartbeat,
int runtime)
{
ix::CobraConnection conn;
conn.configure(config);
conn.connect();
auto tokens = parseFields(fields);
CobraBot bot;
bot.setOnBotMessageCallback([&statsdClient, &tokens, &gauge, &timer](const Json::Value& msg,
const std::string& /*position*/,
const bool verbose,
std::atomic<bool>& /*throttled*/,
std::atomic<bool>& fatalCobraError) -> bool {
std::string id;
for (auto&& attr : tokens)
{
id += ".";
auto val = extractAttr(attr, msg);
id += val.asString();
}
if (gauge.empty() && timer.empty())
{
statsdClient.count(id, 1);
}
else
{
std::string attrName = (!gauge.empty()) ? gauge : timer;
auto val = extractAttr(attrName, msg);
size_t x;
if (val.isInt())
{
x = (size_t) val.asInt();
}
else if (val.isInt64())
{
x = (size_t) val.asInt64();
}
else if (val.isUInt())
{
x = (size_t) val.asUInt();
}
else if (val.isUInt64())
{
x = (size_t) val.asUInt64();
}
else if (val.isDouble())
{
x = (size_t) val.asUInt64();
}
else
{
spdlog::error("Gauge {} is not a numeric type", gauge);
fatalCobraError = true;
return false;
}
if (verbose)
{
spdlog::info("{} - {} -> {}", id, attrName, x);
}
if (!gauge.empty())
{
statsdClient.gauge(id, x);
}
else
{
statsdClient.timing(id, x);
}
}
return true;
});
bool useQueue = true;
return bot.run(config,
channel,
filter,
position,
verbose,
maxQueueSize,
useQueue,
enableHeartbeat,
runtime);
}
} // namespace ix

View File

@ -1,28 +0,0 @@
/*
* IXCobraToStatsdBot.h
* Author: Benjamin Sergeant
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <ixcobra/IXCobraConfig.h>
#include <ixbots/IXStatsdClient.h>
#include <string>
#include <stddef.h>
#include <cstdint>
namespace ix
{
int64_t cobra_to_statsd_bot(const ix::CobraConfig& config,
const std::string& channel,
const std::string& filter,
const std::string& position,
StatsdClient& statsdClient,
const std::string& fields,
const std::string& gauge,
const std::string& timer,
bool verbose,
size_t maxQueueSize,
bool enableHeartbeat,
int runtime);
} // namespace ix

View File

@ -1,106 +0,0 @@
/*
* IXCobraToStdoutBot.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include "IXCobraToStdoutBot.h"
#include "IXCobraBot.h"
#include "IXQueueManager.h"
#include <chrono>
#include <spdlog/spdlog.h>
#include <sstream>
#include <iostream>
namespace ix
{
using StreamWriterPtr = std::unique_ptr<Json::StreamWriter>;
StreamWriterPtr makeStreamWriter()
{
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = ""; // will make the JSON object compact
std::unique_ptr<Json::StreamWriter> jsonWriter(builder.newStreamWriter());
return jsonWriter;
}
std::string timeSinceEpoch()
{
std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
std::chrono::system_clock::duration dtn = tp.time_since_epoch();
std::stringstream ss;
ss << dtn.count() * std::chrono::system_clock::period::num /
std::chrono::system_clock::period::den;
return ss.str();
}
void writeToStdout(bool fluentd,
const StreamWriterPtr& jsonWriter,
const Json::Value& msg,
const std::string& position)
{
Json::Value enveloppe;
if (fluentd)
{
enveloppe["producer"] = "cobra";
enveloppe["consumer"] = "fluentd";
Json::Value nestedMessage(msg);
nestedMessage["position"] = position;
nestedMessage["created_at"] = timeSinceEpoch();
enveloppe["message"] = nestedMessage;
jsonWriter->write(enveloppe, &std::cout);
std::cout << std::endl; // add lf and flush
}
else
{
enveloppe = msg;
std::cout << position << " ";
jsonWriter->write(enveloppe, &std::cout);
std::cout << std::endl;
}
}
int64_t cobra_to_stdout_bot(const CobraConfig& config,
const std::string& channel,
const std::string& filter,
const std::string& position,
bool fluentd,
bool quiet,
bool verbose,
size_t maxQueueSize,
bool enableHeartbeat,
int runtime)
{
CobraBot bot;
auto jsonWriter = makeStreamWriter();
bot.setOnBotMessageCallback([&fluentd, &quiet, &jsonWriter](const Json::Value& msg,
const std::string& position,
const bool /*verbose*/,
std::atomic<bool>& /*throttled*/,
std::atomic<bool>& /*fatalCobraError*/) -> bool {
if (!quiet)
{
writeToStdout(fluentd, jsonWriter, msg, position);
}
return true;
});
bool useQueue = false;
return bot.run(config,
channel,
filter,
position,
verbose,
maxQueueSize,
useQueue,
enableHeartbeat,
runtime);
}
} // namespace ix

View File

@ -1,25 +0,0 @@
/*
* IXCobraToStdoutBot.h
* Author: Benjamin Sergeant
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <ixcobra/IXCobraConfig.h>
#include <string>
#include <stddef.h>
#include <cstdint>
namespace ix
{
int64_t cobra_to_stdout_bot(const ix::CobraConfig& config,
const std::string& channel,
const std::string& filter,
const std::string& position,
bool fluentd,
bool quiet,
bool verbose,
size_t maxQueueSize,
bool enableHeartbeat,
int runtime);
} // namespace ix

View File

@ -1,66 +0,0 @@
/*
* IXQueueManager.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include "IXQueueManager.h"
#include <vector>
#include <algorithm>
namespace ix
{
std::pair<Json::Value, std::string> QueueManager::pop()
{
std::unique_lock<std::mutex> lock(_mutex);
if (_queues.empty())
{
Json::Value val;
return std::make_pair(val, std::string());
}
std::vector<std::string> games;
for (auto it : _queues)
{
games.push_back(it.first);
}
std::random_shuffle(games.begin(), games.end());
std::string game = games[0];
auto duration = std::chrono::seconds(1);
_condition.wait_for(lock, duration);
if (_queues[game].empty())
{
Json::Value val;
return std::make_pair(val, std::string());
}
auto msg = _queues[game].front();
_queues[game].pop();
return msg;
}
void QueueManager::add(const Json::Value& msg, const std::string& position)
{
std::unique_lock<std::mutex> lock(_mutex);
std::string game;
if (msg.isMember("device") && msg["device"].isMember("game"))
{
game = msg["device"]["game"].asString();
}
if (game.empty()) return;
// if the sending is not fast enough there is no point
// in queuing too many events.
if (_queues[game].size() < _maxQueueSize)
{
_queues[game].push(std::make_pair(msg, position));
_condition.notify_one();
}
}
}

View File

@ -1,35 +0,0 @@
/*
* IXQueueManager.h
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <stddef.h>
#include <json/json.h>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <map>
namespace ix
{
class QueueManager
{
public:
QueueManager(size_t maxQueueSize)
: _maxQueueSize(maxQueueSize)
{
}
std::pair<Json::Value, std::string> pop();
void add(const Json::Value& msg, const std::string& position);
private:
std::map<std::string, std::queue<std::pair<Json::Value, std::string>>> _queues;
std::mutex _mutex;
std::condition_variable _condition;
size_t _maxQueueSize;
};
}

View File

@ -1,152 +0,0 @@
/*
* Copyright (c) 2014, Rex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the {organization} nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* IXStatsdClient.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
// Adapted from statsd-client-cpp
// test with netcat as a server: `nc -ul 8125`
#include "IXStatsdClient.h"
#include <ixwebsocket/IXNetSystem.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
namespace ix
{
StatsdClient::StatsdClient(const std::string& host,
int port,
const std::string& prefix)
: _host(host)
, _port(port)
, _prefix(prefix)
, _stop(false)
{
_thread = std::thread([this]
{
while (!_stop)
{
flushQueue();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
}
StatsdClient::~StatsdClient()
{
_stop = true;
if (_thread.joinable()) _thread.join();
_socket.close();
}
bool StatsdClient::init(std::string& errMsg)
{
return _socket.init(_host, _port, errMsg);
}
/* will change the original string */
void StatsdClient::cleanup(std::string& key)
{
size_t pos = key.find_first_of(":|@");
while (pos != std::string::npos)
{
key[pos] = '_';
pos = key.find_first_of(":|@");
}
}
int StatsdClient::dec(const std::string& key)
{
return count(key, -1);
}
int StatsdClient::inc(const std::string& key)
{
return count(key, 1);
}
int StatsdClient::count(const std::string& key, size_t value)
{
return send(key, value, "c");
}
int StatsdClient::gauge(const std::string& key, size_t value)
{
return send(key, value, "g");
}
int StatsdClient::timing(const std::string& key, size_t ms)
{
return send(key, ms, "ms");
}
int StatsdClient::send(std::string key, size_t value, const std::string& type)
{
cleanup(key);
char buf[256];
snprintf(buf, sizeof(buf), "%s%s:%zd|%s\n",
_prefix.c_str(), key.c_str(), value, type.c_str());
enqueue(buf);
return 0;
}
void StatsdClient::enqueue(const std::string& message)
{
std::lock_guard<std::mutex> lock(_mutex);
_queue.push_back(message);
}
void StatsdClient::flushQueue()
{
std::lock_guard<std::mutex> lock(_mutex);
while (!_queue.empty())
{
auto message = _queue.front();
auto ret = _socket.sendto(message);
if (ret != 0)
{
std::cerr << "error: "
<< strerror(UdpSocket::getErrno())
<< std::endl;
}
_queue.pop_front();
}
}
} // end namespace ix

View File

@ -1,58 +0,0 @@
/*
* IXStatsdClient.h
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <ixwebsocket/IXUdpSocket.h>
#include <string>
#include <thread>
#include <deque>
#include <mutex>
#include <atomic>
namespace ix
{
class StatsdClient
{
public:
StatsdClient(const std::string& host="127.0.0.1",
int port=8125,
const std::string& prefix = "");
~StatsdClient();
bool init(std::string& errMsg);
int inc(const std::string& key);
int dec(const std::string& key);
int count(const std::string& key, size_t value);
int gauge(const std::string& key, size_t value);
int timing(const std::string& key, size_t ms);
private:
void enqueue(const std::string& message);
/* (Low Level Api) manually send a message
* type = "c", "g" or "ms"
*/
int send(std::string key, size_t value, const std::string& type);
void cleanup(std::string& key);
void flushQueue();
UdpSocket _socket;
std::string _host;
int _port;
std::string _prefix;
std::atomic<bool> _stop;
std::thread _thread;
std::mutex _mutex; // for the queue
std::deque<std::string> _queue;
};
} // end namespace ix

View File

@ -13,8 +13,6 @@ set (IXCOBRA_HEADERS
ixcobra/IXCobraConnection.h ixcobra/IXCobraConnection.h
ixcobra/IXCobraMetricsThreadedPublisher.h ixcobra/IXCobraMetricsThreadedPublisher.h
ixcobra/IXCobraMetricsPublisher.h ixcobra/IXCobraMetricsPublisher.h
ixcobra/IXCobraConfig.h
ixcobra/IXCobraEventType.h
) )
add_library(ixcobra STATIC add_library(ixcobra STATIC

View File

@ -1,35 +0,0 @@
/*
* IXCobraConfig.h
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
#include <ixwebsocket/IXSocketTLSOptions.h>
namespace ix
{
struct CobraConfig
{
std::string appkey;
std::string endpoint;
std::string rolename;
std::string rolesecret;
WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions;
SocketTLSOptions socketTLSOptions;
CobraConfig(const std::string& a = std::string(),
const std::string& e = std::string(),
const std::string& r = std::string(),
const std::string& s = std::string())
: appkey(a)
, endpoint(e)
, rolename(r)
, rolesecret(s)
{
;
}
};
} // namespace ix

View File

@ -87,7 +87,7 @@ namespace ix
_eventCallback = eventCallback; _eventCallback = eventCallback;
} }
void CobraConnection::invokeEventCallback(ix::CobraEventType eventType, void CobraConnection::invokeEventCallback(ix::CobraConnectionEventType eventType,
const std::string& errorMsg, const std::string& errorMsg,
const WebSocketHttpHeaders& headers, const WebSocketHttpHeaders& headers,
const std::string& subscriptionId, const std::string& subscriptionId,
@ -96,12 +96,7 @@ namespace ix
std::lock_guard<std::mutex> lock(_eventCallbackMutex); std::lock_guard<std::mutex> lock(_eventCallbackMutex);
if (_eventCallback) if (_eventCallback)
{ {
_eventCallback( _eventCallback(eventType, errorMsg, headers, subscriptionId, msgId);
std::make_unique<CobraEvent>(eventType,
errorMsg,
headers,
subscriptionId,
msgId));
} }
} }
@ -110,7 +105,7 @@ namespace ix
{ {
std::stringstream ss; std::stringstream ss;
ss << errorMsg << " : received pdu => " << serializedPdu; ss << errorMsg << " : received pdu => " << serializedPdu;
invokeEventCallback(ix::CobraEventType::Error, ss.str()); invokeEventCallback(ix::CobraConnection_EventType_Error, ss.str());
} }
void CobraConnection::disconnect() void CobraConnection::disconnect()
@ -129,7 +124,7 @@ namespace ix
std::stringstream ss; std::stringstream ss;
if (msg->type == ix::WebSocketMessageType::Open) if (msg->type == ix::WebSocketMessageType::Open)
{ {
invokeEventCallback(ix::CobraEventType::Open, invokeEventCallback(ix::CobraConnection_EventType_Open,
std::string(), std::string(),
msg->openInfo.headers); msg->openInfo.headers);
sendHandshakeMessage(); sendHandshakeMessage();
@ -141,7 +136,7 @@ namespace ix
std::stringstream ss; std::stringstream ss;
ss << "Close code " << msg->closeInfo.code; ss << "Close code " << msg->closeInfo.code;
ss << " reason " << msg->closeInfo.reason; ss << " reason " << msg->closeInfo.reason;
invokeEventCallback(ix::CobraEventType::Closed, invokeEventCallback(ix::CobraConnection_EventType_Closed,
ss.str()); ss.str());
} }
else if (msg->type == ix::WebSocketMessageType::Message) else if (msg->type == ix::WebSocketMessageType::Message)
@ -171,19 +166,17 @@ namespace ix
} }
else if (action == "auth/handshake/error") else if (action == "auth/handshake/error")
{ {
invokeEventCallback(ix::CobraEventType::HandshakeError, invokeErrorCallback("Handshake error", msg->str);
msg->str);
} }
else if (action == "auth/authenticate/ok") else if (action == "auth/authenticate/ok")
{ {
_authenticated = true; _authenticated = true;
invokeEventCallback(ix::CobraEventType::Authenticated); invokeEventCallback(ix::CobraConnection_EventType_Authenticated);
flushQueue(); flushQueue();
} }
else if (action == "auth/authenticate/error") else if (action == "auth/authenticate/error")
{ {
invokeEventCallback(ix::CobraEventType::AuthenticationError, invokeErrorCallback("Authentication error", msg->str);
msg->str);
} }
else if (action == "rtm/subscription/data") else if (action == "rtm/subscription/data")
{ {
@ -198,8 +191,7 @@ namespace ix
} }
else if (action == "rtm/subscribe/error") else if (action == "rtm/subscribe/error")
{ {
invokeEventCallback(ix::CobraEventType::SubscriptionError, invokeErrorCallback("Subscription error", msg->str);
msg->str);
} }
else if (action == "rtm/unsubscribe/ok") else if (action == "rtm/unsubscribe/ok")
{ {
@ -239,7 +231,7 @@ namespace ix
} }
else if (msg->type == ix::WebSocketMessageType::Pong) else if (msg->type == ix::WebSocketMessageType::Pong)
{ {
invokeEventCallback(ix::CobraEventType::Pong, msg->str); invokeEventCallback(ix::CobraConnection_EventType_Pong);
} }
}); });
} }
@ -278,16 +270,10 @@ namespace ix
// This should keep the connection open and prevent some load balancers such as // This should keep the connection open and prevent some load balancers such as
// the Amazon one from shutting it down // the Amazon one from shutting it down
_webSocket->setPingInterval(kPingIntervalSecs); _webSocket->setPingInterval(kPingIntervalSecs);
}
void CobraConnection::configure(const ix::CobraConfig& config) // If we don't receive a pong back, declare loss after 3 * N seconds
{ // (will be 90s now), and close and restart the connection
configure(config.appkey, _webSocket->setPingTimeout(3 * kPingIntervalSecs);
config.endpoint,
config.rolename,
config.rolesecret,
config.webSocketPerMessageDeflateOptions,
config.socketTLSOptions);
} }
// //
@ -401,7 +387,7 @@ namespace ix
if (!subscriptionId.isString()) return false; if (!subscriptionId.isString()) return false;
invokeEventCallback(ix::CobraEventType::Subscribed, invokeEventCallback(ix::CobraConnection_EventType_Subscribed,
std::string(), WebSocketHttpHeaders(), std::string(), WebSocketHttpHeaders(),
subscriptionId.asString()); subscriptionId.asString());
return true; return true;
@ -419,7 +405,7 @@ namespace ix
if (!subscriptionId.isString()) return false; if (!subscriptionId.isString()) return false;
invokeEventCallback(ix::CobraEventType::UnSubscribed, invokeEventCallback(ix::CobraConnection_EventType_UnSubscribed,
std::string(), WebSocketHttpHeaders(), std::string(), WebSocketHttpHeaders(),
subscriptionId.asString()); subscriptionId.asString());
return true; return true;
@ -445,12 +431,9 @@ namespace ix
if (!body.isMember("messages")) return false; if (!body.isMember("messages")) return false;
Json::Value messages = body["messages"]; Json::Value messages = body["messages"];
if (!body.isMember("position")) return false;
std::string position = body["position"].asString();
for (auto&& msg : messages) for (auto&& msg : messages)
{ {
cb->second(msg, position); cb->second(msg);
} }
return true; return true;
@ -467,7 +450,7 @@ namespace ix
uint64_t msgId = id.asUInt64(); uint64_t msgId = id.asUInt64();
invokeEventCallback(ix::CobraEventType::Published, invokeEventCallback(ix::CobraConnection_EventType_Published,
std::string(), WebSocketHttpHeaders(), std::string(), WebSocketHttpHeaders(),
std::string(), msgId); std::string(), msgId);
@ -569,7 +552,6 @@ namespace ix
void CobraConnection::subscribe(const std::string& channel, void CobraConnection::subscribe(const std::string& channel,
const std::string& filter, const std::string& filter,
const std::string& position,
SubscriptionCallback cb) SubscriptionCallback cb)
{ {
// Create and send a subscribe pdu // Create and send a subscribe pdu
@ -581,11 +563,6 @@ namespace ix
body["filter"] = filter; body["filter"] = filter;
} }
if (!position.empty())
{
body["position"] = position;
}
Json::Value pdu; Json::Value pdu;
pdu["action"] = "rtm/subscribe"; pdu["action"] = "rtm/subscribe";
pdu["body"] = body; pdu["body"] = body;

View File

@ -8,8 +8,6 @@
#include <ixwebsocket/IXWebSocketHttpHeaders.h> #include <ixwebsocket/IXWebSocketHttpHeaders.h>
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h> #include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
#include "IXCobraEventType.h"
#include "IXCobraEvent.h"
#include <json/json.h> #include <json/json.h>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
@ -19,25 +17,35 @@
#include <unordered_map> #include <unordered_map>
#include <limits> #include <limits>
#include "IXCobraConfig.h"
#ifdef max
#undef max
#endif
namespace ix namespace ix
{ {
class WebSocket; class WebSocket;
struct SocketTLSOptions; struct SocketTLSOptions;
enum CobraConnectionEventType
{
CobraConnection_EventType_Authenticated = 0,
CobraConnection_EventType_Error = 1,
CobraConnection_EventType_Open = 2,
CobraConnection_EventType_Closed = 3,
CobraConnection_EventType_Subscribed = 4,
CobraConnection_EventType_UnSubscribed = 5,
CobraConnection_EventType_Published = 6,
CobraConnection_EventType_Pong = 7
};
enum CobraConnectionPublishMode enum CobraConnectionPublishMode
{ {
CobraConnection_PublishMode_Immediate = 0, CobraConnection_PublishMode_Immediate = 0,
CobraConnection_PublishMode_Batch = 1 CobraConnection_PublishMode_Batch = 1
}; };
using SubscriptionCallback = std::function<void(const Json::Value&, const std::string&)>; using SubscriptionCallback = std::function<void(const Json::Value&)>;
using EventCallback = std::function<void(const CobraEventPtr&)>; using EventCallback = std::function<void(CobraConnectionEventType,
const std::string&,
const WebSocketHttpHeaders&,
const std::string&,
uint64_t msgId)>;
using TrafficTrackerCallback = std::function<void(size_t size, bool incoming)>; using TrafficTrackerCallback = std::function<void(size_t size, bool incoming)>;
using PublishTrackerCallback = std::function<void(bool sent, bool acked)>; using PublishTrackerCallback = std::function<void(bool sent, bool acked)>;
@ -59,8 +67,6 @@ namespace ix
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions, const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions,
const SocketTLSOptions& socketTLSOptions); const SocketTLSOptions& socketTLSOptions);
void configure(const ix::CobraConfig& config);
/// Set the traffic tracker callback /// Set the traffic tracker callback
static void setTrafficTrackerCallback(const TrafficTrackerCallback& callback); static void setTrafficTrackerCallback(const TrafficTrackerCallback& callback);
@ -88,7 +94,6 @@ namespace ix
// message arrives. // message arrives.
void subscribe(const std::string& channel, void subscribe(const std::string& channel,
const std::string& filter = std::string(), const std::string& filter = std::string(),
const std::string& position = std::string(),
SubscriptionCallback cb = nullptr); SubscriptionCallback cb = nullptr);
/// Unsubscribe from a channel /// Unsubscribe from a channel
@ -154,7 +159,7 @@ namespace ix
static void invokePublishTrackerCallback(bool sent, bool acked); static void invokePublishTrackerCallback(bool sent, bool acked);
/// Invoke event callbacks /// Invoke event callbacks
void invokeEventCallback(CobraEventType eventType, void invokeEventCallback(CobraConnectionEventType eventType,
const std::string& errorMsg = std::string(), const std::string& errorMsg = std::string(),
const WebSocketHttpHeaders& headers = WebSocketHttpHeaders(), const WebSocketHttpHeaders& headers = WebSocketHttpHeaders(),
const std::string& subscriptionId = std::string(), const std::string& subscriptionId = std::string(),

View File

@ -1,41 +0,0 @@
/*
* IXCobraEvent.h
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include "IXCobraEventType.h"
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
#include <memory>
#include <string>
#include <cstdint>
namespace ix
{
struct CobraEvent
{
ix::CobraEventType type;
const std::string& errMsg;
const ix::WebSocketHttpHeaders& headers;
const std::string& subscriptionId;
uint64_t msgId; // CobraConnection::MsgId
CobraEvent(ix::CobraEventType t,
const std::string& e,
const ix::WebSocketHttpHeaders& h,
const std::string& s,
uint64_t m)
: type(t)
, errMsg(e)
, headers(h)
, subscriptionId(s)
, msgId(m)
{
;
}
};
using CobraEventPtr = std::unique_ptr<CobraEvent>;
}

View File

@ -1,25 +0,0 @@
/*
* IXCobraEventType.h
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
namespace ix
{
enum class CobraEventType
{
Authenticated = 0,
Error = 1,
Open = 2,
Closed = 3,
Subscribed = 4,
UnSubscribed = 5,
Published = 6,
Pong = 7,
HandshakeError = 8,
AuthenticationError = 9,
SubscriptionError = 10
};
}

View File

@ -27,12 +27,20 @@ namespace ix
; ;
} }
void CobraMetricsPublisher::configure(const CobraConfig& config, void CobraMetricsPublisher::configure(const std::string& appkey,
const std::string& channel) const std::string& endpoint,
const std::string& channel,
const std::string& rolename,
const std::string& rolesecret,
bool enablePerMessageDeflate,
const SocketTLSOptions& socketTLSOptions)
{ {
// Configure the satori connection and start its publish background thread // Configure the satori connection and start its publish background thread
_cobra_metrics_theaded_publisher.configure(config, channel);
_cobra_metrics_theaded_publisher.start(); _cobra_metrics_theaded_publisher.start();
_cobra_metrics_theaded_publisher.configure(appkey, endpoint, channel,
rolename, rolesecret,
enablePerMessageDeflate, socketTLSOptions);
} }
Json::Value& CobraMetricsPublisher::getGenericAttributes() Json::Value& CobraMetricsPublisher::getGenericAttributes()

View File

@ -40,8 +40,13 @@ namespace ix
/// Configuration / set keys, etc... /// Configuration / set keys, etc...
/// All input data but the channel name is encrypted with rc4 /// All input data but the channel name is encrypted with rc4
void configure(const CobraConfig& config, void configure(const std::string& appkey,
const std::string& channel); const std::string& endpoint,
const std::string& channel,
const std::string& rolename,
const std::string& rolesecret,
bool enablePerMessageDeflate,
const SocketTLSOptions& socketTLSOptions);
/// Setter for the list of blacklisted metrics ids. /// Setter for the list of blacklisted metrics ids.
/// That list is sorted internally for fast lookups /// That list is sorted internally for fast lookups

View File

@ -22,59 +22,53 @@ namespace ix
CobraMetricsThreadedPublisher::CobraMetricsThreadedPublisher() : CobraMetricsThreadedPublisher::CobraMetricsThreadedPublisher() :
_stop(false) _stop(false)
{ {
_cobra_connection.setEventCallback([](const CobraEventPtr& event) _cobra_connection.setEventCallback(
[]
(ix::CobraConnectionEventType eventType,
const std::string& errMsg,
const ix::WebSocketHttpHeaders& headers,
const std::string& subscriptionId,
CobraConnection::MsgId msgId)
{ {
std::stringstream ss; std::stringstream ss;
if (event->type == ix::CobraEventType::Open) if (eventType == ix::CobraConnection_EventType_Open)
{ {
ss << "Handshake headers" << std::endl; ss << "Handshake headers" << std::endl;
for (auto&& it : event->headers) for (auto it : headers)
{ {
ss << it.first << ": " << it.second << std::endl; ss << it.first << ": " << it.second << std::endl;
} }
} }
else if (event->type == ix::CobraEventType::Authenticated) else if (eventType == ix::CobraConnection_EventType_Authenticated)
{ {
ss << "Authenticated"; ss << "Authenticated";
} }
else if (event->type == ix::CobraEventType::Error) else if (eventType == ix::CobraConnection_EventType_Error)
{ {
ss << "Error: " << event->errMsg; ss << "Error: " << errMsg;
} }
else if (event->type == ix::CobraEventType::Closed) else if (eventType == ix::CobraConnection_EventType_Closed)
{ {
ss << "Connection closed: " << event->errMsg; ss << "Connection closed: " << errMsg;
} }
else if (event->type == ix::CobraEventType::Subscribed) else if (eventType == ix::CobraConnection_EventType_Subscribed)
{ {
ss << "Subscribed through subscription id: " << event->subscriptionId; ss << "Subscribed through subscription id: " << subscriptionId;
} }
else if (event->type == ix::CobraEventType::UnSubscribed) else if (eventType == ix::CobraConnection_EventType_UnSubscribed)
{ {
ss << "Unsubscribed through subscription id: " << event->subscriptionId; ss << "Unsubscribed through subscription id: " << subscriptionId;
} }
else if (event->type == ix::CobraEventType::Published) else if (eventType == ix::CobraConnection_EventType_Published)
{ {
ss << "Published message " << event->msgId << " acked"; ss << "Published message " << msgId << " acked";
} }
else if (event->type == ix::CobraEventType::Pong) else if (eventType == ix::CobraConnection_EventType_Pong)
{ {
ss << "Received websocket pong"; ss << "Received websocket pong";
} }
else if (event->type == ix::CobraEventType::HandshakeError)
{
ss << "Handshake error: " << event->errMsg;
}
else if (event->type == ix::CobraEventType::AuthenticationError)
{
ss << "Authentication error: " << event->errMsg;
}
else if (event->type == ix::CobraEventType::SubscriptionError)
{
ss << "Subscription error: " << event->errMsg;
}
ix::IXCoreLogger::Log(ss.str().c_str()); ix::IXCoreLogger::Log(ss.str().c_str());
}); });
@ -98,14 +92,22 @@ namespace ix
_thread = std::thread(&CobraMetricsThreadedPublisher::run, this); _thread = std::thread(&CobraMetricsThreadedPublisher::run, this);
} }
void CobraMetricsThreadedPublisher::configure(const CobraConfig& config, void CobraMetricsThreadedPublisher::configure(const std::string& appkey,
const std::string& channel) const std::string& endpoint,
const std::string& channel,
const std::string& rolename,
const std::string& rolesecret,
bool enablePerMessageDeflate,
const SocketTLSOptions& socketTLSOptions)
{ {
ix::IXCoreLogger::Log(config.socketTLSOptions.getDescription().c_str());
_channel = channel; _channel = channel;
_cobra_connection.configure(config);
ix::IXCoreLogger::Log(socketTLSOptions.getDescription().c_str());
ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions(enablePerMessageDeflate);
_cobra_connection.configure(appkey, endpoint,
rolename, rolesecret,
webSocketPerMessageDeflateOptions, socketTLSOptions);
} }
void CobraMetricsThreadedPublisher::pushMessage(MessageKind messageKind) void CobraMetricsThreadedPublisher::pushMessage(MessageKind messageKind)

View File

@ -27,8 +27,13 @@ namespace ix
~CobraMetricsThreadedPublisher(); ~CobraMetricsThreadedPublisher();
/// Configuration / set keys, etc... /// Configuration / set keys, etc...
void configure(const CobraConfig& config, void configure(const std::string& appkey,
const std::string& channel); const std::string& endpoint,
const std::string& channel,
const std::string& rolename,
const std::string& rolesecret,
bool enablePerMessageDeflate,
const SocketTLSOptions& socketTLSOptions);
/// Start the worker thread, used for background publishing /// Start the worker thread, used for background publishing
void start(); void start();

View File

@ -31,6 +31,10 @@ target_include_directories( ixcrypto PUBLIC ${IXCRYPTO_INCLUDE_DIRS} )
# hmac computation needs a crypto library # hmac computation needs a crypto library
if (WIN32)
set(USE_MBED_TLS TRUE)
endif()
target_compile_definitions(ixcrypto PUBLIC IXCRYPTO_USE_TLS) target_compile_definitions(ixcrypto PUBLIC IXCRYPTO_USE_TLS)
if (USE_MBED_TLS) if (USE_MBED_TLS)
find_package(MbedTLS REQUIRED) find_package(MbedTLS REQUIRED)
@ -47,3 +51,4 @@ else()
target_link_libraries(ixcrypto ${OPENSSL_LIBRARIES}) target_link_libraries(ixcrypto ${OPENSSL_LIBRARIES})
target_compile_definitions(ixcrypto PUBLIC IXCRYPTO_USE_OPEN_SSL) target_compile_definitions(ixcrypto PUBLIC IXCRYPTO_USE_OPEN_SSL)
endif() endif()

View File

@ -14,7 +14,7 @@
#elif defined(IXCRYPTO_USE_OPEN_SSL) #elif defined(IXCRYPTO_USE_OPEN_SSL)
# include <openssl/hmac.h> # include <openssl/hmac.h>
#else #else
# include <assert.h> # error "Unsupported configuration"
#endif #endif
namespace ix namespace ix
@ -40,7 +40,7 @@ namespace ix
(unsigned char *) data.c_str(), (int) data.size(), (unsigned char *) data.c_str(), (int) data.size(),
(unsigned char *) hash, nullptr); (unsigned char *) hash, nullptr);
#else #else
assert(false && "hmac not implemented on this platform"); # error "Unsupported configuration"
#endif #endif
std::string hashString(reinterpret_cast<char*>(hash), hashSize); std::string hashString(reinterpret_cast<char*>(hash), hashSize);

View File

@ -40,11 +40,6 @@ namespace ix
} }
} }
void SentryClient::setTLSOptions(const SocketTLSOptions& tlsOptions)
{
_httpClient->setTLSOptions(tlsOptions);
}
int64_t SentryClient::getTimestamp() int64_t SentryClient::getTimestamp()
{ {
const auto tp = std::chrono::system_clock::now(); const auto tp = std::chrono::system_clock::now();
@ -64,8 +59,7 @@ namespace ix
std::string SentryClient::computeAuthHeader() std::string SentryClient::computeAuthHeader()
{ {
std::string securityHeader("Sentry sentry_version=5"); std::string securityHeader("Sentry sentry_version=5");
securityHeader += ",sentry_client=ws/"; securityHeader += ",sentry_client=ws/1.0.0";
securityHeader += std::string(IX_WEBSOCKET_VERSION);
securityHeader += ",sentry_timestamp=" + std::to_string(SentryClient::getTimestamp()); securityHeader += ",sentry_timestamp=" + std::to_string(SentryClient::getTimestamp());
securityHeader += ",sentry_key=" + _publicKey; securityHeader += ",sentry_key=" + _publicKey;
securityHeader += ",sentry_secret=" + _secretKey; securityHeader += ",sentry_secret=" + _secretKey;
@ -287,24 +281,4 @@ namespace ix
_httpClient->performRequest(args, onResponseCallback); _httpClient->performRequest(args, onResponseCallback);
} }
void SentryClient::uploadPayload(
const Json::Value& payload,
bool verbose,
const OnResponseCallback& onResponseCallback)
{
auto args = _httpClient->createRequest();
args->extraHeaders["X-Sentry-Auth"] = SentryClient::computeAuthHeader();
args->verb = HttpClient::kPost;
args->connectTimeout = 60;
args->transferTimeout = 5 * 60;
args->followRedirects = true;
args->verbose = verbose;
args->logger = [](const std::string& msg) { ix::IXCoreLogger::Log(msg.c_str()); };
args->url = _url;
args->body = _jsonWriter.write(payload);
_httpClient->performRequest(args, onResponseCallback);
}
} // namespace ix } // namespace ix

View File

@ -8,7 +8,6 @@
#include <algorithm> #include <algorithm>
#include <ixwebsocket/IXHttpClient.h> #include <ixwebsocket/IXHttpClient.h>
#include <ixwebsocket/IXSocketTLSOptions.h>
#include <json/json.h> #include <json/json.h>
#include <regex> #include <regex>
#include <memory> #include <memory>
@ -25,9 +24,6 @@ namespace ix
Json::Value parseLuaStackTrace(const std::string& stack); Json::Value parseLuaStackTrace(const std::string& stack);
// Mostly for testing
void setTLSOptions(const SocketTLSOptions& tlsOptions);
void uploadMinidump( void uploadMinidump(
const std::string& sentryMetadata, const std::string& sentryMetadata,
const std::string& minidumpBytes, const std::string& minidumpBytes,
@ -36,11 +32,6 @@ namespace ix
bool verbose, bool verbose,
const OnResponseCallback& onResponseCallback); const OnResponseCallback& onResponseCallback);
void uploadPayload(
const Json::Value& payload,
bool verbose,
const OnResponseCallback& onResponseCallback);
private: private:
int64_t getTimestamp(); int64_t getTimestamp();
std::string computeAuthHeader(); std::string computeAuthHeader();

View File

@ -9,6 +9,7 @@
#include <cstring> #include <cstring>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXSocketFactory.h> #include <ixwebsocket/IXSocketFactory.h>
#include <ixwebsocket/IXSocketTLSOptions.h> #include <ixwebsocket/IXSocketTLSOptions.h>
#include <sstream> #include <sstream>

View File

@ -9,12 +9,11 @@
#include <atomic> #include <atomic>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <string>
#include <ixwebsocket/IXSocket.h>
namespace ix namespace ix
{ {
class Socket;
class RedisClient class RedisClient
{ {
public: public:
@ -57,7 +56,7 @@ namespace ix
private: private:
std::string writeString(const std::string& str); std::string writeString(const std::string& str);
std::unique_ptr<Socket> _socket; std::shared_ptr<Socket> _socket;
std::atomic<bool> _stop; std::atomic<bool> _stop;
}; };
} // namespace ix } // namespace ix

View File

@ -11,6 +11,7 @@
#include <ixwebsocket/IXSocket.h> #include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXCancellationRequest.h> #include <ixwebsocket/IXCancellationRequest.h>
#include <fstream> #include <fstream>
#include <iostream>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
@ -43,7 +44,7 @@ namespace ix
SocketServer::stop(); SocketServer::stop();
} }
void RedisServer::handleConnection(std::unique_ptr<Socket> socket, void RedisServer::handleConnection(std::shared_ptr<Socket> socket,
std::shared_ptr<ConnectionState> connectionState) std::shared_ptr<ConnectionState> connectionState)
{ {
_connectedClientsCount++; _connectedClientsCount++;
@ -102,13 +103,13 @@ namespace ix
_connectedClientsCount--; _connectedClientsCount--;
} }
void RedisServer::cleanupSubscribers(std::unique_ptr<Socket>& socket) void RedisServer::cleanupSubscribers(std::shared_ptr<Socket> socket)
{ {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
for (auto&& it : _subscribers) for (auto&& it : _subscribers)
{ {
it.second.erase(socket.get()); it.second.erase(socket);
} }
for (auto it : _subscribers) for (auto it : _subscribers)
@ -145,7 +146,7 @@ namespace ix
} }
bool RedisServer::parseRequest( bool RedisServer::parseRequest(
std::unique_ptr<Socket>& socket, std::shared_ptr<Socket> socket,
std::vector<std::string>& tokens) std::vector<std::string>& tokens)
{ {
// Parse first line // Parse first line
@ -187,11 +188,17 @@ namespace ix
tokens.push_back(readResult.second); tokens.push_back(readResult.second);
} }
for (auto&& token : tokens)
{
std::cerr << token << " ";
}
std::cerr << std::endl;
return true; return true;
} }
bool RedisServer::handleCommand( bool RedisServer::handleCommand(
std::unique_ptr<Socket>& socket, std::shared_ptr<Socket> socket,
const std::vector<std::string>& tokens) const std::vector<std::string>& tokens)
{ {
if (tokens.size() != 1) return false; if (tokens.size() != 1) return false;
@ -230,7 +237,7 @@ namespace ix
} }
bool RedisServer::handleSubscribe( bool RedisServer::handleSubscribe(
std::unique_ptr<Socket>& socket, std::shared_ptr<Socket> socket,
const std::vector<std::string>& tokens) const std::vector<std::string>& tokens)
{ {
if (tokens.size() != 2) return false; if (tokens.size() != 2) return false;
@ -245,13 +252,13 @@ namespace ix
socket->writeBytes(":1\r\n", cb); socket->writeBytes(":1\r\n", cb);
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
_subscribers[channel].insert(socket.get()); _subscribers[channel].insert(socket);
return true; return true;
} }
bool RedisServer::handlePublish( bool RedisServer::handlePublish(
std::unique_ptr<Socket>& socket, std::shared_ptr<Socket> socket,
const std::vector<std::string>& tokens) const std::vector<std::string>& tokens)
{ {
if (tokens.size() != 3) return false; if (tokens.size() != 3) return false;

View File

@ -37,13 +37,13 @@ namespace ix
// Subscribers // Subscribers
// We could store connection states in there, to add better debugging // We could store connection states in there, to add better debugging
// since a connection state has a readable ID // since a connection state has a readable ID
std::map<std::string, std::set<Socket*>> _subscribers; std::map<std::string, std::set<std::shared_ptr<Socket>>> _subscribers;
std::mutex _mutex; std::mutex _mutex;
std::atomic<bool> _stopHandlingConnections; std::atomic<bool> _stopHandlingConnections;
// Methods // Methods
virtual void handleConnection(std::unique_ptr<Socket>, virtual void handleConnection(std::shared_ptr<Socket>,
std::shared_ptr<ConnectionState> connectionState) final; std::shared_ptr<ConnectionState> connectionState) final;
virtual size_t getConnectedClientsCount() final; virtual size_t getConnectedClientsCount() final;
@ -51,18 +51,18 @@ namespace ix
std::string writeString(const std::string& str); std::string writeString(const std::string& str);
bool parseRequest( bool parseRequest(
std::unique_ptr<Socket>& socket, std::shared_ptr<Socket> socket,
std::vector<std::string>& tokens); std::vector<std::string>& tokens);
bool handlePublish(std::unique_ptr<Socket>& socket, bool handlePublish(std::shared_ptr<Socket> socket,
const std::vector<std::string>& tokens); const std::vector<std::string>& tokens);
bool handleSubscribe(std::unique_ptr<Socket>& socket, bool handleSubscribe(std::shared_ptr<Socket> socket,
const std::vector<std::string>& tokens); const std::vector<std::string>& tokens);
bool handleCommand(std::unique_ptr<Socket>& socket, bool handleCommand(std::shared_ptr<Socket> socket,
const std::vector<std::string>& tokens); const std::vector<std::string>& tokens);
void cleanupSubscribers(std::unique_ptr<Socket>& socket); void cleanupSubscribers(std::shared_ptr<Socket> socket);
}; };
} // namespace ix } // namespace ix

View File

@ -189,7 +189,7 @@ namespace snake
nlohmann::json response = { nlohmann::json response = {
{"action", "rtm/subscription/data"}, {"action", "rtm/subscription/data"},
{"id", id++}, {"id", id++},
{"body", {{"subscription_id", subscriptionId}, {"position", "0-0"}, {"messages", {msg}}}}}; {"body", {{"subscription_id", subscriptionId}, {"messages", {msg}}}}};
ws->sendText(response.dump()); ws->sendText(response.dump());
}; };
@ -251,22 +251,7 @@ namespace snake
const AppConfig& appConfig, const AppConfig& appConfig,
const std::string& str) const std::string& str)
{ {
nlohmann::json pdu; auto pdu = nlohmann::json::parse(str);
try
{
pdu = nlohmann::json::parse(str);
}
catch (const nlohmann::json::parse_error& e)
{
std::stringstream ss;
ss << "malformed json pdu: " << e.what() << " -> " << str << "";
nlohmann::json response = {{"body", {{"error", "invalid_json"},
{"reason", ss.str()}}}};
ws->sendText(response.dump());
return;
}
auto action = pdu["action"]; auto action = pdu["action"];
if (action == "auth/handshake") if (action == "auth/handshake")

View File

@ -1,44 +0,0 @@
/*
* IXBench.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2017-2020 Machine Zone, Inc. All rights reserved.
*/
#include "IXBench.h"
#include <iostream>
namespace ix
{
Bench::Bench(const std::string& description)
: _description(description)
, _start(std::chrono::high_resolution_clock::now())
, _reported(false)
{
;
}
Bench::~Bench()
{
if (!_reported)
{
report();
}
}
void Bench::report()
{
auto now = std::chrono::high_resolution_clock::now();
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now - _start);
_ms = milliseconds.count();
std::cerr << _description << " completed in " << _ms << "ms" << std::endl;
_reported = true;
}
uint64_t Bench::getDuration() const
{
return _ms;
}
} // namespace ix

View File

@ -1,28 +0,0 @@
/*
* IXBench.h
* Author: Benjamin Sergeant
* Copyright (c) 2017-2020 Machine Zone, Inc. All rights reserved.
*/
#include <chrono>
#include <stdint.h>
#include <string>
namespace ix
{
class Bench
{
public:
Bench(const std::string& description);
~Bench();
void report();
uint64_t getDuration() const;
private:
std::string _description;
std::chrono::time_point<std::chrono::high_resolution_clock> _start;
uint64_t _ms;
bool _reported;
};
} // namespace ix

View File

@ -92,8 +92,7 @@ namespace ix
return std::make_tuple(method, requestUri, httpVersion); return std::make_tuple(method, requestUri, httpVersion);
} }
std::tuple<bool, std::string, HttpRequestPtr> Http::parseRequest( std::tuple<bool, std::string, HttpRequestPtr> Http::parseRequest(std::shared_ptr<Socket> socket)
std::unique_ptr<Socket>& socket)
{ {
HttpRequestPtr httpRequest; HttpRequestPtr httpRequest;
@ -134,7 +133,7 @@ namespace ix
return std::make_tuple(true, "", httpRequest); return std::make_tuple(true, "", httpRequest);
} }
bool Http::sendResponse(HttpResponsePtr response, std::unique_ptr<Socket>& socket) bool Http::sendResponse(HttpResponsePtr response, std::shared_ptr<Socket> socket)
{ {
// Write the response to the socket // Write the response to the socket
std::stringstream ss; std::stringstream ss;

View File

@ -115,8 +115,8 @@ namespace ix
{ {
public: public:
static std::tuple<bool, std::string, HttpRequestPtr> parseRequest( static std::tuple<bool, std::string, HttpRequestPtr> parseRequest(
std::unique_ptr<Socket>& socket); std::shared_ptr<Socket> socket);
static bool sendResponse(HttpResponsePtr response, std::unique_ptr<Socket>& socket); static bool sendResponse(HttpResponsePtr response, std::shared_ptr<Socket> socket);
static std::pair<std::string, int> parseStatusLine(const std::string& line); static std::pair<std::string, int> parseStatusLine(const std::string& line);
static std::tuple<std::string, std::string, std::string> parseRequestLine( static std::tuple<std::string, std::string, std::string> parseRequestLine(

View File

@ -95,7 +95,7 @@ namespace ix
std::atomic<bool> _stop; std::atomic<bool> _stop;
std::thread _thread; std::thread _thread;
std::unique_ptr<Socket> _socket; std::shared_ptr<Socket> _socket;
std::mutex _mutex; // to protect accessing the _socket (only one socket per client) std::mutex _mutex; // to protect accessing the _socket (only one socket per client)
SocketTLSOptions _tlsOptions; SocketTLSOptions _tlsOptions;

View File

@ -69,7 +69,7 @@ namespace ix
_onConnectionCallback = callback; _onConnectionCallback = callback;
} }
void HttpServer::handleConnection(std::unique_ptr<Socket> socket, void HttpServer::handleConnection(std::shared_ptr<Socket> socket,
std::shared_ptr<ConnectionState> connectionState) std::shared_ptr<ConnectionState> connectionState)
{ {
_connectedClientsCount++; _connectedClientsCount++;

View File

@ -43,7 +43,7 @@ namespace ix
std::atomic<int> _connectedClientsCount; std::atomic<int> _connectedClientsCount;
// Methods // Methods
virtual void handleConnection(std::unique_ptr<Socket>, virtual void handleConnection(std::shared_ptr<Socket>,
std::shared_ptr<ConnectionState> connectionState) final; std::shared_ptr<ConnectionState> connectionState) final;
virtual size_t getConnectedClientsCount() final; virtual size_t getConnectedClientsCount() final;

View File

@ -14,12 +14,12 @@
namespace ix namespace ix
{ {
SelectInterruptPtr createSelectInterrupt() std::shared_ptr<SelectInterrupt> createSelectInterrupt()
{ {
#if defined(__linux__) || defined(__APPLE__) #if defined(__linux__) || defined(__APPLE__)
return std::make_unique<SelectInterruptPipe>(); return std::make_shared<SelectInterruptPipe>();
#else #else
return std::make_unique<SelectInterrupt>(); return std::make_shared<SelectInterrupt>();
#endif #endif
} }
} // namespace ix } // namespace ix

View File

@ -11,6 +11,5 @@
namespace ix namespace ix
{ {
class SelectInterrupt; class SelectInterrupt;
using SelectInterruptPtr = std::unique_ptr<SelectInterrupt>; std::shared_ptr<SelectInterrupt> createSelectInterrupt();
SelectInterruptPtr createSelectInterrupt();
} // namespace ix } // namespace ix

View File

@ -46,7 +46,7 @@ namespace ix
PollResultType Socket::poll(bool readyToRead, PollResultType Socket::poll(bool readyToRead,
int timeoutMs, int timeoutMs,
int sockfd, int sockfd,
const SelectInterruptPtr& selectInterrupt) std::shared_ptr<SelectInterrupt> selectInterrupt)
{ {
// //
// We used to use ::select to poll but on Android 9 we get large fds out of // We used to use ::select to poll but on Android 9 we get large fds out of
@ -376,8 +376,7 @@ namespace ix
{ {
if (isCancellationRequested && isCancellationRequested()) if (isCancellationRequested && isCancellationRequested())
{ {
const std::string errorMsg("Cancellation Requested"); return std::make_pair(false, std::string());
return std::make_pair(false, errorMsg);
} }
size_t size = std::min(kChunkSize, length - output.size()); size_t size = std::min(kChunkSize, length - output.size());
@ -389,8 +388,7 @@ namespace ix
} }
else if (ret <= 0 && !Socket::isWaitNeeded()) else if (ret <= 0 && !Socket::isWaitNeeded())
{ {
const std::string errorMsg("Recv Error"); return std::make_pair(false, std::string());
return std::make_pair(false, errorMsg);
} }
if (onProgressCallback) onProgressCallback((int) output.size(), (int) length); if (onProgressCallback) onProgressCallback((int) output.size(), (int) length);
@ -399,8 +397,7 @@ namespace ix
// This way we are not busy looping // This way we are not busy looping
if (isReadyToRead(1) == PollResultType::Error) if (isReadyToRead(1) == PollResultType::Error)
{ {
const std::string errorMsg("Poll Error"); return std::make_pair(false, std::string());
return std::make_pair(false, errorMsg);
} }
} }

View File

@ -38,7 +38,6 @@ typedef SSIZE_T ssize_t;
namespace ix namespace ix
{ {
class SelectInterrupt; class SelectInterrupt;
using SelectInterruptPtr = std::unique_ptr<SelectInterrupt>;
enum class PollResultType enum class PollResultType
{ {
@ -67,7 +66,7 @@ namespace ix
// Virtual methods // Virtual methods
virtual bool accept(std::string& errMsg); virtual bool accept(std::string& errMsg);
virtual bool connect(const std::string& host, virtual bool connect(const std::string& url,
int port, int port,
std::string& errMsg, std::string& errMsg,
const CancellationRequest& isCancellationRequested); const CancellationRequest& isCancellationRequested);
@ -94,7 +93,7 @@ namespace ix
static PollResultType poll(bool readyToRead, static PollResultType poll(bool readyToRead,
int timeoutMs, int timeoutMs,
int sockfd, int sockfd,
const SelectInterruptPtr& selectInterrupt); std::shared_ptr<SelectInterrupt> selectInterrupt = nullptr);
// Used as special codes for pipe communication // Used as special codes for pipe communication
@ -113,6 +112,6 @@ namespace ix
std::vector<uint8_t> _readBuffer; std::vector<uint8_t> _readBuffer;
static constexpr size_t kChunkSize = 1 << 15; static constexpr size_t kChunkSize = 1 << 15;
SelectInterruptPtr _selectInterrupt; std::shared_ptr<SelectInterrupt> _selectInterrupt;
}; };
} // namespace ix } // namespace ix

View File

@ -164,26 +164,6 @@ namespace ix
return false; return false;
} }
OSStatus SocketAppleSSL::tlsHandShake(std::string& errMsg,
const CancellationRequest& isCancellationRequested)
{
OSStatus status;
do
{
status = SSLHandshake(_sslContext);
// Interrupt the handshake
if (isCancellationRequested())
{
errMsg = "Cancellation requested";
return errSSLInternal;
}
} while (status == errSSLWouldBlock || status == errSSLServerAuthCompleted);
return status;
}
// No wait support // No wait support
bool SocketAppleSSL::connect(const std::string& host, bool SocketAppleSSL::connect(const std::string& host,
int port, int port,
@ -210,17 +190,26 @@ namespace ix
Boolean option(1); Boolean option(1);
SSLSetSessionOption(_sslContext, kSSLSessionOptionBreakOnServerAuth, option); SSLSetSessionOption(_sslContext, kSSLSessionOptionBreakOnServerAuth, option);
status = tlsHandShake(errMsg, isCancellationRequested); do
{
status = SSLHandshake(_sslContext);
} while (status == errSSLWouldBlock || status == errSSLServerAuthCompleted);
if (status == errSSLServerAuthCompleted) if (status == errSSLServerAuthCompleted)
{ {
// proceed with the handshake // proceed with the handshake
status = tlsHandShake(errMsg, isCancellationRequested); do
{
status = SSLHandshake(_sslContext);
} while (status == errSSLWouldBlock || status == errSSLServerAuthCompleted);
} }
} }
else else
{ {
status = tlsHandShake(errMsg, isCancellationRequested); do
{
status = SSLHandshake(_sslContext);
} while (status == errSSLWouldBlock || status == errSSLServerAuthCompleted);
} }
} }

View File

@ -37,9 +37,6 @@ namespace ix
static OSStatus writeToSocket(SSLConnectionRef connection, const void* data, size_t* len); static OSStatus writeToSocket(SSLConnectionRef connection, const void* data, size_t* len);
static OSStatus readFromSocket(SSLConnectionRef connection, void* data, size_t* len); static OSStatus readFromSocket(SSLConnectionRef connection, void* data, size_t* len);
OSStatus tlsHandShake(std::string& errMsg,
const CancellationRequest& isCancellationRequested);
SSLContextRef _sslContext; SSLContextRef _sslContext;
mutable std::mutex _mutex; // AppleSSL routines are not thread-safe mutable std::mutex _mutex; // AppleSSL routines are not thread-safe

View File

@ -8,7 +8,6 @@
#include "IXDNSLookup.h" #include "IXDNSLookup.h"
#include "IXNetSystem.h" #include "IXNetSystem.h"
#include "IXSelectInterrupt.h"
#include "IXSocket.h" #include "IXSocket.h"
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
@ -65,8 +64,7 @@ namespace ix
int timeoutMs = 10; int timeoutMs = 10;
bool readyToRead = false; bool readyToRead = false;
auto selectInterrupt = std::make_unique<SelectInterrupt>(); PollResultType pollResult = Socket::poll(readyToRead, timeoutMs, fd);
PollResultType pollResult = Socket::poll(readyToRead, timeoutMs, fd, selectInterrupt);
if (pollResult == PollResultType::Timeout) if (pollResult == PollResultType::Timeout)
{ {

View File

@ -24,28 +24,28 @@
namespace ix namespace ix
{ {
std::unique_ptr<Socket> createSocket(bool tls, std::shared_ptr<Socket> createSocket(bool tls,
int fd, int fd,
std::string& errorMsg, std::string& errorMsg,
const SocketTLSOptions& tlsOptions) const SocketTLSOptions& tlsOptions)
{ {
(void) tlsOptions; (void) tlsOptions;
errorMsg.clear(); errorMsg.clear();
std::unique_ptr<Socket> socket; std::shared_ptr<Socket> socket;
if (!tls) if (!tls)
{ {
socket = std::make_unique<Socket>(fd); socket = std::make_shared<Socket>(fd);
} }
else else
{ {
#ifdef IXWEBSOCKET_USE_TLS #ifdef IXWEBSOCKET_USE_TLS
#if defined(IXWEBSOCKET_USE_MBED_TLS) #if defined(IXWEBSOCKET_USE_MBED_TLS)
socket = std::make_unique<SocketMbedTLS>(tlsOptions, fd); socket = std::make_shared<SocketMbedTLS>(tlsOptions, fd);
#elif defined(IXWEBSOCKET_USE_OPEN_SSL) #elif defined(IXWEBSOCKET_USE_OPEN_SSL)
socket = std::make_unique<SocketOpenSSL>(tlsOptions, fd); socket = std::make_shared<SocketOpenSSL>(tlsOptions, fd);
#elif defined(__APPLE__) #elif defined(__APPLE__)
socket = std::make_unique<SocketAppleSSL>(tlsOptions, fd); socket = std::make_shared<SocketAppleSSL>(tlsOptions, fd);
#endif #endif
#else #else
errorMsg = "TLS support is not enabled on this platform."; errorMsg = "TLS support is not enabled on this platform.";

View File

@ -14,7 +14,7 @@
namespace ix namespace ix
{ {
class Socket; class Socket;
std::unique_ptr<Socket> createSocket(bool tls, std::shared_ptr<Socket> createSocket(bool tls,
int fd, int fd,
std::string& errorMsg, std::string& errorMsg,
const SocketTLSOptions& tlsOptions); const SocketTLSOptions& tlsOptions);

View File

@ -195,17 +195,8 @@ namespace ix
int res; int res;
do do
{ {
{ std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_mutex); res = mbedtls_ssl_handshake(&_ssl);
res = mbedtls_ssl_handshake(&_ssl);
}
if (isCancellationRequested())
{
errMsg = "Cancellation requested";
close();
return false;
}
} while (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE); } while (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE);
if (res != 0) if (res != 0)

View File

@ -21,57 +21,6 @@
#endif #endif
#define socketerrno errno #define socketerrno errno
#ifdef _WIN32
namespace
{
bool loadWindowsSystemCertificates(SSL_CTX* ssl, std::string& errorMsg)
{
DWORD flags = CERT_STORE_READONLY_FLAG | CERT_STORE_OPEN_EXISTING_FLAG |
CERT_SYSTEM_STORE_CURRENT_USER;
HCERTSTORE systemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, flags, L"Root");
if (!systemStore)
{
errorMsg = "CertOpenStore failed with ";
errorMsg += std::to_string(GetLastError());
return false;
}
PCCERT_CONTEXT certificateIterator = NULL;
X509_STORE* opensslStore = SSL_CTX_get_cert_store(ssl);
int certificateCount = 0;
while (certificateIterator = CertEnumCertificatesInStore(systemStore, certificateIterator))
{
X509* x509 = d2i_X509(NULL,
(const unsigned char**) &certificateIterator->pbCertEncoded,
certificateIterator->cbCertEncoded);
if (x509)
{
if (X509_STORE_add_cert(opensslStore, x509) == 1)
{
++certificateCount;
}
X509_free(x509);
}
}
CertFreeCertificateContext(certificateIterator);
CertCloseStore(systemStore, 0);
if (certificateCount == 0)
{
errorMsg = "No certificates found";
return false;
}
return true;
}
} // namespace
#endif
namespace ix namespace ix
{ {
const std::string kDefaultCiphers = const std::string kDefaultCiphers =
@ -182,14 +131,8 @@ namespace ix
SSL_CTX_set_mode(ctx, SSL_CTX_set_mode(ctx,
SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
int options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_CIPHER_SERVER_PREFERENCE; SSL_CTX_set_options(
ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_CIPHER_SERVER_PREFERENCE);
#ifdef SSL_OP_NO_TLSv1_3
// (partially?) work around hang in openssl 1.1.1b, by disabling TLS V1.3
// https://github.com/openssl/openssl/issues/7967
options |= SSL_OP_NO_TLSv1_3;
#endif
SSL_CTX_set_options(ctx, options);
} }
return ctx; return ctx;
} }
@ -275,9 +218,7 @@ namespace ix
return true; return true;
} }
bool SocketOpenSSL::openSSLClientHandshake(const std::string& host, bool SocketOpenSSL::openSSLClientHandshake(const std::string& host, std::string& errMsg)
std::string& errMsg,
const CancellationRequest& isCancellationRequested)
{ {
while (true) while (true)
{ {
@ -286,12 +227,6 @@ namespace ix
return false; return false;
} }
if (isCancellationRequested())
{
errMsg = "Cancellation requested";
return false;
}
ERR_clear_error(); ERR_clear_error();
int connect_result = SSL_connect(_ssl_connection); int connect_result = SSL_connect(_ssl_connection);
if (connect_result == 1) if (connect_result == 1)
@ -387,12 +322,6 @@ namespace ix
{ {
if (_tlsOptions.isUsingSystemDefaults()) if (_tlsOptions.isUsingSystemDefaults())
{ {
#ifdef _WIN32
if (!loadWindowsSystemCertificates(_ssl_context, errMsg))
{
return false;
}
#else
if (SSL_CTX_set_default_verify_paths(_ssl_context) == 0) if (SSL_CTX_set_default_verify_paths(_ssl_context) == 0)
{ {
auto sslErr = ERR_get_error(); auto sslErr = ERR_get_error();
@ -400,7 +329,6 @@ namespace ix
errMsg += ERR_error_string(sslErr, nullptr); errMsg += ERR_error_string(sslErr, nullptr);
return false; return false;
} }
#endif
} }
else if (SSL_CTX_load_verify_locations( else if (SSL_CTX_load_verify_locations(
_ssl_context, _tlsOptions.caFile.c_str(), NULL) != 1) _ssl_context, _tlsOptions.caFile.c_str(), NULL) != 1)
@ -643,7 +571,7 @@ namespace ix
X509_VERIFY_PARAM* param = SSL_get0_param(_ssl_connection); X509_VERIFY_PARAM* param = SSL_get0_param(_ssl_connection);
X509_VERIFY_PARAM_set1_host(param, host.c_str(), 0); X509_VERIFY_PARAM_set1_host(param, host.c_str(), 0);
#endif #endif
handshakeSuccessful = openSSLClientHandshake(host, errMsg, isCancellationRequested); handshakeSuccessful = openSSLClientHandshake(host, errMsg);
} }
if (!handshakeSuccessful) if (!handshakeSuccessful)

View File

@ -39,9 +39,7 @@ namespace ix
void openSSLInitialize(); void openSSLInitialize();
std::string getSSLError(int ret); std::string getSSLError(int ret);
SSL_CTX* openSSLCreateContext(std::string& errMsg); SSL_CTX* openSSLCreateContext(std::string& errMsg);
bool openSSLClientHandshake(const std::string& hostname, bool openSSLClientHandshake(const std::string& hostname, std::string& errMsg);
std::string& errMsg,
const CancellationRequest& isCancellationRequested);
bool openSSLCheckServerCert(SSL* ssl, const std::string& hostname, std::string& errMsg); bool openSSLCheckServerCert(SSL* ssl, const std::string& hostname, std::string& errMsg);
bool checkHost(const std::string& host, const char* pattern); bool checkHost(const std::string& host, const char* pattern);
bool handleTLSOptions(std::string& errMsg); bool handleTLSOptions(std::string& errMsg);

View File

@ -7,7 +7,6 @@
#include "IXSocketServer.h" #include "IXSocketServer.h"
#include "IXNetSystem.h" #include "IXNetSystem.h"
#include "IXSelectInterrupt.h"
#include "IXSetThreadName.h" #include "IXSetThreadName.h"
#include "IXSocket.h" #include "IXSocket.h"
#include "IXSocketConnect.h" #include "IXSocketConnect.h"
@ -258,9 +257,7 @@ namespace ix
// Use poll to check whether a new connection is in progress // Use poll to check whether a new connection is in progress
int timeoutMs = 10; int timeoutMs = 10;
bool readyToRead = true; bool readyToRead = true;
auto selectInterrupt = std::make_unique<SelectInterrupt>(); PollResultType pollResult = Socket::poll(readyToRead, timeoutMs, _serverFd);
PollResultType pollResult =
Socket::poll(readyToRead, timeoutMs, _serverFd, selectInterrupt);
if (pollResult == PollResultType::Error) if (pollResult == PollResultType::Error)
{ {
@ -341,8 +338,7 @@ namespace ix
std::lock_guard<std::mutex> lock(_connectionsThreadsMutex); std::lock_guard<std::mutex> lock(_connectionsThreadsMutex);
_connectionsThreads.push_back(std::make_pair( _connectionsThreads.push_back(std::make_pair(
connectionState, connectionState,
std::thread( std::thread(&SocketServer::handleConnection, this, socket, connectionState)));
&SocketServer::handleConnection, this, std::move(socket), connectionState)));
} }
} }

View File

@ -101,7 +101,7 @@ namespace ix
// the factory to create ConnectionState objects // the factory to create ConnectionState objects
ConnectionStateFactory _connectionStateFactory; ConnectionStateFactory _connectionStateFactory;
virtual void handleConnection(std::unique_ptr<Socket>, virtual void handleConnection(std::shared_ptr<Socket>,
std::shared_ptr<ConnectionState> connectionState) = 0; std::shared_ptr<ConnectionState> connectionState) = 0;
virtual size_t getConnectedClientsCount() = 0; virtual size_t getConnectedClientsCount() = 0;

View File

@ -1,96 +0,0 @@
/*
* IXUdpSocket.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#include "IXUdpSocket.h"
#include "IXNetSystem.h"
#include <cstring>
#include <sstream>
namespace ix
{
UdpSocket::UdpSocket(int fd)
: _sockfd(fd)
{
;
}
UdpSocket::~UdpSocket()
{
close();
}
void UdpSocket::close()
{
if (_sockfd == -1) return;
closeSocket(_sockfd);
_sockfd = -1;
}
int UdpSocket::getErrno()
{
int err;
#ifdef _WIN32
err = WSAGetLastError();
#else
err = errno;
#endif
return err;
}
void UdpSocket::closeSocket(int fd)
{
#ifdef _WIN32
closesocket(fd);
#else
::close(fd);
#endif
}
bool UdpSocket::init(const std::string& host, int port, std::string& errMsg)
{
_sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (_sockfd < 0)
{
errMsg = "Could not create socket";
return false;
}
memset(&_server, 0, sizeof(_server));
_server.sin_family = AF_INET;
_server.sin_port = htons(port);
// DNS resolution.
struct addrinfo hints, *result = nullptr;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
int ret = getaddrinfo(host.c_str(), nullptr, &hints, &result);
if (ret != 0)
{
errMsg = strerror(UdpSocket::getErrno());
freeaddrinfo(result);
close();
return false;
}
struct sockaddr_in* host_addr = (struct sockaddr_in*) result->ai_addr;
memcpy(&_server.sin_addr, &host_addr->sin_addr, sizeof(struct in_addr));
freeaddrinfo(result);
return true;
}
ssize_t UdpSocket::sendto(const std::string& buffer)
{
return (ssize_t)::sendto(
_sockfd, buffer.data(), buffer.size(), 0, (struct sockaddr*) &_server, sizeof(_server));
}
} // namespace ix

View File

@ -1,40 +0,0 @@
/*
* IXUdpSocket.h
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <atomic>
#include <memory>
#include <string>
#ifdef _WIN32
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
#include "IXNetSystem.h"
namespace ix
{
class UdpSocket
{
public:
UdpSocket(int fd = -1);
~UdpSocket();
// Virtual methods
bool init(const std::string& host, int port, std::string& errMsg);
ssize_t sendto(const std::string& buffer);
void close();
static int getErrno();
static void closeSocket(int fd);
private:
std::atomic<int> _sockfd;
struct sockaddr_in _server;
};
} // namespace ix

View File

@ -71,7 +71,7 @@ namespace ix
#elif defined(IXWEBSOCKET_USE_OPEN_SSL) #elif defined(IXWEBSOCKET_USE_OPEN_SSL)
ss << " ssl/OpenSSL " << OPENSSL_VERSION_TEXT; ss << " ssl/OpenSSL " << OPENSSL_VERSION_TEXT;
#elif __APPLE__ #elif __APPLE__
ss << " ssl/SecureTransport"; ss << " ssl/DarwinSSL";
#endif #endif
#else #else
ss << " nossl"; ss << " nossl";

View File

@ -19,6 +19,7 @@ namespace ix
OnTrafficTrackerCallback WebSocket::_onTrafficTrackerCallback = nullptr; OnTrafficTrackerCallback WebSocket::_onTrafficTrackerCallback = nullptr;
const int WebSocket::kDefaultHandShakeTimeoutSecs(60); const int WebSocket::kDefaultHandShakeTimeoutSecs(60);
const int WebSocket::kDefaultPingIntervalSecs(-1); const int WebSocket::kDefaultPingIntervalSecs(-1);
const int WebSocket::kDefaultPingTimeoutSecs(-1);
const bool WebSocket::kDefaultEnablePong(true); const bool WebSocket::kDefaultEnablePong(true);
const uint32_t WebSocket::kDefaultMaxWaitBetweenReconnectionRetries(10 * 1000); // 10s const uint32_t WebSocket::kDefaultMaxWaitBetweenReconnectionRetries(10 * 1000); // 10s
@ -30,11 +31,12 @@ namespace ix
, _handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs) , _handshakeTimeoutSecs(kDefaultHandShakeTimeoutSecs)
, _enablePong(kDefaultEnablePong) , _enablePong(kDefaultEnablePong)
, _pingIntervalSecs(kDefaultPingIntervalSecs) , _pingIntervalSecs(kDefaultPingIntervalSecs)
, _pingTimeoutSecs(kDefaultPingTimeoutSecs)
{ {
_ws.setOnCloseCallback( _ws.setOnCloseCallback(
[this](uint16_t code, const std::string& reason, size_t wireSize, bool remote) { [this](uint16_t code, const std::string& reason, size_t wireSize, bool remote) {
_onMessageCallback( _onMessageCallback(
std::make_unique<WebSocketMessage>(WebSocketMessageType::Close, std::make_shared<WebSocketMessage>(WebSocketMessageType::Close,
"", "",
wireSize, wireSize,
WebSocketErrorInfo(), WebSocketErrorInfo(),
@ -84,6 +86,18 @@ namespace ix
return _perMessageDeflateOptions; return _perMessageDeflateOptions;
} }
void WebSocket::setHeartBeatPeriod(int heartBeatPeriodSecs)
{
std::lock_guard<std::mutex> lock(_configMutex);
_pingIntervalSecs = heartBeatPeriodSecs;
}
int WebSocket::getHeartBeatPeriod() const
{
std::lock_guard<std::mutex> lock(_configMutex);
return _pingIntervalSecs;
}
void WebSocket::setPingInterval(int pingIntervalSecs) void WebSocket::setPingInterval(int pingIntervalSecs)
{ {
std::lock_guard<std::mutex> lock(_configMutex); std::lock_guard<std::mutex> lock(_configMutex);
@ -96,6 +110,18 @@ namespace ix
return _pingIntervalSecs; return _pingIntervalSecs;
} }
void WebSocket::setPingTimeout(int pingTimeoutSecs)
{
std::lock_guard<std::mutex> lock(_configMutex);
_pingTimeoutSecs = pingTimeoutSecs;
}
int WebSocket::getPingTimeout() const
{
std::lock_guard<std::mutex> lock(_configMutex);
return _pingTimeoutSecs;
}
void WebSocket::enablePong() void WebSocket::enablePong()
{ {
std::lock_guard<std::mutex> lock(_configMutex); std::lock_guard<std::mutex> lock(_configMutex);
@ -160,8 +186,11 @@ namespace ix
{ {
{ {
std::lock_guard<std::mutex> lock(_configMutex); std::lock_guard<std::mutex> lock(_configMutex);
_ws.configure( _ws.configure(_perMessageDeflateOptions,
_perMessageDeflateOptions, _socketTLSOptions, _enablePong, _pingIntervalSecs); _socketTLSOptions,
_enablePong,
_pingIntervalSecs,
_pingTimeoutSecs);
} }
WebSocketHttpHeaders headers(_extraHeaders); WebSocketHttpHeaders headers(_extraHeaders);
@ -193,51 +222,40 @@ namespace ix
return status; return status;
} }
_onMessageCallback(std::make_unique<WebSocketMessage>( _onMessageCallback(std::make_shared<WebSocketMessage>(
WebSocketMessageType::Open, WebSocketMessageType::Open,
"", "",
0, 0,
WebSocketErrorInfo(), WebSocketErrorInfo(),
WebSocketOpenInfo(status.uri, status.headers, status.protocol), WebSocketOpenInfo(status.uri, status.headers, status.protocol),
WebSocketCloseInfo())); WebSocketCloseInfo()));
if (_pingIntervalSecs > 0)
{
// Send a heart beat right away
_ws.sendHeartBeat();
}
return status; return status;
} }
WebSocketInitResult WebSocket::connectToSocket(std::unique_ptr<Socket> socket, int timeoutSecs) WebSocketInitResult WebSocket::connectToSocket(std::shared_ptr<Socket> socket, int timeoutSecs)
{ {
{ {
std::lock_guard<std::mutex> lock(_configMutex); std::lock_guard<std::mutex> lock(_configMutex);
_ws.configure( _ws.configure(_perMessageDeflateOptions,
_perMessageDeflateOptions, _socketTLSOptions, _enablePong, _pingIntervalSecs); _socketTLSOptions,
_enablePong,
_pingIntervalSecs,
_pingTimeoutSecs);
} }
WebSocketInitResult status = _ws.connectToSocket(std::move(socket), timeoutSecs); WebSocketInitResult status = _ws.connectToSocket(socket, timeoutSecs);
if (!status.success) if (!status.success)
{ {
return status; return status;
} }
_onMessageCallback( _onMessageCallback(
std::make_unique<WebSocketMessage>(WebSocketMessageType::Open, std::make_shared<WebSocketMessage>(WebSocketMessageType::Open,
"", "",
0, 0,
WebSocketErrorInfo(), WebSocketErrorInfo(),
WebSocketOpenInfo(status.uri, status.headers), WebSocketOpenInfo(status.uri, status.headers),
WebSocketCloseInfo())); WebSocketCloseInfo()));
if (_pingIntervalSecs > 0)
{
// Send a heart beat right away
_ws.sendHeartBeat();
}
return status; return status;
} }
@ -310,7 +328,7 @@ namespace ix
connectErr.reason = status.errorStr; connectErr.reason = status.errorStr;
connectErr.http_status = status.http_status; connectErr.http_status = status.http_status;
_onMessageCallback(std::make_unique<WebSocketMessage>(WebSocketMessageType::Error, _onMessageCallback(std::make_shared<WebSocketMessage>(WebSocketMessageType::Error,
"", "",
0, 0,
connectErr, connectErr,
@ -386,7 +404,7 @@ namespace ix
bool binary = messageKind == WebSocketTransport::MessageKind::MSG_BINARY; bool binary = messageKind == WebSocketTransport::MessageKind::MSG_BINARY;
_onMessageCallback(std::make_unique<WebSocketMessage>(webSocketMessageType, _onMessageCallback(std::make_shared<WebSocketMessage>(webSocketMessageType,
msg, msg,
wireSize, wireSize,
webSocketErrorInfo, webSocketErrorInfo,

View File

@ -52,7 +52,9 @@ namespace ix
void setPerMessageDeflateOptions( void setPerMessageDeflateOptions(
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions); const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions);
void setTLSOptions(const SocketTLSOptions& socketTLSOptions); void setTLSOptions(const SocketTLSOptions& socketTLSOptions);
void setPingInterval(int pingIntervalSecs); void setHeartBeatPeriod(int heartBeatPeriodSecs);
void setPingInterval(int pingIntervalSecs); // alias of setHeartBeatPeriod
void setPingTimeout(int pingTimeoutSecs);
void enablePong(); void enablePong();
void disablePong(); void disablePong();
void enablePerMessageDeflate(); void enablePerMessageDeflate();
@ -92,7 +94,9 @@ namespace ix
const std::string& getUrl() const; const std::string& getUrl() const;
const WebSocketPerMessageDeflateOptions& getPerMessageDeflateOptions() const; const WebSocketPerMessageDeflateOptions& getPerMessageDeflateOptions() const;
int getHeartBeatPeriod() const;
int getPingInterval() const; int getPingInterval() const;
int getPingTimeout() const;
size_t bufferedAmount() const; size_t bufferedAmount() const;
void enableAutomaticReconnection(); void enableAutomaticReconnection();
@ -113,7 +117,7 @@ namespace ix
static void invokeTrafficTrackerCallback(size_t size, bool incoming); static void invokeTrafficTrackerCallback(size_t size, bool incoming);
// Server // Server
WebSocketInitResult connectToSocket(std::unique_ptr<Socket>, int timeoutSecs); WebSocketInitResult connectToSocket(std::shared_ptr<Socket>, int timeoutSecs);
WebSocketTransport _ws; WebSocketTransport _ws;

View File

@ -6,9 +6,6 @@
#pragma once #pragma once
#include <cstdint>
#include <string>
namespace ix namespace ix
{ {
struct WebSocketCloseInfo struct WebSocketCloseInfo

View File

@ -6,7 +6,6 @@
#pragma once #pragma once
#include <cstdint>
#include <string> #include <string>
namespace ix namespace ix

View File

@ -21,8 +21,8 @@ namespace ix
{ {
WebSocketHandshake::WebSocketHandshake( WebSocketHandshake::WebSocketHandshake(
std::atomic<bool>& requestInitCancellation, std::atomic<bool>& requestInitCancellation,
std::unique_ptr<Socket>& socket, std::shared_ptr<Socket> socket,
WebSocketPerMessageDeflatePtr& perMessageDeflate, WebSocketPerMessageDeflate& perMessageDeflate,
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions, WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
std::atomic<bool>& enablePerMessageDeflate) std::atomic<bool>& enablePerMessageDeflate)
: _requestInitCancellation(requestInitCancellation) : _requestInitCancellation(requestInitCancellation)
@ -230,7 +230,7 @@ namespace ix
_enablePerMessageDeflate = false; _enablePerMessageDeflate = false;
} }
// Otherwise try to initialize the deflate engine (zlib) // Otherwise try to initialize the deflate engine (zlib)
else if (!_perMessageDeflate->init(webSocketPerMessageDeflateOptions)) else if (!_perMessageDeflate.init(webSocketPerMessageDeflateOptions))
{ {
return WebSocketInitResult( return WebSocketInitResult(
false, 0, "Failed to initialize per message deflate engine"); false, 0, "Failed to initialize per message deflate engine");
@ -341,7 +341,7 @@ namespace ix
{ {
_enablePerMessageDeflate = true; _enablePerMessageDeflate = true;
if (!_perMessageDeflate->init(webSocketPerMessageDeflateOptions)) if (!_perMessageDeflate.init(webSocketPerMessageDeflateOptions))
{ {
return WebSocketInitResult( return WebSocketInitResult(
false, 0, "Failed to initialize per message deflate engine"); false, 0, "Failed to initialize per message deflate engine");

View File

@ -23,8 +23,8 @@ namespace ix
{ {
public: public:
WebSocketHandshake(std::atomic<bool>& requestInitCancellation, WebSocketHandshake(std::atomic<bool>& requestInitCancellation,
std::unique_ptr<Socket>& _socket, std::shared_ptr<Socket> _socket,
WebSocketPerMessageDeflatePtr& perMessageDeflate, WebSocketPerMessageDeflate& perMessageDeflate,
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions, WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
std::atomic<bool>& enablePerMessageDeflate); std::atomic<bool>& enablePerMessageDeflate);
@ -46,8 +46,8 @@ namespace ix
bool insensitiveStringCompare(const std::string& a, const std::string& b); bool insensitiveStringCompare(const std::string& a, const std::string& b);
std::atomic<bool>& _requestInitCancellation; std::atomic<bool>& _requestInitCancellation;
std::unique_ptr<Socket>& _socket; std::shared_ptr<Socket> _socket;
WebSocketPerMessageDeflatePtr& _perMessageDeflate; WebSocketPerMessageDeflate& _perMessageDeflate;
WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions; WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions;
std::atomic<bool>& _enablePerMessageDeflate; std::atomic<bool>& _enablePerMessageDeflate;
}; };

View File

@ -32,7 +32,7 @@ namespace ix
} }
std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders( std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders(
std::unique_ptr<Socket>& socket, const CancellationRequest& isCancellationRequested) std::shared_ptr<Socket> socket, const CancellationRequest& isCancellationRequested)
{ {
WebSocketHttpHeaders headers; WebSocketHttpHeaders headers;

View File

@ -29,5 +29,5 @@ namespace ix
using WebSocketHttpHeaders = std::map<std::string, std::string, CaseInsensitiveLess>; using WebSocketHttpHeaders = std::map<std::string, std::string, CaseInsensitiveLess>;
std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders( std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders(
std::unique_ptr<Socket>& socket, const CancellationRequest& isCancellationRequested); std::shared_ptr<Socket> socket, const CancellationRequest& isCancellationRequested);
} // namespace ix } // namespace ix

View File

@ -19,7 +19,7 @@ namespace ix
struct WebSocketMessage struct WebSocketMessage
{ {
WebSocketMessageType type; WebSocketMessageType type;
const std::string& str; std::string str;
size_t wireSize; size_t wireSize;
WebSocketErrorInfo errorInfo; WebSocketErrorInfo errorInfo;
WebSocketOpenInfo openInfo; WebSocketOpenInfo openInfo;
@ -34,7 +34,7 @@ namespace ix
WebSocketCloseInfo c, WebSocketCloseInfo c,
bool b = false) bool b = false)
: type(t) : type(t)
, str(s) , str(std::move(s))
, wireSize(w) , wireSize(w)
, errorInfo(e) , errorInfo(e)
, openInfo(o) , openInfo(o)
@ -45,5 +45,5 @@ namespace ix
} }
}; };
using WebSocketMessagePtr = std::unique_ptr<WebSocketMessage>; using WebSocketMessagePtr = std::shared_ptr<WebSocketMessage>;
} // namespace ix } // namespace ix

View File

@ -0,0 +1,86 @@
/*
* IXWebSocketMessageQueue.cpp
* Author: Korchynskyi Dmytro
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
*/
#include "IXWebSocketMessageQueue.h"
namespace ix
{
WebSocketMessageQueue::WebSocketMessageQueue(WebSocket* websocket)
{
bindWebsocket(websocket);
}
WebSocketMessageQueue::~WebSocketMessageQueue()
{
if (!_messages.empty())
{
// not handled all messages
}
bindWebsocket(nullptr);
}
void WebSocketMessageQueue::bindWebsocket(WebSocket* websocket)
{
if (_websocket == websocket) return;
// unbind old
if (_websocket)
{
// set dummy callback just to avoid crash
_websocket->setOnMessageCallback([](const WebSocketMessagePtr&) {});
}
_websocket = websocket;
// bind new
if (_websocket)
{
_websocket->setOnMessageCallback([this](const WebSocketMessagePtr& msg) {
std::lock_guard<std::mutex> lock(_messagesMutex);
_messages.emplace_back(std::move(msg));
});
}
}
void WebSocketMessageQueue::setOnMessageCallback(const OnMessageCallback& callback)
{
_onMessageUserCallback = callback;
}
void WebSocketMessageQueue::setOnMessageCallback(OnMessageCallback&& callback)
{
_onMessageUserCallback = std::move(callback);
}
WebSocketMessagePtr WebSocketMessageQueue::popMessage()
{
WebSocketMessagePtr message;
std::lock_guard<std::mutex> lock(_messagesMutex);
if (!_messages.empty())
{
message = std::move(_messages.front());
_messages.pop_front();
}
return message;
}
void WebSocketMessageQueue::poll(int count)
{
if (!_onMessageUserCallback) return;
WebSocketMessagePtr message;
while (count > 0 && (message = popMessage()))
{
_onMessageUserCallback(message);
--count;
}
}
} // namespace ix

View File

@ -0,0 +1,41 @@
/*
* IXWebSocketMessageQueue.h
* Author: Korchynskyi Dmytro
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include "IXWebSocket.h"
#include <list>
#include <memory>
#include <thread>
namespace ix
{
//
// A helper class to dispatch websocket message callbacks in your thread.
//
class WebSocketMessageQueue
{
public:
WebSocketMessageQueue(WebSocket* websocket = nullptr);
~WebSocketMessageQueue();
void bindWebsocket(WebSocket* websocket);
void setOnMessageCallback(const OnMessageCallback& callback);
void setOnMessageCallback(OnMessageCallback&& callback);
void poll(int count = 512);
protected:
WebSocketMessagePtr popMessage();
private:
WebSocket* _websocket = nullptr;
OnMessageCallback _onMessageUserCallback;
std::mutex _messagesMutex;
std::list<WebSocketMessagePtr> _messages;
};
} // namespace ix

View File

@ -6,10 +6,6 @@
#pragma once #pragma once
#include "IXWebSocketHttpHeaders.h"
#include <cstdint>
#include <string>
namespace ix namespace ix
{ {
struct WebSocketOpenInfo struct WebSocketOpenInfo

View File

@ -57,6 +57,4 @@ namespace ix
std::unique_ptr<WebSocketPerMessageDeflateCompressor> _compressor; std::unique_ptr<WebSocketPerMessageDeflateCompressor> _compressor;
std::unique_ptr<WebSocketPerMessageDeflateDecompressor> _decompressor; std::unique_ptr<WebSocketPerMessageDeflateDecompressor> _decompressor;
}; };
using WebSocketPerMessageDeflatePtr = std::unique_ptr<WebSocketPerMessageDeflate>;
} // namespace ix } // namespace ix

View File

@ -87,17 +87,10 @@ namespace ix
// //
size_t output; size_t output;
// Clear output
out.clear();
if (in.empty()) if (in.empty())
{ {
// See issue #167 uint8_t buf[6] = {0x02, 0x00, 0x00, 0x00, 0xff, 0xff};
// The normal buffer size should be 6 but out.append((char*) (buf), 6);
// we remove the 4 octets from the tail (#4)
uint8_t buf[2] = {0x02, 0x00};
out.append((char*) (buf), 2);
return true; return true;
} }
@ -177,9 +170,6 @@ namespace ix
_inflateState.avail_in = (uInt) inFixed.size(); _inflateState.avail_in = (uInt) inFixed.size();
_inflateState.next_in = (unsigned char*) (const_cast<char*>(inFixed.data())); _inflateState.next_in = (unsigned char*) (const_cast<char*>(inFixed.data()));
// Clear output
out.clear();
do do
{ {
_inflateState.avail_out = (uInt) _compressBufferSize; _inflateState.avail_out = (uInt) _compressBufferSize;

View File

@ -71,7 +71,7 @@ namespace ix
_onConnectionCallback = callback; _onConnectionCallback = callback;
} }
void WebSocketServer::handleConnection(std::unique_ptr<Socket> socket, void WebSocketServer::handleConnection(std::shared_ptr<Socket> socket,
std::shared_ptr<ConnectionState> connectionState) std::shared_ptr<ConnectionState> connectionState)
{ {
setThreadName("WebSocketServer::" + connectionState->getId()); setThreadName("WebSocketServer::" + connectionState->getId());
@ -96,7 +96,7 @@ namespace ix
_clients.insert(webSocket); _clients.insert(webSocket);
} }
auto status = webSocket->connectToSocket(std::move(socket), _handshakeTimeoutSecs); auto status = webSocket->connectToSocket(socket, _handshakeTimeoutSecs);
if (status.success) if (status.success)
{ {
// Process incoming messages and execute callbacks // Process incoming messages and execute callbacks

View File

@ -59,7 +59,7 @@ namespace ix
const static bool kDefaultEnablePong; const static bool kDefaultEnablePong;
// Methods // Methods
virtual void handleConnection(std::unique_ptr<Socket> socket, virtual void handleConnection(std::shared_ptr<Socket> socket,
std::shared_ptr<ConnectionState> connectionState) final; std::shared_ptr<ConnectionState> connectionState) final;
virtual size_t getConnectedClientsCount() final; virtual size_t getConnectedClientsCount() final;
}; };

View File

@ -51,10 +51,26 @@
#include <vector> #include <vector>
namespace
{
int greatestCommonDivisor(int a, int b)
{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
} // namespace
namespace ix namespace ix
{ {
const std::string WebSocketTransport::kPingMessage("ixwebsocket::heartbeat"); const std::string WebSocketTransport::kPingMessage("ixwebsocket::heartbeat");
const int WebSocketTransport::kDefaultPingIntervalSecs(-1); const int WebSocketTransport::kDefaultPingIntervalSecs(-1);
const int WebSocketTransport::kDefaultPingTimeoutSecs(-1);
const bool WebSocketTransport::kDefaultEnablePong(true); const bool WebSocketTransport::kDefaultEnablePong(true);
const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(300); const int WebSocketTransport::kClosingMaximumWaitingDelayInMs(300);
constexpr size_t WebSocketTransport::kChunkSize; constexpr size_t WebSocketTransport::kChunkSize;
@ -62,7 +78,7 @@ namespace ix
WebSocketTransport::WebSocketTransport() WebSocketTransport::WebSocketTransport()
: _useMask(true) : _useMask(true)
, _blockingSend(false) , _blockingSend(false)
, _receivedMessageCompressed(false) , _compressedMessage(false)
, _readyState(ReadyState::CLOSED) , _readyState(ReadyState::CLOSED)
, _closeCode(WebSocketCloseConstants::kInternalErrorCode) , _closeCode(WebSocketCloseConstants::kInternalErrorCode)
, _closeReason(WebSocketCloseConstants::kInternalErrorMessage) , _closeReason(WebSocketCloseConstants::kInternalErrorMessage)
@ -73,9 +89,11 @@ namespace ix
, _closingTimePoint(std::chrono::steady_clock::now()) , _closingTimePoint(std::chrono::steady_clock::now())
, _enablePong(kDefaultEnablePong) , _enablePong(kDefaultEnablePong)
, _pingIntervalSecs(kDefaultPingIntervalSecs) , _pingIntervalSecs(kDefaultPingIntervalSecs)
, _pongReceived(false) , _pingTimeoutSecs(kDefaultPingTimeoutSecs)
, _pingCount(0) , _pingIntervalOrTimeoutGCDSecs(-1)
, _nextGCDTimePoint(std::chrono::steady_clock::now())
, _lastSendPingTimePoint(std::chrono::steady_clock::now()) , _lastSendPingTimePoint(std::chrono::steady_clock::now())
, _lastReceivePongTimePoint(std::chrono::steady_clock::now())
{ {
_readbuf.resize(kChunkSize); _readbuf.resize(kChunkSize);
} }
@ -89,13 +107,29 @@ namespace ix
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions, const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
const SocketTLSOptions& socketTLSOptions, const SocketTLSOptions& socketTLSOptions,
bool enablePong, bool enablePong,
int pingIntervalSecs) int pingIntervalSecs,
int pingTimeoutSecs)
{ {
_perMessageDeflateOptions = perMessageDeflateOptions; _perMessageDeflateOptions = perMessageDeflateOptions;
_enablePerMessageDeflate = _perMessageDeflateOptions.enabled(); _enablePerMessageDeflate = _perMessageDeflateOptions.enabled();
_socketTLSOptions = socketTLSOptions; _socketTLSOptions = socketTLSOptions;
_enablePong = enablePong; _enablePong = enablePong;
_pingIntervalSecs = pingIntervalSecs; _pingIntervalSecs = pingIntervalSecs;
_pingTimeoutSecs = pingTimeoutSecs;
if (pingIntervalSecs > 0 && pingTimeoutSecs > 0)
{
_pingIntervalOrTimeoutGCDSecs =
greatestCommonDivisor(pingIntervalSecs, pingTimeoutSecs);
}
else if (_pingTimeoutSecs > 0)
{
_pingIntervalOrTimeoutGCDSecs = pingTimeoutSecs;
}
else
{
_pingIntervalOrTimeoutGCDSecs = pingIntervalSecs;
}
} }
// Client // Client
@ -118,7 +152,6 @@ namespace ix
std::string errorMsg; std::string errorMsg;
bool tls = protocol == "wss"; bool tls = protocol == "wss";
_socket = createSocket(tls, -1, errorMsg, _socketTLSOptions); _socket = createSocket(tls, -1, errorMsg, _socketTLSOptions);
_perMessageDeflate = std::make_unique<WebSocketPerMessageDeflate>();
if (!_socket) if (!_socket)
{ {
@ -141,7 +174,7 @@ namespace ix
} }
// Server // Server
WebSocketInitResult WebSocketTransport::connectToSocket(std::unique_ptr<Socket> socket, WebSocketInitResult WebSocketTransport::connectToSocket(std::shared_ptr<Socket> socket,
int timeoutSecs) int timeoutSecs)
{ {
std::lock_guard<std::mutex> lock(_socketMutex); std::lock_guard<std::mutex> lock(_socketMutex);
@ -150,8 +183,7 @@ namespace ix
_useMask = false; _useMask = false;
_blockingSend = true; _blockingSend = true;
_socket = std::move(socket); _socket = socket;
_perMessageDeflate = std::make_unique<WebSocketPerMessageDeflate>();
WebSocketHandshake webSocketHandshake(_requestInitCancellation, WebSocketHandshake webSocketHandshake(_requestInitCancellation,
_socket, _socket,
@ -188,8 +220,7 @@ namespace ix
} }
else if (readyState == ReadyState::OPEN) else if (readyState == ReadyState::OPEN)
{ {
initTimePointsAfterConnect(); initTimePointsAndGCDAfterConnect();
_pongReceived = false;
} }
_readyState = readyState; _readyState = readyState;
@ -200,12 +231,22 @@ namespace ix
_onCloseCallback = onCloseCallback; _onCloseCallback = onCloseCallback;
} }
void WebSocketTransport::initTimePointsAfterConnect() void WebSocketTransport::initTimePointsAndGCDAfterConnect()
{ {
{ {
std::lock_guard<std::mutex> lock(_lastSendPingTimePointMutex); std::lock_guard<std::mutex> lock(_lastSendPingTimePointMutex);
_lastSendPingTimePoint = std::chrono::steady_clock::now(); _lastSendPingTimePoint = std::chrono::steady_clock::now();
} }
{
std::lock_guard<std::mutex> lock(_lastReceivePongTimePointMutex);
_lastReceivePongTimePoint = std::chrono::steady_clock::now();
}
if (_pingIntervalOrTimeoutGCDSecs > 0)
{
_nextGCDTimePoint = std::chrono::steady_clock::now() +
std::chrono::seconds(_pingIntervalOrTimeoutGCDSecs);
}
} }
// Only consider send PING time points for that computation. // Only consider send PING time points for that computation.
@ -218,13 +259,13 @@ namespace ix
return now - _lastSendPingTimePoint > std::chrono::seconds(_pingIntervalSecs); return now - _lastSendPingTimePoint > std::chrono::seconds(_pingIntervalSecs);
} }
WebSocketSendInfo WebSocketTransport::sendHeartBeat() bool WebSocketTransport::pingTimeoutExceeded()
{ {
_pongReceived = false; if (_pingTimeoutSecs <= 0) return false;
std::stringstream ss;
ss << kPingMessage << "::" << _pingIntervalSecs << "s" std::lock_guard<std::mutex> lock(_lastReceivePongTimePointMutex);
<< "::" << _pingCount++; auto now = std::chrono::steady_clock::now();
return sendPing(ss.str()); return now - _lastReceivePongTimePoint > std::chrono::seconds(_pingTimeoutSecs);
} }
bool WebSocketTransport::closingDelayExceeded() bool WebSocketTransport::closingDelayExceeded()
@ -238,32 +279,46 @@ namespace ix
{ {
if (_readyState == ReadyState::OPEN) if (_readyState == ReadyState::OPEN)
{ {
if (pingIntervalExceeded()) // if (1) ping timeout is enabled and (2) duration since last received
// ping response (PONG) exceeds the maximum delay, then close the connection
if (pingTimeoutExceeded())
{ {
if (!_pongReceived) close(WebSocketCloseConstants::kInternalErrorCode,
{ WebSocketCloseConstants::kPingTimeoutMessage);
// ping response (PONG) exceeds the maximum delay, close the connection }
close(WebSocketCloseConstants::kInternalErrorCode, // If ping is enabled and no ping has been sent for a duration
WebSocketCloseConstants::kPingTimeoutMessage); // exceeding our ping interval, send a ping to the server.
} else if (pingIntervalExceeded())
else {
{ std::stringstream ss;
sendHeartBeat(); ss << kPingMessage << "::" << _pingIntervalSecs << "s";
} sendPing(ss.str());
} }
} }
// No timeout if state is not OPEN, otherwise computed // No timeout if state is not OPEN, otherwise computed
// pingIntervalOrTimeoutGCD (equals to -1 if no ping and no ping timeout are set) // pingIntervalOrTimeoutGCD (equals to -1 if no ping and no ping timeout are set)
int lastingTimeoutDelayInMs = (_readyState != ReadyState::OPEN) ? 0 : _pingIntervalSecs; int lastingTimeoutDelayInMs =
(_readyState != ReadyState::OPEN) ? 0 : _pingIntervalOrTimeoutGCDSecs;
if (_pingIntervalSecs > 0) if (_pingIntervalOrTimeoutGCDSecs > 0)
{ {
// compute lasting delay to wait for next ping / timeout, if at least one set // compute lasting delay to wait for next ping / timeout, if at least one set
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
lastingTimeoutDelayInMs = (int) std::chrono::duration_cast<std::chrono::milliseconds>(
now - _lastSendPingTimePoint) if (now >= _nextGCDTimePoint)
.count(); {
_nextGCDTimePoint = now + std::chrono::seconds(_pingIntervalOrTimeoutGCDSecs);
lastingTimeoutDelayInMs = _pingIntervalOrTimeoutGCDSecs * 1000;
}
else
{
lastingTimeoutDelayInMs =
(int) std::chrono::duration_cast<std::chrono::milliseconds>(_nextGCDTimePoint -
now)
.count();
}
} }
#ifdef _WIN32 #ifdef _WIN32
@ -495,7 +550,7 @@ namespace ix
? MessageKind::MSG_TEXT ? MessageKind::MSG_TEXT
: MessageKind::MSG_BINARY; : MessageKind::MSG_BINARY;
_receivedMessageCompressed = _enablePerMessageDeflate && ws.rsv1; _compressedMessage = _enablePerMessageDeflate && ws.rsv1;
// Continuation message needs to follow a non-fin TEXT or BINARY message // Continuation message needs to follow a non-fin TEXT or BINARY message
if (!_chunks.empty()) if (!_chunks.empty())
@ -517,12 +572,10 @@ namespace ix
// //
if (ws.fin && _chunks.empty()) if (ws.fin && _chunks.empty())
{ {
emitMessage(_fragmentedMessageKind, emitMessage(
frameData, _fragmentedMessageKind, frameData, _compressedMessage, onMessageCallback);
_receivedMessageCompressed,
onMessageCallback);
_receivedMessageCompressed = false; _compressedMessage = false;
} }
else else
{ {
@ -539,11 +592,11 @@ namespace ix
{ {
emitMessage(_fragmentedMessageKind, emitMessage(_fragmentedMessageKind,
getMergedChunks(), getMergedChunks(),
_receivedMessageCompressed, _compressedMessage,
onMessageCallback); onMessageCallback);
_chunks.clear(); _chunks.clear();
_receivedMessageCompressed = false; _compressedMessage = false;
} }
else else
{ {
@ -573,7 +626,9 @@ namespace ix
} }
else if (ws.opcode == wsheader_type::PONG) else if (ws.opcode == wsheader_type::PONG)
{ {
_pongReceived = true; std::lock_guard<std::mutex> lck(_lastReceivePongTimePointMutex);
_lastReceivePongTimePoint = std::chrono::steady_clock::now();
emitMessage(MessageKind::PONG, frameData, false, onMessageCallback); emitMessage(MessageKind::PONG, frameData, false, onMessageCallback);
} }
else if (ws.opcode == wsheader_type::CLOSE) else if (ws.opcode == wsheader_type::CLOSE)
@ -629,7 +684,7 @@ namespace ix
// send back the CLOSE frame // send back the CLOSE frame
sendCloseFrame(code, reason); sendCloseFrame(code, reason);
wakeUpFromPoll(Socket::kCloseRequest); _socket->wakeUpFromPoll(Socket::kCloseRequest);
bool remote = true; bool remote = true;
closeSocketAndSwitchToClosedState(code, reason, _rxbuf.size(), remote); closeSocketAndSwitchToClosedState(code, reason, _rxbuf.size(), remote);
@ -716,16 +771,17 @@ namespace ix
// When the RSV1 bit is 1 it means the message is compressed // When the RSV1 bit is 1 it means the message is compressed
if (compressedMessage && messageKind != MessageKind::FRAGMENT) if (compressedMessage && messageKind != MessageKind::FRAGMENT)
{ {
bool success = _perMessageDeflate->decompress(message, _decompressedMessage); std::string decompressedMessage;
bool success = _perMessageDeflate.decompress(message, decompressedMessage);
if (messageKind == MessageKind::MSG_TEXT && !validateUtf8(_decompressedMessage)) if (messageKind == MessageKind::MSG_TEXT && !validateUtf8(decompressedMessage))
{ {
close(WebSocketCloseConstants::kInvalidFramePayloadData, close(WebSocketCloseConstants::kInvalidFramePayloadData,
WebSocketCloseConstants::kInvalidFramePayloadDataMessage); WebSocketCloseConstants::kInvalidFramePayloadDataMessage);
} }
else else
{ {
onMessageCallback(_decompressedMessage, wireSize, !success, messageKind); onMessageCallback(decompressedMessage, wireSize, !success, messageKind);
} }
} }
else else
@ -762,6 +818,7 @@ namespace ix
size_t payloadSize = message.size(); size_t payloadSize = message.size();
size_t wireSize = message.size(); size_t wireSize = message.size();
std::string compressedMessage;
bool compressionError = false; bool compressionError = false;
std::string::const_iterator message_begin = message.begin(); std::string::const_iterator message_begin = message.begin();
@ -769,7 +826,7 @@ namespace ix
if (compress) if (compress)
{ {
if (!_perMessageDeflate->compress(message, _compressedMessage)) if (!_perMessageDeflate.compress(message, compressedMessage))
{ {
bool success = false; bool success = false;
compressionError = true; compressionError = true;
@ -778,10 +835,10 @@ namespace ix
return WebSocketSendInfo(success, compressionError, payloadSize, wireSize); return WebSocketSendInfo(success, compressionError, payloadSize, wireSize);
} }
compressionError = false; compressionError = false;
wireSize = _compressedMessage.size(); wireSize = compressedMessage.size();
message_begin = _compressedMessage.begin(); message_begin = compressedMessage.begin();
message_end = _compressedMessage.end(); message_end = compressedMessage.end();
} }
{ {
@ -847,7 +904,7 @@ namespace ix
// Request to flush the send buffer on the background thread if it isn't empty // Request to flush the send buffer on the background thread if it isn't empty
if (!isSendBufferEmpty()) if (!isSendBufferEmpty())
{ {
wakeUpFromPoll(Socket::kSendRequest); _socket->wakeUpFromPoll(Socket::kSendRequest);
// FIXME: we should have a timeout when sending large messages: see #131 // FIXME: we should have a timeout when sending large messages: see #131
if (_blockingSend && !flushSendBuffer()) if (_blockingSend && !flushSendBuffer())
@ -1065,12 +1122,6 @@ namespace ix
_socket->close(); _socket->close();
} }
bool WebSocketTransport::wakeUpFromPoll(uint64_t wakeUpCode)
{
std::lock_guard<std::mutex> lock(_socketMutex);
return _socket->wakeUpFromPoll(wakeUpCode);
}
void WebSocketTransport::closeSocketAndSwitchToClosedState(uint16_t code, void WebSocketTransport::closeSocketAndSwitchToClosedState(uint16_t code,
const std::string& reason, const std::string& reason,
size_t closeWireSize, size_t closeWireSize,
@ -1118,9 +1169,8 @@ namespace ix
setReadyState(ReadyState::CLOSING); setReadyState(ReadyState::CLOSING);
sendCloseFrame(code, reason); sendCloseFrame(code, reason);
// wake up the poll, but do not close yet // wake up the poll, but do not close yet
wakeUpFromPoll(Socket::kSendRequest); _socket->wakeUpFromPoll(Socket::kSendRequest);
} }
size_t WebSocketTransport::bufferedAmount() const size_t WebSocketTransport::bufferedAmount() const

View File

@ -75,7 +75,8 @@ namespace ix
void configure(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions, void configure(const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
const SocketTLSOptions& socketTLSOptions, const SocketTLSOptions& socketTLSOptions,
bool enablePong, bool enablePong,
int pingIntervalSecs); int pingIntervalSecs,
int pingTimeoutSecs);
// Client // Client
WebSocketInitResult connectToUrl(const std::string& url, WebSocketInitResult connectToUrl(const std::string& url,
@ -83,7 +84,7 @@ namespace ix
int timeoutSecs); int timeoutSecs);
// Server // Server
WebSocketInitResult connectToSocket(std::unique_ptr<Socket> socket, int timeoutSecs); WebSocketInitResult connectToSocket(std::shared_ptr<Socket> socket, int timeoutSecs);
PollResult poll(); PollResult poll();
WebSocketSendInfo sendBinary(const std::string& message, WebSocketSendInfo sendBinary(const std::string& message,
@ -105,9 +106,6 @@ namespace ix
void dispatch(PollResult pollResult, const OnMessageCallback& onMessageCallback); void dispatch(PollResult pollResult, const OnMessageCallback& onMessageCallback);
size_t bufferedAmount() const; size_t bufferedAmount() const;
// internal
WebSocketSendInfo sendHeartBeat();
private: private:
std::string _url; std::string _url;
@ -165,13 +163,13 @@ namespace ix
MessageKind _fragmentedMessageKind; MessageKind _fragmentedMessageKind;
// Ditto for whether a message is compressed // Ditto for whether a message is compressed
bool _receivedMessageCompressed; bool _compressedMessage;
// Fragments are 32K long // Fragments are 32K long
static constexpr size_t kChunkSize = 1 << 15; static constexpr size_t kChunkSize = 1 << 15;
// Underlying TCP socket // Underlying TCP socket
std::unique_ptr<Socket> _socket; std::shared_ptr<Socket> _socket;
std::mutex _socketMutex; std::mutex _socketMutex;
// Hold the state of the connection (OPEN, CLOSED, etc...) // Hold the state of the connection (OPEN, CLOSED, etc...)
@ -185,13 +183,10 @@ namespace ix
mutable std::mutex _closeDataMutex; mutable std::mutex _closeDataMutex;
// Data used for Per Message Deflate compression (with zlib) // Data used for Per Message Deflate compression (with zlib)
WebSocketPerMessageDeflatePtr _perMessageDeflate; WebSocketPerMessageDeflate _perMessageDeflate;
WebSocketPerMessageDeflateOptions _perMessageDeflateOptions; WebSocketPerMessageDeflateOptions _perMessageDeflateOptions;
std::atomic<bool> _enablePerMessageDeflate; std::atomic<bool> _enablePerMessageDeflate;
std::string _decompressedMessage;
std::string _compressedMessage;
// Used to control TLS connection behavior // Used to control TLS connection behavior
SocketTLSOptions _socketTLSOptions; SocketTLSOptions _socketTLSOptions;
@ -207,25 +202,39 @@ namespace ix
static const bool kDefaultEnablePong; static const bool kDefaultEnablePong;
// Optional ping and pong timeout // Optional ping and pong timeout
// if both ping interval and timeout are set (> 0),
// then use GCD of these value to wait for the lowest time
int _pingIntervalSecs; int _pingIntervalSecs;
std::atomic<bool> _pongReceived; int _pingTimeoutSecs;
int _pingIntervalOrTimeoutGCDSecs;
static const int kDefaultPingIntervalSecs; static const int kDefaultPingIntervalSecs;
static const int kDefaultPingTimeoutSecs;
static const std::string kPingMessage; static const std::string kPingMessage;
std::atomic<uint64_t> _pingCount;
// Record time step for ping/ ping timeout to ensure we wait for the right left duration
std::chrono::time_point<std::chrono::steady_clock> _nextGCDTimePoint;
// We record when ping are being sent so that we can know when to send the next one // We record when ping are being sent so that we can know when to send the next one
// We also record when pong are being sent as a reply to pings, to close the connections
// if no pong were received sufficiently fast.
mutable std::mutex _lastSendPingTimePointMutex; mutable std::mutex _lastSendPingTimePointMutex;
mutable std::mutex _lastReceivePongTimePointMutex;
std::chrono::time_point<std::chrono::steady_clock> _lastSendPingTimePoint; std::chrono::time_point<std::chrono::steady_clock> _lastSendPingTimePoint;
std::chrono::time_point<std::chrono::steady_clock> _lastReceivePongTimePoint;
// If this function returns true, it is time to send a new ping // If this function returns true, it is time to send a new ping
bool pingIntervalExceeded(); bool pingIntervalExceeded();
void initTimePointsAfterConnect();
// No PONG data was received through the socket for longer than ping timeout delay
bool pingTimeoutExceeded();
// after calling close(), if no CLOSE frame answer is received back from the remote, we // after calling close(), if no CLOSE frame answer is received back from the remote, we
// should close the connexion // should close the connexion
bool closingDelayExceeded(); bool closingDelayExceeded();
void initTimePointsAndGCDAfterConnect();
void sendCloseFrame(uint16_t code, const std::string& reason); void sendCloseFrame(uint16_t code, const std::string& reason);
void closeSocketAndSwitchToClosedState(uint16_t code, void closeSocketAndSwitchToClosedState(uint16_t code,
@ -233,8 +242,6 @@ namespace ix
size_t closeWireSize, size_t closeWireSize,
bool remote); bool remote);
bool wakeUpFromPoll(uint64_t wakeUpCode);
bool flushSendBuffer(); bool flushSendBuffer();
bool sendOnSocket(); bool sendOnSocket();
bool receiveFromSocket(); bool receiveFromSocket();

View File

@ -6,4 +6,4 @@
#pragma once #pragma once
#define IX_WEBSOCKET_VERSION "9.3.3" #define IX_WEBSOCKET_VERSION "8.1.7"

View File

@ -1,13 +1,5 @@
# #
# This makefile is used for convenience, and wrap simple cmake commands # This makefile is just used to easily work with docker (linux build)
# You don't need to use it as an end user, it is more for developer.
#
# * work with docker (linux build)
# * execute the unittest
#
# The default target will install ws, the command line tool coming with
# IXWebSocket into /usr/local/bin
#
# #
all: brew all: brew
@ -16,23 +8,14 @@ install: brew
# Use -DCMAKE_INSTALL_PREFIX= to install into another location # Use -DCMAKE_INSTALL_PREFIX= to install into another location
# on osx it is good practice to make /usr/local user writable # on osx it is good practice to make /usr/local user writable
# sudo chown -R `whoami`/staff /usr/local # sudo chown -R `whoami`/staff /usr/local
#
# Release, Debug, MinSizeRel, RelWithDebInfo are the build types
#
brew: brew:
mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_WS=1 -DUSE_TEST=1 .. ; make -j 4 install) mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_WS=1 -DUSE_TEST=1 .. ; make -j 4 install)
# Docker default target. We've add problem with OpenSSL and TLS 1.3 (on the
# server side ?) and I can't work-around it easily, so we're using mbedtls on
# Linux for the SSL backend, which works great.
ws_mbedtls_install:
mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DUSE_TLS=1 -DUSE_WS=1 -DUSE_MBED_TLS=1 .. ; make -j 4 install)
ws: ws:
mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_WS=1 .. ; make -j 4) mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_WS=1 .. ; make -j 4)
ws_install: ws_install:
mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DUSE_TLS=1 -DUSE_WS=1 .. ; make -j 4 install) mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_WS=1 .. ; make -j 4 install)
ws_openssl: ws_openssl:
mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_WS=1 -DUSE_OPEN_SSL=1 .. ; make -j 4) mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_WS=1 -DUSE_OPEN_SSL=1 .. ; make -j 4)
@ -82,8 +65,6 @@ docker_push:
docker push ${LATEST} docker push ${LATEST}
docker push ${IMG} docker push ${IMG}
deploy: docker docker_push
run: run:
docker run --cap-add sys_ptrace --entrypoint=sh -it bsergean/ws:build docker run --cap-add sys_ptrace --entrypoint=sh -it bsergean/ws:build
@ -106,53 +87,12 @@ test:
mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_WS=1 -DUSE_TEST=1 .. ; make -j 4) mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_WS=1 -DUSE_TEST=1 .. ; make -j 4)
(cd test ; python2.7 run.py -r) (cd test ; python2.7 run.py -r)
test_tsan: test_openssl:
mkdir -p build && (cd build && cmake -GXcode -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 .. && xcodebuild -project ixwebsocket.xcodeproj -target ixwebsocket_unittest -enableThreadSanitizer YES)
(cd build/test ; ln -sf Debug/ixwebsocket_unittest)
(cd test ; python2.7 run.py -r)
test_ubsan:
mkdir -p build && (cd build && cmake -GXcode -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 .. && xcodebuild -project ixwebsocket.xcodeproj -target ixwebsocket_unittest -enableUndefinedBehaviorSanitizer YES)
(cd build/test ; ln -sf Debug/ixwebsocket_unittest)
(cd test ; python2.7 run.py -r)
test_asan: build_test_asan
(cd test ; python2.7 run.py -r)
build_test_asan:
mkdir -p build && (cd build && cmake -GXcode -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 .. && xcodebuild -project ixwebsocket.xcodeproj -target ixwebsocket_unittest -enableAddressSanitizer YES)
(cd build/test ; ln -sf Debug/ixwebsocket_unittest)
test_tsan_openssl:
mkdir -p build && (cd build && cmake -GXcode -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 -DUSE_OPEN_SSL=1 .. && xcodebuild -project ixwebsocket.xcodeproj -target ixwebsocket_unittest -enableThreadSanitizer YES)
(cd build/test ; ln -sf Debug/ixwebsocket_unittest)
(cd test ; python2.7 run.py -r)
test_ubsan_openssl:
mkdir -p build && (cd build && cmake -GXcode -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 -DUSE_OPEN_SSL=1 .. && xcodebuild -project ixwebsocket.xcodeproj -target ixwebsocket_unittest -enableUndefinedBehaviorSanitizer YES)
(cd build/test ; ln -sf Debug/ixwebsocket_unittest)
(cd test ; python2.7 run.py -r)
test_tsan_openssl_release:
mkdir -p build && (cd build && cmake -GXcode -DCMAKE_BUILD_TYPE=Release -DUSE_TLS=1 -DUSE_TEST=1 -DUSE_OPEN_SSL=1 .. && xcodebuild -project ixwebsocket.xcodeproj -configuration Release -target ixwebsocket_unittest -enableThreadSanitizer YES)
(cd build/test ; ln -sf Release/ixwebsocket_unittest)
(cd test ; python2.7 run.py -r)
test_tsan_mbedtls:
mkdir -p build && (cd build && cmake -GXcode -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_TEST=1 -DUSE_MBED_TLS=1 .. && xcodebuild -project ixwebsocket.xcodeproj -target ixwebsocket_unittest -enableThreadSanitizer YES)
(cd build/test ; ln -sf Debug/ixwebsocket_unittest)
(cd test ; python2.7 run.py -r)
build_test_openssl:
mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_OPEN_SSL=1 -DUSE_TEST=1 .. ; make -j 4) mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_OPEN_SSL=1 -DUSE_TEST=1 .. ; make -j 4)
test_openssl: build_test_openssl
(cd test ; python2.7 run.py -r) (cd test ; python2.7 run.py -r)
build_test_mbedtls: test_mbedtls:
mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_MBED_TLS=1 -DUSE_TEST=1 .. ; make -j 4) mkdir -p build && (cd build ; cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_TLS=1 -DUSE_MBED_TLS=1 -DUSE_TEST=1 .. ; make -j 4)
test_mbedtls: build_test_mbedtls
(cd test ; python2.7 run.py -r) (cd test ; python2.7 run.py -r)
test_no_ssl: test_no_ssl:

View File

@ -0,0 +1,10 @@
# Generated by CMake, DO NOT EDIT
# Custom rules for ALL_BUILD
.SUFFIXES:
all: \
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ALL_BUILD
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ALL_BUILD:
echo ""
echo Build\ all\ projects

View File

@ -0,0 +1,10 @@
# Generated by CMake, DO NOT EDIT
# Custom rules for ALL_BUILD
.SUFFIXES:
all: \
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ALL_BUILD
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ALL_BUILD:
echo ""
echo Build\ all\ projects

View File

@ -0,0 +1,10 @@
# Generated by CMake, DO NOT EDIT
# Custom rules for ALL_BUILD
.SUFFIXES:
all: \
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ALL_BUILD
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ALL_BUILD:
echo ""
echo Build\ all\ projects

View File

@ -0,0 +1,10 @@
# Generated by CMake, DO NOT EDIT
# Custom rules for ALL_BUILD
.SUFFIXES:
all: \
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ALL_BUILD
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ALL_BUILD:
echo ""
echo Build\ all\ projects

View File

@ -0,0 +1,18 @@
# Generated by CMake, DO NOT EDIT
TARGETS:=
empty:=
space:= $(empty) $(empty)
spaceplus:= $(empty)\ $(empty)
TARGETS += $(subst $(space),$(spaceplus),$(wildcard /Applications/CMake.app/Contents/share/cmake-3.12/Modules/CMakeASMInformation.cmake))
TARGETS += $(subst $(space),$(spaceplus),$(wildcard /Applications/CMake.app/Contents/share/cmake-3.12/Modules/Compiler/AppleClang-ASM.cmake))
TARGETS += $(subst $(space),$(spaceplus),$(wildcard /Applications/CMake.app/Contents/share/cmake-3.12/Modules/Compiler/Clang-ASM.cmake))
TARGETS += $(subst $(space),$(spaceplus),$(wildcard /Applications/CMake.app/Contents/share/cmake-3.12/Modules/Compiler/Clang.cmake))
TARGETS += $(subst $(space),$(spaceplus),$(wildcard /Applications/CMake.app/Contents/share/cmake-3.12/Modules/Platform/Apple-clang.cmake))
TARGETS += $(subst $(space),$(spaceplus),$(wildcard /Users/bsergeant/src/foss/IXWebSocket/CMakeFiles/3.12.3/CMakeASMCompiler.cmake))
TARGETS += $(subst $(space),$(spaceplus),$(wildcard /Users/bsergeant/src/foss/IXWebSocket/third_party/sentry-native/CMakeLists.txt))
TARGETS += $(subst $(space),$(spaceplus),$(wildcard /Users/bsergeant/src/foss/IXWebSocket/third_party/sentry-native/src/CMakeLists.txt))
/Users/bsergeant/src/foss/IXWebSocket/CMakeFiles/cmake.check_cache: $(TARGETS)
/Applications/CMake.app/Contents/bin/cmake -H/Users/bsergeant/src/foss/IXWebSocket -B/Users/bsergeant/src/foss/IXWebSocket

View File

@ -0,0 +1,139 @@
# DO NOT EDIT
# This makefile makes sure all linkable targets are
# up-to-date with anything they link to
default:
echo "Do not invoke directly"
# Rules to remove targets that are older than anything to which they
# link. This forces Xcode to relink the targets from scratch. It
# does not seem to check these dependencies itself.
PostBuild.example.Debug:
PostBuild.sentry.Debug: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/example
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/example:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/example
PostBuild.example_crashpad.Debug:
PostBuild.sentry.Debug: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/example_crashpad
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/example_crashpad:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/example_crashpad
PostBuild.sentry.Debug:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a:
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a
PostBuild.sentry_test_integration.Debug:
PostBuild.sentry.Debug: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_integration
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_integration:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_integration
PostBuild.sentry_test_unit.Debug:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_unit:
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_unit
PostBuild.example.Release:
PostBuild.sentry.Release: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/example
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/example:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/example
PostBuild.example_crashpad.Release:
PostBuild.sentry.Release: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/example_crashpad
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/example_crashpad:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/example_crashpad
PostBuild.sentry.Release:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a:
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a
PostBuild.sentry_test_integration.Release:
PostBuild.sentry.Release: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_integration
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_integration:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_integration
PostBuild.sentry_test_unit.Release:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_unit:
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/sentry_test_unit
PostBuild.example.MinSizeRel:
PostBuild.sentry.MinSizeRel: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/example
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/example:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/example
PostBuild.example_crashpad.MinSizeRel:
PostBuild.sentry.MinSizeRel: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/example_crashpad
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/example_crashpad:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/example_crashpad
PostBuild.sentry.MinSizeRel:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/libsentry.a:
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/libsentry.a
PostBuild.sentry_test_integration.MinSizeRel:
PostBuild.sentry.MinSizeRel: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/sentry_test_integration
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/sentry_test_integration:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/sentry_test_integration
PostBuild.sentry_test_unit.MinSizeRel:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/sentry_test_unit:
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/sentry_test_unit
PostBuild.example.RelWithDebInfo:
PostBuild.sentry.RelWithDebInfo: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/example
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/example:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/example
PostBuild.example_crashpad.RelWithDebInfo:
PostBuild.sentry.RelWithDebInfo: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/example_crashpad
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/example_crashpad:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/example_crashpad
PostBuild.sentry.RelWithDebInfo:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/libsentry.a:
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/libsentry.a
PostBuild.sentry_test_integration.RelWithDebInfo:
PostBuild.sentry.RelWithDebInfo: /Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/sentry_test_integration
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/sentry_test_integration:\
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/libsentry.a
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/sentry_test_integration
PostBuild.sentry_test_unit.RelWithDebInfo:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/sentry_test_unit:
/bin/rm -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/sentry_test_unit
# For each target create a dummy ruleso the target does not have to exist
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/MinSizeRel/libsentry.a:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/RelWithDebInfo/libsentry.a:
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/libsentry.a:

View File

@ -0,0 +1,10 @@
# Generated by CMake, DO NOT EDIT
# Custom rules for ZERO_CHECK
.SUFFIXES:
all: \
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ZERO_CHECK
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ZERO_CHECK:
echo ""
make -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeScripts/ReRunCMake.make

View File

@ -0,0 +1,10 @@
# Generated by CMake, DO NOT EDIT
# Custom rules for ZERO_CHECK
.SUFFIXES:
all: \
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ZERO_CHECK
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ZERO_CHECK:
echo ""
make -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeScripts/ReRunCMake.make

View File

@ -0,0 +1,10 @@
# Generated by CMake, DO NOT EDIT
# Custom rules for ZERO_CHECK
.SUFFIXES:
all: \
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ZERO_CHECK
/Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeFiles/ZERO_CHECK:
echo ""
make -f /Users/bsergeant/src/foss/IXWebSocket/sentry-native/CMakeScripts/ReRunCMake.make

Some files were not shown because too many files have changed in this diff Show More