Compare commits
124 Commits
v8.2.3
...
feature/lu
Author | SHA1 | Date | |
---|---|---|---|
22fc8e981d | |||
fb0de53efd | |||
62a7483e41 | |||
36fbdd0daa | |||
feff2e38c0 | |||
a040ff06e8 | |||
755493eaf3 | |||
4c61aede2e | |||
fbca513008 | |||
33ebd00932 | |||
fbe5e74109 | |||
a9f5d5353f | |||
22e0083832 | |||
5632360fbd | |||
20294841b3 | |||
74efdfebba | |||
0ab04f51fe | |||
4ed7968b05 | |||
287e48962f | |||
953c680eee | |||
2802cad8c4 | |||
9f770b10c0 | |||
677f79b0ea | |||
646b18bf28 | |||
0670954faf | |||
2469d7102e | |||
79acb915ce | |||
bad3adb6b4 | |||
e3dd4e60c0 | |||
c70f1d09a8 | |||
4b2b133c10 | |||
cd5fae6a5b | |||
5860c5c80b | |||
36257cbfe4 | |||
68ee57a6a7 | |||
9d79596629 | |||
0b6fd989f5 | |||
1c19a57fef | |||
a2abe861d3 | |||
0f5d15aa11 | |||
ccfd196863 | |||
9b8cfa0a37 | |||
85f6b1e0b7 | |||
64754df66c | |||
71a421eefc | |||
386ef3ab04 | |||
2c4bf8f4bd | |||
3a2c446225 | |||
35630fe7ed | |||
bea582c208 | |||
783d1d92dd | |||
415f6b4832 | |||
13d3300a40 | |||
432f0570f4 | |||
37a054723a | |||
c57cf413fb | |||
f1c106728b | |||
2eb5c9480e | |||
f9d75c9374 | |||
d1cd5e62ac | |||
f3b97097cd | |||
605be72579 | |||
49ff3789b5 | |||
96d61c6e5b | |||
9a23c5aaac | |||
d81e4d4fc0 | |||
bd44d32fdb | |||
b6abc12ecd | |||
2268b743ae | |||
1d3db5f75b | |||
296762ce06 | |||
e465f7af52 | |||
f8bf1fe7cd | |||
cfa5718e40 | |||
40c619c1ec | |||
22b02e0e5c | |||
738a3bf1c5 | |||
598fb071e3 | |||
686aface26 | |||
3073dd3f06 | |||
68c64f3f69 | |||
771ebb2a4c | |||
0fffb1e894 | |||
18164c0c38 | |||
d2db7310ff | |||
09e4584fc8 | |||
da36856d85 | |||
dffa759f71 | |||
61e789d6a4 | |||
37cb2cc266 | |||
179e17895d | |||
9f818c7acf | |||
9dcc2538ae | |||
f41a54186c | |||
e0733d205c | |||
f72f845ad2 | |||
b7e7837d76 | |||
fe966b19c7 | |||
a0ffb2ba53 | |||
5ad54a8904 | |||
10e132e8ef | |||
5ce846f48b | |||
1d6373335c | |||
829751b7af | |||
5691b55967 | |||
575bceb1ec | |||
6085839ef7 | |||
696d802703 | |||
b287730c19 | |||
d6f534de06 | |||
8ec515f292 | |||
c6204f4d90 | |||
7dfad9c0cc | |||
21fac0be6c | |||
0bddf5e096 | |||
946a8231e0 | |||
49d1e8493a | |||
6198657dd6 | |||
385d6f5f4a | |||
3919153a7b | |||
e8f81776f9 | |||
0bb5462504 | |||
44f599747e | |||
9801ebdb36 |
85
.github/workflows/ccpp.yml
vendored
85
.github/workflows/ccpp.yml
vendored
@ -1,51 +1,66 @@
|
|||||||
name: unittest
|
name: unittest
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
paths-ignore:
|
||||||
|
- 'docs/**'
|
||||||
|
|
||||||
on: [push]
|
|
||||||
|
|
||||||
# fake comment to trigger an action 1
|
|
||||||
jobs:
|
jobs:
|
||||||
linux:
|
linux:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- name: make test
|
- name: make test
|
||||||
run: make test
|
run: make test
|
||||||
|
|
||||||
mac:
|
mac_tsan_sectransport:
|
||||||
runs-on: macOS-latest
|
runs-on: macOS-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
|
- name: make test_tsan
|
||||||
|
run: make test_tsan
|
||||||
|
|
||||||
|
mac_tsan_mbedtls:
|
||||||
|
runs-on: macOS-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
- name: install mbedtls
|
||||||
|
run: brew install mbedtls
|
||||||
- name: make test
|
- name: make test
|
||||||
run: make test
|
run: make test_tsan_mbedtls
|
||||||
|
|
||||||
# We don't need to have redis running anymore, as we have our fake limited one
|
windows_openssl:
|
||||||
# - name: install redis
|
runs-on: windows-latest
|
||||||
# run: brew install redis
|
steps:
|
||||||
#
|
- uses: actions/checkout@v1
|
||||||
# - name: start redis server
|
- uses: seanmiddleditch/gha-setup-vsdevenv@master
|
||||||
# run: brew services start redis
|
- run: |
|
||||||
|
mkdir build
|
||||||
|
cd build
|
||||||
|
cmake -DCMAKE_CXX_COMPILER=cl.exe -DUSE_WS=1 -DUSE_TEST=1 ..
|
||||||
|
- run: cmake --build build
|
||||||
|
|
||||||
|
# Running the unittest does not work, the binary cannot be found
|
||||||
|
#- run: ../build/test/ixwebsocket_unittest.exe
|
||||||
|
# working-directory: test
|
||||||
|
|
||||||
|
#
|
||||||
|
# Windows with OpenSSL is working but disabled as it takes 13 minutes (10 for openssl) to build with vcpkg
|
||||||
|
#
|
||||||
|
# windows_openssl:
|
||||||
|
# runs-on: windows-latest
|
||||||
|
# steps:
|
||||||
|
# - uses: actions/checkout@v1
|
||||||
|
# - uses: seanmiddleditch/gha-setup-vsdevenv@master
|
||||||
|
# - run: |
|
||||||
|
# vcpkg install zlib:x64-windows
|
||||||
|
# vcpkg install openssl:x64-windows
|
||||||
|
# - run: |
|
||||||
|
# mkdir build
|
||||||
|
# cd build
|
||||||
|
# cmake -DCMAKE_TOOLCHAIN_FILE=c:/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_CXX_COMPILER=cl.exe -DUSE_OPEN_SSL=1 -DUSE_TLS=1 -DUSE_WS=1 -DUSE_TEST=1 ..
|
||||||
|
# - run: cmake --build build
|
||||||
|
#
|
||||||
|
# # Running the unittest does not work, the binary cannot be found
|
||||||
|
# #- run: ../build/test/ixwebsocket_unittest.exe
|
||||||
|
# # working-directory: test
|
||||||
|
|
||||||
# # 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
|
|
||||||
|
25
.github/workflows/mkdocs.yml
vendored
Normal file
25
.github/workflows/mkdocs.yml
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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
|
19
.github/workflows/stale.yml
vendored
Normal file
19
.github/workflows/stale.yml
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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'
|
@ -1,7 +1,12 @@
|
|||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v2.3.0
|
rev: v2.5.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: check-yaml
|
- id: check-yaml
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
|
- repo: https://github.com/pocc/pre-commit-hooks
|
||||||
|
rev: v1.1.1
|
||||||
|
hooks:
|
||||||
|
- id: clang-format
|
||||||
|
args: [-i, -style=file]
|
||||||
|
59
.travis.yml
59
.travis.yml
@ -1,59 +0,0 @@
|
|||||||
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
|
|
@ -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
|
||||||
|
120
CMakeLists.txt
120
CMakeLists.txt
@ -21,6 +21,7 @@ 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
|
||||||
@ -36,22 +37,22 @@ 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
|
||||||
ixwebsocket/IXWebSocketServer.cpp
|
ixwebsocket/IXWebSocketServer.cpp
|
||||||
ixwebsocket/IXWebSocketTransport.cpp
|
ixwebsocket/IXWebSocketTransport.cpp
|
||||||
ixwebsocket/LUrlParser.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
||||||
@ -69,6 +70,7 @@ 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
|
||||||
@ -77,10 +79,10 @@ set( IXWEBSOCKET_HEADERS
|
|||||||
ixwebsocket/IXWebSocketCloseInfo.h
|
ixwebsocket/IXWebSocketCloseInfo.h
|
||||||
ixwebsocket/IXWebSocketErrorInfo.h
|
ixwebsocket/IXWebSocketErrorInfo.h
|
||||||
ixwebsocket/IXWebSocketHandshake.h
|
ixwebsocket/IXWebSocketHandshake.h
|
||||||
|
ixwebsocket/IXWebSocketHandshakeKeyGen.h
|
||||||
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
|
||||||
@ -90,8 +92,6 @@ set( IXWEBSOCKET_HEADERS
|
|||||||
ixwebsocket/IXWebSocketServer.h
|
ixwebsocket/IXWebSocketServer.h
|
||||||
ixwebsocket/IXWebSocketTransport.h
|
ixwebsocket/IXWebSocketTransport.h
|
||||||
ixwebsocket/IXWebSocketVersion.h
|
ixwebsocket/IXWebSocketVersion.h
|
||||||
ixwebsocket/LUrlParser.h
|
|
||||||
ixwebsocket/libwshandshake.hpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (UNIX)
|
if (UNIX)
|
||||||
@ -109,30 +109,33 @@ elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
|
|||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/freebsd/IXSetThreadName_freebsd.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/freebsd/IXSetThreadName_freebsd.cpp)
|
||||||
else()
|
else()
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/linux/IXSetThreadName_linux.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/linux/IXSetThreadName_linux.cpp)
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSelectInterruptEventFd.cpp)
|
|
||||||
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSelectInterruptEventFd.h)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
option(USE_TLS "Enable TLS support" FALSE)
|
option(USE_TLS "Enable TLS support" FALSE)
|
||||||
|
|
||||||
if (USE_TLS)
|
if (USE_TLS)
|
||||||
if (WIN32)
|
# default to securetranport on Apple if nothing is configured
|
||||||
option(USE_MBED_TLS "Use Mbed TLS" ON)
|
if (APPLE)
|
||||||
else()
|
if (NOT USE_MBED_TLS AND NOT USE_OPEN_SSL) # unless we want something else
|
||||||
option(USE_MBED_TLS "Use Mbed TLS" OFF)
|
set(USE_SECURE_TRANSPORT ON)
|
||||||
|
endif()
|
||||||
|
else() # default to OpenSSL on all other platforms
|
||||||
|
if (NOT USE_MBED_TLS) # Unless mbedtls is requested
|
||||||
|
set(USE_OPEN_SSL ON)
|
||||||
|
endif()
|
||||||
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)
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketMbedTLS.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketMbedTLS.cpp)
|
||||||
elseif (APPLE AND NOT USE_OPEN_SSL)
|
elseif (USE_SECURE_TRANSPORT)
|
||||||
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketAppleSSL.h)
|
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketAppleSSL.h)
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketAppleSSL.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketAppleSSL.cpp)
|
||||||
else()
|
elseif (USE_OPEN_SSL)
|
||||||
set(USE_OPEN_SSL ON)
|
|
||||||
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketOpenSSL.h)
|
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketOpenSSL.h)
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketOpenSSL.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketOpenSSL.cpp)
|
||||||
|
else()
|
||||||
|
message(FATAL_ERROR "TLS Configuration error: unknown backend")
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
@ -147,56 +150,48 @@ if (USE_TLS)
|
|||||||
target_compile_definitions(ixwebsocket PUBLIC IXWEBSOCKET_USE_MBED_TLS)
|
target_compile_definitions(ixwebsocket PUBLIC IXWEBSOCKET_USE_MBED_TLS)
|
||||||
elseif (USE_OPEN_SSL)
|
elseif (USE_OPEN_SSL)
|
||||||
target_compile_definitions(ixwebsocket PUBLIC IXWEBSOCKET_USE_OPEN_SSL)
|
target_compile_definitions(ixwebsocket PUBLIC IXWEBSOCKET_USE_OPEN_SSL)
|
||||||
|
elseif (USE_SECURE_TRANSPORT)
|
||||||
|
target_compile_definitions(ixwebsocket PUBLIC IXWEBSOCKET_USE_SECURE_TRANSPORT)
|
||||||
|
else()
|
||||||
|
message(FATAL_ERROR "TLS Configuration error: unknown backend")
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (APPLE AND USE_TLS AND NOT USE_MBED_TLS AND NOT USE_OPEN_SSL)
|
if (USE_TLS)
|
||||||
target_link_libraries(ixwebsocket "-framework foundation" "-framework security")
|
if (USE_OPEN_SSL)
|
||||||
endif()
|
message(STATUS "TLS configured to use openssl")
|
||||||
|
|
||||||
if (WIN32)
|
# Help finding Homebrew's OpenSSL on macOS
|
||||||
target_link_libraries(ixwebsocket wsock32 ws2_32 shlwapi)
|
if (APPLE)
|
||||||
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /usr/local/opt/openssl/lib)
|
||||||
endif()
|
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} /usr/local/opt/openssl/include)
|
||||||
|
endif()
|
||||||
|
|
||||||
if (UNIX)
|
# This OPENSSL_FOUND check is to help find a cmake manually configured OpenSSL
|
||||||
find_package(Threads)
|
if (NOT OPENSSL_FOUND)
|
||||||
target_link_libraries(ixwebsocket ${CMAKE_THREAD_LIBS_INIT})
|
find_package(OpenSSL REQUIRED)
|
||||||
endif()
|
endif()
|
||||||
|
message(STATUS "OpenSSL: " ${OPENSSL_VERSION})
|
||||||
|
|
||||||
if (USE_TLS AND USE_OPEN_SSL)
|
add_definitions(${OPENSSL_DEFINITIONS})
|
||||||
|
target_include_directories(ixwebsocket PUBLIC ${OPENSSL_INCLUDE_DIR})
|
||||||
|
target_link_libraries(ixwebsocket ${OPENSSL_LIBRARIES})
|
||||||
|
elseif (USE_MBED_TLS)
|
||||||
|
message(STATUS "TLS configured to use mbedtls")
|
||||||
|
|
||||||
# Help finding Homebrew's OpenSSL on macOS
|
|
||||||
if (APPLE)
|
|
||||||
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /usr/local/opt/openssl/lib)
|
|
||||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} /usr/local/opt/openssl/include)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(NOT OPENSSL_FOUND)
|
|
||||||
find_package(OpenSSL REQUIRED)
|
|
||||||
endif()
|
|
||||||
add_definitions(${OPENSSL_DEFINITIONS})
|
|
||||||
message(STATUS "OpenSSL: " ${OPENSSL_VERSION})
|
|
||||||
include_directories(${OPENSSL_INCLUDE_DIR})
|
|
||||||
target_link_libraries(ixwebsocket ${OPENSSL_LIBRARIES})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (USE_TLS AND USE_MBED_TLS)
|
|
||||||
# FIXME I'm not too sure that this USE_VENDORED_THIRD_PARTY thing works
|
|
||||||
if (USE_VENDORED_THIRD_PARTY)
|
|
||||||
set (ENABLE_PROGRAMS OFF)
|
|
||||||
add_subdirectory(third_party/mbedtls)
|
|
||||||
include_directories(third_party/mbedtls/include)
|
|
||||||
|
|
||||||
target_link_libraries(ixwebsocket mbedtls)
|
|
||||||
else()
|
|
||||||
find_package(MbedTLS REQUIRED)
|
find_package(MbedTLS REQUIRED)
|
||||||
target_include_directories(ixwebsocket PUBLIC ${MBEDTLS_INCLUDE_DIRS})
|
target_include_directories(ixwebsocket PUBLIC ${MBEDTLS_INCLUDE_DIRS})
|
||||||
target_link_libraries(ixwebsocket ${MBEDTLS_LIBRARIES})
|
target_link_libraries(ixwebsocket ${MBEDTLS_LIBRARIES})
|
||||||
|
elseif (USE_SECURE_TRANSPORT)
|
||||||
|
message(STATUS "TLS configured to use secure transport")
|
||||||
|
target_link_libraries(ixwebsocket "-framework foundation" "-framework security")
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
find_package(ZLIB)
|
# This ZLIB_FOUND check is to help find a cmake manually configured zlib
|
||||||
|
if (NOT ZLIB_FOUND)
|
||||||
|
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})
|
||||||
@ -206,6 +201,21 @@ else()
|
|||||||
target_link_libraries(ixwebsocket zlibstatic)
|
target_link_libraries(ixwebsocket zlibstatic)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (WIN32)
|
||||||
|
target_link_libraries(ixwebsocket wsock32 ws2_32 shlwapi)
|
||||||
|
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
||||||
|
|
||||||
|
if (USE_TLS)
|
||||||
|
target_link_libraries(ixwebsocket Crypt32)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (UNIX)
|
||||||
|
find_package(Threads)
|
||||||
|
target_link_libraries(ixwebsocket ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
set( IXWEBSOCKET_INCLUDE_DIRS
|
set( IXWEBSOCKET_INCLUDE_DIRS
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
)
|
)
|
||||||
@ -241,3 +251,7 @@ if (USE_WS OR USE_TEST)
|
|||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (USE_LUAROCKS)
|
||||||
|
add_subdirectory(luarocks)
|
||||||
|
endif()
|
||||||
|
@ -1 +1 @@
|
|||||||
docker/Dockerfile.centos
|
docker/Dockerfile.alpine
|
@ -1,12 +1,13 @@
|
|||||||
FROM alpine:3.11 as build
|
FROM alpine:3.11 as build
|
||||||
|
|
||||||
RUN apk add --no-cache gcc g++ musl-dev linux-headers cmake openssl-dev
|
RUN apk add --no-cache \
|
||||||
RUN apk add --no-cache make
|
gcc g++ musl-dev linux-headers \
|
||||||
RUN apk add --no-cache zlib-dev
|
cmake mbedtls-dev make zlib-dev
|
||||||
|
|
||||||
RUN addgroup -S app && adduser -S -G app app
|
RUN addgroup -S app && \
|
||||||
RUN chown -R app:app /opt
|
adduser -S -G app app && \
|
||||||
RUN chown -R app:app /usr/local
|
chown -R app:app /opt && \
|
||||||
|
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
|
||||||
@ -14,22 +15,21 @@ COPY --chown=app:app . /opt
|
|||||||
WORKDIR /opt
|
WORKDIR /opt
|
||||||
|
|
||||||
USER app
|
USER app
|
||||||
RUN [ "make", "ws_install" ]
|
RUN make ws_mbedtls_install && \
|
||||||
RUN [ "rm", "-rf", "build" ]
|
sh tools/trim_repo_for_docker.sh
|
||||||
|
|
||||||
FROM alpine:3.11 as runtime
|
FROM alpine:3.11 as runtime
|
||||||
|
|
||||||
RUN apk add --no-cache libstdc++
|
RUN apk add --no-cache libstdc++ mbedtls ca-certificates && \
|
||||||
RUN apk add --no-cache strace
|
addgroup -S app && \
|
||||||
RUN apk add --no-cache gdb
|
adduser -S -G app app
|
||||||
|
|
||||||
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 source code for gcc
|
# COPY --chown=app:app --from=build /opt /opt
|
||||||
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
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
FROM centos:8 as build
|
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
|
||||||
@ -12,13 +15,16 @@ COPY --chown=app:app . /opt
|
|||||||
WORKDIR /opt
|
WORKDIR /opt
|
||||||
|
|
||||||
USER app
|
USER app
|
||||||
RUN [ "make", "ws_install" ]
|
RUN [ "make", "ws_mbedtls_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
|
||||||
|
@ -2,14 +2,14 @@
|
|||||||
FROM debian:buster as build
|
FROM debian:buster as build
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
ENV DEBIAN_FRONTEND noninteractive
|
||||||
RUN apt-get update
|
RUN apt-get update
|
||||||
RUN apt-get -y install wget
|
RUN apt-get -y install wget
|
||||||
RUN mkdir -p /tmp/cmake
|
RUN mkdir -p /tmp/cmake
|
||||||
WORKDIR /tmp/cmake
|
WORKDIR /tmp/cmake
|
||||||
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
|
|
||||||
RUN apt-get -y install g++
|
RUN apt-get -y install g++
|
||||||
RUN apt-get -y install libssl-dev
|
RUN apt-get -y install libssl-dev
|
||||||
RUN apt-get -y install libz-dev
|
RUN apt-get -y install libz-dev
|
||||||
RUN apt-get -y install make
|
RUN apt-get -y install make
|
||||||
@ -25,9 +25,9 @@ RUN ["make"]
|
|||||||
FROM debian:buster as runtime
|
FROM debian:buster as runtime
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
ENV DEBIAN_FRONTEND noninteractive
|
||||||
RUN apt-get update
|
RUN apt-get update
|
||||||
# Runtime
|
# Runtime
|
||||||
RUN apt-get install -y libssl1.1
|
RUN apt-get install -y libssl1.1
|
||||||
RUN apt-get install -y ca-certificates
|
RUN apt-get install -y ca-certificates
|
||||||
RUN ["update-ca-certificates"]
|
RUN ["update-ca-certificates"]
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ RUN yum install -y openssl-devel
|
|||||||
RUN yum install -y wget
|
RUN yum install -y wget
|
||||||
RUN mkdir -p /tmp/cmake
|
RUN mkdir -p /tmp/cmake
|
||||||
WORKDIR /tmp/cmake
|
WORKDIR /tmp/cmake
|
||||||
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
|
|
||||||
ARG CMAKE_BIN_PATH=/tmp/cmake/cmake-3.14.0-Linux-x86_64/bin
|
ARG CMAKE_BIN_PATH=/tmp/cmake/cmake-3.14.0-Linux-x86_64/bin
|
||||||
@ -27,7 +27,7 @@ FROM fedora:30 as runtime
|
|||||||
|
|
||||||
RUN yum install -y libtsan
|
RUN yum install -y libtsan
|
||||||
|
|
||||||
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
|
||||||
RUN ldd /usr/local/bin/ws
|
RUN ldd /usr/local/bin/ws
|
||||||
|
@ -2,14 +2,14 @@
|
|||||||
FROM ubuntu:bionic as build
|
FROM ubuntu:bionic as build
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
ENV DEBIAN_FRONTEND noninteractive
|
||||||
RUN apt-get update
|
RUN apt-get update
|
||||||
RUN apt-get -y install wget
|
RUN apt-get -y install wget
|
||||||
RUN mkdir -p /tmp/cmake
|
RUN mkdir -p /tmp/cmake
|
||||||
WORKDIR /tmp/cmake
|
WORKDIR /tmp/cmake
|
||||||
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
|
|
||||||
RUN apt-get -y install g++
|
RUN apt-get -y install g++
|
||||||
RUN apt-get -y install libssl-dev
|
RUN apt-get -y install libssl-dev
|
||||||
RUN apt-get -y install libz-dev
|
RUN apt-get -y install libz-dev
|
||||||
RUN apt-get -y install make
|
RUN apt-get -y install make
|
||||||
|
@ -2,14 +2,14 @@
|
|||||||
FROM ubuntu:disco as build
|
FROM ubuntu:disco as build
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
ENV DEBIAN_FRONTEND noninteractive
|
||||||
RUN apt-get update
|
RUN apt-get update
|
||||||
RUN apt-get -y install wget
|
RUN apt-get -y install wget
|
||||||
RUN mkdir -p /tmp/cmake
|
RUN mkdir -p /tmp/cmake
|
||||||
WORKDIR /tmp/cmake
|
WORKDIR /tmp/cmake
|
||||||
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
|
|
||||||
RUN apt-get -y install g++
|
RUN apt-get -y install g++
|
||||||
RUN apt-get -y install libssl-dev
|
RUN apt-get -y install libssl-dev
|
||||||
RUN apt-get -y install libz-dev
|
RUN apt-get -y install libz-dev
|
||||||
RUN apt-get -y install make
|
RUN apt-get -y install make
|
||||||
|
@ -2,14 +2,14 @@
|
|||||||
FROM ubuntu:xenial as build
|
FROM ubuntu:xenial as build
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
ENV DEBIAN_FRONTEND noninteractive
|
||||||
RUN apt-get update
|
RUN apt-get update
|
||||||
RUN apt-get -y install wget
|
RUN apt-get -y install wget
|
||||||
RUN mkdir -p /tmp/cmake
|
RUN mkdir -p /tmp/cmake
|
||||||
WORKDIR /tmp/cmake
|
WORKDIR /tmp/cmake
|
||||||
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN wget https://github.com/Kitware/CMake/releases/download/v3.14.0/cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
RUN tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
||||||
|
|
||||||
RUN apt-get -y install g++
|
RUN apt-get -y install g++
|
||||||
RUN apt-get -y install libssl-dev
|
RUN apt-get -y install libssl-dev
|
||||||
RUN apt-get -y install libz-dev
|
RUN apt-get -y install libz-dev
|
||||||
RUN apt-get -y install make
|
RUN apt-get -y install make
|
||||||
|
@ -1,6 +1,204 @@
|
|||||||
# 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.5.2] - 2020-04-27
|
||||||
|
|
||||||
|
(cmake) fix cmake broken tls option parsing
|
||||||
|
|
||||||
|
## [9.5.1] - 2020-04-27
|
||||||
|
|
||||||
|
(http client) Set default values for most HttpRequestArgs struct members (fix #185)
|
||||||
|
|
||||||
|
## [9.5.0] - 2020-04-25
|
||||||
|
|
||||||
|
(ssl) Default to OpenSSL on Windows, since it can load the system certificates by default
|
||||||
|
|
||||||
|
## [9.4.1] - 2020-04-25
|
||||||
|
|
||||||
|
(header) Add a space between header name and header value since most http parsers expects it, although it it not required. Cf #184 and #155
|
||||||
|
|
||||||
|
## [9.4.0] - 2020-04-24
|
||||||
|
|
||||||
|
(ssl) Add support for supplying SSL CA from memory, for OpenSSL and MbedTLS backends
|
||||||
|
|
||||||
|
## [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
|
## [8.2.3] - 2020-03-13
|
||||||
|
|
||||||
(cobra client) pass the message position to the subscription data callback
|
(cobra client) pass the message position to the subscription data callback
|
||||||
@ -63,7 +261,7 @@ All changes to this project will be documented in this file.
|
|||||||
|
|
||||||
## [8.0.6] - 2020-01-31
|
## [8.0.6] - 2020-01-31
|
||||||
|
|
||||||
(snake) add an option to disable answering pongs as response to pings, to test cobra client behavior with hanged connections
|
(snake) add an option to disable answering pongs as response to pings, to test cobra client behavior with hanged connections
|
||||||
|
|
||||||
## [8.0.5] - 2020-01-31
|
## [8.0.5] - 2020-01-31
|
||||||
|
|
||||||
|
@ -18,10 +18,12 @@ There is a unittest which can be executed by typing `make test`.
|
|||||||
Options for building:
|
Options for building:
|
||||||
|
|
||||||
* `-DUSE_TLS=1` will enable TLS support
|
* `-DUSE_TLS=1` will enable TLS support
|
||||||
* `-DUSE_MBED_TLS=1` will use [mbedlts](https://tls.mbed.org/) for the TLS support (default on Windows)
|
* `-DUSE_OPEN_SSL=1` will use [openssl](https://www.openssl.org/) for the TLS support (default on Linux and Windows)
|
||||||
|
* `-DUSE_MBED_TLS=1` will use [mbedlts](https://tls.mbed.org/) for the TLS support
|
||||||
* `-DUSE_WS=1` will build the ws interactive command line tool
|
* `-DUSE_WS=1` will build the ws interactive command line tool
|
||||||
|
* `-DUSE_TEST=1` will build the unittest
|
||||||
|
|
||||||
If you are on Windows, look at the [appveyor](https://github.com/machinezone/IXWebSocket/blob/master/appveyor.yml) file that has instructions for building dependencies.
|
If you are on Windows, look at the [appveyor](https://github.com/machinezone/IXWebSocket/blob/master/appveyor.yml) file (not maintained much though) or rather the [github actions](https://github.com/machinezone/IXWebSocket/blob/master/.github/workflows/ccpp.yml#L40) which have instructions for building dependencies.
|
||||||
|
|
||||||
It is also possible to externally include the project, so that everything is fetched over the wire when you build like so:
|
It is also possible to externally include the project, so that everything is fetched over the wire when you build like so:
|
||||||
|
|
||||||
@ -47,19 +49,19 @@ vcpkg install ixwebsocket
|
|||||||
|
|
||||||
Conan is currently supported through a recipe in [Conan Center](https://github.com/conan-io/conan-center-index/tree/master/recipes/ixwebsocket) ([Bintray entry](https://bintray.com/conan/conan-center/ixwebsocket%3A_)).
|
Conan is currently supported through a recipe in [Conan Center](https://github.com/conan-io/conan-center-index/tree/master/recipes/ixwebsocket) ([Bintray entry](https://bintray.com/conan/conan-center/ixwebsocket%3A_)).
|
||||||
|
|
||||||
Package reference
|
Package reference
|
||||||
|
|
||||||
* Conan 1.21.0 and up: `ixwebsocket/7.9.2`
|
* Conan 1.21.0 and up: `ixwebsocket/7.9.2`
|
||||||
* Earlier versions: `ixwebsocket/7.9.2@_/_`
|
* Earlier versions: `ixwebsocket/7.9.2@_/_`
|
||||||
|
|
||||||
Note that the version listed here might not be the latest one. See Bintray or the recipe itself for the latest version. If you're migrating from the previous, custom Bintray remote, note that the package reference _has_ to be lower-case.
|
Note that the version listed here might not be the latest one. See Bintray or the recipe itself for the latest version. If you're migrating from the previous, custom Bintray remote, note that the package reference _has_ to be lower-case.
|
||||||
|
|
||||||
### Docker
|
### Docker
|
||||||
|
|
||||||
There is a Dockerfile for running the unittest on Linux, and to run the `ws` tool. It is also available on the docker registry.
|
There is a Dockerfile for running the unittest on Linux, and to run the `ws` tool. It is also available on the docker registry.
|
||||||
|
|
||||||
```
|
```
|
||||||
docker run bsergean/ws
|
docker run docker.pkg.github.com/machinezone/ixwebsocket/ws:latest --help
|
||||||
```
|
```
|
||||||
|
|
||||||
To use docker-compose you must make a docker container first.
|
To use docker-compose you must make a docker container first.
|
||||||
|
@ -8,6 +8,8 @@ The per message deflate compression option is supported. It can lead to very nic
|
|||||||
|
|
||||||
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 and mbedTLS can be used on Android, Linux and 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
|
||||||
|
|
||||||
No manual polling to fetch data is required. Data is sent and received instantly by using a background thread for receiving data and the select [system](http://man7.org/linux/man-pages/man2/select.2.html) call to be notified by the OS of incoming data. No timeout is used for select so that the background thread is only woken up when data is available, to optimize battery life. This is also the recommended way of using select according to the select tutorial, section [select law](https://linux.die.net/man/2/select_tut). Read and Writes to the socket are non blocking. Data is sent right away and not enqueued by writing directly to the socket, which is [possible](https://stackoverflow.com/questions/1981372/are-parallel-calls-to-send-recv-on-the-same-socket-valid) since system socket implementations allow concurrent read/writes. However concurrent writes need to be protected with mutex.
|
No manual polling to fetch data is required. Data is sent and received instantly by using a background thread for receiving data and the select [system](http://man7.org/linux/man-pages/man2/select.2.html) call to be notified by the OS of incoming data. No timeout is used for select so that the background thread is only woken up when data is available, to optimize battery life. This is also the recommended way of using select according to the select tutorial, section [select law](https://linux.die.net/man/2/select_tut). Read and Writes to the socket are non blocking. Data is sent right away and not enqueued by writing directly to the socket, which is [possible](https://stackoverflow.com/questions/1981372/are-parallel-calls-to-send-recv-on-the-same-socket-valid) since system socket implementations allow concurrent read/writes. However concurrent writes need to be protected with mutex.
|
||||||
@ -26,12 +28,17 @@ 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 travis.
|
The regression test is running after each commit on github actions for multiple configurations.
|
||||||
|
|
||||||
|
* 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
|
||||||
|
|
||||||
* On Windows and Android certificate validation needs to be setup so that SocketTLSOptions.caFile point to a pem file, such as the one distributed by Firefox. Unless that setup is done connecting to a wss endpoint will display an error. On Windows with mbedtls the message will contain `error in handshake : X509 - Certificate verification failed, e.g. CRL, CA or signature check failed`.
|
* On some configuration (mostly Android) certificate validation needs to be setup so that SocketTLSOptions.caFile point to a pem file, such as the one distributed by Firefox. Unless that setup is done connecting to a wss endpoint will display an error. With mbedtls the message will contain `error in handshake : X509 - Certificate verification failed, e.g. CRL, CA or signature check failed`.
|
||||||
* There is no convenient way to embed a ca cert.
|
|
||||||
* Automatic reconnection works at the TCP socket level, and will detect remote end disconnects. However, if the device/computer network become unreachable (by turning off wifi), it is quite hard to reliably and timely detect it at the socket level using `recv` and `send` error codes. [Here](https://stackoverflow.com/questions/14782143/linux-socket-how-to-detect-disconnected-network-in-a-client-program) is a good discussion on the subject. This behavior is consistent with other runtimes such as node.js. One way to detect a disconnected device with low level C code is to do a name resolution with DNS but this can be expensive. Mobile devices have good and reliable API to do that.
|
* Automatic reconnection works at the TCP socket level, and will detect remote end disconnects. However, if the device/computer network become unreachable (by turning off wifi), it is quite hard to reliably and timely detect it at the socket level using `recv` and `send` error codes. [Here](https://stackoverflow.com/questions/14782143/linux-socket-how-to-detect-disconnected-network-in-a-client-program) is a good discussion on the subject. This behavior is consistent with other runtimes such as node.js. One way to detect a disconnected device with low level C code is to do a name resolution with DNS but this can be expensive. Mobile devices have good and reliable API to do that.
|
||||||
* The server code is using select to detect incoming data, and creates one OS thread per connection. This is not as scalable as strategies using epoll or kqueue.
|
* The server code is using select to detect incoming data, and creates one OS thread per connection. This is not as scalable as strategies using epoll or kqueue.
|
||||||
|
|
||||||
@ -73,5 +80,3 @@ Here is a simplistic diagram which explains how the code is structured in term o
|
|||||||
| |
|
| |
|
||||||
+-----------------------+
|
+-----------------------+
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||

|

|
||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
## Example code
|
## Example code
|
||||||
|
|
||||||
```cpp
|
```c++
|
||||||
// Required on Windows
|
// Required on Windows
|
||||||
ix::initNetSystem();
|
ix::initNetSystem();
|
||||||
|
|
||||||
@ -44,7 +44,22 @@ 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.
|
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`.
|
||||||
|
|
||||||
|
* 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
|
||||||
|
|
||||||
|
94
docs/packages.md
Normal file
94
docs/packages.md
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
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.
|
@ -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.setHeartBeatPeriod(45);
|
webSocket.setPingInterval(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.setHeartBeatPeriod(45);
|
webSocket.setPingInterval(45);
|
||||||
```
|
```
|
||||||
|
|
||||||
### Supply extra HTTP headers.
|
### Supply extra HTTP headers.
|
||||||
@ -244,39 +244,6 @@ 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
|
||||||
@ -464,3 +431,40 @@ 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", // as a file, or in memory buffer in PEM format
|
||||||
|
.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.
|
||||||
|
- If the value contain the special value `-----BEGIN CERTIFICATE-----`, the value will be read from memory, and not from a file. This is convenient on platforms like Android where reading / writing to the file system can be challenging without proper permissions, or without knowing the location of a temp directory.
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -4,15 +4,21 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
set (IXBOTS_SOURCES
|
set (IXBOTS_SOURCES
|
||||||
|
ixbots/IXCobraBot.cpp
|
||||||
ixbots/IXCobraToSentryBot.cpp
|
ixbots/IXCobraToSentryBot.cpp
|
||||||
ixbots/IXCobraToStatsdBot.cpp
|
ixbots/IXCobraToStatsdBot.cpp
|
||||||
|
ixbots/IXCobraToStdoutBot.cpp
|
||||||
ixbots/IXQueueManager.cpp
|
ixbots/IXQueueManager.cpp
|
||||||
|
ixbots/IXStatsdClient.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set (IXBOTS_HEADERS
|
set (IXBOTS_HEADERS
|
||||||
|
ixbots/IXCobraBot.h
|
||||||
ixbots/IXCobraToSentryBot.h
|
ixbots/IXCobraToSentryBot.h
|
||||||
ixbots/IXCobraToStatsdBot.h
|
ixbots/IXCobraToStatsdBot.h
|
||||||
|
ixbots/IXCobraToStdoutBot.h
|
||||||
ixbots/IXQueueManager.h
|
ixbots/IXQueueManager.h
|
||||||
|
ixbots/IXStatsdClient.h
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(ixbots STATIC
|
add_library(ixbots STATIC
|
||||||
@ -25,21 +31,14 @@ if (NOT JSONCPP_FOUND)
|
|||||||
set(JSONCPP_INCLUDE_DIRS ../third_party/jsoncpp)
|
set(JSONCPP_INCLUDE_DIRS ../third_party/jsoncpp)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
find_package(SpdLog)
|
|
||||||
if (NOT SPDLOG_FOUND)
|
|
||||||
set(SPDLOG_INCLUDE_DIRS ../third_party/spdlog/include)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set(STATSD_CLIENT_INCLUDE_DIRS ../third_party/statsd-client-cpp/src)
|
|
||||||
|
|
||||||
set(IXBOTS_INCLUDE_DIRS
|
set(IXBOTS_INCLUDE_DIRS
|
||||||
.
|
.
|
||||||
..
|
..
|
||||||
../ixcore
|
../ixcore
|
||||||
|
../ixwebsocket
|
||||||
../ixcobra
|
../ixcobra
|
||||||
../ixsentry
|
../ixsentry
|
||||||
${JSONCPP_INCLUDE_DIRS}
|
${JSONCPP_INCLUDE_DIRS}
|
||||||
${SPDLOG_INCLUDE_DIRS}
|
${SPDLOG_INCLUDE_DIRS})
|
||||||
${STATSD_CLIENT_INCLUDE_DIRS})
|
|
||||||
|
|
||||||
target_include_directories( ixbots PUBLIC ${IXBOTS_INCLUDE_DIRS} )
|
target_include_directories( ixbots PUBLIC ${IXBOTS_INCLUDE_DIRS} )
|
||||||
|
321
ixbots/ixbots/IXCobraBot.cpp
Normal file
321
ixbots/ixbots/IXCobraBot.cpp
Normal file
@ -0,0 +1,321 @@
|
|||||||
|
/*
|
||||||
|
* IXCobraBot.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "IXCobraBot.h"
|
||||||
|
|
||||||
|
#include "IXQueueManager.h"
|
||||||
|
#include <ixcobra/IXCobraConnection.h>
|
||||||
|
#include <ixcore/utils/IXCoreLogger.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#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);
|
||||||
|
uint64_t sentCountTotal(0);
|
||||||
|
uint64_t receivedCountTotal(0);
|
||||||
|
uint64_t sentCountPerSecs(0);
|
||||||
|
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
|
||||||
|
//
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "messages received "
|
||||||
|
<< receivedCountPerSecs
|
||||||
|
<< " "
|
||||||
|
<< receivedCountTotal
|
||||||
|
<< " sent "
|
||||||
|
<< sentCountPerSecs
|
||||||
|
<< " "
|
||||||
|
<< sentCountTotal;
|
||||||
|
CoreLogger::info(ss.str());
|
||||||
|
|
||||||
|
receivedCountPerSecs = receivedCount - receivedCountTotal;
|
||||||
|
sentCountPerSecs = sentCount - receivedCountTotal;
|
||||||
|
|
||||||
|
receivedCountTotal += receivedCountPerSecs;
|
||||||
|
sentCountTotal += sentCountPerSecs;
|
||||||
|
|
||||||
|
auto duration = std::chrono::seconds(1);
|
||||||
|
std::this_thread::sleep_for(duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
CoreLogger::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)
|
||||||
|
{
|
||||||
|
CoreLogger::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);
|
||||||
|
}
|
||||||
|
|
||||||
|
CoreLogger::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)
|
||||||
|
{
|
||||||
|
CoreLogger::info("cobra bot: sending succesfull");
|
||||||
|
}
|
||||||
|
++sentCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CoreLogger::error("cobra bot: error sending");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stop) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
CoreLogger::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)
|
||||||
|
{
|
||||||
|
CoreLogger::info("Subscriber connected");
|
||||||
|
|
||||||
|
for (auto&& it : event->headers)
|
||||||
|
{
|
||||||
|
CoreLogger::info(it.first + "::" + it.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Closed)
|
||||||
|
{
|
||||||
|
CoreLogger::info("Subscriber closed: {}" + event->errMsg);
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Authenticated)
|
||||||
|
{
|
||||||
|
CoreLogger::info("Subscriber authenticated");
|
||||||
|
CoreLogger::info("Subscribing to " + channel);
|
||||||
|
CoreLogger::info("Subscribing at position " + subscriptionPosition);
|
||||||
|
CoreLogger::info("Subscribing with 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)
|
||||||
|
{
|
||||||
|
CoreLogger::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)
|
||||||
|
{
|
||||||
|
CoreLogger::info("cobra bot: sending succesfull");
|
||||||
|
}
|
||||||
|
++sentCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CoreLogger::error("cobra bot: error sending");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Subscribed)
|
||||||
|
{
|
||||||
|
CoreLogger::info("Subscriber: subscribed to channel " + event->subscriptionId);
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::UnSubscribed)
|
||||||
|
{
|
||||||
|
CoreLogger::info("Subscriber: unsubscribed from channel " + event->subscriptionId);
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Error)
|
||||||
|
{
|
||||||
|
CoreLogger::error("Subscriber: error " + event->errMsg);
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Published)
|
||||||
|
{
|
||||||
|
CoreLogger::error("Published message hacked: " + std::to_string(event->msgId));
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Pong)
|
||||||
|
{
|
||||||
|
CoreLogger::info("Received websocket pong: " + event->errMsg);
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::HandshakeError)
|
||||||
|
{
|
||||||
|
CoreLogger::error("Subscriber: Handshake error: " + event->errMsg);
|
||||||
|
fatalCobraError = true;
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::AuthenticationError)
|
||||||
|
{
|
||||||
|
CoreLogger::error("Subscriber: Authentication error: " + event->errMsg);
|
||||||
|
fatalCobraError = true;
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::SubscriptionError)
|
||||||
|
{
|
||||||
|
CoreLogger::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;
|
||||||
|
}
|
||||||
|
} // namespace ix
|
43
ixbots/ixbots/IXCobraBot.h
Normal file
43
ixbots/ixbots/IXCobraBot.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* IXCobraBot.h
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <functional>
|
||||||
|
#include <ixcobra/IXCobraConfig.h>
|
||||||
|
#include <json/json.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
} // namespace ix
|
@ -5,276 +5,113 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "IXCobraToSentryBot.h"
|
#include "IXCobraToSentryBot.h"
|
||||||
|
|
||||||
|
#include "IXCobraBot.h"
|
||||||
#include "IXQueueManager.h"
|
#include "IXQueueManager.h"
|
||||||
|
#include <ixcobra/IXCobraConnection.h>
|
||||||
|
#include <ixcore/utils/IXCoreLogger.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ixcobra/IXCobraConnection.h>
|
|
||||||
#include <spdlog/spdlog.h>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <thread>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
int cobra_to_sentry_bot(const CobraConfig& config,
|
int64_t cobra_to_sentry_bot(const CobraConfig& config,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
const std::string& filter,
|
const std::string& filter,
|
||||||
SentryClient& sentryClient,
|
const std::string& position,
|
||||||
bool verbose,
|
SentryClient& sentryClient,
|
||||||
bool strict,
|
bool verbose,
|
||||||
size_t maxQueueSize,
|
size_t maxQueueSize,
|
||||||
bool enableHeartbeat,
|
bool enableHeartbeat,
|
||||||
int runtime)
|
int runtime)
|
||||||
{
|
{
|
||||||
ix::CobraConnection conn;
|
CobraBot bot;
|
||||||
conn.configure(config);
|
bot.setOnBotMessageCallback([&sentryClient](const Json::Value& msg,
|
||||||
conn.connect();
|
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;
|
||||||
|
|
||||||
Json::FastWriter jsonWriter;
|
if (!response)
|
||||||
std::atomic<uint64_t> sentCount(0);
|
|
||||||
std::atomic<uint64_t> receivedCount(0);
|
|
||||||
std::atomic<bool> errorSending(false);
|
|
||||||
std::atomic<bool> stop(false);
|
|
||||||
std::atomic<bool> throttled(false);
|
|
||||||
|
|
||||||
QueueManager queueManager(maxQueueSize, stop);
|
|
||||||
|
|
||||||
auto timer = [&sentCount, &receivedCount, &stop] {
|
|
||||||
while (!stop)
|
|
||||||
{
|
{
|
||||||
spdlog::info("messages received {} sent {}", receivedCount, sentCount);
|
CoreLogger::warn("Null HTTP Response");
|
||||||
|
return false;
|
||||||
auto duration = std::chrono::seconds(1);
|
|
||||||
std::this_thread::sleep_for(duration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
spdlog::info("timer thread done");
|
if (verbose)
|
||||||
};
|
|
||||||
|
|
||||||
std::thread t1(timer);
|
|
||||||
|
|
||||||
auto heartbeat = [&sentCount, &receivedCount, &stop, &enableHeartbeat] {
|
|
||||||
std::string state("na");
|
|
||||||
|
|
||||||
if (!enableHeartbeat) return;
|
|
||||||
|
|
||||||
while (!stop)
|
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
for (auto it : response->headers)
|
||||||
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");
|
CoreLogger::info(it.first + ": " + it.second);
|
||||||
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 sentrySender =
|
|
||||||
[&queueManager, verbose, &errorSending, &sentCount, &stop, &throttled, &sentryClient] {
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
Json::Value msg = queueManager.pop();
|
|
||||||
|
|
||||||
if (stop) break;
|
|
||||||
if (msg.isNull()) continue;
|
|
||||||
|
|
||||||
auto ret = sentryClient.send(msg, verbose);
|
|
||||||
HttpResponsePtr response = ret.first;
|
|
||||||
|
|
||||||
if (!response)
|
|
||||||
{
|
|
||||||
spdlog::warn("Null HTTP Response");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response->statusCode != 200)
|
|
||||||
{
|
|
||||||
spdlog::error("Error sending data to sentry: {}", response->statusCode);
|
|
||||||
spdlog::error("Body: {}", ret.second);
|
|
||||||
spdlog::error("Response: {}", response->payload);
|
|
||||||
errorSending = true;
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++sentCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stop) break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
spdlog::info("sentrySender thread done");
|
CoreLogger::info("Upload size: " + std::to_string(response->uploadSize));
|
||||||
};
|
CoreLogger::info("Download size: " + std::to_string(response->downloadSize));
|
||||||
|
|
||||||
std::thread t3(sentrySender);
|
CoreLogger::info("Status: " + std::to_string(response->statusCode));
|
||||||
|
if (response->errorCode != HttpErrorCode::Ok)
|
||||||
conn.setEventCallback([&conn,
|
|
||||||
&channel,
|
|
||||||
&filter,
|
|
||||||
&jsonWriter,
|
|
||||||
verbose,
|
|
||||||
&throttled,
|
|
||||||
&receivedCount,
|
|
||||||
&queueManager](ix::CobraConnectionEventType eventType,
|
|
||||||
const std::string& errMsg,
|
|
||||||
const ix::WebSocketHttpHeaders& headers,
|
|
||||||
const std::string& subscriptionId,
|
|
||||||
CobraConnection::MsgId msgId) {
|
|
||||||
if (eventType == ix::CobraConnection_EventType_Open)
|
|
||||||
{
|
|
||||||
spdlog::info("Subscriber connected");
|
|
||||||
|
|
||||||
for (auto it : headers)
|
|
||||||
{
|
{
|
||||||
spdlog::info("{}: {}", it.first, it.second);
|
CoreLogger::info("error message: " + response->errorMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response->headers["Content-Type"] != "application/octet-stream")
|
||||||
|
{
|
||||||
|
CoreLogger::info("payload: " + response->payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (eventType == ix::CobraConnection_EventType_Closed)
|
|
||||||
{
|
|
||||||
spdlog::info("Subscriber closed");
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Authenticated)
|
|
||||||
{
|
|
||||||
spdlog::info("Subscriber authenticated");
|
|
||||||
conn.subscribe(channel,
|
|
||||||
filter,
|
|
||||||
[&jsonWriter, verbose, &throttled, &receivedCount, &queueManager](
|
|
||||||
const Json::Value& msg, const std::string& position) {
|
|
||||||
if (verbose)
|
|
||||||
{
|
|
||||||
spdlog::info("Subscriber received message {} -> {}", position, jsonWriter.write(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we cannot send to sentry fast enough, drop the message
|
bool success = response->statusCode == 200;
|
||||||
if (throttled)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
++receivedCount;
|
if (!success)
|
||||||
queueManager.add(msg);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Subscribed)
|
|
||||||
{
|
{
|
||||||
spdlog::info("Subscriber: subscribed to channel {}", subscriptionId);
|
CoreLogger::error("Error sending data to sentry: " + std::to_string(response->statusCode));
|
||||||
}
|
CoreLogger::error("Body: " + ret.second);
|
||||||
else if (eventType == ix::CobraConnection_EventType_UnSubscribed)
|
CoreLogger::error("Response: " + response->payload);
|
||||||
{
|
|
||||||
spdlog::info("Subscriber: unsubscribed from channel {}", subscriptionId);
|
// Error 429 Too Many Requests
|
||||||
}
|
if (response->statusCode == 429)
|
||||||
else if (eventType == ix::CobraConnection_EventType_Error)
|
{
|
||||||
{
|
auto retryAfter = response->headers["Retry-After"];
|
||||||
spdlog::error("Subscriber: error {}", errMsg);
|
std::stringstream ss;
|
||||||
}
|
ss << retryAfter;
|
||||||
else if (eventType == ix::CobraConnection_EventType_Published)
|
int seconds;
|
||||||
{
|
ss >> seconds;
|
||||||
spdlog::error("Published message hacked: {}", msgId);
|
|
||||||
}
|
if (!ss.eof() || ss.fail())
|
||||||
else if (eventType == ix::CobraConnection_EventType_Pong)
|
{
|
||||||
{
|
seconds = 30;
|
||||||
spdlog::info("Received websocket pong");
|
CoreLogger::warn("Error parsing Retry-After header. "
|
||||||
|
"Using " + retryAfter + " for the sleep duration");
|
||||||
|
}
|
||||||
|
|
||||||
|
CoreLogger::warn("Error 429 - Too Many Requests. ws will sleep "
|
||||||
|
"and retry after " + retryAfter + " seconds");
|
||||||
|
|
||||||
|
throttled = true;
|
||||||
|
auto duration = std::chrono::seconds(seconds);
|
||||||
|
std::this_thread::sleep_for(duration);
|
||||||
|
throttled = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Run forever
|
bool useQueue = true;
|
||||||
if (runtime == -1)
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
auto duration = std::chrono::seconds(1);
|
|
||||||
std::this_thread::sleep_for(duration);
|
|
||||||
|
|
||||||
if (strict && errorSending) break;
|
return bot.run(config,
|
||||||
}
|
channel,
|
||||||
}
|
filter,
|
||||||
// Run for a duration, used by unittesting now
|
position,
|
||||||
else
|
verbose,
|
||||||
{
|
maxQueueSize,
|
||||||
for (int i = 0 ; i < runtime; ++i)
|
useQueue,
|
||||||
{
|
enableHeartbeat,
|
||||||
auto duration = std::chrono::seconds(1);
|
runtime);
|
||||||
std::this_thread::sleep_for(duration);
|
|
||||||
|
|
||||||
if (strict && errorSending) break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Cleanup.
|
|
||||||
// join all the bg threads and stop them.
|
|
||||||
//
|
|
||||||
conn.disconnect();
|
|
||||||
stop = true;
|
|
||||||
|
|
||||||
t1.join();
|
|
||||||
if (t2.joinable()) t2.join();
|
|
||||||
spdlog::info("heartbeat thread done");
|
|
||||||
|
|
||||||
t3.join();
|
|
||||||
|
|
||||||
return (strict && errorSending) ? -1 : (int) sentCount;
|
|
||||||
}
|
}
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -1,23 +1,24 @@
|
|||||||
/*
|
/*
|
||||||
* IXCobraToSentryBot.h
|
* IXCobraToSentryBot.h
|
||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <ixcobra/IXCobraConfig.h>
|
#include <ixcobra/IXCobraConfig.h>
|
||||||
#include <ixsentry/IXSentryClient.h>
|
#include <ixsentry/IXSentryClient.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
int cobra_to_sentry_bot(const CobraConfig& config,
|
int64_t cobra_to_sentry_bot(const CobraConfig& config,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
const std::string& filter,
|
const std::string& filter,
|
||||||
SentryClient& sentryClient,
|
const std::string& position,
|
||||||
bool verbose,
|
SentryClient& sentryClient,
|
||||||
bool strict,
|
bool verbose,
|
||||||
size_t maxQueueSize,
|
size_t maxQueueSize,
|
||||||
bool enableHeartbeat,
|
bool enableHeartbeat,
|
||||||
int runtime);
|
int runtime);
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -5,21 +5,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "IXCobraToStatsdBot.h"
|
#include "IXCobraToStatsdBot.h"
|
||||||
|
|
||||||
|
#include "IXCobraBot.h"
|
||||||
#include "IXQueueManager.h"
|
#include "IXQueueManager.h"
|
||||||
|
#include "IXStatsdClient.h"
|
||||||
#include <atomic>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <condition_variable>
|
|
||||||
#include <ixcobra/IXCobraConnection.h>
|
#include <ixcobra/IXCobraConnection.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <ixcore/utils/IXCoreLogger.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <thread>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
#include <statsd_client.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
// fields are command line argument that can be specified multiple times
|
// fields are command line argument that can be specified multiple times
|
||||||
@ -43,7 +38,7 @@ namespace ix
|
|||||||
// Extract an attribute from a Json Value.
|
// Extract an attribute from a Json Value.
|
||||||
// extractAttr("foo.bar", {"foo": {"bar": "baz"}}) => baz
|
// extractAttr("foo.bar", {"foo": {"bar": "baz"}}) => baz
|
||||||
//
|
//
|
||||||
std::string extractAttr(const std::string& attr, const Json::Value& jsonValue)
|
Json::Value extractAttr(const std::string& attr, const Json::Value& jsonValue)
|
||||||
{
|
{
|
||||||
// Split by .
|
// Split by .
|
||||||
std::string token;
|
std::string token;
|
||||||
@ -56,17 +51,21 @@ namespace ix
|
|||||||
val = val[token];
|
val = val[token];
|
||||||
}
|
}
|
||||||
|
|
||||||
return val.asString();
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cobra_to_statsd_bot(const ix::CobraConfig& config,
|
int64_t cobra_to_statsd_bot(const ix::CobraConfig& config,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
const std::string& filter,
|
const std::string& filter,
|
||||||
const std::string& host,
|
const std::string& position,
|
||||||
int port,
|
StatsdClient& statsdClient,
|
||||||
const std::string& prefix,
|
const std::string& fields,
|
||||||
const std::string& fields,
|
const std::string& gauge,
|
||||||
bool verbose)
|
const std::string& timer,
|
||||||
|
bool verbose,
|
||||||
|
size_t maxQueueSize,
|
||||||
|
bool enableHeartbeat,
|
||||||
|
int runtime)
|
||||||
{
|
{
|
||||||
ix::CobraConnection conn;
|
ix::CobraConnection conn;
|
||||||
conn.configure(config);
|
conn.configure(config);
|
||||||
@ -74,150 +73,86 @@ namespace ix
|
|||||||
|
|
||||||
auto tokens = parseFields(fields);
|
auto tokens = parseFields(fields);
|
||||||
|
|
||||||
Json::FastWriter jsonWriter;
|
CobraBot bot;
|
||||||
std::atomic<uint64_t> sentCount(0);
|
bot.setOnBotMessageCallback(
|
||||||
std::atomic<uint64_t> receivedCount(0);
|
[&statsdClient, &tokens, &gauge, &timer](const Json::Value& msg,
|
||||||
std::atomic<bool> stop(false);
|
const std::string& /*position*/,
|
||||||
|
const bool verbose,
|
||||||
size_t maxQueueSize = 1000;
|
std::atomic<bool>& /*throttled*/,
|
||||||
QueueManager queueManager(maxQueueSize, stop);
|
std::atomic<bool>& fatalCobraError) -> bool {
|
||||||
|
|
||||||
auto timer = [&sentCount, &receivedCount] {
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
spdlog::info("messages received {} sent {}", receivedCount, sentCount);
|
|
||||||
|
|
||||||
auto duration = std::chrono::seconds(1);
|
|
||||||
std::this_thread::sleep_for(duration);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::thread t1(timer);
|
|
||||||
|
|
||||||
auto heartbeat = [&sentCount, &receivedCount] {
|
|
||||||
std::string state("na");
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::thread t2(heartbeat);
|
|
||||||
|
|
||||||
auto statsdSender = [&queueManager, &host, &port, &sentCount, &tokens, &prefix, &stop] {
|
|
||||||
// statsd client
|
|
||||||
// test with netcat as a server: `nc -ul 8125`
|
|
||||||
bool statsdBatch = true;
|
|
||||||
#ifndef _WIN32
|
|
||||||
statsd::StatsdClient statsdClient(host, port, prefix, statsdBatch);
|
|
||||||
#else
|
|
||||||
int statsdClient;
|
|
||||||
#endif
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
Json::Value msg = queueManager.pop();
|
|
||||||
|
|
||||||
if (msg.isNull()) continue;
|
|
||||||
if (stop) return;
|
|
||||||
|
|
||||||
std::string id;
|
std::string id;
|
||||||
for (auto&& attr : tokens)
|
for (auto&& attr : tokens)
|
||||||
{
|
{
|
||||||
id += ".";
|
id += ".";
|
||||||
id += extractAttr(attr, msg);
|
auto val = extractAttr(attr, msg);
|
||||||
|
id += val.asString();
|
||||||
}
|
}
|
||||||
|
|
||||||
sentCount += 1;
|
if (gauge.empty() && timer.empty())
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
statsdClient.count(id, 1);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::thread t3(statsdSender);
|
|
||||||
|
|
||||||
conn.setEventCallback(
|
|
||||||
[&conn, &channel, &filter, &jsonWriter, verbose, &queueManager, &receivedCount](
|
|
||||||
ix::CobraConnectionEventType eventType,
|
|
||||||
const std::string& errMsg,
|
|
||||||
const ix::WebSocketHttpHeaders& headers,
|
|
||||||
const std::string& subscriptionId,
|
|
||||||
CobraConnection::MsgId msgId) {
|
|
||||||
if (eventType == ix::CobraConnection_EventType_Open)
|
|
||||||
{
|
{
|
||||||
spdlog::info("Subscriber connected");
|
statsdClient.count(id, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::string attrName = (!gauge.empty()) ? gauge : timer;
|
||||||
|
auto val = extractAttr(attrName, msg);
|
||||||
|
size_t x;
|
||||||
|
|
||||||
for (auto it : headers)
|
if (val.isInt())
|
||||||
{
|
{
|
||||||
spdlog::info("{}: {}", it.first, it.second);
|
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
|
||||||
|
{
|
||||||
|
CoreLogger::error("Gauge " + gauge + " is not a numeric type");
|
||||||
|
fatalCobraError = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (verbose)
|
||||||
|
{
|
||||||
|
CoreLogger::info(id + " - " + attrName + " -> " + std::to_string(x));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!gauge.empty())
|
||||||
|
{
|
||||||
|
statsdClient.gauge(id, x);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
statsdClient.timing(id, x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (eventType == ix::CobraConnection_EventType_Closed)
|
|
||||||
{
|
|
||||||
spdlog::info("Subscriber closed");
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Authenticated)
|
|
||||||
{
|
|
||||||
spdlog::info("Subscriber authenticated");
|
|
||||||
conn.subscribe(channel,
|
|
||||||
filter,
|
|
||||||
[&jsonWriter, &queueManager, verbose, &receivedCount](
|
|
||||||
const Json::Value& msg, const std::string& position) {
|
|
||||||
if (verbose)
|
|
||||||
{
|
|
||||||
spdlog::info("Subscriber received message {} -> {}", position, jsonWriter.write(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
receivedCount++;
|
return true;
|
||||||
|
|
||||||
++receivedCount;
|
|
||||||
queueManager.add(msg);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Subscribed)
|
|
||||||
{
|
|
||||||
spdlog::info("Subscriber: subscribed to channel {}", subscriptionId);
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_UnSubscribed)
|
|
||||||
{
|
|
||||||
spdlog::info("Subscriber: unsubscribed from channel {}", subscriptionId);
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Error)
|
|
||||||
{
|
|
||||||
spdlog::error("Subscriber: error {}", errMsg);
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Published)
|
|
||||||
{
|
|
||||||
spdlog::error("Published message hacked: {}", msgId);
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Pong)
|
|
||||||
{
|
|
||||||
spdlog::info("Received websocket pong");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
while (true)
|
bool useQueue = true;
|
||||||
{
|
|
||||||
std::chrono::duration<double, std::milli> duration(1000);
|
|
||||||
std::this_thread::sleep_for(duration);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return bot.run(config,
|
||||||
|
channel,
|
||||||
|
filter,
|
||||||
|
position,
|
||||||
|
verbose,
|
||||||
|
maxQueueSize,
|
||||||
|
useQueue,
|
||||||
|
enableHeartbeat,
|
||||||
|
runtime);
|
||||||
}
|
}
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -1,22 +1,28 @@
|
|||||||
/*
|
/*
|
||||||
* IXCobraToStatsdBot.h
|
* IXCobraToStatsdBot.h
|
||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <ixbots/IXStatsdClient.h>
|
||||||
#include <ixcobra/IXCobraConfig.h>
|
#include <ixcobra/IXCobraConfig.h>
|
||||||
#include <string>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
int cobra_to_statsd_bot(const ix::CobraConfig& config,
|
int64_t cobra_to_statsd_bot(const ix::CobraConfig& config,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
const std::string& filter,
|
const std::string& filter,
|
||||||
const std::string& host,
|
const std::string& position,
|
||||||
int port,
|
StatsdClient& statsdClient,
|
||||||
const std::string& prefix,
|
const std::string& fields,
|
||||||
const std::string& fields,
|
const std::string& gauge,
|
||||||
bool verbose);
|
const std::string& timer,
|
||||||
|
bool verbose,
|
||||||
|
size_t maxQueueSize,
|
||||||
|
bool enableHeartbeat,
|
||||||
|
int runtime);
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
107
ixbots/ixbots/IXCobraToStdoutBot.cpp
Normal file
107
ixbots/ixbots/IXCobraToStdoutBot.cpp
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* 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 <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
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
|
25
ixbots/ixbots/IXCobraToStdoutBot.h
Normal file
25
ixbots/ixbots/IXCobraToStdoutBot.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* IXCobraToStdoutBot.h
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <ixcobra/IXCobraConfig.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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
|
@ -5,19 +5,20 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "IXQueueManager.h"
|
#include "IXQueueManager.h"
|
||||||
#include <vector>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
Json::Value QueueManager::pop()
|
std::pair<Json::Value, std::string> QueueManager::pop()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(_mutex);
|
std::unique_lock<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
if (_queues.empty())
|
if (_queues.empty())
|
||||||
{
|
{
|
||||||
Json::Value val;
|
Json::Value val;
|
||||||
return val;
|
return std::make_pair(val, std::string());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> games;
|
std::vector<std::string> games;
|
||||||
@ -35,7 +36,7 @@ namespace ix
|
|||||||
if (_queues[game].empty())
|
if (_queues[game].empty())
|
||||||
{
|
{
|
||||||
Json::Value val;
|
Json::Value val;
|
||||||
return val;
|
return std::make_pair(val, std::string());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto msg = _queues[game].front();
|
auto msg = _queues[game].front();
|
||||||
@ -43,7 +44,7 @@ namespace ix
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QueueManager::add(Json::Value msg)
|
void QueueManager::add(const Json::Value& msg, const std::string& position)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(_mutex);
|
std::unique_lock<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
@ -59,8 +60,8 @@ namespace ix
|
|||||||
// in queuing too many events.
|
// in queuing too many events.
|
||||||
if (_queues[game].size() < _maxQueueSize)
|
if (_queues[game].size() < _maxQueueSize)
|
||||||
{
|
{
|
||||||
_queues[game].push(msg);
|
_queues[game].push(std::make_pair(msg, position));
|
||||||
_condition.notify_one();
|
_condition.notify_one();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} // namespace ix
|
||||||
|
@ -6,33 +6,30 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <atomic>
|
|
||||||
#include <json/json.h>
|
|
||||||
#include <mutex>
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <queue>
|
#include <json/json.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <mutex>
|
||||||
|
#include <queue>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class QueueManager
|
class QueueManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QueueManager(size_t maxQueueSize, std::atomic<bool>& stop)
|
QueueManager(size_t maxQueueSize)
|
||||||
: _maxQueueSize(maxQueueSize)
|
: _maxQueueSize(maxQueueSize)
|
||||||
, _stop(stop)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::Value pop();
|
std::pair<Json::Value, std::string> pop();
|
||||||
void add(Json::Value msg);
|
void add(const Json::Value& msg, const std::string& position);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::queue<Json::Value>> _queues;
|
std::map<std::string, std::queue<std::pair<Json::Value, std::string>>> _queues;
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
std::condition_variable _condition;
|
std::condition_variable _condition;
|
||||||
size_t _maxQueueSize;
|
size_t _maxQueueSize;
|
||||||
std::atomic<bool>& _stop;
|
|
||||||
};
|
};
|
||||||
}
|
} // namespace ix
|
||||||
|
147
ixbots/ixbots/IXStatsdClient.cpp
Normal file
147
ixbots/ixbots/IXStatsdClient.cpp
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
/*
|
||||||
|
* 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 <iostream>
|
||||||
|
#include <ixwebsocket/IXNetSystem.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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
|
57
ixbots/ixbots/IXStatsdClient.h
Normal file
57
ixbots/ixbots/IXStatsdClient.h
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* IXStatsdClient.h
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <deque>
|
||||||
|
#include <ixwebsocket/IXUdpSocket.h>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
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
|
@ -14,6 +14,7 @@ set (IXCOBRA_HEADERS
|
|||||||
ixcobra/IXCobraMetricsThreadedPublisher.h
|
ixcobra/IXCobraMetricsThreadedPublisher.h
|
||||||
ixcobra/IXCobraMetricsPublisher.h
|
ixcobra/IXCobraMetricsPublisher.h
|
||||||
ixcobra/IXCobraConfig.h
|
ixcobra/IXCobraConfig.h
|
||||||
|
ixcobra/IXCobraEventType.h
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(ixcobra STATIC
|
add_library(ixcobra STATIC
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
|
|
||||||
#include <ixwebsocket/IXSocketTLSOptions.h>
|
#include <ixwebsocket/IXSocketTLSOptions.h>
|
||||||
|
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
|
@ -5,17 +5,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "IXCobraConnection.h"
|
#include "IXCobraConnection.h"
|
||||||
#include <ixcrypto/IXHMac.h>
|
|
||||||
#include <ixwebsocket/IXWebSocket.h>
|
|
||||||
#include <ixwebsocket/IXSocketTLSOptions.h>
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <stdexcept>
|
|
||||||
#include <cmath>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <ixcrypto/IXHMac.h>
|
||||||
|
#include <ixwebsocket/IXSocketTLSOptions.h>
|
||||||
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
@ -26,12 +26,12 @@ namespace ix
|
|||||||
constexpr CobraConnection::MsgId CobraConnection::kInvalidMsgId;
|
constexpr CobraConnection::MsgId CobraConnection::kInvalidMsgId;
|
||||||
constexpr int CobraConnection::kPingIntervalSecs;
|
constexpr int CobraConnection::kPingIntervalSecs;
|
||||||
|
|
||||||
CobraConnection::CobraConnection() :
|
CobraConnection::CobraConnection()
|
||||||
_webSocket(new WebSocket()),
|
: _webSocket(new WebSocket())
|
||||||
_publishMode(CobraConnection_PublishMode_Immediate),
|
, _publishMode(CobraConnection_PublishMode_Immediate)
|
||||||
_authenticated(false),
|
, _authenticated(false)
|
||||||
_eventCallback(nullptr),
|
, _eventCallback(nullptr)
|
||||||
_id(1)
|
, _id(1)
|
||||||
{
|
{
|
||||||
_pdu["action"] = "rtm/publish";
|
_pdu["action"] = "rtm/publish";
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ namespace ix
|
|||||||
_eventCallback = eventCallback;
|
_eventCallback = eventCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraConnection::invokeEventCallback(ix::CobraConnectionEventType eventType,
|
void CobraConnection::invokeEventCallback(ix::CobraEventType eventType,
|
||||||
const std::string& errorMsg,
|
const std::string& errorMsg,
|
||||||
const WebSocketHttpHeaders& headers,
|
const WebSocketHttpHeaders& headers,
|
||||||
const std::string& subscriptionId,
|
const std::string& subscriptionId,
|
||||||
@ -96,7 +96,8 @@ namespace ix
|
|||||||
std::lock_guard<std::mutex> lock(_eventCallbackMutex);
|
std::lock_guard<std::mutex> lock(_eventCallbackMutex);
|
||||||
if (_eventCallback)
|
if (_eventCallback)
|
||||||
{
|
{
|
||||||
_eventCallback(eventType, errorMsg, headers, subscriptionId, msgId);
|
_eventCallback(
|
||||||
|
std::make_unique<CobraEvent>(eventType, errorMsg, headers, subscriptionId, msgId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +106,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << errorMsg << " : received pdu => " << serializedPdu;
|
ss << errorMsg << " : received pdu => " << serializedPdu;
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Error, ss.str());
|
invokeEventCallback(ix::CobraEventType::Error, ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraConnection::disconnect()
|
void CobraConnection::disconnect()
|
||||||
@ -116,123 +117,119 @@ namespace ix
|
|||||||
|
|
||||||
void CobraConnection::initWebSocketOnMessageCallback()
|
void CobraConnection::initWebSocketOnMessageCallback()
|
||||||
{
|
{
|
||||||
_webSocket->setOnMessageCallback(
|
_webSocket->setOnMessageCallback([this](const ix::WebSocketMessagePtr& msg) {
|
||||||
[this](const ix::WebSocketMessagePtr& msg)
|
CobraConnection::invokeTrafficTrackerCallback(msg->wireSize, true);
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
CobraConnection::invokeTrafficTrackerCallback(msg->wireSize, true);
|
invokeEventCallback(ix::CobraEventType::Open, std::string(), msg->openInfo.headers);
|
||||||
|
sendHandshakeMessage();
|
||||||
|
}
|
||||||
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
||||||
|
{
|
||||||
|
_authenticated = false;
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
ss << "Close code " << msg->closeInfo.code;
|
||||||
|
ss << " reason " << msg->closeInfo.reason;
|
||||||
|
invokeEventCallback(ix::CobraEventType::Closed, ss.str());
|
||||||
|
}
|
||||||
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
||||||
|
{
|
||||||
|
Json::Value data;
|
||||||
|
Json::Reader reader;
|
||||||
|
if (!reader.parse(msg->str, data))
|
||||||
{
|
{
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Open,
|
invokeErrorCallback("Invalid json", msg->str);
|
||||||
std::string(),
|
return;
|
||||||
msg->openInfo.headers);
|
|
||||||
sendHandshakeMessage();
|
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Close)
|
|
||||||
{
|
|
||||||
_authenticated = false;
|
|
||||||
|
|
||||||
std::stringstream ss;
|
if (!data.isMember("action"))
|
||||||
ss << "Close code " << msg->closeInfo.code;
|
|
||||||
ss << " reason " << msg->closeInfo.reason;
|
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Closed,
|
|
||||||
ss.str());
|
|
||||||
}
|
|
||||||
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
||||||
{
|
{
|
||||||
Json::Value data;
|
invokeErrorCallback("Missing action", msg->str);
|
||||||
Json::Reader reader;
|
return;
|
||||||
if (!reader.parse(msg->str, data))
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Invalid json", msg->str);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!data.isMember("action"))
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Missing action", msg->str);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto action = data["action"].asString();
|
|
||||||
|
|
||||||
if (action == "auth/handshake/ok")
|
|
||||||
{
|
|
||||||
if (!handleHandshakeResponse(data))
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Error extracting nonce from handshake response", msg->str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (action == "auth/handshake/error")
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Handshake error", msg->str);
|
|
||||||
}
|
|
||||||
else if (action == "auth/authenticate/ok")
|
|
||||||
{
|
|
||||||
_authenticated = true;
|
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Authenticated);
|
|
||||||
flushQueue();
|
|
||||||
}
|
|
||||||
else if (action == "auth/authenticate/error")
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Authentication error", msg->str);
|
|
||||||
}
|
|
||||||
else if (action == "rtm/subscription/data")
|
|
||||||
{
|
|
||||||
handleSubscriptionData(data);
|
|
||||||
}
|
|
||||||
else if (action == "rtm/subscribe/ok")
|
|
||||||
{
|
|
||||||
if (!handleSubscriptionResponse(data))
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Error processing subscribe response", msg->str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (action == "rtm/subscribe/error")
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Subscription error", msg->str);
|
|
||||||
}
|
|
||||||
else if (action == "rtm/unsubscribe/ok")
|
|
||||||
{
|
|
||||||
if (!handleUnsubscriptionResponse(data))
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Error processing unsubscribe response", msg->str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (action == "rtm/unsubscribe/error")
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Unsubscription error", msg->str);
|
|
||||||
}
|
|
||||||
else if (action == "rtm/publish/ok")
|
|
||||||
{
|
|
||||||
if (!handlePublishResponse(data))
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Error processing publish response", msg->str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (action == "rtm/publish/error")
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Publish error", msg->str);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Un-handled message type", msg->str);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Error)
|
|
||||||
|
auto action = data["action"].asString();
|
||||||
|
|
||||||
|
if (action == "auth/handshake/ok")
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
if (!handleHandshakeResponse(data))
|
||||||
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
{
|
||||||
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
invokeErrorCallback("Error extracting nonce from handshake response",
|
||||||
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
msg->str);
|
||||||
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
}
|
||||||
invokeErrorCallback(ss.str(), std::string());
|
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Pong)
|
else if (action == "auth/handshake/error")
|
||||||
{
|
{
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Pong);
|
invokeEventCallback(ix::CobraEventType::HandshakeError, msg->str);
|
||||||
}
|
}
|
||||||
|
else if (action == "auth/authenticate/ok")
|
||||||
|
{
|
||||||
|
_authenticated = true;
|
||||||
|
invokeEventCallback(ix::CobraEventType::Authenticated);
|
||||||
|
flushQueue();
|
||||||
|
}
|
||||||
|
else if (action == "auth/authenticate/error")
|
||||||
|
{
|
||||||
|
invokeEventCallback(ix::CobraEventType::AuthenticationError, msg->str);
|
||||||
|
}
|
||||||
|
else if (action == "rtm/subscription/data")
|
||||||
|
{
|
||||||
|
handleSubscriptionData(data);
|
||||||
|
}
|
||||||
|
else if (action == "rtm/subscribe/ok")
|
||||||
|
{
|
||||||
|
if (!handleSubscriptionResponse(data))
|
||||||
|
{
|
||||||
|
invokeErrorCallback("Error processing subscribe response", msg->str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (action == "rtm/subscribe/error")
|
||||||
|
{
|
||||||
|
invokeEventCallback(ix::CobraEventType::SubscriptionError, msg->str);
|
||||||
|
}
|
||||||
|
else if (action == "rtm/unsubscribe/ok")
|
||||||
|
{
|
||||||
|
if (!handleUnsubscriptionResponse(data))
|
||||||
|
{
|
||||||
|
invokeErrorCallback("Error processing unsubscribe response", msg->str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (action == "rtm/unsubscribe/error")
|
||||||
|
{
|
||||||
|
invokeErrorCallback("Unsubscription error", msg->str);
|
||||||
|
}
|
||||||
|
else if (action == "rtm/publish/ok")
|
||||||
|
{
|
||||||
|
if (!handlePublishResponse(data))
|
||||||
|
{
|
||||||
|
invokeErrorCallback("Error processing publish response", msg->str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (action == "rtm/publish/error")
|
||||||
|
{
|
||||||
|
invokeErrorCallback("Publish error", msg->str);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
invokeErrorCallback("Un-handled message type", msg->str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
||||||
|
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
||||||
|
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
||||||
|
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
||||||
|
invokeErrorCallback(ss.str(), std::string());
|
||||||
|
}
|
||||||
|
else if (msg->type == ix::WebSocketMessageType::Pong)
|
||||||
|
{
|
||||||
|
invokeEventCallback(ix::CobraEventType::Pong, msg->str);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,12 +243,13 @@ namespace ix
|
|||||||
return _publishMode;
|
return _publishMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraConnection::configure(const std::string& appkey,
|
void CobraConnection::configure(
|
||||||
const std::string& endpoint,
|
const std::string& appkey,
|
||||||
const std::string& rolename,
|
const std::string& endpoint,
|
||||||
const std::string& rolesecret,
|
const std::string& rolename,
|
||||||
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions,
|
const std::string& rolesecret,
|
||||||
const SocketTLSOptions& socketTLSOptions)
|
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions,
|
||||||
|
const SocketTLSOptions& socketTLSOptions)
|
||||||
{
|
{
|
||||||
_roleName = rolename;
|
_roleName = rolename;
|
||||||
_roleSecret = rolesecret;
|
_roleSecret = rolesecret;
|
||||||
@ -270,10 +268,6 @@ 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);
|
||||||
|
|
||||||
// If we don't receive a pong back, declare loss after 3 * N seconds
|
|
||||||
// (will be 90s now), and close and restart the connection
|
|
||||||
_webSocket->setPingTimeout(3 * kPingIntervalSecs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraConnection::configure(const ix::CobraConfig& config)
|
void CobraConnection::configure(const ix::CobraConfig& config)
|
||||||
@ -397,8 +391,9 @@ namespace ix
|
|||||||
|
|
||||||
if (!subscriptionId.isString()) return false;
|
if (!subscriptionId.isString()) return false;
|
||||||
|
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Subscribed,
|
invokeEventCallback(ix::CobraEventType::Subscribed,
|
||||||
std::string(), WebSocketHttpHeaders(),
|
std::string(),
|
||||||
|
WebSocketHttpHeaders(),
|
||||||
subscriptionId.asString());
|
subscriptionId.asString());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -415,8 +410,9 @@ namespace ix
|
|||||||
|
|
||||||
if (!subscriptionId.isString()) return false;
|
if (!subscriptionId.isString()) return false;
|
||||||
|
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_UnSubscribed,
|
invokeEventCallback(ix::CobraEventType::UnSubscribed,
|
||||||
std::string(), WebSocketHttpHeaders(),
|
std::string(),
|
||||||
|
WebSocketHttpHeaders(),
|
||||||
subscriptionId.asString());
|
subscriptionId.asString());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -463,9 +459,11 @@ namespace ix
|
|||||||
|
|
||||||
uint64_t msgId = id.asUInt64();
|
uint64_t msgId = id.asUInt64();
|
||||||
|
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Published,
|
invokeEventCallback(ix::CobraEventType::Published,
|
||||||
std::string(), WebSocketHttpHeaders(),
|
std::string(),
|
||||||
std::string(), msgId);
|
WebSocketHttpHeaders(),
|
||||||
|
std::string(),
|
||||||
|
msgId);
|
||||||
|
|
||||||
invokePublishTrackerCallback(false, true);
|
invokePublishTrackerCallback(false, true);
|
||||||
|
|
||||||
@ -495,9 +493,7 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::pair<CobraConnection::MsgId, std::string> CobraConnection::prePublish(
|
std::pair<CobraConnection::MsgId, std::string> CobraConnection::prePublish(
|
||||||
const Json::Value& channels,
|
const Json::Value& channels, const Json::Value& msg, bool addToQueue)
|
||||||
const Json::Value& msg,
|
|
||||||
bool addToQueue)
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_prePublishMutex);
|
std::lock_guard<std::mutex> lock(_prePublishMutex);
|
||||||
|
|
||||||
@ -565,6 +561,7 @@ 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
|
||||||
@ -576,6 +573,11 @@ 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;
|
||||||
@ -657,8 +659,7 @@ namespace ix
|
|||||||
bool CobraConnection::publishMessage(const std::string& serializedJson)
|
bool CobraConnection::publishMessage(const std::string& serializedJson)
|
||||||
{
|
{
|
||||||
auto webSocketSendInfo = _webSocket->send(serializedJson);
|
auto webSocketSendInfo = _webSocket->send(serializedJson);
|
||||||
CobraConnection::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize,
|
CobraConnection::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize, false);
|
||||||
false);
|
|
||||||
return webSocketSendInfo.success;
|
return webSocketSendInfo.success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,36 +6,29 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "IXCobraConfig.h"
|
||||||
|
#include "IXCobraEvent.h"
|
||||||
|
#include "IXCobraEventType.h"
|
||||||
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
|
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
|
||||||
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
|
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
|
||||||
#include <json/json.h>
|
#include <json/json.h>
|
||||||
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#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,
|
||||||
@ -43,11 +36,7 @@ namespace ix
|
|||||||
};
|
};
|
||||||
|
|
||||||
using SubscriptionCallback = std::function<void(const Json::Value&, const std::string&)>;
|
using SubscriptionCallback = std::function<void(const Json::Value&, const std::string&)>;
|
||||||
using EventCallback = std::function<void(CobraConnectionEventType,
|
using EventCallback = std::function<void(const CobraEventPtr&)>;
|
||||||
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)>;
|
||||||
@ -98,6 +87,7 @@ 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
|
||||||
@ -130,10 +120,9 @@ namespace ix
|
|||||||
|
|
||||||
/// Prepare a message for transmission
|
/// Prepare a message for transmission
|
||||||
/// (update the pdu, compute a msgId, serialize json to a string)
|
/// (update the pdu, compute a msgId, serialize json to a string)
|
||||||
std::pair<CobraConnection::MsgId, std::string> prePublish(
|
std::pair<CobraConnection::MsgId, std::string> prePublish(const Json::Value& channels,
|
||||||
const Json::Value& channels,
|
const Json::Value& msg,
|
||||||
const Json::Value& msg,
|
bool addToQueue);
|
||||||
bool addToQueue);
|
|
||||||
|
|
||||||
/// Attempt to send next message from the internal queue
|
/// Attempt to send next message from the internal queue
|
||||||
bool publishNext();
|
bool publishNext();
|
||||||
@ -163,7 +152,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(CobraConnectionEventType eventType,
|
void invokeEventCallback(CobraEventType 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(),
|
||||||
|
41
ixcobra/ixcobra/IXCobraEvent.h
Normal file
41
ixcobra/ixcobra/IXCobraEvent.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* IXCobraEvent.h
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "IXCobraEventType.h"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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>;
|
||||||
|
} // namespace ix
|
25
ixcobra/ixcobra/IXCobraEventType.h
Normal file
25
ixcobra/ixcobra/IXCobraEventType.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
};
|
||||||
|
}
|
@ -5,9 +5,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "IXCobraMetricsPublisher.h"
|
#include "IXCobraMetricsPublisher.h"
|
||||||
#include <ixwebsocket/IXSocketTLSOptions.h>
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <ixwebsocket/IXSocketTLSOptions.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
|
||||||
@ -17,8 +17,8 @@ namespace ix
|
|||||||
const std::string CobraMetricsPublisher::kSetRateControlId = "sms_set_rate_control_id";
|
const std::string CobraMetricsPublisher::kSetRateControlId = "sms_set_rate_control_id";
|
||||||
const std::string CobraMetricsPublisher::kSetBlacklistId = "sms_set_blacklist_id";
|
const std::string CobraMetricsPublisher::kSetBlacklistId = "sms_set_blacklist_id";
|
||||||
|
|
||||||
CobraMetricsPublisher::CobraMetricsPublisher() :
|
CobraMetricsPublisher::CobraMetricsPublisher()
|
||||||
_enabled(true)
|
: _enabled(true)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,20 +27,11 @@ namespace ix
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraMetricsPublisher::configure(const std::string& appkey,
|
void CobraMetricsPublisher::configure(const CobraConfig& config, 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()
|
||||||
@ -50,7 +41,7 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CobraMetricsPublisher::setGenericAttributes(const std::string& attrName,
|
void CobraMetricsPublisher::setGenericAttributes(const std::string& attrName,
|
||||||
const Json::Value& value)
|
const Json::Value& value)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_device_mutex);
|
std::lock_guard<std::mutex> lock(_device_mutex);
|
||||||
_device[attrName] = value;
|
_device[attrName] = value;
|
||||||
@ -115,8 +106,7 @@ namespace ix
|
|||||||
auto last_update = _last_update.find(id);
|
auto last_update = _last_update.find(id);
|
||||||
if (last_update == _last_update.end()) return false;
|
if (last_update == _last_update.end()) return false;
|
||||||
|
|
||||||
auto timeDeltaFromLastSend =
|
auto timeDeltaFromLastSend = std::chrono::steady_clock::now() - last_update->second;
|
||||||
std::chrono::steady_clock::now() - last_update->second;
|
|
||||||
|
|
||||||
return timeDeltaFromLastSend < std::chrono::seconds(rate_control_it->second);
|
return timeDeltaFromLastSend < std::chrono::seconds(rate_control_it->second);
|
||||||
}
|
}
|
||||||
@ -131,8 +121,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
auto ms =
|
auto ms =
|
||||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
||||||
now.time_since_epoch()).count();
|
|
||||||
|
|
||||||
return ms;
|
return ms;
|
||||||
}
|
}
|
||||||
@ -173,10 +162,9 @@ namespace ix
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
CobraConnection::MsgId CobraMetricsPublisher::push(
|
CobraConnection::MsgId CobraMetricsPublisher::push(const std::string& id,
|
||||||
const std::string& id,
|
const Json::Value& data,
|
||||||
const Json::Value& data,
|
bool shouldPushTest)
|
||||||
bool shouldPushTest)
|
|
||||||
{
|
{
|
||||||
if (shouldPushTest && !shouldPush(id)) return CobraConnection::kInvalidMsgId;
|
if (shouldPushTest && !shouldPush(id)) return CobraConnection::kInvalidMsgId;
|
||||||
|
|
||||||
|
@ -40,13 +40,7 @@ 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 std::string& appkey,
|
void configure(const CobraConfig& config, 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
|
||||||
@ -73,10 +67,14 @@ namespace ix
|
|||||||
/// shouldPush method for places where we want to be as lightweight as possible when
|
/// shouldPush method for places where we want to be as lightweight as possible when
|
||||||
/// collecting metrics. When set to false, it is used so that we don't do double work when
|
/// collecting metrics. When set to false, it is used so that we don't do double work when
|
||||||
/// computing whether a metrics should be sent or not.
|
/// computing whether a metrics should be sent or not.
|
||||||
CobraConnection::MsgId push(const std::string& id, const Json::Value& data, bool shouldPushTest = true);
|
CobraConnection::MsgId push(const std::string& id,
|
||||||
|
const Json::Value& data,
|
||||||
|
bool shouldPushTest = true);
|
||||||
|
|
||||||
/// Interface used by lua. msg is a json encoded string.
|
/// Interface used by lua. msg is a json encoded string.
|
||||||
CobraConnection::MsgId push(const std::string& id, const std::string& data, bool shouldPushTest = true);
|
CobraConnection::MsgId push(const std::string& id,
|
||||||
|
const std::string& data,
|
||||||
|
bool shouldPushTest = true);
|
||||||
|
|
||||||
/// Tells whether a metric can be pushed.
|
/// Tells whether a metric can be pushed.
|
||||||
/// A metric can be pushed if it satisfies those conditions:
|
/// A metric can be pushed if it satisfies those conditions:
|
||||||
@ -94,10 +92,16 @@ namespace ix
|
|||||||
void setGenericAttributes(const std::string& attrName, const Json::Value& value);
|
void setGenericAttributes(const std::string& attrName, const Json::Value& value);
|
||||||
|
|
||||||
/// Set a unique id for the session. A uuid can be used.
|
/// Set a unique id for the session. A uuid can be used.
|
||||||
void setSession(const std::string& session) { _session = session; }
|
void setSession(const std::string& session)
|
||||||
|
{
|
||||||
|
_session = session;
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the unique id used to identify the current session
|
/// Get the unique id used to identify the current session
|
||||||
const std::string& getSession() const { return _session; }
|
const std::string& getSession() const
|
||||||
|
{
|
||||||
|
return _session;
|
||||||
|
}
|
||||||
|
|
||||||
/// Return the number of milliseconds since the epoch (~1970)
|
/// Return the number of milliseconds since the epoch (~1970)
|
||||||
uint64_t getMillisecondsSinceEpoch() const;
|
uint64_t getMillisecondsSinceEpoch() const;
|
||||||
|
@ -5,72 +5,77 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "IXCobraMetricsThreadedPublisher.h"
|
#include "IXCobraMetricsThreadedPublisher.h"
|
||||||
#include <ixwebsocket/IXSetThreadName.h>
|
|
||||||
#include <ixwebsocket/IXSocketTLSOptions.h>
|
|
||||||
#include <ixcore/utils/IXCoreLogger.h>
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <stdexcept>
|
|
||||||
#include <cmath>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <ixcore/utils/IXCoreLogger.h>
|
||||||
|
#include <ixwebsocket/IXSetThreadName.h>
|
||||||
|
#include <ixwebsocket/IXSocketTLSOptions.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
CobraMetricsThreadedPublisher::CobraMetricsThreadedPublisher() :
|
CobraMetricsThreadedPublisher::CobraMetricsThreadedPublisher()
|
||||||
_stop(false)
|
: _stop(false)
|
||||||
{
|
{
|
||||||
_cobra_connection.setEventCallback(
|
_cobra_connection.setEventCallback([](const CobraEventPtr& event) {
|
||||||
[]
|
std::stringstream ss;
|
||||||
(ix::CobraConnectionEventType eventType,
|
|
||||||
const std::string& errMsg,
|
if (event->type == ix::CobraEventType::Open)
|
||||||
const ix::WebSocketHttpHeaders& headers,
|
|
||||||
const std::string& subscriptionId,
|
|
||||||
CobraConnection::MsgId msgId)
|
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
ss << "Handshake headers" << std::endl;
|
||||||
|
|
||||||
if (eventType == ix::CobraConnection_EventType_Open)
|
for (auto&& it : event->headers)
|
||||||
{
|
{
|
||||||
ss << "Handshake headers" << std::endl;
|
ss << it.first << ": " << it.second << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Authenticated)
|
||||||
|
{
|
||||||
|
ss << "Authenticated";
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Error)
|
||||||
|
{
|
||||||
|
ss << "Error: " << event->errMsg;
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Closed)
|
||||||
|
{
|
||||||
|
ss << "Connection closed: " << event->errMsg;
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Subscribed)
|
||||||
|
{
|
||||||
|
ss << "Subscribed through subscription id: " << event->subscriptionId;
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::UnSubscribed)
|
||||||
|
{
|
||||||
|
ss << "Unsubscribed through subscription id: " << event->subscriptionId;
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::Published)
|
||||||
|
{
|
||||||
|
ss << "Published message " << event->msgId << " acked";
|
||||||
|
}
|
||||||
|
else if (event->type == ix::CobraEventType::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;
|
||||||
|
}
|
||||||
|
|
||||||
for (auto it : headers)
|
CoreLogger::log(ss.str().c_str());
|
||||||
{
|
|
||||||
ss << it.first << ": " << it.second << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Authenticated)
|
|
||||||
{
|
|
||||||
ss << "Authenticated";
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Error)
|
|
||||||
{
|
|
||||||
ss << "Error: " << errMsg;
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Closed)
|
|
||||||
{
|
|
||||||
ss << "Connection closed: " << errMsg;
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Subscribed)
|
|
||||||
{
|
|
||||||
ss << "Subscribed through subscription id: " << subscriptionId;
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_UnSubscribed)
|
|
||||||
{
|
|
||||||
ss << "Unsubscribed through subscription id: " << subscriptionId;
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Published)
|
|
||||||
{
|
|
||||||
ss << "Published message " << msgId << " acked";
|
|
||||||
}
|
|
||||||
else if (eventType == ix::CobraConnection_EventType_Pong)
|
|
||||||
{
|
|
||||||
ss << "Received websocket pong";
|
|
||||||
}
|
|
||||||
|
|
||||||
ix::IXCoreLogger::Log(ss.str().c_str());
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,22 +97,13 @@ namespace ix
|
|||||||
_thread = std::thread(&CobraMetricsThreadedPublisher::run, this);
|
_thread = std::thread(&CobraMetricsThreadedPublisher::run, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraMetricsThreadedPublisher::configure(const std::string& appkey,
|
void CobraMetricsThreadedPublisher::configure(const CobraConfig& config,
|
||||||
const std::string& endpoint,
|
const std::string& channel)
|
||||||
const std::string& channel,
|
|
||||||
const std::string& rolename,
|
|
||||||
const std::string& rolesecret,
|
|
||||||
bool enablePerMessageDeflate,
|
|
||||||
const SocketTLSOptions& socketTLSOptions)
|
|
||||||
{
|
{
|
||||||
|
CoreLogger::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)
|
||||||
@ -165,13 +161,15 @@ namespace ix
|
|||||||
{
|
{
|
||||||
_cobra_connection.suspend();
|
_cobra_connection.suspend();
|
||||||
continue;
|
continue;
|
||||||
}; break;
|
};
|
||||||
|
break;
|
||||||
|
|
||||||
case MessageKind::Resume:
|
case MessageKind::Resume:
|
||||||
{
|
{
|
||||||
_cobra_connection.resume();
|
_cobra_connection.resume();
|
||||||
continue;
|
continue;
|
||||||
}; break;
|
};
|
||||||
|
break;
|
||||||
|
|
||||||
case MessageKind::Message:
|
case MessageKind::Message:
|
||||||
{
|
{
|
||||||
@ -179,7 +177,8 @@ namespace ix
|
|||||||
{
|
{
|
||||||
_cobra_connection.publishNext();
|
_cobra_connection.publishNext();
|
||||||
}
|
}
|
||||||
}; break;
|
};
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,13 +27,7 @@ namespace ix
|
|||||||
~CobraMetricsThreadedPublisher();
|
~CobraMetricsThreadedPublisher();
|
||||||
|
|
||||||
/// Configuration / set keys, etc...
|
/// Configuration / set keys, etc...
|
||||||
void configure(const std::string& appkey,
|
void configure(const CobraConfig& config, 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();
|
||||||
|
@ -1,14 +1,44 @@
|
|||||||
#include "ixcore/utils/IXCoreLogger.h"
|
/*
|
||||||
|
* IXCoreLogger.cpp
|
||||||
|
* Author: Thomas Wells, Benjamin Sergeant
|
||||||
|
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "ixcore/utils/IXCoreLogger.h"
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
// Default do nothing logger
|
// Default do a no-op logger
|
||||||
IXCoreLogger::LogFunc IXCoreLogger::_currentLogger = [](const char* /*msg*/){};
|
CoreLogger::LogFunc CoreLogger::_currentLogger = [](const char*, LogLevel) {};
|
||||||
|
|
||||||
void IXCoreLogger::Log(const char* msg)
|
void CoreLogger::log(const char* msg, LogLevel level)
|
||||||
{
|
{
|
||||||
_currentLogger(msg);
|
_currentLogger(msg, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // ix
|
void CoreLogger::debug(const std::string& msg)
|
||||||
|
{
|
||||||
|
_currentLogger(msg.c_str(), LogLevel::Debug);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CoreLogger::info(const std::string& msg)
|
||||||
|
{
|
||||||
|
_currentLogger(msg.c_str(), LogLevel::Info);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CoreLogger::warn(const std::string& msg)
|
||||||
|
{
|
||||||
|
_currentLogger(msg.c_str(), LogLevel::Warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CoreLogger::error(const std::string& msg)
|
||||||
|
{
|
||||||
|
_currentLogger(msg.c_str(), LogLevel::Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CoreLogger::critical(const std::string& msg)
|
||||||
|
{
|
||||||
|
_currentLogger(msg.c_str(), LogLevel::Critical);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ix
|
||||||
|
@ -1,15 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* IXCoreLogger.h
|
||||||
|
* Author: Thomas Wells, Benjamin Sergeant
|
||||||
|
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class IXCoreLogger
|
enum class LogLevel
|
||||||
|
{
|
||||||
|
Debug = 0,
|
||||||
|
Info = 1,
|
||||||
|
Warning = 2,
|
||||||
|
Error = 3,
|
||||||
|
Critical = 4
|
||||||
|
};
|
||||||
|
|
||||||
|
class CoreLogger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using LogFunc = std::function<void(const char*)>;
|
using LogFunc = std::function<void(const char*, LogLevel level)>;
|
||||||
static void Log(const char* msg);
|
|
||||||
|
|
||||||
static void setLogFunction(LogFunc& func) { _currentLogger = func; }
|
static void log(const char* msg, LogLevel level = LogLevel::Debug);
|
||||||
|
|
||||||
|
static void debug(const std::string& msg);
|
||||||
|
static void info(const std::string& msg);
|
||||||
|
static void warn(const std::string& msg);
|
||||||
|
static void error(const std::string& msg);
|
||||||
|
static void critical(const std::string& msg);
|
||||||
|
|
||||||
|
static void setLogFunction(LogFunc& func)
|
||||||
|
{
|
||||||
|
_currentLogger = func;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static LogFunc _currentLogger;
|
static LogFunc _currentLogger;
|
||||||
|
@ -23,7 +23,7 @@ add_library(ixcrypto STATIC
|
|||||||
${IXCRYPTO_HEADERS}
|
${IXCRYPTO_HEADERS}
|
||||||
)
|
)
|
||||||
|
|
||||||
set(IXCRYPTO_INCLUDE_DIRS
|
set(IXCRYPTO_INCLUDE_DIRS
|
||||||
.
|
.
|
||||||
../ixcore)
|
../ixcore)
|
||||||
|
|
||||||
@ -31,10 +31,6 @@ 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)
|
||||||
@ -51,4 +47,3 @@ 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()
|
||||||
|
|
||||||
|
@ -29,10 +29,9 @@
|
|||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
static const std::string base64_chars =
|
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
"abcdefghijklmnopqrstuvwxyz"
|
||||||
"abcdefghijklmnopqrstuvwxyz"
|
"0123456789+/";
|
||||||
"0123456789+/";
|
|
||||||
|
|
||||||
std::string base64_encode(const std::string& data, size_t len)
|
std::string base64_encode(const std::string& data, size_t len)
|
||||||
{
|
{
|
||||||
@ -50,26 +49,26 @@ namespace ix
|
|||||||
unsigned char char_array_3[3];
|
unsigned char char_array_3[3];
|
||||||
unsigned char char_array_4[4];
|
unsigned char char_array_4[4];
|
||||||
|
|
||||||
while(len--)
|
while (len--)
|
||||||
{
|
{
|
||||||
char_array_3[i++] = *(bytes_to_encode++);
|
char_array_3[i++] = *(bytes_to_encode++);
|
||||||
if(i == 3)
|
if (i == 3)
|
||||||
{
|
{
|
||||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||||
|
|
||||||
for(i = 0; (i <4) ; i++)
|
for (i = 0; (i < 4); i++)
|
||||||
ret += base64_chars[char_array_4[i]];
|
ret += base64_chars[char_array_4[i]];
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(i)
|
if (i)
|
||||||
{
|
{
|
||||||
for(j = i; j < 3; j++)
|
for (j = i; j < 3; j++)
|
||||||
char_array_3[j] = '\0';
|
char_array_3[j] = '\0';
|
||||||
|
|
||||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||||
@ -77,12 +76,11 @@ namespace ix
|
|||||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||||
|
|
||||||
for(j = 0; (j < i + 1); j++)
|
for (j = 0; (j < i + 1); j++)
|
||||||
ret += base64_chars[char_array_4[j]];
|
ret += base64_chars[char_array_4[j]];
|
||||||
|
|
||||||
while((i++ < 3))
|
while ((i++ < 3))
|
||||||
ret += '=';
|
ret += '=';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@ -95,7 +93,7 @@ namespace ix
|
|||||||
|
|
||||||
std::string base64_decode(const std::string& encoded_string)
|
std::string base64_decode(const std::string& encoded_string)
|
||||||
{
|
{
|
||||||
int in_len = (int)encoded_string.size();
|
int in_len = (int) encoded_string.size();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int j = 0;
|
int j = 0;
|
||||||
int in_ = 0;
|
int in_ = 0;
|
||||||
@ -103,40 +101,42 @@ namespace ix
|
|||||||
std::string ret;
|
std::string ret;
|
||||||
ret.reserve(((in_len + 3) / 4) * 3);
|
ret.reserve(((in_len + 3) / 4) * 3);
|
||||||
|
|
||||||
while(in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
|
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
|
||||||
{
|
{
|
||||||
char_array_4[i++] = encoded_string[in_]; in_++;
|
char_array_4[i++] = encoded_string[in_];
|
||||||
if(i ==4)
|
in_++;
|
||||||
|
if (i == 4)
|
||||||
{
|
{
|
||||||
for(i = 0; i <4; i++)
|
for (i = 0; i < 4; i++)
|
||||||
char_array_4[i] = base64_chars.find(char_array_4[i]);
|
char_array_4[i] = base64_chars.find(char_array_4[i]);
|
||||||
|
|
||||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||||
|
|
||||||
for(i = 0; (i < 3); i++)
|
for (i = 0; (i < 3); i++)
|
||||||
ret += char_array_3[i];
|
ret += char_array_3[i];
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(i)
|
if (i)
|
||||||
{
|
{
|
||||||
for(j = i; j <4; j++)
|
for (j = i; j < 4; j++)
|
||||||
char_array_4[j] = 0;
|
char_array_4[j] = 0;
|
||||||
|
|
||||||
for(j = 0; j <4; j++)
|
for (j = 0; j < 4; j++)
|
||||||
char_array_4[j] = base64_chars.find(char_array_4[j]);
|
char_array_4[j] = base64_chars.find(char_array_4[j]);
|
||||||
|
|
||||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||||
|
|
||||||
for(j = 0; (j < i - 1); j++) ret += char_array_3[j];
|
for (j = 0; (j < i - 1); j++)
|
||||||
|
ret += char_array_3[j];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
} // namespace ix
|
||||||
|
@ -5,16 +5,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "IXHMac.h"
|
#include "IXHMac.h"
|
||||||
|
|
||||||
#include "IXBase64.h"
|
#include "IXBase64.h"
|
||||||
|
|
||||||
#if defined(IXCRYPTO_USE_MBED_TLS)
|
#if defined(IXCRYPTO_USE_MBED_TLS)
|
||||||
# include <mbedtls/md.h>
|
#include <mbedtls/md.h>
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
# include <CommonCrypto/CommonHMAC.h>
|
#include <CommonCrypto/CommonHMAC.h>
|
||||||
#elif defined(IXCRYPTO_USE_OPEN_SSL)
|
#elif defined(IXCRYPTO_USE_OPEN_SSL)
|
||||||
# include <openssl/hmac.h>
|
#include <openssl/hmac.h>
|
||||||
#else
|
#else
|
||||||
# error "Unsupported configuration"
|
#include <assert.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
@ -26,25 +27,27 @@ namespace ix
|
|||||||
|
|
||||||
#if defined(IXCRYPTO_USE_MBED_TLS)
|
#if defined(IXCRYPTO_USE_MBED_TLS)
|
||||||
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_MD5),
|
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_MD5),
|
||||||
(unsigned char *) key.c_str(), key.size(),
|
(unsigned char*) key.c_str(),
|
||||||
(unsigned char *) data.c_str(), data.size(),
|
key.size(),
|
||||||
(unsigned char *) &hash);
|
(unsigned char*) data.c_str(),
|
||||||
|
data.size(),
|
||||||
|
(unsigned char*) &hash);
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
CCHmac(kCCHmacAlgMD5,
|
CCHmac(kCCHmacAlgMD5, key.c_str(), key.size(), data.c_str(), data.size(), &hash);
|
||||||
key.c_str(), key.size(),
|
|
||||||
data.c_str(), data.size(),
|
|
||||||
&hash);
|
|
||||||
#elif defined(IXCRYPTO_USE_OPEN_SSL)
|
#elif defined(IXCRYPTO_USE_OPEN_SSL)
|
||||||
HMAC(EVP_md5(),
|
HMAC(EVP_md5(),
|
||||||
key.c_str(), (int) key.size(),
|
key.c_str(),
|
||||||
(unsigned char *) data.c_str(), (int) data.size(),
|
(int) key.size(),
|
||||||
(unsigned char *) hash, nullptr);
|
(unsigned char*) data.c_str(),
|
||||||
|
(int) data.size(),
|
||||||
|
(unsigned char*) hash,
|
||||||
|
nullptr);
|
||||||
#else
|
#else
|
||||||
# error "Unsupported configuration"
|
assert(false && "hmac not implemented on this platform");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::string hashString(reinterpret_cast<char*>(hash), hashSize);
|
std::string hashString(reinterpret_cast<char*>(hash), hashSize);
|
||||||
|
|
||||||
return base64_encode(hashString, (uint32_t) hashString.size());
|
return base64_encode(hashString, (uint32_t) hashString.size());
|
||||||
}
|
}
|
||||||
}
|
} // namespace ix
|
||||||
|
@ -19,4 +19,4 @@ namespace ix
|
|||||||
|
|
||||||
return hashAddress;
|
return hashAddress;
|
||||||
}
|
}
|
||||||
}
|
} // namespace ix
|
||||||
|
@ -16,23 +16,23 @@
|
|||||||
|
|
||||||
#include "IXUuid.h"
|
#include "IXUuid.h"
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class Uuid
|
class Uuid
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Uuid();
|
Uuid();
|
||||||
std::string toString() const;
|
std::string toString() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint64_t _ab;
|
uint64_t _ab;
|
||||||
uint64_t _cd;
|
uint64_t _cd;
|
||||||
};
|
};
|
||||||
|
|
||||||
Uuid::Uuid()
|
Uuid::Uuid()
|
||||||
@ -60,7 +60,7 @@ namespace ix
|
|||||||
ss << std::setw(8) << (a);
|
ss << std::setw(8) << (a);
|
||||||
ss << std::setw(4) << (b >> 16);
|
ss << std::setw(4) << (b >> 16);
|
||||||
ss << std::setw(4) << (b & 0xFFFF);
|
ss << std::setw(4) << (b & 0xFFFF);
|
||||||
ss << std::setw(4) << (c >> 16 );
|
ss << std::setw(4) << (c >> 16);
|
||||||
ss << std::setw(4) << (c & 0xFFFF);
|
ss << std::setw(4) << (c & 0xFFFF);
|
||||||
ss << std::setw(8) << d;
|
ss << std::setw(8) << d;
|
||||||
|
|
||||||
@ -72,4 +72,4 @@ namespace ix
|
|||||||
Uuid id;
|
Uuid id;
|
||||||
return id.toString();
|
return id.toString();
|
||||||
}
|
}
|
||||||
}
|
} // namespace ix
|
||||||
|
@ -7,12 +7,12 @@
|
|||||||
#include "IXSentryClient.h"
|
#include "IXSentryClient.h"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <iostream>
|
||||||
|
#include <ixcore/utils/IXCoreLogger.h>
|
||||||
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
|
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
|
||||||
#include <ixwebsocket/IXWebSocketVersion.h>
|
#include <ixwebsocket/IXWebSocketVersion.h>
|
||||||
#include <ixcore/utils/IXCoreLogger.h>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
@ -64,7 +64,8 @@ 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/1.0.0";
|
securityHeader += ",sentry_client=ws/";
|
||||||
|
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;
|
||||||
@ -233,7 +234,7 @@ namespace ix
|
|||||||
args->transferTimeout = 5 * 60;
|
args->transferTimeout = 5 * 60;
|
||||||
args->followRedirects = true;
|
args->followRedirects = true;
|
||||||
args->verbose = verbose;
|
args->verbose = verbose;
|
||||||
args->logger = [](const std::string& msg) { ix::IXCoreLogger::Log(msg.c_str()); };
|
args->logger = [](const std::string& msg) { CoreLogger::log(msg.c_str()); };
|
||||||
|
|
||||||
std::string body = computePayload(msg);
|
std::string body = computePayload(msg);
|
||||||
HttpResponsePtr response = _httpClient->post(_url, body, args);
|
HttpResponsePtr response = _httpClient->post(_url, body, args);
|
||||||
@ -245,24 +246,21 @@ namespace ix
|
|||||||
std::string SentryClient::computeUrl(const std::string& project, const std::string& key)
|
std::string SentryClient::computeUrl(const std::string& project, const std::string& key)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "https://sentry.io/api/"
|
ss << "https://sentry.io/api/" << project << "/minidump?sentry_key=" << key;
|
||||||
<< project
|
|
||||||
<< "/minidump?sentry_key="
|
|
||||||
<< key;
|
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// curl -v -X POST -F upload_file_minidump=@ws/crash.dmp 'https://sentry.io/api/123456/minidump?sentry_key=12344567890'
|
// curl -v -X POST -F upload_file_minidump=@ws/crash.dmp
|
||||||
|
// 'https://sentry.io/api/123456/minidump?sentry_key=12344567890'
|
||||||
//
|
//
|
||||||
void SentryClient::uploadMinidump(
|
void SentryClient::uploadMinidump(const std::string& sentryMetadata,
|
||||||
const std::string& sentryMetadata,
|
const std::string& minidumpBytes,
|
||||||
const std::string& minidumpBytes,
|
const std::string& project,
|
||||||
const std::string& project,
|
const std::string& key,
|
||||||
const std::string& key,
|
bool verbose,
|
||||||
bool verbose,
|
const OnResponseCallback& onResponseCallback)
|
||||||
const OnResponseCallback& onResponseCallback)
|
|
||||||
{
|
{
|
||||||
std::string multipartBoundary = _httpClient->generateMultipartBoundary();
|
std::string multipartBoundary = _httpClient->generateMultipartBoundary();
|
||||||
|
|
||||||
@ -273,7 +271,7 @@ namespace ix
|
|||||||
args->followRedirects = true;
|
args->followRedirects = true;
|
||||||
args->verbose = verbose;
|
args->verbose = verbose;
|
||||||
args->multipartBoundary = multipartBoundary;
|
args->multipartBoundary = multipartBoundary;
|
||||||
args->logger = [](const std::string& msg) { ix::IXCoreLogger::Log(msg.c_str()); };
|
args->logger = [](const std::string& msg) { CoreLogger::log(msg.c_str()); };
|
||||||
|
|
||||||
HttpFormDataParameters httpFormDataParameters;
|
HttpFormDataParameters httpFormDataParameters;
|
||||||
httpFormDataParameters["upload_file_minidump"] = minidumpBytes;
|
httpFormDataParameters["upload_file_minidump"] = minidumpBytes;
|
||||||
@ -282,7 +280,27 @@ namespace ix
|
|||||||
httpParameters["sentry"] = sentryMetadata;
|
httpParameters["sentry"] = sentryMetadata;
|
||||||
|
|
||||||
args->url = computeUrl(project, key);
|
args->url = computeUrl(project, key);
|
||||||
args->body = _httpClient->serializeHttpFormDataParameters(multipartBoundary, httpFormDataParameters, httpParameters);
|
args->body = _httpClient->serializeHttpFormDataParameters(
|
||||||
|
multipartBoundary, httpFormDataParameters, httpParameters);
|
||||||
|
|
||||||
|
_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) { CoreLogger::log(msg.c_str()); };
|
||||||
|
|
||||||
|
args->url = _url;
|
||||||
|
args->body = _jsonWriter.write(payload);
|
||||||
|
|
||||||
_httpClient->performRequest(args, onResponseCallback);
|
_httpClient->performRequest(args, onResponseCallback);
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,8 @@
|
|||||||
#include <ixwebsocket/IXHttpClient.h>
|
#include <ixwebsocket/IXHttpClient.h>
|
||||||
#include <ixwebsocket/IXSocketTLSOptions.h>
|
#include <ixwebsocket/IXSocketTLSOptions.h>
|
||||||
#include <json/json.h>
|
#include <json/json.h>
|
||||||
#include <regex>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
@ -28,13 +28,16 @@ namespace ix
|
|||||||
// Mostly for testing
|
// Mostly for testing
|
||||||
void setTLSOptions(const SocketTLSOptions& tlsOptions);
|
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,
|
const std::string& project,
|
||||||
const std::string& project,
|
const std::string& key,
|
||||||
const std::string& key,
|
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();
|
||||||
|
@ -6,10 +6,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <ixwebsocket/IXSocketTLSOptions.h>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ixwebsocket/IXSocketTLSOptions.h>
|
|
||||||
|
|
||||||
namespace snake
|
namespace snake
|
||||||
{
|
{
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
#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>
|
||||||
@ -29,10 +28,7 @@ namespace ix
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
CancellationRequest cancellationRequest = []() -> bool
|
CancellationRequest cancellationRequest = []() -> bool { return false; };
|
||||||
{
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::string errMsg;
|
std::string errMsg;
|
||||||
return _socket->connect(hostname, port, errMsg, cancellationRequest);
|
return _socket->connect(hostname, port, errMsg, cancellationRequest);
|
||||||
@ -253,9 +249,8 @@ namespace ix
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string RedisClient::prepareXaddCommand(
|
std::string RedisClient::prepareXaddCommand(const std::string& stream,
|
||||||
const std::string& stream,
|
const std::string& message)
|
||||||
const std::string& message)
|
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "*5\r\n";
|
ss << "*5\r\n";
|
||||||
@ -329,7 +324,9 @@ namespace ix
|
|||||||
return streamId;
|
return streamId;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RedisClient::sendCommand(const std::string& commands, int commandsCount, std::string& errMsg)
|
bool RedisClient::sendCommand(const std::string& commands,
|
||||||
|
int commandsCount,
|
||||||
|
std::string& errMsg)
|
||||||
{
|
{
|
||||||
bool sent = _socket->writeBytes(commands, nullptr);
|
bool sent = _socket->writeBytes(commands, nullptr);
|
||||||
if (!sent)
|
if (!sent)
|
||||||
|
@ -8,12 +8,12 @@
|
|||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <ixwebsocket/IXSocket.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class Socket;
|
|
||||||
|
|
||||||
class RedisClient
|
class RedisClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -38,14 +38,11 @@ namespace ix
|
|||||||
const OnRedisSubscribeCallback& callback);
|
const OnRedisSubscribeCallback& callback);
|
||||||
|
|
||||||
// XADD
|
// XADD
|
||||||
std::string xadd(
|
std::string xadd(const std::string& channel,
|
||||||
const std::string& channel,
|
const std::string& message,
|
||||||
const std::string& message,
|
std::string& errMsg);
|
||||||
std::string& errMsg);
|
|
||||||
|
|
||||||
std::string prepareXaddCommand(
|
std::string prepareXaddCommand(const std::string& stream, const std::string& message);
|
||||||
const std::string& stream,
|
|
||||||
const std::string& message);
|
|
||||||
|
|
||||||
std::string readXaddReply(std::string& errMsg);
|
std::string readXaddReply(std::string& errMsg);
|
||||||
|
|
||||||
@ -56,7 +53,7 @@ namespace ix
|
|||||||
private:
|
private:
|
||||||
std::string writeString(const std::string& str);
|
std::string writeString(const std::string& str);
|
||||||
|
|
||||||
std::shared_ptr<Socket> _socket;
|
std::unique_ptr<Socket> _socket;
|
||||||
std::atomic<bool> _stop;
|
std::atomic<bool> _stop;
|
||||||
};
|
};
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -6,18 +6,18 @@
|
|||||||
|
|
||||||
#include "IXRedisServer.h"
|
#include "IXRedisServer.h"
|
||||||
|
|
||||||
#include <ixwebsocket/IXNetSystem.h>
|
|
||||||
#include <ixwebsocket/IXSocketConnect.h>
|
|
||||||
#include <ixwebsocket/IXSocket.h>
|
|
||||||
#include <ixwebsocket/IXCancellationRequest.h>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <ixwebsocket/IXCancellationRequest.h>
|
||||||
|
#include <ixwebsocket/IXNetSystem.h>
|
||||||
|
#include <ixwebsocket/IXSocket.h>
|
||||||
|
#include <ixwebsocket/IXSocketConnect.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
RedisServer::RedisServer(int port, const std::string& host, int backlog, size_t maxConnections, int addressFamily)
|
RedisServer::RedisServer(
|
||||||
|
int port, const std::string& host, int backlog, size_t maxConnections, int addressFamily)
|
||||||
: SocketServer(port, host, backlog, maxConnections, addressFamily)
|
: SocketServer(port, host, backlog, maxConnections, addressFamily)
|
||||||
, _connectedClientsCount(0)
|
, _connectedClientsCount(0)
|
||||||
, _stopHandlingConnections(false)
|
, _stopHandlingConnections(false)
|
||||||
@ -44,7 +44,7 @@ namespace ix
|
|||||||
SocketServer::stop();
|
SocketServer::stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RedisServer::handleConnection(std::shared_ptr<Socket> socket,
|
void RedisServer::handleConnection(std::unique_ptr<Socket> socket,
|
||||||
std::shared_ptr<ConnectionState> connectionState)
|
std::shared_ptr<ConnectionState> connectionState)
|
||||||
{
|
{
|
||||||
_connectedClientsCount++;
|
_connectedClientsCount++;
|
||||||
@ -103,20 +103,19 @@ namespace ix
|
|||||||
_connectedClientsCount--;
|
_connectedClientsCount--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RedisServer::cleanupSubscribers(std::shared_ptr<Socket> socket)
|
void RedisServer::cleanupSubscribers(std::unique_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);
|
it.second.erase(socket.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto it : _subscribers)
|
for (auto it : _subscribers)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "Subscription id: " << it.first
|
ss << "Subscription id: " << it.first << " #subscribers: " << it.second.size();
|
||||||
<< " #subscribers: " << it.second.size();
|
|
||||||
|
|
||||||
logInfo(ss.str());
|
logInfo(ss.str());
|
||||||
}
|
}
|
||||||
@ -127,8 +126,7 @@ namespace ix
|
|||||||
return _connectedClientsCount;
|
return _connectedClientsCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RedisServer::startsWith(const std::string& str,
|
bool RedisServer::startsWith(const std::string& str, const std::string& start)
|
||||||
const std::string& start)
|
|
||||||
{
|
{
|
||||||
return str.compare(0, start.length(), start) == 0;
|
return str.compare(0, start.length(), start) == 0;
|
||||||
}
|
}
|
||||||
@ -145,9 +143,8 @@ namespace ix
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
auto cb = makeCancellationRequestWithTimeout(30, _stopHandlingConnections);
|
auto cb = makeCancellationRequestWithTimeout(30, _stopHandlingConnections);
|
||||||
@ -188,18 +185,11 @@ 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;
|
||||||
|
|
||||||
@ -214,31 +204,30 @@ namespace ix
|
|||||||
//
|
//
|
||||||
ss << "*6\r\n";
|
ss << "*6\r\n";
|
||||||
ss << writeString("publish"); // 1
|
ss << writeString("publish"); // 1
|
||||||
ss << ":3\r\n"; // 2
|
ss << ":3\r\n"; // 2
|
||||||
ss << "*0\r\n"; // 3
|
ss << "*0\r\n"; // 3
|
||||||
ss << ":1\r\n"; // 4
|
ss << ":1\r\n"; // 4
|
||||||
ss << ":2\r\n"; // 5
|
ss << ":2\r\n"; // 5
|
||||||
ss << ":1\r\n"; // 6
|
ss << ":1\r\n"; // 6
|
||||||
|
|
||||||
//
|
//
|
||||||
// subscribe
|
// subscribe
|
||||||
//
|
//
|
||||||
ss << "*6\r\n";
|
ss << "*6\r\n";
|
||||||
ss << writeString("subscribe"); // 1
|
ss << writeString("subscribe"); // 1
|
||||||
ss << ":2\r\n"; // 2
|
ss << ":2\r\n"; // 2
|
||||||
ss << "*0\r\n"; // 3
|
ss << "*0\r\n"; // 3
|
||||||
ss << ":1\r\n"; // 4
|
ss << ":1\r\n"; // 4
|
||||||
ss << ":1\r\n"; // 5
|
ss << ":1\r\n"; // 5
|
||||||
ss << ":1\r\n"; // 6
|
ss << ":1\r\n"; // 6
|
||||||
|
|
||||||
socket->writeBytes(ss.str(), cb);
|
socket->writeBytes(ss.str(), cb);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
@ -252,14 +241,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);
|
_subscribers[channel].insert(socket.get());
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
@ -288,9 +276,7 @@ namespace ix
|
|||||||
|
|
||||||
// return the number of clients that received the message.
|
// return the number of clients that received the message.
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << ":"
|
ss << ":" << std::to_string(subscribers.size()) << "\r\n";
|
||||||
<< std::to_string(subscribers.size())
|
|
||||||
<< "\r\n";
|
|
||||||
socket->writeBytes(ss.str(), cb);
|
socket->writeBytes(ss.str(), cb);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -6,13 +6,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IXSocketServer.h"
|
|
||||||
#include "IXSocket.h"
|
#include "IXSocket.h"
|
||||||
|
#include "IXSocketServer.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <map>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <utility> // pair
|
#include <utility> // pair
|
||||||
@ -37,32 +37,28 @@ 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<std::shared_ptr<Socket>>> _subscribers;
|
std::map<std::string, std::set<Socket*>> _subscribers;
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
|
|
||||||
std::atomic<bool> _stopHandlingConnections;
|
std::atomic<bool> _stopHandlingConnections;
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
virtual void handleConnection(std::shared_ptr<Socket>,
|
virtual void handleConnection(std::unique_ptr<Socket>,
|
||||||
std::shared_ptr<ConnectionState> connectionState) final;
|
std::shared_ptr<ConnectionState> connectionState) final;
|
||||||
virtual size_t getConnectedClientsCount() final;
|
virtual size_t getConnectedClientsCount() final;
|
||||||
|
|
||||||
bool startsWith(const std::string& str, const std::string& start);
|
bool startsWith(const std::string& str, const std::string& start);
|
||||||
std::string writeString(const std::string& str);
|
std::string writeString(const std::string& str);
|
||||||
|
|
||||||
bool parseRequest(
|
bool parseRequest(std::unique_ptr<Socket>& socket, std::vector<std::string>& tokens);
|
||||||
std::shared_ptr<Socket> socket,
|
|
||||||
std::vector<std::string>& tokens);
|
|
||||||
|
|
||||||
bool handlePublish(std::shared_ptr<Socket> socket,
|
bool handlePublish(std::unique_ptr<Socket>& socket, const std::vector<std::string>& tokens);
|
||||||
const std::vector<std::string>& tokens);
|
|
||||||
|
|
||||||
bool handleSubscribe(std::shared_ptr<Socket> socket,
|
bool handleSubscribe(std::unique_ptr<Socket>& socket,
|
||||||
const std::vector<std::string>& tokens);
|
const std::vector<std::string>& tokens);
|
||||||
|
|
||||||
bool handleCommand(std::shared_ptr<Socket> socket,
|
bool handleCommand(std::unique_ptr<Socket>& socket, const std::vector<std::string>& tokens);
|
||||||
const std::vector<std::string>& tokens);
|
|
||||||
|
|
||||||
void cleanupSubscribers(std::shared_ptr<Socket> socket);
|
void cleanupSubscribers(std::unique_ptr<Socket>& socket);
|
||||||
};
|
};
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -10,9 +10,9 @@
|
|||||||
#include "IXSnakeConnectionState.h"
|
#include "IXSnakeConnectionState.h"
|
||||||
#include "nlohmann/json.hpp"
|
#include "nlohmann/json.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <ixcore/utils/IXCoreLogger.h>
|
||||||
#include <ixcrypto/IXHMac.h>
|
#include <ixcrypto/IXHMac.h>
|
||||||
#include <ixwebsocket/IXWebSocket.h>
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
#include <ixcore/utils/IXCoreLogger.h>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
namespace snake
|
namespace snake
|
||||||
@ -189,7 +189,8 @@ 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}, {"position", "0-0"}, {"messages", {msg}}}}};
|
||||||
|
|
||||||
ws->sendText(response.dump());
|
ws->sendText(response.dump());
|
||||||
};
|
};
|
||||||
@ -197,7 +198,7 @@ namespace snake
|
|||||||
auto responseCallback = [ws, pdu, &subscriptionId](const std::string& redisResponse) {
|
auto responseCallback = [ws, pdu, &subscriptionId](const std::string& redisResponse) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "Redis Response: " << redisResponse << "...";
|
ss << "Redis Response: " << redisResponse << "...";
|
||||||
ix::IXCoreLogger::Log(ss.str().c_str());
|
ix::CoreLogger::log(ss.str().c_str());
|
||||||
|
|
||||||
// Success
|
// Success
|
||||||
nlohmann::json response = {{"action", "rtm/subscribe/ok"},
|
nlohmann::json response = {{"action", "rtm/subscribe/ok"},
|
||||||
@ -209,7 +210,7 @@ namespace snake
|
|||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "Subscribing to " << appChannel << "...";
|
ss << "Subscribing to " << appChannel << "...";
|
||||||
ix::IXCoreLogger::Log(ss.str().c_str());
|
ix::CoreLogger::log(ss.str().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!redisClient.subscribe(appChannel, responseCallback, callback))
|
if (!redisClient.subscribe(appChannel, responseCallback, callback))
|
||||||
@ -251,7 +252,21 @@ namespace snake
|
|||||||
const AppConfig& appConfig,
|
const AppConfig& appConfig,
|
||||||
const std::string& str)
|
const std::string& str)
|
||||||
{
|
{
|
||||||
auto pdu = nlohmann::json::parse(str);
|
nlohmann::json pdu;
|
||||||
|
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")
|
||||||
|
@ -10,8 +10,8 @@
|
|||||||
#include "IXSnakeConnectionState.h"
|
#include "IXSnakeConnectionState.h"
|
||||||
#include "IXSnakeProtocol.h"
|
#include "IXSnakeProtocol.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
|
||||||
#include <ixcore/utils/IXCoreLogger.h>
|
#include <ixcore/utils/IXCoreLogger.h>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
namespace snake
|
namespace snake
|
||||||
@ -29,7 +29,7 @@ namespace snake
|
|||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "Listening on " << appConfig.hostname << ":" << appConfig.port;
|
ss << "Listening on " << appConfig.hostname << ":" << appConfig.port;
|
||||||
ix::IXCoreLogger::Log(ss.str().c_str());
|
ix::CoreLogger::log(ss.str().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -67,6 +67,7 @@ namespace snake
|
|||||||
webSocket->setOnMessageCallback(
|
webSocket->setOnMessageCallback(
|
||||||
[this, webSocket, state](const ix::WebSocketMessagePtr& msg) {
|
[this, webSocket, state](const ix::WebSocketMessagePtr& msg) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
ix::LogLevel logLevel = ix::LogLevel::Debug;
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (msg->type == ix::WebSocketMessageType::Open)
|
||||||
{
|
{
|
||||||
ss << "New connection" << std::endl;
|
ss << "New connection" << std::endl;
|
||||||
@ -86,6 +87,7 @@ namespace snake
|
|||||||
_appConfig.redisPort))
|
_appConfig.redisPort))
|
||||||
{
|
{
|
||||||
ss << "Cannot connect to redis host" << std::endl;
|
ss << "Cannot connect to redis host" << std::endl;
|
||||||
|
logLevel = ix::LogLevel::Error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Close)
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
||||||
@ -101,6 +103,7 @@ namespace snake
|
|||||||
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
||||||
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
||||||
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
||||||
|
logLevel = ix::LogLevel::Error;
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
||||||
{
|
{
|
||||||
@ -112,7 +115,7 @@ namespace snake
|
|||||||
processCobraMessage(state, webSocket, _appConfig, msg->str);
|
processCobraMessage(state, webSocket, _appConfig, msg->str);
|
||||||
}
|
}
|
||||||
|
|
||||||
ix::IXCoreLogger::Log(ss.str().c_str());
|
ix::CoreLogger::log(ss.str().c_str(), logLevel);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
44
ixwebsocket/IXBench.cpp
Normal file
44
ixwebsocket/IXBench.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
28
ixwebsocket/IXBench.h
Normal file
28
ixwebsocket/IXBench.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
@ -92,7 +92,8 @@ 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::shared_ptr<Socket> socket)
|
std::tuple<bool, std::string, HttpRequestPtr> Http::parseRequest(
|
||||||
|
std::unique_ptr<Socket>& socket)
|
||||||
{
|
{
|
||||||
HttpRequestPtr httpRequest;
|
HttpRequestPtr httpRequest;
|
||||||
|
|
||||||
@ -133,7 +134,7 @@ namespace ix
|
|||||||
return std::make_tuple(true, "", httpRequest);
|
return std::make_tuple(true, "", httpRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Http::sendResponse(HttpResponsePtr response, std::shared_ptr<Socket> socket)
|
bool Http::sendResponse(HttpResponsePtr response, std::unique_ptr<Socket>& socket)
|
||||||
{
|
{
|
||||||
// Write the response to the socket
|
// Write the response to the socket
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
@ -78,12 +78,12 @@ namespace ix
|
|||||||
WebSocketHttpHeaders extraHeaders;
|
WebSocketHttpHeaders extraHeaders;
|
||||||
std::string body;
|
std::string body;
|
||||||
std::string multipartBoundary;
|
std::string multipartBoundary;
|
||||||
int connectTimeout;
|
int connectTimeout = 60;
|
||||||
int transferTimeout;
|
int transferTimeout = 1800;
|
||||||
bool followRedirects;
|
bool followRedirects = true;
|
||||||
int maxRedirects;
|
int maxRedirects = 5;
|
||||||
bool verbose;
|
bool verbose = false;
|
||||||
bool compress;
|
bool compress = true;
|
||||||
Logger logger;
|
Logger logger;
|
||||||
OnProgressCallback onProgressCallback;
|
OnProgressCallback onProgressCallback;
|
||||||
};
|
};
|
||||||
@ -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::shared_ptr<Socket> socket);
|
std::unique_ptr<Socket>& socket);
|
||||||
static bool sendResponse(HttpResponsePtr response, std::shared_ptr<Socket> socket);
|
static bool sendResponse(HttpResponsePtr response, std::unique_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(
|
||||||
|
@ -95,7 +95,7 @@ namespace ix
|
|||||||
std::atomic<bool> _stop;
|
std::atomic<bool> _stop;
|
||||||
std::thread _thread;
|
std::thread _thread;
|
||||||
|
|
||||||
std::shared_ptr<Socket> _socket;
|
std::unique_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;
|
||||||
|
@ -69,7 +69,7 @@ namespace ix
|
|||||||
_onConnectionCallback = callback;
|
_onConnectionCallback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpServer::handleConnection(std::shared_ptr<Socket> socket,
|
void HttpServer::handleConnection(std::unique_ptr<Socket> socket,
|
||||||
std::shared_ptr<ConnectionState> connectionState)
|
std::shared_ptr<ConnectionState> connectionState)
|
||||||
{
|
{
|
||||||
_connectedClientsCount++;
|
_connectedClientsCount++;
|
||||||
|
@ -43,7 +43,7 @@ namespace ix
|
|||||||
std::atomic<int> _connectedClientsCount;
|
std::atomic<int> _connectedClientsCount;
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
virtual void handleConnection(std::shared_ptr<Socket>,
|
virtual void handleConnection(std::unique_ptr<Socket>,
|
||||||
std::shared_ptr<ConnectionState> connectionState) final;
|
std::shared_ptr<ConnectionState> connectionState) final;
|
||||||
virtual size_t getConnectedClientsCount() final;
|
virtual size_t getConnectedClientsCount() final;
|
||||||
|
|
||||||
|
@ -1,115 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSelectInterruptEventFd.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//
|
|
||||||
// On Linux we use eventd to wake up select.
|
|
||||||
//
|
|
||||||
|
|
||||||
//
|
|
||||||
// Linux/Android has a special type of virtual files. select(2) will react
|
|
||||||
// when reading/writing to those files, unlike closing sockets.
|
|
||||||
//
|
|
||||||
// https://linux.die.net/man/2/eventfd
|
|
||||||
// http://www.sourcexr.com/articles/2013/10/26/lightweight-inter-process-signaling-with-eventfd
|
|
||||||
//
|
|
||||||
// eventfd was added in Linux kernel 2.x, and our oldest Android (Kitkat 4.4)
|
|
||||||
// is on Kernel 3.x
|
|
||||||
//
|
|
||||||
// cf Android/Kernel table here
|
|
||||||
// https://android.stackexchange.com/questions/51651/which-android-runs-which-linux-kernel
|
|
||||||
//
|
|
||||||
// On macOS we use UNIX pipes to wake up select.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "IXSelectInterruptEventFd.h"
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string.h> // for strerror
|
|
||||||
#include <sys/eventfd.h>
|
|
||||||
#include <unistd.h> // for write
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
SelectInterruptEventFd::SelectInterruptEventFd()
|
|
||||||
{
|
|
||||||
_eventfd = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
SelectInterruptEventFd::~SelectInterruptEventFd()
|
|
||||||
{
|
|
||||||
::close(_eventfd);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SelectInterruptEventFd::init(std::string& errorMsg)
|
|
||||||
{
|
|
||||||
// calling init twice is a programming error
|
|
||||||
assert(_eventfd == -1);
|
|
||||||
|
|
||||||
_eventfd = eventfd(0, 0);
|
|
||||||
if (_eventfd < 0)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "SelectInterruptEventFd::init() failed in eventfd()"
|
|
||||||
<< " : " << strerror(errno);
|
|
||||||
errorMsg = ss.str();
|
|
||||||
|
|
||||||
_eventfd = -1;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fcntl(_eventfd, F_SETFL, O_NONBLOCK) == -1)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "SelectInterruptEventFd::init() failed in fcntl() call"
|
|
||||||
<< " : " << strerror(errno);
|
|
||||||
errorMsg = ss.str();
|
|
||||||
|
|
||||||
_eventfd = -1;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SelectInterruptEventFd::notify(uint64_t value)
|
|
||||||
{
|
|
||||||
int fd = _eventfd;
|
|
||||||
|
|
||||||
if (fd == -1) return false;
|
|
||||||
|
|
||||||
// we should write 8 bytes for an uint64_t
|
|
||||||
return write(fd, &value, sizeof(value)) == 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: return max uint64_t for errors ?
|
|
||||||
uint64_t SelectInterruptEventFd::read()
|
|
||||||
{
|
|
||||||
int fd = _eventfd;
|
|
||||||
|
|
||||||
uint64_t value = 0;
|
|
||||||
::read(fd, &value, sizeof(value));
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SelectInterruptEventFd::clear()
|
|
||||||
{
|
|
||||||
if (_eventfd == -1) return false;
|
|
||||||
|
|
||||||
// 0 is a special value ; select will not wake up
|
|
||||||
uint64_t value = 0;
|
|
||||||
|
|
||||||
// we should write 8 bytes for an uint64_t
|
|
||||||
return write(_eventfd, &value, sizeof(value)) == 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SelectInterruptEventFd::getFd() const
|
|
||||||
{
|
|
||||||
return _eventfd;
|
|
||||||
}
|
|
||||||
} // namespace ix
|
|
@ -1,31 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSelectInterruptEventFd.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "IXSelectInterrupt.h"
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
class SelectInterruptEventFd final : public SelectInterrupt
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
SelectInterruptEventFd();
|
|
||||||
virtual ~SelectInterruptEventFd();
|
|
||||||
|
|
||||||
bool init(std::string& errorMsg) final;
|
|
||||||
|
|
||||||
bool notify(uint64_t value) final;
|
|
||||||
bool clear() final;
|
|
||||||
uint64_t read() final;
|
|
||||||
int getFd() const final;
|
|
||||||
|
|
||||||
private:
|
|
||||||
int _eventfd;
|
|
||||||
};
|
|
||||||
} // namespace ix
|
|
@ -14,12 +14,12 @@
|
|||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
std::shared_ptr<SelectInterrupt> createSelectInterrupt()
|
SelectInterruptPtr createSelectInterrupt()
|
||||||
{
|
{
|
||||||
#if defined(__linux__) || defined(__APPLE__)
|
#if defined(__linux__) || defined(__APPLE__)
|
||||||
return std::make_shared<SelectInterruptPipe>();
|
return std::make_unique<SelectInterruptPipe>();
|
||||||
#else
|
#else
|
||||||
return std::make_shared<SelectInterrupt>();
|
return std::make_unique<SelectInterrupt>();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -11,5 +11,6 @@
|
|||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class SelectInterrupt;
|
class SelectInterrupt;
|
||||||
std::shared_ptr<SelectInterrupt> createSelectInterrupt();
|
using SelectInterruptPtr = std::unique_ptr<SelectInterrupt>;
|
||||||
|
SelectInterruptPtr createSelectInterrupt();
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -46,7 +46,7 @@ namespace ix
|
|||||||
PollResultType Socket::poll(bool readyToRead,
|
PollResultType Socket::poll(bool readyToRead,
|
||||||
int timeoutMs,
|
int timeoutMs,
|
||||||
int sockfd,
|
int sockfd,
|
||||||
std::shared_ptr<SelectInterrupt> selectInterrupt)
|
const SelectInterruptPtr& 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,7 +376,8 @@ namespace ix
|
|||||||
{
|
{
|
||||||
if (isCancellationRequested && isCancellationRequested())
|
if (isCancellationRequested && isCancellationRequested())
|
||||||
{
|
{
|
||||||
return std::make_pair(false, std::string());
|
const std::string errorMsg("Cancellation Requested");
|
||||||
|
return std::make_pair(false, errorMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size = std::min(kChunkSize, length - output.size());
|
size_t size = std::min(kChunkSize, length - output.size());
|
||||||
@ -388,7 +389,8 @@ namespace ix
|
|||||||
}
|
}
|
||||||
else if (ret <= 0 && !Socket::isWaitNeeded())
|
else if (ret <= 0 && !Socket::isWaitNeeded())
|
||||||
{
|
{
|
||||||
return std::make_pair(false, std::string());
|
const std::string errorMsg("Recv Error");
|
||||||
|
return std::make_pair(false, errorMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (onProgressCallback) onProgressCallback((int) output.size(), (int) length);
|
if (onProgressCallback) onProgressCallback((int) output.size(), (int) length);
|
||||||
@ -397,7 +399,8 @@ 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)
|
||||||
{
|
{
|
||||||
return std::make_pair(false, std::string());
|
const std::string errorMsg("Poll Error");
|
||||||
|
return std::make_pair(false, errorMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@ 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
|
||||||
{
|
{
|
||||||
@ -66,7 +67,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& url,
|
virtual bool connect(const std::string& host,
|
||||||
int port,
|
int port,
|
||||||
std::string& errMsg,
|
std::string& errMsg,
|
||||||
const CancellationRequest& isCancellationRequested);
|
const CancellationRequest& isCancellationRequested);
|
||||||
@ -93,7 +94,7 @@ namespace ix
|
|||||||
static PollResultType poll(bool readyToRead,
|
static PollResultType poll(bool readyToRead,
|
||||||
int timeoutMs,
|
int timeoutMs,
|
||||||
int sockfd,
|
int sockfd,
|
||||||
std::shared_ptr<SelectInterrupt> selectInterrupt = nullptr);
|
const SelectInterruptPtr& selectInterrupt);
|
||||||
|
|
||||||
|
|
||||||
// Used as special codes for pipe communication
|
// Used as special codes for pipe communication
|
||||||
@ -112,6 +113,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;
|
||||||
|
|
||||||
std::shared_ptr<SelectInterrupt> _selectInterrupt;
|
SelectInterruptPtr _selectInterrupt;
|
||||||
};
|
};
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
/*
|
/*
|
||||||
* IXSocketAppleSSL.cpp
|
* IXSocketAppleSSL.cpp
|
||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2017-2020 Machine Zone, Inc. All rights reserved.
|
||||||
*
|
*
|
||||||
* Adapted from Satori SDK Apple SSL code.
|
* Adapted from Satori SDK Apple SSL code.
|
||||||
*/
|
*/
|
||||||
|
#ifdef IXWEBSOCKET_USE_SECURE_TRANSPORT
|
||||||
|
|
||||||
#include "IXSocketAppleSSL.h"
|
#include "IXSocketAppleSSL.h"
|
||||||
|
|
||||||
#include "IXSocketConnect.h"
|
#include "IXSocketConnect.h"
|
||||||
@ -164,6 +166,26 @@ 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,
|
||||||
@ -190,26 +212,17 @@ namespace ix
|
|||||||
Boolean option(1);
|
Boolean option(1);
|
||||||
SSLSetSessionOption(_sslContext, kSSLSessionOptionBreakOnServerAuth, option);
|
SSLSetSessionOption(_sslContext, kSSLSessionOptionBreakOnServerAuth, option);
|
||||||
|
|
||||||
do
|
status = tlsHandShake(errMsg, isCancellationRequested);
|
||||||
{
|
|
||||||
status = SSLHandshake(_sslContext);
|
|
||||||
} while (status == errSSLWouldBlock || status == errSSLServerAuthCompleted);
|
|
||||||
|
|
||||||
if (status == errSSLServerAuthCompleted)
|
if (status == errSSLServerAuthCompleted)
|
||||||
{
|
{
|
||||||
// proceed with the handshake
|
// proceed with the handshake
|
||||||
do
|
status = tlsHandShake(errMsg, isCancellationRequested);
|
||||||
{
|
|
||||||
status = SSLHandshake(_sslContext);
|
|
||||||
} while (status == errSSLWouldBlock || status == errSSLServerAuthCompleted);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
do
|
status = tlsHandShake(errMsg, isCancellationRequested);
|
||||||
{
|
|
||||||
status = SSLHandshake(_sslContext);
|
|
||||||
} while (status == errSSLWouldBlock || status == errSSLServerAuthCompleted);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,3 +309,5 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
|
||||||
|
#endif // IXWEBSOCKET_USE_SECURE_TRANSPORT
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
/*
|
/*
|
||||||
* IXSocketAppleSSL.h
|
* IXSocketAppleSSL.h
|
||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2017-2020 Machine Zone, Inc. All rights reserved.
|
||||||
*/
|
*/
|
||||||
|
#ifdef IXWEBSOCKET_USE_SECURE_TRANSPORT
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
@ -37,6 +38,9 @@ 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
|
||||||
|
|
||||||
@ -44,3 +48,5 @@ namespace ix
|
|||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
|
||||||
|
#endif // IXWEBSOCKET_USE_SECURE_TRANSPORT
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#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>
|
||||||
@ -64,7 +65,8 @@ namespace ix
|
|||||||
|
|
||||||
int timeoutMs = 10;
|
int timeoutMs = 10;
|
||||||
bool readyToRead = false;
|
bool readyToRead = false;
|
||||||
PollResultType pollResult = Socket::poll(readyToRead, timeoutMs, fd);
|
auto selectInterrupt = std::make_unique<SelectInterrupt>();
|
||||||
|
PollResultType pollResult = Socket::poll(readyToRead, timeoutMs, fd, selectInterrupt);
|
||||||
|
|
||||||
if (pollResult == PollResultType::Timeout)
|
if (pollResult == PollResultType::Timeout)
|
||||||
{
|
{
|
||||||
|
@ -24,28 +24,28 @@
|
|||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
std::shared_ptr<Socket> createSocket(bool tls,
|
std::unique_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::shared_ptr<Socket> socket;
|
std::unique_ptr<Socket> socket;
|
||||||
|
|
||||||
if (!tls)
|
if (!tls)
|
||||||
{
|
{
|
||||||
socket = std::make_shared<Socket>(fd);
|
socket = std::make_unique<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_shared<SocketMbedTLS>(tlsOptions, fd);
|
socket = std::make_unique<SocketMbedTLS>(tlsOptions, fd);
|
||||||
#elif defined(IXWEBSOCKET_USE_OPEN_SSL)
|
#elif defined(IXWEBSOCKET_USE_OPEN_SSL)
|
||||||
socket = std::make_shared<SocketOpenSSL>(tlsOptions, fd);
|
socket = std::make_unique<SocketOpenSSL>(tlsOptions, fd);
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
socket = std::make_shared<SocketAppleSSL>(tlsOptions, fd);
|
socket = std::make_unique<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.";
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class Socket;
|
class Socket;
|
||||||
std::shared_ptr<Socket> createSocket(bool tls,
|
std::unique_ptr<Socket> createSocket(bool tls,
|
||||||
int fd,
|
int fd,
|
||||||
std::string& errorMsg,
|
std::string& errorMsg,
|
||||||
const SocketTLSOptions& tlsOptions);
|
const SocketTLSOptions& tlsOptions);
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
/*
|
/*
|
||||||
* IXSocketMbedTLS.cpp
|
* IXSocketMbedTLS.cpp
|
||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant, Max Weisel
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
|
||||||
*
|
*
|
||||||
* Some code taken from
|
* Some code taken from
|
||||||
* https://github.com/rottor12/WsClientLib/blob/master/lib/src/WsClientLib.cpp
|
* https://github.com/rottor12/WsClientLib/blob/master/lib/src/WsClientLib.cpp
|
||||||
* and mini_client.c example from mbedtls
|
* and mini_client.c example from mbedtls
|
||||||
*/
|
*/
|
||||||
|
#ifdef IXWEBSOCKET_USE_MBED_TLS
|
||||||
|
|
||||||
#include "IXSocketMbedTLS.h"
|
#include "IXSocketMbedTLS.h"
|
||||||
|
|
||||||
@ -103,10 +104,26 @@ namespace ix
|
|||||||
{
|
{
|
||||||
; // FIXME
|
; // FIXME
|
||||||
}
|
}
|
||||||
else if (mbedtls_x509_crt_parse_file(&_cacert, _tlsOptions.caFile.c_str()) < 0)
|
else
|
||||||
{
|
{
|
||||||
errMsg = "Cannot parse CA file '" + _tlsOptions.caFile + "'";
|
if (_tlsOptions.isUsingInMemoryCAs())
|
||||||
return false;
|
{
|
||||||
|
const char* buffer = _tlsOptions.caFile.c_str();
|
||||||
|
size_t bufferSize =
|
||||||
|
_tlsOptions.caFile.size() + 1; // Needs to include null terminating
|
||||||
|
// character otherwise mbedtls will fail.
|
||||||
|
if (mbedtls_x509_crt_parse(
|
||||||
|
&_cacert, (const unsigned char*) buffer, bufferSize) < 0)
|
||||||
|
{
|
||||||
|
errMsg = "Cannot parse CA from memory.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (mbedtls_x509_crt_parse_file(&_cacert, _tlsOptions.caFile.c_str()) < 0)
|
||||||
|
{
|
||||||
|
errMsg = "Cannot parse CA file '" + _tlsOptions.caFile + "'";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_ssl_conf_ca_chain(&_conf, &_cacert, NULL);
|
mbedtls_ssl_conf_ca_chain(&_conf, &_cacert, NULL);
|
||||||
@ -195,8 +212,17 @@ namespace ix
|
|||||||
int res;
|
int res;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
{
|
||||||
res = mbedtls_ssl_handshake(&_ssl);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
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)
|
||||||
@ -271,3 +297,5 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
|
||||||
|
#endif // IXWEBSOCKET_USE_MBED_TLS
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
/*
|
/*
|
||||||
* IXSocketMbedTLS.h
|
* IXSocketMbedTLS.h
|
||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2019-2020 Machine Zone, Inc. All rights reserved.
|
||||||
*/
|
*/
|
||||||
|
#ifdef IXWEBSOCKET_USE_MBED_TLS
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
@ -54,3 +55,5 @@ namespace ix
|
|||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
|
||||||
|
#endif // IXWEBSOCKET_USE_MBED_TLS
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
/*
|
/*
|
||||||
* IXSocketOpenSSL.cpp
|
* IXSocketOpenSSL.cpp
|
||||||
* Author: Benjamin Sergeant, Matt DeBoer
|
* Author: Benjamin Sergeant, Matt DeBoer, Max Weisel
|
||||||
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2017-2020 Machine Zone, Inc. All rights reserved.
|
||||||
*
|
*
|
||||||
* Adapted from Satori SDK OpenSSL code.
|
* Adapted from Satori SDK OpenSSL code.
|
||||||
*/
|
*/
|
||||||
|
#ifdef IXWEBSOCKET_USE_OPEN_SSL
|
||||||
|
|
||||||
#include "IXSocketOpenSSL.h"
|
#include "IXSocketOpenSSL.h"
|
||||||
|
|
||||||
@ -21,6 +22,57 @@
|
|||||||
#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 =
|
||||||
@ -143,6 +195,66 @@ namespace ix
|
|||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SocketOpenSSL::openSSLAddCARootsFromString(const std::string roots)
|
||||||
|
{
|
||||||
|
// Create certificate store
|
||||||
|
X509_STORE* certificate_store = SSL_CTX_get_cert_store(_ssl_context);
|
||||||
|
if (certificate_store == nullptr) return false;
|
||||||
|
|
||||||
|
// Configure to allow intermediate certs
|
||||||
|
X509_STORE_set_flags(certificate_store,
|
||||||
|
X509_V_FLAG_TRUSTED_FIRST | X509_V_FLAG_PARTIAL_CHAIN);
|
||||||
|
|
||||||
|
// Create a new buffer and populate it with the roots
|
||||||
|
BIO* buffer = BIO_new_mem_buf((void*) roots.c_str(), static_cast<int>(roots.length()));
|
||||||
|
if (buffer == nullptr) return false;
|
||||||
|
|
||||||
|
// Read each root in the buffer and add to the certificate store
|
||||||
|
bool success = true;
|
||||||
|
size_t number_of_roots = 0;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
// Read the next root in the buffer
|
||||||
|
X509* root = PEM_read_bio_X509_AUX(buffer, nullptr, nullptr, (void*) "");
|
||||||
|
if (root == nullptr)
|
||||||
|
{
|
||||||
|
// No more certs left in the buffer, we're done.
|
||||||
|
ERR_clear_error();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try adding the root to the certificate store
|
||||||
|
ERR_clear_error();
|
||||||
|
if (!X509_STORE_add_cert(certificate_store, root))
|
||||||
|
{
|
||||||
|
// Failed to add. If the error is unrelated to the x509 lib or the cert already
|
||||||
|
// exists, we're safe to continue.
|
||||||
|
unsigned long error = ERR_get_error();
|
||||||
|
if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
|
||||||
|
ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
|
||||||
|
{
|
||||||
|
// Failed. Clean up and bail.
|
||||||
|
success = false;
|
||||||
|
X509_free(root);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up and loop
|
||||||
|
X509_free(root);
|
||||||
|
number_of_roots++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up buffer
|
||||||
|
BIO_free(buffer);
|
||||||
|
|
||||||
|
// Make sure we loaded at least one certificate.
|
||||||
|
if (number_of_roots == 0) success = false;
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a hostname matches a pattern
|
* Check whether a hostname matches a pattern
|
||||||
*/
|
*/
|
||||||
@ -224,7 +336,9 @@ namespace ix
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SocketOpenSSL::openSSLClientHandshake(const std::string& host, std::string& errMsg)
|
bool SocketOpenSSL::openSSLClientHandshake(const std::string& host,
|
||||||
|
std::string& errMsg,
|
||||||
|
const CancellationRequest& isCancellationRequested)
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -233,6 +347,12 @@ 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)
|
||||||
@ -328,6 +448,12 @@ 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();
|
||||||
@ -335,21 +461,34 @@ 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
|
||||||
_ssl_context, _tlsOptions.caFile.c_str(), NULL) != 1)
|
|
||||||
{
|
{
|
||||||
auto sslErr = ERR_get_error();
|
if (_tlsOptions.isUsingInMemoryCAs())
|
||||||
errMsg = "OpenSSL failed - SSL_CTX_load_verify_locations(\"" + _tlsOptions.caFile +
|
{
|
||||||
"\") failed: ";
|
// Load from memory
|
||||||
errMsg += ERR_error_string(sslErr, nullptr);
|
openSSLAddCARootsFromString(_tlsOptions.caFile);
|
||||||
return false;
|
}
|
||||||
}
|
else
|
||||||
|
{
|
||||||
|
if (SSL_CTX_load_verify_locations(
|
||||||
|
_ssl_context, _tlsOptions.caFile.c_str(), NULL) != 1)
|
||||||
|
{
|
||||||
|
auto sslErr = ERR_get_error();
|
||||||
|
errMsg = "OpenSSL failed - SSL_CTX_load_verify_locations(\"" +
|
||||||
|
_tlsOptions.caFile + "\") failed: ";
|
||||||
|
errMsg += ERR_error_string(sslErr, nullptr);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
SSL_CTX_set_verify(_ssl_context,
|
SSL_CTX_set_verify(
|
||||||
SSL_VERIFY_PEER,
|
_ssl_context, SSL_VERIFY_PEER, [](int preverify, X509_STORE_CTX*) -> int {
|
||||||
[](int preverify, X509_STORE_CTX*) -> int { return preverify; });
|
return preverify;
|
||||||
SSL_CTX_set_verify_depth(_ssl_context, 4);
|
});
|
||||||
|
SSL_CTX_set_verify_depth(_ssl_context, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -459,26 +598,35 @@ namespace ix
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const char* root_ca_file = _tlsOptions.caFile.c_str();
|
if (_tlsOptions.isUsingInMemoryCAs())
|
||||||
STACK_OF(X509_NAME) * rootCAs;
|
|
||||||
rootCAs = SSL_load_client_CA_file(root_ca_file);
|
|
||||||
if (rootCAs == NULL)
|
|
||||||
{
|
{
|
||||||
auto sslErr = ERR_get_error();
|
// Load from memory
|
||||||
errMsg = "OpenSSL failed - SSL_load_client_CA_file('" + _tlsOptions.caFile +
|
openSSLAddCARootsFromString(_tlsOptions.caFile);
|
||||||
"') failed: ";
|
|
||||||
errMsg += ERR_error_string(sslErr, nullptr);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SSL_CTX_set_client_CA_list(_ssl_context, rootCAs);
|
const char* root_ca_file = _tlsOptions.caFile.c_str();
|
||||||
if (SSL_CTX_load_verify_locations(_ssl_context, root_ca_file, nullptr) != 1)
|
STACK_OF(X509_NAME) * rootCAs;
|
||||||
|
rootCAs = SSL_load_client_CA_file(root_ca_file);
|
||||||
|
if (rootCAs == NULL)
|
||||||
{
|
{
|
||||||
auto sslErr = ERR_get_error();
|
auto sslErr = ERR_get_error();
|
||||||
errMsg = "OpenSSL failed - SSL_CTX_load_verify_locations(\"" +
|
errMsg = "OpenSSL failed - SSL_load_client_CA_file('" +
|
||||||
_tlsOptions.caFile + "\") failed: ";
|
_tlsOptions.caFile + "') failed: ";
|
||||||
errMsg += ERR_error_string(sslErr, nullptr);
|
errMsg += ERR_error_string(sslErr, nullptr);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SSL_CTX_set_client_CA_list(_ssl_context, rootCAs);
|
||||||
|
if (SSL_CTX_load_verify_locations(
|
||||||
|
_ssl_context, root_ca_file, nullptr) != 1)
|
||||||
|
{
|
||||||
|
auto sslErr = ERR_get_error();
|
||||||
|
errMsg = "OpenSSL failed - SSL_CTX_load_verify_locations(\"" +
|
||||||
|
_tlsOptions.caFile + "\") failed: ";
|
||||||
|
errMsg += ERR_error_string(sslErr, nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -577,7 +725,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);
|
handshakeSuccessful = openSSLClientHandshake(host, errMsg, isCancellationRequested);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!handshakeSuccessful)
|
if (!handshakeSuccessful)
|
||||||
@ -665,3 +813,5 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
|
||||||
|
#endif // IXWEBSOCKET_USE_OPEN_SSL
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
/*
|
/*
|
||||||
* IXSocketOpenSSL.h
|
* IXSocketOpenSSL.h
|
||||||
* Author: Benjamin Sergeant, Matt DeBoer
|
* Author: Benjamin Sergeant, Matt DeBoer
|
||||||
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2017-2020 Machine Zone, Inc. All rights reserved.
|
||||||
*/
|
*/
|
||||||
|
#ifdef IXWEBSOCKET_USE_OPEN_SSL
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
@ -39,7 +40,10 @@ 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, std::string& errMsg);
|
bool openSSLAddCARootsFromString(const std::string roots);
|
||||||
|
bool openSSLClientHandshake(const std::string& hostname,
|
||||||
|
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);
|
||||||
@ -57,3 +61,5 @@ namespace ix
|
|||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
|
||||||
|
#endif // IXWEBSOCKET_USE_OPEN_SSL
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#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"
|
||||||
@ -257,7 +258,9 @@ 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;
|
||||||
PollResultType pollResult = Socket::poll(readyToRead, timeoutMs, _serverFd);
|
auto selectInterrupt = std::make_unique<SelectInterrupt>();
|
||||||
|
PollResultType pollResult =
|
||||||
|
Socket::poll(readyToRead, timeoutMs, _serverFd, selectInterrupt);
|
||||||
|
|
||||||
if (pollResult == PollResultType::Error)
|
if (pollResult == PollResultType::Error)
|
||||||
{
|
{
|
||||||
@ -338,7 +341,8 @@ 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(&SocketServer::handleConnection, this, socket, connectionState)));
|
std::thread(
|
||||||
|
&SocketServer::handleConnection, this, std::move(socket), connectionState)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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::shared_ptr<Socket>,
|
virtual void handleConnection(std::unique_ptr<Socket>,
|
||||||
std::shared_ptr<ConnectionState> connectionState) = 0;
|
std::shared_ptr<ConnectionState> connectionState) = 0;
|
||||||
virtual size_t getConnectedClientsCount() = 0;
|
virtual size_t getConnectedClientsCount() = 0;
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ namespace ix
|
|||||||
const char* kTLSCAFileUseSystemDefaults = "SYSTEM";
|
const char* kTLSCAFileUseSystemDefaults = "SYSTEM";
|
||||||
const char* kTLSCAFileDisableVerify = "NONE";
|
const char* kTLSCAFileDisableVerify = "NONE";
|
||||||
const char* kTLSCiphersUseDefault = "DEFAULT";
|
const char* kTLSCiphersUseDefault = "DEFAULT";
|
||||||
|
const char* kTLSInMemoryMarker = "-----BEGIN CERTIFICATE-----";
|
||||||
|
|
||||||
bool SocketTLSOptions::isValid() const
|
bool SocketTLSOptions::isValid() const
|
||||||
{
|
{
|
||||||
@ -58,6 +59,11 @@ namespace ix
|
|||||||
return caFile == kTLSCAFileUseSystemDefaults;
|
return caFile == kTLSCAFileUseSystemDefaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SocketTLSOptions::isUsingInMemoryCAs() const
|
||||||
|
{
|
||||||
|
return caFile.find(kTLSInMemoryMarker) != std::string::npos;
|
||||||
|
}
|
||||||
|
|
||||||
bool SocketTLSOptions::isPeerVerifyDisabled() const
|
bool SocketTLSOptions::isPeerVerifyDisabled() const
|
||||||
{
|
{
|
||||||
return caFile == kTLSCAFileDisableVerify;
|
return caFile == kTLSCAFileDisableVerify;
|
||||||
|
@ -37,6 +37,8 @@ namespace ix
|
|||||||
|
|
||||||
bool isUsingSystemDefaults() const;
|
bool isUsingSystemDefaults() const;
|
||||||
|
|
||||||
|
bool isUsingInMemoryCAs() const;
|
||||||
|
|
||||||
bool isPeerVerifyDisabled() const;
|
bool isPeerVerifyDisabled() const;
|
||||||
|
|
||||||
bool isUsingDefaultCiphers() const;
|
bool isUsingDefaultCiphers() const;
|
||||||
|
96
ixwebsocket/IXUdpSocket.cpp
Normal file
96
ixwebsocket/IXUdpSocket.cpp
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
40
ixwebsocket/IXUdpSocket.h
Normal file
40
ixwebsocket/IXUdpSocket.h
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
@ -1,4 +1,29 @@
|
|||||||
/*
|
/*
|
||||||
|
* Lightweight URL & URI parser (RFC 1738, RFC 3986)
|
||||||
|
* https://github.com/corporateshark/LUrlParser
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (C) 2015 Sergey Kosarevsky (sk@linderdaum.com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
* IXUrlParser.cpp
|
* IXUrlParser.cpp
|
||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||||
@ -6,7 +31,308 @@
|
|||||||
|
|
||||||
#include "IXUrlParser.h"
|
#include "IXUrlParser.h"
|
||||||
|
|
||||||
#include "LUrlParser.h"
|
#include <algorithm>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
enum LUrlParserError
|
||||||
|
{
|
||||||
|
LUrlParserError_Ok = 0,
|
||||||
|
LUrlParserError_Uninitialized = 1,
|
||||||
|
LUrlParserError_NoUrlCharacter = 2,
|
||||||
|
LUrlParserError_InvalidSchemeName = 3,
|
||||||
|
LUrlParserError_NoDoubleSlash = 4,
|
||||||
|
LUrlParserError_NoAtSign = 5,
|
||||||
|
LUrlParserError_UnexpectedEndOfLine = 6,
|
||||||
|
LUrlParserError_NoSlash = 7,
|
||||||
|
};
|
||||||
|
|
||||||
|
class clParseURL
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LUrlParserError m_ErrorCode;
|
||||||
|
std::string m_Scheme;
|
||||||
|
std::string m_Host;
|
||||||
|
std::string m_Port;
|
||||||
|
std::string m_Path;
|
||||||
|
std::string m_Query;
|
||||||
|
std::string m_Fragment;
|
||||||
|
std::string m_UserName;
|
||||||
|
std::string m_Password;
|
||||||
|
|
||||||
|
clParseURL()
|
||||||
|
: m_ErrorCode(LUrlParserError_Uninitialized)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return 'true' if the parsing was successful
|
||||||
|
bool IsValid() const
|
||||||
|
{
|
||||||
|
return m_ErrorCode == LUrlParserError_Ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// helper to convert the port number to int, return 'true' if the port is valid (within the
|
||||||
|
/// 0..65535 range)
|
||||||
|
bool GetPort(int* OutPort) const;
|
||||||
|
|
||||||
|
/// parse the URL
|
||||||
|
static clParseURL ParseURL(const std::string& URL);
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit clParseURL(LUrlParserError ErrorCode)
|
||||||
|
: m_ErrorCode(ErrorCode)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool IsSchemeValid(const std::string& SchemeName)
|
||||||
|
{
|
||||||
|
for (auto c : SchemeName)
|
||||||
|
{
|
||||||
|
if (!isalpha(c) && c != '+' && c != '-' && c != '.') return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool clParseURL::GetPort(int* OutPort) const
|
||||||
|
{
|
||||||
|
if (!IsValid())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Port = atoi(m_Port.c_str());
|
||||||
|
|
||||||
|
if (Port <= 0 || Port > 65535)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (OutPort)
|
||||||
|
{
|
||||||
|
*OutPort = Port;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// based on RFC 1738 and RFC 3986
|
||||||
|
clParseURL clParseURL::ParseURL(const std::string& URL)
|
||||||
|
{
|
||||||
|
clParseURL Result;
|
||||||
|
|
||||||
|
const char* CurrentString = URL.c_str();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* <scheme>:<scheme-specific-part>
|
||||||
|
* <scheme> := [a-z\+\-\.]+
|
||||||
|
* For resiliency, programs interpreting URLs should treat upper case letters as
|
||||||
|
*equivalent to lower case in scheme names
|
||||||
|
*/
|
||||||
|
|
||||||
|
// try to read scheme
|
||||||
|
{
|
||||||
|
const char* LocalString = strchr(CurrentString, ':');
|
||||||
|
|
||||||
|
if (!LocalString)
|
||||||
|
{
|
||||||
|
return clParseURL(LUrlParserError_NoUrlCharacter);
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the scheme name
|
||||||
|
Result.m_Scheme = std::string(CurrentString, LocalString - CurrentString);
|
||||||
|
|
||||||
|
if (!IsSchemeValid(Result.m_Scheme))
|
||||||
|
{
|
||||||
|
return clParseURL(LUrlParserError_InvalidSchemeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// scheme should be lowercase
|
||||||
|
std::transform(
|
||||||
|
Result.m_Scheme.begin(), Result.m_Scheme.end(), Result.m_Scheme.begin(), ::tolower);
|
||||||
|
|
||||||
|
// skip ':'
|
||||||
|
CurrentString = LocalString + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* //<user>:<password>@<host>:<port>/<url-path>
|
||||||
|
* any ":", "@" and "/" must be normalized
|
||||||
|
*/
|
||||||
|
|
||||||
|
// skip "//"
|
||||||
|
if (*CurrentString++ != '/') return clParseURL(LUrlParserError_NoDoubleSlash);
|
||||||
|
if (*CurrentString++ != '/') return clParseURL(LUrlParserError_NoDoubleSlash);
|
||||||
|
|
||||||
|
// check if the user name and password are specified
|
||||||
|
bool bHasUserName = false;
|
||||||
|
|
||||||
|
const char* LocalString = CurrentString;
|
||||||
|
|
||||||
|
while (*LocalString)
|
||||||
|
{
|
||||||
|
if (*LocalString == '@')
|
||||||
|
{
|
||||||
|
// user name and password are specified
|
||||||
|
bHasUserName = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (*LocalString == '/')
|
||||||
|
{
|
||||||
|
// end of <host>:<port> specification
|
||||||
|
bHasUserName = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalString++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// user name and password
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
if (bHasUserName)
|
||||||
|
{
|
||||||
|
// read user name
|
||||||
|
while (*LocalString && *LocalString != ':' && *LocalString != '@')
|
||||||
|
LocalString++;
|
||||||
|
|
||||||
|
Result.m_UserName = std::string(CurrentString, LocalString - CurrentString);
|
||||||
|
|
||||||
|
// proceed with the current pointer
|
||||||
|
CurrentString = LocalString;
|
||||||
|
|
||||||
|
if (*CurrentString == ':')
|
||||||
|
{
|
||||||
|
// skip ':'
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// read password
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while (*LocalString && *LocalString != '@')
|
||||||
|
LocalString++;
|
||||||
|
|
||||||
|
Result.m_Password = std::string(CurrentString, LocalString - CurrentString);
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip '@'
|
||||||
|
if (*CurrentString != '@')
|
||||||
|
{
|
||||||
|
return clParseURL(LUrlParserError_NoAtSign);
|
||||||
|
}
|
||||||
|
|
||||||
|
CurrentString++;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bHasBracket = (*CurrentString == '[');
|
||||||
|
|
||||||
|
// go ahead, read the host name
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while (*LocalString)
|
||||||
|
{
|
||||||
|
if (bHasBracket && *LocalString == ']')
|
||||||
|
{
|
||||||
|
// end of IPv6 address
|
||||||
|
LocalString++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (!bHasBracket && (*LocalString == ':' || *LocalString == '/'))
|
||||||
|
{
|
||||||
|
// port number is specified
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalString++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result.m_Host = std::string(CurrentString, LocalString - CurrentString);
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
|
||||||
|
// is port number specified?
|
||||||
|
if (*CurrentString == ':')
|
||||||
|
{
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// read port number
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while (*LocalString && *LocalString != '/')
|
||||||
|
LocalString++;
|
||||||
|
|
||||||
|
Result.m_Port = std::string(CurrentString, LocalString - CurrentString);
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
}
|
||||||
|
|
||||||
|
// end of string
|
||||||
|
if (!*CurrentString)
|
||||||
|
{
|
||||||
|
Result.m_ErrorCode = LUrlParserError_Ok;
|
||||||
|
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip '/'
|
||||||
|
if (*CurrentString != '/')
|
||||||
|
{
|
||||||
|
return clParseURL(LUrlParserError_NoSlash);
|
||||||
|
}
|
||||||
|
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// parse the path
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while (*LocalString && *LocalString != '#' && *LocalString != '?')
|
||||||
|
LocalString++;
|
||||||
|
|
||||||
|
Result.m_Path = std::string(CurrentString, LocalString - CurrentString);
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
|
||||||
|
// check for query
|
||||||
|
if (*CurrentString == '?')
|
||||||
|
{
|
||||||
|
// skip '?'
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// read query
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while (*LocalString && *LocalString != '#')
|
||||||
|
LocalString++;
|
||||||
|
|
||||||
|
Result.m_Query = std::string(CurrentString, LocalString - CurrentString);
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for fragment
|
||||||
|
if (*CurrentString == '#')
|
||||||
|
{
|
||||||
|
// skip '#'
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// read fragment
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while (*LocalString)
|
||||||
|
LocalString++;
|
||||||
|
|
||||||
|
Result.m_Fragment = std::string(CurrentString, LocalString - CurrentString);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result.m_ErrorCode = LUrlParserError_Ok;
|
||||||
|
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
@ -17,7 +343,7 @@ namespace ix
|
|||||||
std::string& query,
|
std::string& query,
|
||||||
int& port)
|
int& port)
|
||||||
{
|
{
|
||||||
LUrlParser::clParseURL res = LUrlParser::clParseURL::ParseURL(url);
|
clParseURL res = clParseURL::ParseURL(url);
|
||||||
|
|
||||||
if (!res.IsValid())
|
if (!res.IsValid())
|
||||||
{
|
{
|
||||||
|
@ -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/DarwinSSL";
|
ss << " ssl/SecureTransport";
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
ss << " nossl";
|
ss << " nossl";
|
||||||
|
@ -19,7 +19,6 @@ 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
|
||||||
|
|
||||||
@ -31,12 +30,11 @@ 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_shared<WebSocketMessage>(WebSocketMessageType::Close,
|
std::make_unique<WebSocketMessage>(WebSocketMessageType::Close,
|
||||||
"",
|
"",
|
||||||
wireSize,
|
wireSize,
|
||||||
WebSocketErrorInfo(),
|
WebSocketErrorInfo(),
|
||||||
@ -86,18 +84,6 @@ 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);
|
||||||
@ -110,18 +96,6 @@ 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);
|
||||||
@ -186,11 +160,8 @@ namespace ix
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_configMutex);
|
std::lock_guard<std::mutex> lock(_configMutex);
|
||||||
_ws.configure(_perMessageDeflateOptions,
|
_ws.configure(
|
||||||
_socketTLSOptions,
|
_perMessageDeflateOptions, _socketTLSOptions, _enablePong, _pingIntervalSecs);
|
||||||
_enablePong,
|
|
||||||
_pingIntervalSecs,
|
|
||||||
_pingTimeoutSecs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketHttpHeaders headers(_extraHeaders);
|
WebSocketHttpHeaders headers(_extraHeaders);
|
||||||
@ -222,40 +193,51 @@ namespace ix
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMessageCallback(std::make_shared<WebSocketMessage>(
|
_onMessageCallback(std::make_unique<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::shared_ptr<Socket> socket, int timeoutSecs)
|
WebSocketInitResult WebSocket::connectToSocket(std::unique_ptr<Socket> socket, int timeoutSecs)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_configMutex);
|
std::lock_guard<std::mutex> lock(_configMutex);
|
||||||
_ws.configure(_perMessageDeflateOptions,
|
_ws.configure(
|
||||||
_socketTLSOptions,
|
_perMessageDeflateOptions, _socketTLSOptions, _enablePong, _pingIntervalSecs);
|
||||||
_enablePong,
|
|
||||||
_pingIntervalSecs,
|
|
||||||
_pingTimeoutSecs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketInitResult status = _ws.connectToSocket(socket, timeoutSecs);
|
WebSocketInitResult status = _ws.connectToSocket(std::move(socket), timeoutSecs);
|
||||||
if (!status.success)
|
if (!status.success)
|
||||||
{
|
{
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMessageCallback(
|
_onMessageCallback(
|
||||||
std::make_shared<WebSocketMessage>(WebSocketMessageType::Open,
|
std::make_unique<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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -328,7 +310,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_shared<WebSocketMessage>(WebSocketMessageType::Error,
|
_onMessageCallback(std::make_unique<WebSocketMessage>(WebSocketMessageType::Error,
|
||||||
"",
|
"",
|
||||||
0,
|
0,
|
||||||
connectErr,
|
connectErr,
|
||||||
@ -404,7 +386,7 @@ namespace ix
|
|||||||
|
|
||||||
bool binary = messageKind == WebSocketTransport::MessageKind::MSG_BINARY;
|
bool binary = messageKind == WebSocketTransport::MessageKind::MSG_BINARY;
|
||||||
|
|
||||||
_onMessageCallback(std::make_shared<WebSocketMessage>(webSocketMessageType,
|
_onMessageCallback(std::make_unique<WebSocketMessage>(webSocketMessageType,
|
||||||
msg,
|
msg,
|
||||||
wireSize,
|
wireSize,
|
||||||
webSocketErrorInfo,
|
webSocketErrorInfo,
|
||||||
|
@ -52,9 +52,7 @@ namespace ix
|
|||||||
void setPerMessageDeflateOptions(
|
void setPerMessageDeflateOptions(
|
||||||
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions);
|
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions);
|
||||||
void setTLSOptions(const SocketTLSOptions& socketTLSOptions);
|
void setTLSOptions(const SocketTLSOptions& socketTLSOptions);
|
||||||
void setHeartBeatPeriod(int heartBeatPeriodSecs);
|
void setPingInterval(int pingIntervalSecs);
|
||||||
void setPingInterval(int pingIntervalSecs); // alias of setHeartBeatPeriod
|
|
||||||
void setPingTimeout(int pingTimeoutSecs);
|
|
||||||
void enablePong();
|
void enablePong();
|
||||||
void disablePong();
|
void disablePong();
|
||||||
void enablePerMessageDeflate();
|
void enablePerMessageDeflate();
|
||||||
@ -94,9 +92,7 @@ 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();
|
||||||
@ -117,7 +113,7 @@ namespace ix
|
|||||||
static void invokeTrafficTrackerCallback(size_t size, bool incoming);
|
static void invokeTrafficTrackerCallback(size_t size, bool incoming);
|
||||||
|
|
||||||
// Server
|
// Server
|
||||||
WebSocketInitResult connectToSocket(std::shared_ptr<Socket>, int timeoutSecs);
|
WebSocketInitResult connectToSocket(std::unique_ptr<Socket>, int timeoutSecs);
|
||||||
|
|
||||||
WebSocketTransport _ws;
|
WebSocketTransport _ws;
|
||||||
|
|
||||||
|
@ -6,6 +6,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
struct WebSocketCloseInfo
|
struct WebSocketCloseInfo
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
#include "IXSocketConnect.h"
|
#include "IXSocketConnect.h"
|
||||||
#include "IXUrlParser.h"
|
#include "IXUrlParser.h"
|
||||||
#include "IXUserAgent.h"
|
#include "IXUserAgent.h"
|
||||||
#include "libwshandshake.hpp"
|
#include "IXWebSocketHandshakeKeyGen.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random>
|
#include <random>
|
||||||
@ -21,8 +21,8 @@ namespace ix
|
|||||||
{
|
{
|
||||||
WebSocketHandshake::WebSocketHandshake(
|
WebSocketHandshake::WebSocketHandshake(
|
||||||
std::atomic<bool>& requestInitCancellation,
|
std::atomic<bool>& requestInitCancellation,
|
||||||
std::shared_ptr<Socket> socket,
|
std::unique_ptr<Socket>& socket,
|
||||||
WebSocketPerMessageDeflate& perMessageDeflate,
|
WebSocketPerMessageDeflatePtr& perMessageDeflate,
|
||||||
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
||||||
std::atomic<bool>& enablePerMessageDeflate)
|
std::atomic<bool>& enablePerMessageDeflate)
|
||||||
: _requestInitCancellation(requestInitCancellation)
|
: _requestInitCancellation(requestInitCancellation)
|
||||||
@ -133,7 +133,7 @@ namespace ix
|
|||||||
|
|
||||||
for (auto& it : extraHeaders)
|
for (auto& it : extraHeaders)
|
||||||
{
|
{
|
||||||
ss << it.first << ":" << it.second << "\r\n";
|
ss << it.first << ": " << it.second << "\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_enablePerMessageDeflate)
|
if (_enablePerMessageDeflate)
|
||||||
@ -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");
|
||||||
|
@ -23,8 +23,8 @@ namespace ix
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WebSocketHandshake(std::atomic<bool>& requestInitCancellation,
|
WebSocketHandshake(std::atomic<bool>& requestInitCancellation,
|
||||||
std::shared_ptr<Socket> _socket,
|
std::unique_ptr<Socket>& _socket,
|
||||||
WebSocketPerMessageDeflate& perMessageDeflate,
|
WebSocketPerMessageDeflatePtr& 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::shared_ptr<Socket> _socket;
|
std::unique_ptr<Socket>& _socket;
|
||||||
WebSocketPerMessageDeflate& _perMessageDeflate;
|
WebSocketPerMessageDeflatePtr& _perMessageDeflate;
|
||||||
WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions;
|
WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions;
|
||||||
std::atomic<bool>& _enablePerMessageDeflate;
|
std::atomic<bool>& _enablePerMessageDeflate;
|
||||||
};
|
};
|
||||||
|
171
ixwebsocket/IXWebSocketHandshakeKeyGen.h
Normal file
171
ixwebsocket/IXWebSocketHandshakeKeyGen.h
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
// Copyright (c) 2016 Alex Hultman and contributors
|
||||||
|
|
||||||
|
// This software is provided 'as-is', without any express or implied
|
||||||
|
// warranty. In no event will the authors be held liable for any damages
|
||||||
|
// arising from the use of this software.
|
||||||
|
|
||||||
|
// Permission is granted to anyone to use this software for any purpose,
|
||||||
|
// including commercial applications, and to alter it and redistribute it
|
||||||
|
// freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
// 1. The origin of this software must not be misrepresented; you must not
|
||||||
|
// claim that you wrote the original software. If you use this software
|
||||||
|
// in a product, an acknowledgement in the product documentation would be
|
||||||
|
// appreciated but is not required.
|
||||||
|
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
// misrepresented as being the original software.
|
||||||
|
// 3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class WebSocketHandshakeKeyGen
|
||||||
|
{
|
||||||
|
template<int N, typename T>
|
||||||
|
struct static_for
|
||||||
|
{
|
||||||
|
void operator()(uint32_t* a, uint32_t* b)
|
||||||
|
{
|
||||||
|
static_for<N - 1, T>()(a, b);
|
||||||
|
T::template f<N - 1>(a, b);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct static_for<0, T>
|
||||||
|
{
|
||||||
|
void operator()(uint32_t* /*a*/, uint32_t* /*hash*/)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<int state>
|
||||||
|
struct Sha1Loop
|
||||||
|
{
|
||||||
|
static inline uint32_t rol(uint32_t value, size_t bits)
|
||||||
|
{
|
||||||
|
return (value << bits) | (value >> (32 - bits));
|
||||||
|
}
|
||||||
|
static inline uint32_t blk(uint32_t b[16], size_t i)
|
||||||
|
{
|
||||||
|
return rol(b[(i + 13) & 15] ^ b[(i + 8) & 15] ^ b[(i + 2) & 15] ^ b[i], 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<int i>
|
||||||
|
static inline void f(uint32_t* a, uint32_t* b)
|
||||||
|
{
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
a[i % 5] +=
|
||||||
|
((a[(3 + i) % 5] & (a[(2 + i) % 5] ^ a[(1 + i) % 5])) ^ a[(1 + i) % 5]) +
|
||||||
|
b[i] + 0x5a827999 + rol(a[(4 + i) % 5], 5);
|
||||||
|
a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
b[i] = blk(b, i);
|
||||||
|
a[(1 + i) % 5] +=
|
||||||
|
((a[(4 + i) % 5] & (a[(3 + i) % 5] ^ a[(2 + i) % 5])) ^ a[(2 + i) % 5]) +
|
||||||
|
b[i] + 0x5a827999 + rol(a[(5 + i) % 5], 5);
|
||||||
|
a[(4 + i) % 5] = rol(a[(4 + i) % 5], 30);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
b[(i + 4) % 16] = blk(b, (i + 4) % 16);
|
||||||
|
a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) +
|
||||||
|
b[(i + 4) % 16] + 0x6ed9eba1 + rol(a[(4 + i) % 5], 5);
|
||||||
|
a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
b[(i + 8) % 16] = blk(b, (i + 8) % 16);
|
||||||
|
a[i % 5] += (((a[(3 + i) % 5] | a[(2 + i) % 5]) & a[(1 + i) % 5]) |
|
||||||
|
(a[(3 + i) % 5] & a[(2 + i) % 5])) +
|
||||||
|
b[(i + 8) % 16] + 0x8f1bbcdc + rol(a[(4 + i) % 5], 5);
|
||||||
|
a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
b[(i + 12) % 16] = blk(b, (i + 12) % 16);
|
||||||
|
a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) +
|
||||||
|
b[(i + 12) % 16] + 0xca62c1d6 + rol(a[(4 + i) % 5], 5);
|
||||||
|
a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
|
||||||
|
break;
|
||||||
|
case 6: b[i] += a[4 - i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline void sha1(uint32_t hash[5], uint32_t b[16])
|
||||||
|
{
|
||||||
|
uint32_t a[5] = {hash[4], hash[3], hash[2], hash[1], hash[0]};
|
||||||
|
static_for<16, Sha1Loop<1>>()(a, b);
|
||||||
|
static_for<4, Sha1Loop<2>>()(a, b);
|
||||||
|
static_for<20, Sha1Loop<3>>()(a, b);
|
||||||
|
static_for<20, Sha1Loop<4>>()(a, b);
|
||||||
|
static_for<20, Sha1Loop<5>>()(a, b);
|
||||||
|
static_for<5, Sha1Loop<6>>()(a, hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void base64(unsigned char* src, char* dst)
|
||||||
|
{
|
||||||
|
const char* b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
for (int i = 0; i < 18; i += 3)
|
||||||
|
{
|
||||||
|
*dst++ = b64[(src[i] >> 2) & 63];
|
||||||
|
*dst++ = b64[((src[i] & 3) << 4) | ((src[i + 1] & 240) >> 4)];
|
||||||
|
*dst++ = b64[((src[i + 1] & 15) << 2) | ((src[i + 2] & 192) >> 6)];
|
||||||
|
*dst++ = b64[src[i + 2] & 63];
|
||||||
|
}
|
||||||
|
*dst++ = b64[(src[18] >> 2) & 63];
|
||||||
|
*dst++ = b64[((src[18] & 3) << 4) | ((src[19] & 240) >> 4)];
|
||||||
|
*dst++ = b64[((src[19] & 15) << 2)];
|
||||||
|
*dst++ = '=';
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
static inline void generate(const std::string& inputStr, char output[28])
|
||||||
|
{
|
||||||
|
char input[25] = {};
|
||||||
|
strncpy(input, inputStr.c_str(), 25 - 1);
|
||||||
|
input[25 - 1] = '\0';
|
||||||
|
|
||||||
|
uint32_t b_output[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0};
|
||||||
|
uint32_t b_input[16] = {0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0x32353845,
|
||||||
|
0x41464135,
|
||||||
|
0x2d453931,
|
||||||
|
0x342d3437,
|
||||||
|
0x44412d39,
|
||||||
|
0x3543412d,
|
||||||
|
0x43354142,
|
||||||
|
0x30444338,
|
||||||
|
0x35423131,
|
||||||
|
0x80000000};
|
||||||
|
|
||||||
|
for (int i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
|
b_input[i] = (input[4 * i + 3] & 0xff) | (input[4 * i + 2] & 0xff) << 8 |
|
||||||
|
(input[4 * i + 1] & 0xff) << 16 | (input[4 * i + 0] & 0xff) << 24;
|
||||||
|
}
|
||||||
|
sha1(b_output, b_input);
|
||||||
|
uint32_t last_b[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 480};
|
||||||
|
sha1(b_output, last_b);
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
uint32_t tmp = b_output[i];
|
||||||
|
char* bytes = (char*) &b_output[i];
|
||||||
|
bytes[3] = tmp & 0xff;
|
||||||
|
bytes[2] = (tmp >> 8) & 0xff;
|
||||||
|
bytes[1] = (tmp >> 16) & 0xff;
|
||||||
|
bytes[0] = (tmp >> 24) & 0xff;
|
||||||
|
}
|
||||||
|
base64((unsigned char*) b_output, output);
|
||||||
|
}
|
||||||
|
};
|
@ -32,7 +32,7 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders(
|
std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders(
|
||||||
std::shared_ptr<Socket> socket, const CancellationRequest& isCancellationRequested)
|
std::unique_ptr<Socket>& socket, const CancellationRequest& isCancellationRequested)
|
||||||
{
|
{
|
||||||
WebSocketHttpHeaders headers;
|
WebSocketHttpHeaders headers;
|
||||||
|
|
||||||
|
@ -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::shared_ptr<Socket> socket, const CancellationRequest& isCancellationRequested);
|
std::unique_ptr<Socket>& socket, const CancellationRequest& isCancellationRequested);
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
@ -19,7 +19,7 @@ namespace ix
|
|||||||
struct WebSocketMessage
|
struct WebSocketMessage
|
||||||
{
|
{
|
||||||
WebSocketMessageType type;
|
WebSocketMessageType type;
|
||||||
std::string str;
|
const 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(std::move(s))
|
, str(s)
|
||||||
, wireSize(w)
|
, wireSize(w)
|
||||||
, errorInfo(e)
|
, errorInfo(e)
|
||||||
, openInfo(o)
|
, openInfo(o)
|
||||||
@ -45,5 +45,5 @@ namespace ix
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
using WebSocketMessagePtr = std::shared_ptr<WebSocketMessage>;
|
using WebSocketMessagePtr = std::unique_ptr<WebSocketMessage>;
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user