Compare commits
6 Commits
feature/vc
...
user/bserg
Author | SHA1 | Date | |
---|---|---|---|
4373a92c61 | |||
91e67f6e53 | |||
1ca1f612be | |||
1b9e55d3f8 | |||
0d80971328 | |||
80c1ed0611 |
@ -1,46 +0,0 @@
|
|||||||
# https://releases.llvm.org/7.0.0/tools/clang/docs/ClangFormatStyleOptions.html
|
|
||||||
|
|
||||||
---
|
|
||||||
Language: Cpp
|
|
||||||
|
|
||||||
BasedOnStyle: WebKit
|
|
||||||
|
|
||||||
AlignAfterOpenBracket: Align
|
|
||||||
AlignOperands: true
|
|
||||||
AlignTrailingComments: true
|
|
||||||
AllowAllParametersOfDeclarationOnNextLine: true
|
|
||||||
AllowShortBlocksOnASingleLine: false
|
|
||||||
AllowShortCaseLabelsOnASingleLine: true
|
|
||||||
AllowShortFunctionsOnASingleLine: InlineOnly
|
|
||||||
AllowShortIfStatementsOnASingleLine: true
|
|
||||||
AllowShortLoopsOnASingleLine: false
|
|
||||||
AlwaysBreakTemplateDeclarations: true
|
|
||||||
BinPackArguments: false
|
|
||||||
BinPackParameters: false
|
|
||||||
BreakBeforeBinaryOperators: None
|
|
||||||
BreakBeforeBraces: Allman
|
|
||||||
BreakConstructorInitializersBeforeComma: true
|
|
||||||
ColumnLimit: 100
|
|
||||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
|
||||||
Cpp11BracedListStyle: true
|
|
||||||
FixNamespaceComments: true
|
|
||||||
IncludeBlocks: Regroup
|
|
||||||
IncludeCategories:
|
|
||||||
- Regex: '^["<](stdafx|pch)\.h[">]$'
|
|
||||||
Priority: -1
|
|
||||||
- Regex: '^<Windows\.h>$'
|
|
||||||
Priority: 3
|
|
||||||
- Regex: '^<(WinIoCtl|winhttp|Shellapi)\.h>$'
|
|
||||||
Priority: 4
|
|
||||||
- Regex: '.*'
|
|
||||||
Priority: 2
|
|
||||||
IndentCaseLabels: true
|
|
||||||
IndentWidth: 4
|
|
||||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
|
||||||
MaxEmptyLinesToKeep: 2
|
|
||||||
NamespaceIndentation: All
|
|
||||||
PenaltyReturnTypeOnItsOwnLine: 1000
|
|
||||||
PointerAlignment: Left
|
|
||||||
SpaceAfterTemplateKeyword: false
|
|
||||||
Standard: Cpp11
|
|
||||||
UseTab: Never
|
|
@ -1,4 +0,0 @@
|
|||||||
build
|
|
||||||
CMakeCache.txt
|
|
||||||
ws/CMakeCache.txt
|
|
||||||
test/build
|
|
78
.travis.yml
78
.travis.yml
@ -1,71 +1,11 @@
|
|||||||
language: bash
|
language: cpp
|
||||||
|
dist: xenial
|
||||||
|
|
||||||
# See https://github.com/amaiorano/vectrexy/blob/master/.travis.yml
|
compiler:
|
||||||
# for ideas on installing vcpkg
|
- clang
|
||||||
|
# - gcc
|
||||||
|
|
||||||
matrix:
|
os: osx
|
||||||
include:
|
# os: windows
|
||||||
# macOS
|
# script: make test
|
||||||
- os: osx
|
script: python test/run.py
|
||||||
compiler: clang
|
|
||||||
script:
|
|
||||||
- python test/run.py
|
|
||||||
- make ws
|
|
||||||
|
|
||||||
# Linux
|
|
||||||
- os: linux
|
|
||||||
dist: xenial
|
|
||||||
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:
|
|
||||||
- export PATH=$CMAKE_PATH:$PATH
|
|
||||||
- cmake -DUSE_TLS=1 -DUSE_WS=1 -DUSE_MBED_TLS=1 -DUSE_VENDORED_THIRD_PARTY=1 .
|
|
||||||
- cmake --build --parallel .
|
|
||||||
- python test/run.py
|
|
||||||
|
|
||||||
install:
|
|
||||||
# HACK: gcc 8.0.1 is missing movdirintrin.h so just download it. We need this for GLM and Vectrexy to build.
|
|
||||||
- sudo wget https://raw.githubusercontent.com/gcc-mirror/gcc/gcc-8-branch/gcc/config/i386/movdirintrin.h -P /usr/lib/gcc/x86_64-linux-gnu/8/include/
|
|
||||||
|
|
||||||
# Create deps dir
|
|
||||||
- mkdir -p ${DEPS_DIR}
|
|
||||||
|
|
||||||
# Set compiler vars
|
|
||||||
- export CC=${CC_COMPILER}
|
|
||||||
- export CXX=${CXX_COMPILER}
|
|
||||||
|
|
||||||
# Install vcpkg and dependencies
|
|
||||||
- |
|
|
||||||
set -e
|
|
||||||
mkdir -p ${DEPS_DIR}/vcpkg
|
|
||||||
pushd ${DEPS_DIR}/vcpkg
|
|
||||||
git init
|
|
||||||
git remote add origin https://github.com/Microsoft/vcpkg.git
|
|
||||||
git fetch origin master
|
|
||||||
git checkout -b master origin/master
|
|
||||||
./bootstrap-vcpkg.sh
|
|
||||||
# Only build release libs to save time. We inject a new line first since some cmake files don't end with one.
|
|
||||||
echo -e '\nset(VCPKG_BUILD_TYPE release)' >> ./triplets/${VCPKG_TRIPLET}.cmake
|
|
||||||
./vcpkg install sdl2 sdl2-net glew glm stb imgui
|
|
||||||
popd
|
|
||||||
|
|
||||||
cache:
|
|
||||||
directories:
|
|
||||||
- ${DEPS_DIR}/vcpkg/installed
|
|
||||||
|
31
CHANGELOG.md
31
CHANGELOG.md
@ -1,31 +0,0 @@
|
|||||||
# Changelog
|
|
||||||
All notable changes to this project will be documented in this file.
|
|
||||||
|
|
||||||
## [unreleased] - 2019-06-09
|
|
||||||
### Changed
|
|
||||||
- mbedtls and zlib are searched with find_package, and we use the vendored version if nothing is found
|
|
||||||
- travis CI uses g++ on Linux
|
|
||||||
|
|
||||||
## [4.0.0] - 2019-06-09
|
|
||||||
### Changed
|
|
||||||
- WebSocket::send() sends message in TEXT mode by default
|
|
||||||
- WebSocketMessage sets a new binary field, which tells whether the received incoming message is binary or text
|
|
||||||
- WebSocket::send takes a third arg, binary which default to true (can be text too)
|
|
||||||
- WebSocket callback only take one object, a const ix::WebSocketMessagePtr& msg
|
|
||||||
- Add explicit WebSocket::sendBinary method
|
|
||||||
- New headers + WebSocketMessage class to hold message data, still not used across the board
|
|
||||||
- Add test/compatibility folder with small servers and clients written in different languages and different libraries to test compatibility.
|
|
||||||
- ws echo_server has a -g option to print a greeting message on connect
|
|
||||||
- IXSocketMbedTLS: better error handling in close and connect
|
|
||||||
|
|
||||||
## [3.1.2] - 2019-06-06
|
|
||||||
### Added
|
|
||||||
- ws connect has a -x option to disable per message deflate
|
|
||||||
- Add WebSocket::disablePerMessageDeflate() option.
|
|
||||||
|
|
||||||
## [3.0.0] - 2019-06-xx
|
|
||||||
### Changed
|
|
||||||
- TLS, aka SSL works on Windows (websocket and http clients)
|
|
||||||
- ws command line tool build on Windows
|
|
||||||
- Async API for HttpClient
|
|
||||||
- HttpClient API changed to use shared_ptr for response and request
|
|
@ -1,13 +0,0 @@
|
|||||||
find_path(MBEDTLS_INCLUDE_DIRS mbedtls/ssl.h)
|
|
||||||
|
|
||||||
find_library(MBEDTLS_LIBRARY mbedtls)
|
|
||||||
find_library(MBEDX509_LIBRARY mbedx509)
|
|
||||||
find_library(MBEDCRYPTO_LIBRARY mbedcrypto)
|
|
||||||
|
|
||||||
set(MBEDTLS_LIBRARIES "${MBEDTLS_LIBRARY}" "${MBEDX509_LIBRARY}" "${MBEDCRYPTO_LIBRARY}")
|
|
||||||
|
|
||||||
include(FindPackageHandleStandardArgs)
|
|
||||||
find_package_handle_standard_args(MBEDTLS DEFAULT_MSG
|
|
||||||
MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
|
|
||||||
|
|
||||||
mark_as_advanced(MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
|
|
146
CMakeLists.txt
146
CMakeLists.txt
@ -4,90 +4,53 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.4.1)
|
cmake_minimum_required(VERSION 3.4.1)
|
||||||
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
|
|
||||||
|
|
||||||
project(ixwebsocket C CXX)
|
project(ixwebsocket C CXX)
|
||||||
|
|
||||||
set (CMAKE_CXX_STANDARD 14)
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
set (CXX_STANDARD_REQUIRED ON)
|
set (CXX_STANDARD_REQUIRED ON)
|
||||||
set (CMAKE_CXX_EXTENSIONS OFF)
|
set (CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
# -Wshorten-64-to-32 does not work with clang
|
|
||||||
if (NOT WIN32)
|
if (NOT WIN32)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshorten-64-to-32")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set( IXWEBSOCKET_SOURCES
|
set( IXWEBSOCKET_SOURCES
|
||||||
ixwebsocket/IXCancellationRequest.cpp
|
ixwebsocket/IXEventFd.cpp
|
||||||
ixwebsocket/IXConnectionState.cpp
|
|
||||||
ixwebsocket/IXDNSLookup.cpp
|
|
||||||
ixwebsocket/IXHttpClient.cpp
|
|
||||||
ixwebsocket/IXNetSystem.cpp
|
|
||||||
ixwebsocket/IXSelectInterrupt.cpp
|
|
||||||
ixwebsocket/IXSelectInterruptFactory.cpp
|
|
||||||
ixwebsocket/IXSocket.cpp
|
ixwebsocket/IXSocket.cpp
|
||||||
ixwebsocket/IXSocketConnect.cpp
|
|
||||||
ixwebsocket/IXSocketFactory.cpp
|
|
||||||
ixwebsocket/IXSocketServer.cpp
|
ixwebsocket/IXSocketServer.cpp
|
||||||
ixwebsocket/IXUrlParser.cpp
|
ixwebsocket/IXSocketConnect.cpp
|
||||||
|
ixwebsocket/IXDNSLookup.cpp
|
||||||
|
ixwebsocket/IXCancellationRequest.cpp
|
||||||
ixwebsocket/IXWebSocket.cpp
|
ixwebsocket/IXWebSocket.cpp
|
||||||
ixwebsocket/IXWebSocketCloseConstants.cpp
|
ixwebsocket/IXWebSocketServer.cpp
|
||||||
|
ixwebsocket/IXWebSocketTransport.cpp
|
||||||
ixwebsocket/IXWebSocketHandshake.cpp
|
ixwebsocket/IXWebSocketHandshake.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/IXWebSocketTransport.cpp
|
|
||||||
ixwebsocket/LUrlParser.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
set( IXWEBSOCKET_HEADERS
|
set( IXWEBSOCKET_HEADERS
|
||||||
ixwebsocket/IXCancellationRequest.h
|
ixwebsocket/IXEventFd.h
|
||||||
ixwebsocket/IXConnectionState.h
|
|
||||||
ixwebsocket/IXDNSLookup.h
|
|
||||||
ixwebsocket/IXHttpClient.h
|
|
||||||
ixwebsocket/IXNetSystem.h
|
|
||||||
ixwebsocket/IXProgressCallback.h
|
|
||||||
ixwebsocket/IXSelectInterrupt.h
|
|
||||||
ixwebsocket/IXSelectInterruptFactory.h
|
|
||||||
ixwebsocket/IXSetThreadName.h
|
|
||||||
ixwebsocket/IXSocket.h
|
ixwebsocket/IXSocket.h
|
||||||
ixwebsocket/IXSocketConnect.h
|
|
||||||
ixwebsocket/IXSocketFactory.h
|
|
||||||
ixwebsocket/IXSocketServer.h
|
ixwebsocket/IXSocketServer.h
|
||||||
ixwebsocket/IXUrlParser.h
|
ixwebsocket/IXSocketConnect.h
|
||||||
|
ixwebsocket/IXSetThreadName.h
|
||||||
|
ixwebsocket/IXDNSLookup.h
|
||||||
|
ixwebsocket/IXCancellationRequest.h
|
||||||
ixwebsocket/IXWebSocket.h
|
ixwebsocket/IXWebSocket.h
|
||||||
ixwebsocket/IXWebSocketCloseConstants.h
|
ixwebsocket/IXWebSocketServer.h
|
||||||
ixwebsocket/IXWebSocketCloseInfo.h
|
ixwebsocket/IXWebSocketTransport.h
|
||||||
ixwebsocket/IXWebSocketErrorInfo.h
|
|
||||||
ixwebsocket/IXWebSocketHandshake.h
|
ixwebsocket/IXWebSocketHandshake.h
|
||||||
ixwebsocket/IXWebSocketHttpHeaders.h
|
ixwebsocket/IXWebSocketSendInfo.h
|
||||||
ixwebsocket/IXWebSocketMessage.h
|
ixwebsocket/IXWebSocketErrorInfo.h
|
||||||
ixwebsocket/IXWebSocketMessageQueue.h
|
|
||||||
ixwebsocket/IXWebSocketMessageType.h
|
|
||||||
ixwebsocket/IXWebSocketOpenInfo.h
|
|
||||||
ixwebsocket/IXWebSocketPerMessageDeflate.h
|
ixwebsocket/IXWebSocketPerMessageDeflate.h
|
||||||
ixwebsocket/IXWebSocketPerMessageDeflateCodec.h
|
ixwebsocket/IXWebSocketPerMessageDeflateCodec.h
|
||||||
ixwebsocket/IXWebSocketPerMessageDeflateOptions.h
|
ixwebsocket/IXWebSocketPerMessageDeflateOptions.h
|
||||||
ixwebsocket/IXWebSocketSendInfo.h
|
ixwebsocket/IXWebSocketHttpHeaders.h
|
||||||
ixwebsocket/IXWebSocketServer.h
|
|
||||||
ixwebsocket/IXWebSocketTransport.h
|
|
||||||
ixwebsocket/LUrlParser.h
|
|
||||||
ixwebsocket/libwshandshake.hpp
|
ixwebsocket/libwshandshake.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
if (UNIX)
|
|
||||||
# Linux, Mac, iOS, Android
|
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSelectInterruptPipe.cpp )
|
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSelectInterruptPipe.h )
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Platform specific code
|
# Platform specific code
|
||||||
if (APPLE)
|
if (APPLE)
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/apple/IXSetThreadName_apple.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/apple/IXSetThreadName_apple.cpp)
|
||||||
@ -95,31 +58,18 @@ elseif (WIN32)
|
|||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/windows/IXSetThreadName_windows.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/windows/IXSetThreadName_windows.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()
|
||||||
|
|
||||||
if (WIN32)
|
|
||||||
set(USE_MBED_TLS TRUE)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set(USE_OPEN_SSL FALSE)
|
|
||||||
if (USE_TLS)
|
if (USE_TLS)
|
||||||
add_definitions(-DIXWEBSOCKET_USE_TLS)
|
add_definitions(-DIXWEBSOCKET_USE_TLS)
|
||||||
|
|
||||||
if (USE_MBED_TLS)
|
if (APPLE)
|
||||||
add_definitions(-DIXWEBSOCKET_USE_MBED_TLS)
|
|
||||||
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketMbedTLS.h)
|
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketMbedTLS.cpp)
|
|
||||||
elseif (APPLE)
|
|
||||||
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)
|
||||||
elseif (WIN32)
|
elseif (WIN32)
|
||||||
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketSChannel.h)
|
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketSChannel.h)
|
||||||
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketSChannel.cpp)
|
list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketSChannel.cpp)
|
||||||
else()
|
else()
|
||||||
add_definitions(-DIXWEBSOCKET_USE_OPEN_SSL)
|
|
||||||
set(USE_OPEN_SSL TRUE)
|
|
||||||
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)
|
||||||
endif()
|
endif()
|
||||||
@ -130,61 +80,35 @@ add_library( ixwebsocket STATIC
|
|||||||
${IXWEBSOCKET_HEADERS}
|
${IXWEBSOCKET_HEADERS}
|
||||||
)
|
)
|
||||||
|
|
||||||
if (APPLE AND USE_TLS AND NOT USE_MBED_TLS)
|
# gcc/Linux needs -pthread
|
||||||
target_link_libraries(ixwebsocket "-framework foundation" "-framework security")
|
find_package(Threads)
|
||||||
endif()
|
|
||||||
|
|
||||||
if (USE_OPEN_SSL)
|
if(UNIX AND NOT APPLE)
|
||||||
find_package(OpenSSL REQUIRED)
|
find_package(OpenSSL REQUIRED)
|
||||||
add_definitions(${OPENSSL_DEFINITIONS})
|
add_definitions(${OPENSSL_DEFINITIONS})
|
||||||
message(STATUS "OpenSSL: " ${OPENSSL_VERSION})
|
message(STATUS "OpenSSL: " ${OPENSSL_VERSION})
|
||||||
include_directories(${OPENSSL_INCLUDE_DIR})
|
include_directories(${OPENSSL_INCLUDE_DIR})
|
||||||
target_link_libraries(ixwebsocket ${OPENSSL_LIBRARIES})
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (USE_MBED_TLS)
|
if (WIN32)
|
||||||
if (USE_VENDORED_THIRD_PARTY)
|
get_filename_component(libz_path
|
||||||
set (ENABLE_PROGRAMS OFF)
|
${PROJECT_SOURCE_DIR}/third_party/ZLIB-Windows/zlib-1.2.11_deploy_v140/release_dynamic/x64/lib/zlib.lib
|
||||||
add_subdirectory(third_party/mbedtls)
|
ABSOLUTE)
|
||||||
include_directories(third_party/mbedtls/include)
|
add_library(libz STATIC IMPORTED)
|
||||||
|
set_target_properties(libz PROPERTIES IMPORTED_LOCATION
|
||||||
|
${libz_path})
|
||||||
|
|
||||||
target_link_libraries(ixwebsocket mbedtls)
|
include_directories(${PROJECT_SOURCE_DIR}/third_party/ZLIB-Windows/zlib-1.2.11_deploy_v140/include)
|
||||||
else()
|
|
||||||
find_package(MbedTLS REQUIRED)
|
|
||||||
include_directories(${MBEDTLS_INCLUDE_DIRS})
|
|
||||||
target_link_libraries(ixwebsocket ${MBEDTLS_LIBRARIES})
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
|
|
||||||
find_package(ZLIB REQUIRED)
|
target_link_libraries(ixwebsocket libz wsock32 ws2_32)
|
||||||
if (ZLIB_FOUND)
|
|
||||||
include_directories(${ZLIB_INCLUDE_DIRS})
|
|
||||||
target_link_libraries(ixwebsocket ${ZLIB_LIBRARIES})
|
|
||||||
else()
|
|
||||||
add_subdirectory(third_party/zlib)
|
|
||||||
include_directories(third_party/zlib ${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib)
|
|
||||||
target_link_libraries(ixwebsocket zlibstatic wsock32 ws2_32)
|
|
||||||
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
||||||
|
|
||||||
|
else()
|
||||||
|
target_link_libraries(ixwebsocket
|
||||||
|
z ${OPENSSL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set( IXWEBSOCKET_INCLUDE_DIRS
|
set( IXWEBSOCKET_INCLUDE_DIRS
|
||||||
.
|
.
|
||||||
)
|
../../shared/OpenSSL/include)
|
||||||
|
target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} )
|
||||||
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
|
|
||||||
# Build with Multiple Processes
|
|
||||||
target_compile_options(ixwebsocket PRIVATE /MP)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
target_include_directories(ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS})
|
|
||||||
|
|
||||||
set_target_properties(ixwebsocket PROPERTIES PUBLIC_HEADER "${IXWEBSOCKET_HEADERS}")
|
|
||||||
|
|
||||||
install(TARGETS ixwebsocket
|
|
||||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
|
|
||||||
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/ixwebsocket/
|
|
||||||
)
|
|
||||||
|
|
||||||
if (USE_WS)
|
|
||||||
add_subdirectory(ws)
|
|
||||||
endif()
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
4.0.0
|
|
@ -1 +1 @@
|
|||||||
docker/Dockerfile.alpine
|
docker/Dockerfile.debian
|
266
README.md
266
README.md
@ -4,17 +4,18 @@
|
|||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
[*WebSocket*](https://en.wikipedia.org/wiki/WebSocket) is a computer communications protocol, providing full-duplex and bi-directionnal communication channels over a single TCP connection. *IXWebSocket* is a C++ library for client and server Websocket communication, and for client HTTP communication. The code is derived from [easywsclient](https://github.com/dhbaird/easywsclient) and from the [Satori C SDK](https://github.com/satori-com/satori-rtm-sdk-c). It has been tested on the following platforms.
|
[*WebSocket*](https://en.wikipedia.org/wiki/WebSocket) is a computer communications protocol, providing full-duplex
|
||||||
|
communication channels over a single TCP connection. *IXWebSocket* is a C++ library for client and server Websocket communication. The code is derived from [easywsclient](https://github.com/dhbaird/easywsclient) and from the [Satori C SDK](https://github.com/satori-com/satori-rtm-sdk-c). It has been tested on the following platforms.
|
||||||
|
|
||||||
* macOS
|
* macOS
|
||||||
* iOS
|
* iOS
|
||||||
* Linux
|
* Linux
|
||||||
* Android
|
* Android
|
||||||
* Windows
|
* Windows (no TLS support yet)
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
The [*ws*](https://github.com/machinezone/IXWebSocket/tree/master/ws) folder countains many interactive programs for chat, [file transfers](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_send.cpp), [curl like](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_http_client.cpp) http clients, demonstrating client and server usage.
|
The examples folder countains a simple chat program, using a node.js broadcast server.
|
||||||
|
|
||||||
Here is what the client API looks like.
|
Here is what the client API looks like.
|
||||||
|
|
||||||
@ -24,32 +25,31 @@ ix::WebSocket webSocket;
|
|||||||
std::string url("ws://localhost:8080/");
|
std::string url("ws://localhost:8080/");
|
||||||
webSocket.setUrl(url);
|
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 isn't 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.setHeartBeatPeriod(45);
|
||||||
|
|
||||||
// Per message deflate connection is enabled by default. You can tweak its parameters or disable it
|
|
||||||
webSocket.disablePerMessageDeflate();
|
|
||||||
|
|
||||||
// Setup a callback to be fired when a message or an event (open, close, error) is received
|
// Setup a callback to be fired when a message or an event (open, close, error) is received
|
||||||
webSocket.setOnMessageCallback(
|
webSocket.setOnMessageCallback(
|
||||||
[](const ix::WebSocketMessagePtr& msg)
|
[](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo,
|
||||||
|
const ix::WebSocketHttpHeaders& headers)
|
||||||
{
|
{
|
||||||
if (msg->type == ix::WebSocketMessageType::Message)
|
if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
{
|
{
|
||||||
std::cout << msg->str << std::endl;
|
std::cout << str << std::endl;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Now that our callback is setup, we can start our background thread and receive messages
|
// Now that our callback is setup, we can start our background thread and receive messages
|
||||||
webSocket.start();
|
webSocket.start();
|
||||||
|
|
||||||
// Send a message to the server (default to TEXT mode)
|
// Send a message to the server
|
||||||
webSocket.send("hello world");
|
webSocket.send("hello world");
|
||||||
|
|
||||||
// The message can be sent in BINARY mode (useful if you send MsgPack data for example)
|
|
||||||
webSocket.sendBinary("some serialized binary data");
|
|
||||||
|
|
||||||
// ... finally ...
|
// ... finally ...
|
||||||
|
|
||||||
// Stop the connection
|
// Stop the connection
|
||||||
@ -64,38 +64,31 @@ Here is what the server API looks like. Note that server support is very recent
|
|||||||
ix::WebSocketServer server(port);
|
ix::WebSocketServer server(port);
|
||||||
|
|
||||||
server.setOnConnectionCallback(
|
server.setOnConnectionCallback(
|
||||||
[&server](std::shared_ptr<WebSocket> webSocket,
|
[&server](std::shared_ptr<ix::WebSocket> webSocket)
|
||||||
std::shared_ptr<ConnectionState> connectionState)
|
|
||||||
{
|
{
|
||||||
webSocket->setOnMessageCallback(
|
webSocket->setOnMessageCallback(
|
||||||
[webSocket, connectionState, &server](const ix::WebSocketMessagePtr msg)
|
[webSocket, &server](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketOpenInfo& openInfo,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo)
|
||||||
{
|
{
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
{
|
{
|
||||||
std::cerr << "New connection" << std::endl;
|
std::cerr << "New connection" << std::endl;
|
||||||
|
std::cerr << "Uri: " << openInfo.uri << std::endl;
|
||||||
// A connection state object is available, and has a default id
|
|
||||||
// You can subclass ConnectionState and pass an alternate factory
|
|
||||||
// to override it. It is useful if you want to store custom
|
|
||||||
// attributes per connection (authenticated bool flag, attributes, etc...)
|
|
||||||
std::cerr << "id: " << connectionState->getId() << std::endl;
|
|
||||||
|
|
||||||
// The uri the client did connect to.
|
|
||||||
std::cerr << "Uri: " << msg->openInfo.uri << std::endl;
|
|
||||||
|
|
||||||
std::cerr << "Headers:" << std::endl;
|
std::cerr << "Headers:" << std::endl;
|
||||||
for (auto it : msg->openInfo.headers)
|
for (auto it : openInfo.headers)
|
||||||
{
|
{
|
||||||
std::cerr << it.first << ": " << it.second << std::endl;
|
std::cerr << it.first << ": " << it.second << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Message)
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
{
|
{
|
||||||
// For an echo server, we just send back to the client whatever was received by the server
|
// For an echo server, we just send back to the client whatever was received by the server
|
||||||
// All connected clients are available in an std::set. See the broadcast cpp example.
|
// All connected clients are available in an std::set. See the broadcast cpp example.
|
||||||
// Second parameter tells whether we are sending the message in binary or text mode.
|
webSocket->send(str);
|
||||||
// Here we send it in the same mode as it was received.
|
|
||||||
webSocket->send(msg->str, msg->binary);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -117,122 +110,11 @@ server.wait();
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Here is what the HTTP client API looks like. Note that HTTP client support is very recent and subject to changes.
|
|
||||||
|
|
||||||
```
|
|
||||||
//
|
|
||||||
// Preparation
|
|
||||||
//
|
|
||||||
HttpClient httpClient;
|
|
||||||
HttpRequestArgsPtr args = httpClient.createRequest();
|
|
||||||
|
|
||||||
// Custom headers can be set
|
|
||||||
WebSocketHttpHeaders headers;
|
|
||||||
headers["Foo"] = "bar";
|
|
||||||
args->extraHeaders = headers;
|
|
||||||
|
|
||||||
// Timeout options
|
|
||||||
args->connectTimeout = connectTimeout;
|
|
||||||
args->transferTimeout = transferTimeout;
|
|
||||||
|
|
||||||
// Redirect options
|
|
||||||
args->followRedirects = followRedirects;
|
|
||||||
args->maxRedirects = maxRedirects;
|
|
||||||
|
|
||||||
// Misc
|
|
||||||
args->compress = compress; // Enable gzip compression
|
|
||||||
args->verbose = verbose;
|
|
||||||
args->logger = [](const std::string& msg)
|
|
||||||
{
|
|
||||||
std::cout << msg;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// Synchronous Request
|
|
||||||
//
|
|
||||||
HttpResponsePtr out;
|
|
||||||
std::string url = "https://www.google.com";
|
|
||||||
|
|
||||||
// HEAD request
|
|
||||||
out = httpClient.head(url, args);
|
|
||||||
|
|
||||||
// GET request
|
|
||||||
out = httpClient.get(url, args);
|
|
||||||
|
|
||||||
// POST request with parameters
|
|
||||||
HttpParameters httpParameters;
|
|
||||||
httpParameters["foo"] = "bar";
|
|
||||||
out = httpClient.post(url, httpParameters, args);
|
|
||||||
|
|
||||||
// POST request with a body
|
|
||||||
out = httpClient.post(url, std::string("foo=bar"), args);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Result
|
|
||||||
//
|
|
||||||
auto errorCode = response->errorCode; // Can be HttpErrorCode::Ok, HttpErrorCode::UrlMalformed, etc...
|
|
||||||
auto errorCode = response->errorCode; // 200, 404, etc...
|
|
||||||
auto responseHeaders = response->headers; // All the headers in a special case-insensitive unordered_map of (string, string)
|
|
||||||
auto payload = response->payload; // All the bytes from the response as an std::string
|
|
||||||
auto errorMsg = response->errorMsg; // Descriptive error message in case of failure
|
|
||||||
auto uploadSize = response->uploadSize; // Byte count of uploaded data
|
|
||||||
auto downloadSize = response->downloadSize; // Byte count of downloaded data
|
|
||||||
|
|
||||||
//
|
|
||||||
// Asynchronous Request
|
|
||||||
//
|
|
||||||
bool async = true;
|
|
||||||
HttpClient httpClient(async);
|
|
||||||
auto args = httpClient.createRequest(url, HttpClient::kGet);
|
|
||||||
|
|
||||||
// Push the request to a queue,
|
|
||||||
bool ok = httpClient.performRequest(args, [](const HttpResponsePtr& response)
|
|
||||||
{
|
|
||||||
// This callback execute in a background thread. Make sure you uses appropriate protection such as mutex
|
|
||||||
auto statusCode = response->statusCode; // acess results
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// ok will be false if your httpClient is not async
|
|
||||||
```
|
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
|
||||||
CMakefiles for the library and the examples are available. This library has few dependencies, so it is possible to just add the source files into your project. Otherwise the usual way will suffice.
|
CMakefiles for the library and the examples are available. This library has few dependencies, so it is possible to just add the source files into your project.
|
||||||
|
|
||||||
```
|
There is a Dockerfile for running some code on Linux, and a unittest which can be executed by typing `make test`.
|
||||||
mkdir build # make a build dir so that you can build out of tree.
|
|
||||||
cd build
|
|
||||||
cmake ..
|
|
||||||
make -j
|
|
||||||
make install # will install to /usr/local on Unix, on macOS it is a good idea to sudo chown -R `whoami`:staff /usr/local
|
|
||||||
```
|
|
||||||
|
|
||||||
Headers and a static library will be installed to the target dir.
|
|
||||||
|
|
||||||
A [conan](https://conan.io/) file is available at [conan-IXWebSocket](https://github.com/Zinnion/conan-IXWebSocket).
|
|
||||||
|
|
||||||
There is a unittest which can be executed by typing `make test`.
|
|
||||||
|
|
||||||
There is a Dockerfile for running some code on Linux. To use docker-compose you must make a docker container first.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ make docker
|
|
||||||
...
|
|
||||||
$ docker compose up &
|
|
||||||
...
|
|
||||||
$ docker exec -it ixwebsocket_ws_1 bash
|
|
||||||
app@ca2340eb9106:~$ ws --help
|
|
||||||
ws is a websocket tool
|
|
||||||
...
|
|
||||||
```
|
|
||||||
|
|
||||||
Finally you can build and install the `ws command line tool` with Homebrew. The homebrew version might be slightly out of date.
|
|
||||||
|
|
||||||
```
|
|
||||||
brew tap bsergean/IXWebSocket
|
|
||||||
brew install IXWebSocket
|
|
||||||
```
|
|
||||||
|
|
||||||
## Implementation details
|
## Implementation details
|
||||||
|
|
||||||
@ -242,7 +124,7 @@ The per message deflate compression option is supported. It can lead to very nic
|
|||||||
|
|
||||||
### TLS/SSL
|
### TLS/SSL
|
||||||
|
|
||||||
Connections can be optionally secured and encrypted with TLS/SSL when using a wss:// endpoint, or using normal un-encrypted socket with ws:// endpoints. AppleSSL is used on iOS and macOS, OpenSSL is used on Android and Linux, mbedTLS is used on 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, and OpenSSL is used on Android and Linux.
|
||||||
|
|
||||||
### Polling and background thread work
|
### Polling and background thread work
|
||||||
|
|
||||||
@ -250,35 +132,39 @@ No manual polling to fetch data is required. Data is sent and received instantly
|
|||||||
|
|
||||||
### Automatic reconnection
|
### Automatic reconnection
|
||||||
|
|
||||||
If the remote end (server) breaks the connection, the code will try to perpetually reconnect, by using an exponential backoff strategy, capped at one retry every 10 seconds. This behavior can be disabled.
|
If the remote end (server) breaks the connection, the code will try to perpetually reconnect, by using an exponential backoff strategy, capped at one retry every 10 seconds.
|
||||||
|
|
||||||
### Large messages
|
|
||||||
|
|
||||||
Large frames are broken up into smaller chunks or messages to avoid filling up the os tcp buffers, which is permitted thanks to WebSocket [fragmentation](https://tools.ietf.org/html/rfc6455#section-5.4). Messages up to 1G were sent and received succesfully.
|
|
||||||
|
|
||||||
## Limitations
|
## Limitations
|
||||||
|
|
||||||
* On Windows TLS is not setup yet to validate certificates.
|
* There is no text support for sending data, only the binary protocol is supported. Sending json or text over the binary protocol works well.
|
||||||
* There is no convenient way to embed a ca cert.
|
|
||||||
* No utf-8 validation is made when sending TEXT message with sendText()
|
|
||||||
* 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 isn't as scalable as strategies using epoll or kqueue.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
1. Bring up a terminal and jump to the examples folder.
|
||||||
|
2. Compile the example C++ code. `sh build.sh`
|
||||||
|
3. Install node.js from [here](https://nodejs.org/en/download/).
|
||||||
|
4. Type `npm install` to install the node.js dependencies. Then `node broadcast-server.js` to run the server.
|
||||||
|
5. Bring up a second terminal. `./cmd_websocket_chat bob`
|
||||||
|
6. Bring up a third terminal. `./cmd_websocket_chat bill`
|
||||||
|
7. Start typing things in any of those terminals. Hopefully you should see your message being received on the other end.
|
||||||
|
|
||||||
## C++ code organization
|
## C++ code organization
|
||||||
|
|
||||||
Here is a simplistic diagram which explains how the code is structured in term of class/modules.
|
Here's a simplistic diagram which explains how the code is structured in term of class/modules.
|
||||||
|
|
||||||
```
|
```
|
||||||
+-----------------------+ --- Public
|
+-----------------------+ --- Public
|
||||||
| | Start the receiving Background thread. Auto reconnection. Simple websocket Ping.
|
| | Start the receiving Background thread. Auto reconnection. Simple websocket Ping.
|
||||||
| IXWebSocket | Interface used by C++ test clients. No IX dependencies.
|
| IXWebSocket | Interface used by C++ test clients. No IX dependencies.
|
||||||
| |
|
| |
|
||||||
+-----------------------+
|
+-----------------------+
|
||||||
| |
|
| |
|
||||||
| IXWebSocketServer | Run a server and give each connections its own WebSocket object.
|
| IXWebSocketServer | Run a server and give each connections its own WebSocket object.
|
||||||
| | Each connection is handled in a new OS thread.
|
| | Each connection is handled in a new OS thread.
|
||||||
| |
|
| |
|
||||||
+-----------------------+ --- Private
|
+-----------------------+ --- Private
|
||||||
| |
|
| |
|
||||||
| IXWebSocketTransport | Low level websocket code, framing, managing raw socket. Adapted from easywsclient.
|
| IXWebSocketTransport | Low level websocket code, framing, managing raw socket. Adapted from easywsclient.
|
||||||
| |
|
| |
|
||||||
@ -315,10 +201,10 @@ If the connection was closed and sending failed, the return value will be set to
|
|||||||
|
|
||||||
`getReadyState()` returns the state of the connection. There are 4 possible states.
|
`getReadyState()` returns the state of the connection. There are 4 possible states.
|
||||||
|
|
||||||
1. ReadyState::Connecting - The connection is not yet open.
|
1. WebSocket_ReadyState_Connecting - The connection is not yet open.
|
||||||
2. ReadyState::Open - The connection is open and ready to communicate.
|
2. WebSocket_ReadyState_Open - The connection is open and ready to communicate.
|
||||||
3. ReadyState::Closing - The connection is in the process of closing.
|
3. WebSocket_ReadyState_Closing - The connection is in the process of closing.
|
||||||
4. ReadyState::Closed - The connection is closed or could not be opened.
|
4. WebSocket_MessageType_Close - The connection is closed or couldn't be opened.
|
||||||
|
|
||||||
### Open and Close notifications
|
### Open and Close notifications
|
||||||
|
|
||||||
@ -326,27 +212,32 @@ The onMessage event will be fired when the connection is opened or closed. This
|
|||||||
|
|
||||||
```
|
```
|
||||||
webSocket.setOnMessageCallback(
|
webSocket.setOnMessageCallback(
|
||||||
[](const ix::WebSocketMessagePtr& msg)
|
[](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo,
|
||||||
|
const ix::WebSocketHttpHeaders& headers)
|
||||||
{
|
{
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
{
|
{
|
||||||
std::cout << "send greetings" << std::endl;
|
std::cout << "send greetings" << std::endl;
|
||||||
|
|
||||||
// Headers can be inspected (pairs of string/string)
|
// Headers can be inspected (pairs of string/string)
|
||||||
std::cout << "Handshake Headers:" << std::endl;
|
std::cout << "Handshake Headers:" << std::endl;
|
||||||
for (auto it : msg->headers)
|
for (auto it : headers)
|
||||||
{
|
{
|
||||||
std::cout << it.first << ": " << it.second << std::endl;
|
std::cout << it.first << ": " << it.second << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Close)
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
{
|
{
|
||||||
std::cout << "disconnected" << std::endl;
|
std::cout << "disconnected" << std::endl;
|
||||||
|
|
||||||
// The server can send an explicit code and reason for closing.
|
// The server can send an explicit code and reason for closing.
|
||||||
// This data can be accessed through the closeInfo object.
|
// This data can be accessed through the closeInfo object.
|
||||||
std::cout << msg->closeInfo.code << std::endl;
|
std::cout << closeInfo.code << std::endl;
|
||||||
std::cout << msg->closeInfo.reason << std::endl;
|
std::cout << closeInfo.reason << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -354,19 +245,24 @@ webSocket.setOnMessageCallback(
|
|||||||
|
|
||||||
### Error notification
|
### Error notification
|
||||||
|
|
||||||
A message will be fired when there is an error with the connection. The message type will be `ix::WebSocketMessageType::Error`. Multiple fields will be available on the event to describe the error.
|
A message will be fired when there is an error with the connection. The message type will be `ix::WebSocket_MessageType_Error`. Multiple fields will be available on the event to describe the error.
|
||||||
|
|
||||||
```
|
```
|
||||||
webSocket.setOnMessageCallback(
|
webSocket.setOnMessageCallback(
|
||||||
[](const ix::WebSocketMessagePtr& msg)
|
[](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo,
|
||||||
|
const ix::WebSocketHttpHeaders& headers)
|
||||||
{
|
{
|
||||||
if (msg->type == ix::WebSocketMessageType::Error)
|
if (messageType == ix::WebSocket_MessageType_Error)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "Error: " << msg->errorInfo.reason << std::endl;
|
ss << "Error: " << error.reason << std::endl;
|
||||||
ss << "#retries: " << msg->eventInfo.retries << std::endl;
|
ss << "#retries: " << event.retries << std::endl;
|
||||||
ss << "Wait time(ms): " << msg->eventInfo.wait_time << std::endl;
|
ss << "Wait time(ms): " << event.wait_time << std::endl;
|
||||||
ss << "HTTP Status: " << msg->eventInfo.http_status << std::endl;
|
ss << "HTTP Status: " << event.http_status << std::endl;
|
||||||
std::cout << ss.str() << std::endl;
|
std::cout << ss.str() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -393,12 +289,17 @@ Ping/pong messages are used to implement keep-alive. 2 message types exists to i
|
|||||||
|
|
||||||
```
|
```
|
||||||
webSocket.setOnMessageCallback(
|
webSocket.setOnMessageCallback(
|
||||||
[](const ix::WebSocketMessagePtr& msg)
|
[](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo,
|
||||||
|
const ix::WebSocketHttpHeaders& headers)
|
||||||
{
|
{
|
||||||
if (msg->type == ix::WebSocketMessageType::Ping ||
|
if (messageType == ix::WebSocket_MessageType_Ping ||
|
||||||
msg->type == ix::WebSocketMessageType::Pong)
|
messageType == ix::WebSocket_MessageType_Pong)
|
||||||
{
|
{
|
||||||
std::cout << "pong data: " << msg->str << std::endl;
|
std::cout << "pong data: " << str << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -408,12 +309,11 @@ A ping message can be sent to the server, with an optional data string.
|
|||||||
|
|
||||||
```
|
```
|
||||||
websocket.ping("ping data, optional (empty string is ok): limited to 125 bytes long");
|
websocket.ping("ping data, optional (empty string is ok): limited to 125 bytes long");
|
||||||
```
|
|
||||||
|
|
||||||
### Heartbeat.
|
### Heartbeat.
|
||||||
|
|
||||||
You can configure an optional heart beat / keep-alive, sent every 45 seconds
|
You can configure an optional heart beat / keep-alive, sent every 45 seconds
|
||||||
when there is no any traffic to make sure that load balancers do not kill an
|
when there isn't any traffic to make sure that load balancers do not kill an
|
||||||
idle connection.
|
idle connection.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -1,14 +1,10 @@
|
|||||||
image:
|
image:
|
||||||
- Visual Studio 2017
|
- Visual Studio 2017
|
||||||
|
- Ubuntu
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- ls -al
|
- ls -al
|
||||||
- cmd: call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
|
- cmd: call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
|
||||||
- cd test
|
- python test/run.py
|
||||||
- mkdir build
|
|
||||||
- cd build
|
|
||||||
- cmake -G"NMake Makefiles" ..
|
|
||||||
- nmake
|
|
||||||
- ixwebsocket_unittest.exe
|
|
||||||
|
|
||||||
build: off
|
build: off
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
version: "3"
|
|
||||||
services:
|
|
||||||
snake:
|
|
||||||
image: bsergean/ws:build
|
|
||||||
entrypoint: ws snake --port 8765 --host 0.0.0.0 --redis_hosts redis1
|
|
||||||
ports:
|
|
||||||
- "8765:8765"
|
|
||||||
networks:
|
|
||||||
- ws-net
|
|
||||||
depends_on:
|
|
||||||
- redis1
|
|
||||||
|
|
||||||
ws:
|
|
||||||
security_opt:
|
|
||||||
- seccomp:unconfined
|
|
||||||
cap_add:
|
|
||||||
- SYS_PTRACE
|
|
||||||
stdin_open: true
|
|
||||||
tty: true
|
|
||||||
image: bsergean/ws:build
|
|
||||||
entrypoint: bash
|
|
||||||
networks:
|
|
||||||
- ws-net
|
|
||||||
depends_on:
|
|
||||||
- redis1
|
|
||||||
|
|
||||||
redis1:
|
|
||||||
image: redis:alpine
|
|
||||||
networks:
|
|
||||||
- ws-net
|
|
||||||
|
|
||||||
statsd:
|
|
||||||
image: jaconel/statsd
|
|
||||||
ports:
|
|
||||||
- "8125:8125"
|
|
||||||
environment:
|
|
||||||
- STATSD_DUMP_MSG=true
|
|
||||||
- GRAPHITE_HOST=127.0.0.1
|
|
||||||
networks:
|
|
||||||
- ws-net
|
|
||||||
|
|
||||||
networks:
|
|
||||||
ws-net:
|
|
16
docker/Dockerfile
Normal file
16
docker/Dockerfile
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
FROM debian:stretch
|
||||||
|
|
||||||
|
# RUN yum install -y gcc-c++ make cmake openssl-devel gdb
|
||||||
|
ENV DEBIAN_FRONTEND noninteractive
|
||||||
|
RUN apt-get update
|
||||||
|
RUN apt-get -y install g++
|
||||||
|
RUN apt-get -y install libssl-dev
|
||||||
|
RUN apt-get -y install gdb
|
||||||
|
RUN apt-get -y install screen
|
||||||
|
RUN apt-get -y install procps
|
||||||
|
RUN apt-get -y install lsof
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
WORKDIR examples/ws_connect
|
||||||
|
RUN ["sh", "build_linux.sh"]
|
@ -1,33 +1,11 @@
|
|||||||
FROM alpine as build
|
FROM alpine:3.8
|
||||||
|
|
||||||
RUN apk add --no-cache gcc g++ musl-dev linux-headers cmake openssl-dev
|
RUN apk add --no-cache g++ musl-dev make cmake openssl-dev
|
||||||
RUN apk add --no-cache make
|
|
||||||
RUN apk add --no-cache zlib-dev
|
|
||||||
|
|
||||||
RUN addgroup -S app && adduser -S -G app app
|
COPY . .
|
||||||
RUN chown -R app:app /opt
|
|
||||||
RUN chown -R app:app /usr/local
|
|
||||||
|
|
||||||
# There is a bug in CMake where we cannot build from the root top folder
|
WORKDIR examples/ws_connect
|
||||||
# So we build from /opt
|
RUN ["sh", "build_linux.sh"]
|
||||||
COPY --chown=app:app . /opt
|
|
||||||
WORKDIR /opt
|
|
||||||
|
|
||||||
USER app
|
EXPOSE 8765
|
||||||
RUN [ "make" ]
|
CMD ["ws_connect"]
|
||||||
|
|
||||||
FROM alpine as runtime
|
|
||||||
|
|
||||||
RUN apk add --no-cache libstdc++
|
|
||||||
|
|
||||||
RUN addgroup -S app && adduser -S -G app app
|
|
||||||
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
|
|
||||||
|
|
||||||
# Now run in usermode
|
|
||||||
USER app
|
|
||||||
WORKDIR /home/app
|
|
||||||
|
|
||||||
ENTRYPOINT ["ws"]
|
|
||||||
CMD ["--help"]
|
|
||||||
|
11
docker/Dockerfile.centos
Normal file
11
docker/Dockerfile.centos
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
FROM alpine:3.8
|
||||||
|
|
||||||
|
RUN apk add --no-cache g++ musl-dev make cmake openssl-dev
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
WORKDIR examples/ws_connect
|
||||||
|
RUN ["sh", "build_linux.sh"]
|
||||||
|
|
||||||
|
EXPOSE 8765
|
||||||
|
CMD ["ws_connect"]
|
@ -1,52 +1,19 @@
|
|||||||
# Build time
|
FROM debian:stretch
|
||||||
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 mkdir -p /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 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 gdb
|
||||||
|
RUN apt-get -y install screen
|
||||||
|
RUN apt-get -y install procps
|
||||||
|
RUN apt-get -y install lsof
|
||||||
RUN apt-get -y install libz-dev
|
RUN apt-get -y install libz-dev
|
||||||
|
RUN apt-get -y install vim
|
||||||
RUN apt-get -y install make
|
RUN apt-get -y install make
|
||||||
|
RUN apt-get -y install cmake
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
ARG CMAKE_BIN_PATH=/tmp/cmake/cmake-3.14.0-Linux-x86_64/bin
|
WORKDIR test
|
||||||
ENV PATH="${CMAKE_BIN_PATH}:${PATH}"
|
RUN ["sh", "build_linux.sh"]
|
||||||
|
|
||||||
RUN ["make"]
|
|
||||||
|
|
||||||
# Runtime
|
|
||||||
FROM debian:buster as runtime
|
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
|
||||||
RUN apt-get update
|
|
||||||
# Runtime
|
|
||||||
RUN apt-get install -y libssl1.1
|
|
||||||
RUN apt-get install -y ca-certificates
|
|
||||||
RUN ["update-ca-certificates"]
|
|
||||||
|
|
||||||
# Debugging
|
|
||||||
RUN apt-get install -y strace
|
|
||||||
RUN apt-get install -y procps
|
|
||||||
RUN apt-get install -y htop
|
|
||||||
|
|
||||||
RUN adduser --disabled-password --gecos '' app
|
|
||||||
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
|
|
||||||
|
|
||||||
# Now run in usermode
|
|
||||||
USER app
|
|
||||||
WORKDIR /home/app
|
|
||||||
|
|
||||||
COPY --chown=app:app ws/snake/appsConfig.json .
|
|
||||||
COPY --chown=app:app ws/cobraMetricsSample.json .
|
|
||||||
|
|
||||||
ENTRYPOINT ["ws"]
|
|
||||||
CMD ["--help"]
|
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
FROM fedora:30 as build
|
|
||||||
|
|
||||||
RUN yum install -y gcc-g++
|
|
||||||
RUN yum install -y cmake
|
|
||||||
RUN yum install -y make
|
|
||||||
RUN yum install -y openssl-devel
|
|
||||||
|
|
||||||
RUN yum install -y wget
|
|
||||||
RUN mkdir -p /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 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
|
|
||||||
ENV PATH="${CMAKE_BIN_PATH}:${PATH}"
|
|
||||||
|
|
||||||
RUN yum install -y python
|
|
||||||
RUN yum install -y libtsan
|
|
||||||
RUN yum install -y zlib-devel
|
|
||||||
|
|
||||||
COPY . .
|
|
||||||
# RUN ["make", "test"]
|
|
||||||
RUN ["make"]
|
|
||||||
|
|
||||||
# Runtime
|
|
||||||
FROM fedora:30 as runtime
|
|
||||||
|
|
||||||
RUN yum install -y libtsan
|
|
||||||
|
|
||||||
RUN groupadd app && useradd -g app app
|
|
||||||
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
|
|
||||||
|
|
||||||
# Now run in usermode
|
|
||||||
USER app
|
|
||||||
WORKDIR /home/app
|
|
||||||
|
|
||||||
COPY --chown=app:app ws/snake/appsConfig.json .
|
|
||||||
COPY --chown=app:app ws/cobraMetricsSample.json .
|
|
||||||
|
|
||||||
ENTRYPOINT ["ws"]
|
|
||||||
CMD ["--help"]
|
|
8
docker/Dockerfile.gcc
Normal file
8
docker/Dockerfile.gcc
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
FROM gcc:8
|
||||||
|
|
||||||
|
# RUN yum install -y gcc-c++ make cmake openssl-devel gdb
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
WORKDIR examples/ws_connect
|
||||||
|
RUN ["sh", "build_linux.sh"]
|
@ -1,23 +0,0 @@
|
|||||||
# Build time
|
|
||||||
FROM ubuntu:bionic as build
|
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
|
||||||
RUN apt-get update
|
|
||||||
RUN apt-get -y install wget
|
|
||||||
RUN mkdir -p /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 tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
|
||||||
|
|
||||||
RUN apt-get -y install g++
|
|
||||||
RUN apt-get -y install libssl-dev
|
|
||||||
RUN apt-get -y install libz-dev
|
|
||||||
RUN apt-get -y install make
|
|
||||||
RUN apt-get -y install python
|
|
||||||
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
ARG CMAKE_BIN_PATH=/tmp/cmake/cmake-3.14.0-Linux-x86_64/bin
|
|
||||||
ENV PATH="${CMAKE_BIN_PATH}:${PATH}"
|
|
||||||
|
|
||||||
RUN ["make", "ws"]
|
|
@ -1,24 +0,0 @@
|
|||||||
# Build time
|
|
||||||
FROM ubuntu:xenial as build
|
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
|
||||||
RUN apt-get update
|
|
||||||
RUN apt-get -y install wget
|
|
||||||
RUN mkdir -p /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 tar zxf cmake-3.14.0-Linux-x86_64.tar.gz
|
|
||||||
|
|
||||||
RUN apt-get -y install g++
|
|
||||||
RUN apt-get -y install libssl-dev
|
|
||||||
RUN apt-get -y install libz-dev
|
|
||||||
RUN apt-get -y install make
|
|
||||||
RUN apt-get -y install python
|
|
||||||
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
ARG CMAKE_BIN_PATH=/tmp/cmake/cmake-3.14.0-Linux-x86_64/bin
|
|
||||||
ENV PATH="${CMAKE_BIN_PATH}:${PATH}"
|
|
||||||
|
|
||||||
# RUN ["make"]
|
|
||||||
RUN ["make", "test"]
|
|
9
examples/broadcast_server/.gitignore
vendored
Normal file
9
examples/broadcast_server/.gitignore
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
CMakeCache.txt
|
||||||
|
package-lock.json
|
||||||
|
CMakeFiles
|
||||||
|
ixwebsocket_unittest
|
||||||
|
cmake_install.cmake
|
||||||
|
node_modules
|
||||||
|
ixwebsocket
|
||||||
|
Makefile
|
||||||
|
build
|
30
examples/broadcast_server/CMakeLists.txt
Normal file
30
examples/broadcast_server/CMakeLists.txt
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
cmake_minimum_required (VERSION 3.4.1)
|
||||||
|
project (broadcast_server)
|
||||||
|
|
||||||
|
# There's -Weverything too for clang
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32")
|
||||||
|
|
||||||
|
set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
|
option(USE_TLS "Add TLS support" ON)
|
||||||
|
|
||||||
|
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
|
||||||
|
|
||||||
|
include_directories(broadcast_server .)
|
||||||
|
|
||||||
|
add_executable(broadcast_server
|
||||||
|
broadcast_server.cpp)
|
||||||
|
|
||||||
|
if (APPLE AND USE_TLS)
|
||||||
|
target_link_libraries(broadcast_server "-framework foundation" "-framework security")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(broadcast_server ixwebsocket)
|
||||||
|
install(TARGETS broadcast_server DESTINATION bin)
|
74
examples/broadcast_server/broadcast_server.cpp
Normal file
74
examples/broadcast_server/broadcast_server.cpp
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* broadcast_server.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <ixwebsocket/IXWebSocketServer.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
int port = 8080;
|
||||||
|
if (argc == 2)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << argv[1];
|
||||||
|
ss >> port;
|
||||||
|
}
|
||||||
|
|
||||||
|
ix::WebSocketServer server(port);
|
||||||
|
|
||||||
|
server.setOnConnectionCallback(
|
||||||
|
[&server](std::shared_ptr<ix::WebSocket> webSocket)
|
||||||
|
{
|
||||||
|
webSocket->setOnMessageCallback(
|
||||||
|
[webSocket, &server](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketOpenInfo& openInfo,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo)
|
||||||
|
{
|
||||||
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
|
{
|
||||||
|
std::cerr << "New connection" << std::endl;
|
||||||
|
std::cerr << "Uri: " << openInfo.uri << std::endl;
|
||||||
|
std::cerr << "Headers:" << std::endl;
|
||||||
|
for (auto it : openInfo.headers)
|
||||||
|
{
|
||||||
|
std::cerr << it.first << ": " << it.second << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
|
{
|
||||||
|
std::cerr << "Closed connection" << std::endl;
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
|
{
|
||||||
|
for (auto&& client : server.getClients())
|
||||||
|
{
|
||||||
|
if (client != webSocket)
|
||||||
|
{
|
||||||
|
client->send(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
auto res = server.listen();
|
||||||
|
if (!res.first)
|
||||||
|
{
|
||||||
|
std::cerr << res.second << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
server.wait();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
1
ws/.gitignore → examples/chat/.gitignore
vendored
1
ws/.gitignore → examples/chat/.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
build
|
build
|
||||||
|
venv
|
||||||
node_modules
|
node_modules
|
23
examples/chat/CMakeLists.txt
Normal file
23
examples/chat/CMakeLists.txt
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#
|
||||||
|
# cmd_websocket_chat.cpp
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
cmake_minimum_required (VERSION 3.4.1)
|
||||||
|
project (cmd_websocket_chat)
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
|
option(USE_TLS "Add TLS support" ON)
|
||||||
|
|
||||||
|
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
|
||||||
|
|
||||||
|
add_executable(cmd_websocket_chat cmd_websocket_chat.cpp)
|
||||||
|
|
||||||
|
if (APPLE AND USE_TLS)
|
||||||
|
target_link_libraries(cmd_websocket_chat "-framework foundation" "-framework security")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(cmd_websocket_chat ixwebsocket)
|
||||||
|
install(TARGETS cmd_websocket_chat DESTINATION bin)
|
39
examples/chat/README.md
Normal file
39
examples/chat/README.md
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
# Building
|
||||||
|
|
||||||
|
1. cmake -G .
|
||||||
|
2. make
|
||||||
|
|
||||||
|
## Disable TLS
|
||||||
|
|
||||||
|
chat$ cmake -DUSE_TLS=OFF .
|
||||||
|
-- Configuring done
|
||||||
|
-- Generating done
|
||||||
|
-- Build files have been written to: /Users/bsergeant/src/foss/ixwebsocket/examples/chat
|
||||||
|
chat$ make
|
||||||
|
Scanning dependencies of target ixwebsocket
|
||||||
|
[ 16%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocket.cpp.o
|
||||||
|
[ 33%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocket.cpp.o
|
||||||
|
[ 50%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocketTransport.cpp.o
|
||||||
|
[ 66%] Linking CXX static library libixwebsocket.a
|
||||||
|
[ 66%] Built target ixwebsocket
|
||||||
|
[ 83%] Linking CXX executable cmd_websocket_chat
|
||||||
|
[100%] Built target cmd_websocket_chat
|
||||||
|
|
||||||
|
## Enable TLS (default)
|
||||||
|
|
||||||
|
```
|
||||||
|
chat$ cmake -DUSE_TLS=ON .
|
||||||
|
-- Configuring done
|
||||||
|
-- Generating done
|
||||||
|
-- Build files have been written to: /Users/bsergeant/src/foss/ixwebsocket/examples/chat
|
||||||
|
(venv) chat$ make
|
||||||
|
Scanning dependencies of target ixwebsocket
|
||||||
|
[ 14%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocket.cpp.o
|
||||||
|
[ 28%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocket.cpp.o
|
||||||
|
[ 42%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocketTransport.cpp.o
|
||||||
|
[ 57%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocketAppleSSL.cpp.o
|
||||||
|
[ 71%] Linking CXX static library libixwebsocket.a
|
||||||
|
[ 71%] Built target ixwebsocket
|
||||||
|
[ 85%] Linking CXX executable cmd_websocket_chat
|
||||||
|
[100%] Built target cmd_websocket_chat
|
||||||
|
```
|
15
examples/chat/build_linux.sh
Normal file
15
examples/chat/build_linux.sh
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
# 'manual' way of building. You can also use cmake.
|
||||||
|
|
||||||
|
g++ --std=c++11 \
|
||||||
|
../../ixwebsocket/IXSocket.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocketTransport.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocket.cpp \
|
||||||
|
-I ../.. \
|
||||||
|
cmd_websocket_chat.cpp \
|
||||||
|
-o cmd_websocket_chat
|
17
examples/chat/build_macos.sh
Normal file
17
examples/chat/build_macos.sh
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
# 'manual' way of building. You can also use cmake.
|
||||||
|
|
||||||
|
clang++ --std=c++11 --stdlib=libc++ \
|
||||||
|
../../ixwebsocket/IXSocket.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocketTransport.cpp \
|
||||||
|
../../ixwebsocket/IXSocketAppleSSL.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocket.cpp \
|
||||||
|
cmd_websocket_chat.cpp \
|
||||||
|
-o cmd_websocket_chat \
|
||||||
|
-framework Security \
|
||||||
|
-framework Foundation
|
@ -1,12 +1,12 @@
|
|||||||
/*
|
/*
|
||||||
* ws_chat.cpp
|
* cmd_websocket_chat.cpp
|
||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//
|
//
|
||||||
// Simple chat program that talks to a broadcast server
|
// Simple chat program that talks to the node.js server at
|
||||||
// Broadcast server can be ran with `ws broadcast_server`
|
// websocket_chat_server/broacast-server.js
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -20,13 +20,19 @@
|
|||||||
// for convenience
|
// for convenience
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
namespace ix
|
using namespace ix;
|
||||||
|
|
||||||
|
namespace
|
||||||
{
|
{
|
||||||
|
void log(const std::string& msg)
|
||||||
|
{
|
||||||
|
std::cout << msg << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
class WebSocketChat
|
class WebSocketChat
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WebSocketChat(const std::string& url,
|
WebSocketChat(const std::string& user);
|
||||||
const std::string& user);
|
|
||||||
|
|
||||||
void subscribe(const std::string& channel);
|
void subscribe(const std::string& channel);
|
||||||
void start();
|
void start();
|
||||||
@ -40,27 +46,19 @@ namespace ix
|
|||||||
std::pair<std::string, std::string> decodeMessage(const std::string& str);
|
std::pair<std::string, std::string> decodeMessage(const std::string& str);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _url;
|
|
||||||
std::string _user;
|
std::string _user;
|
||||||
ix::WebSocket _webSocket;
|
|
||||||
std::queue<std::string> _receivedQueue;
|
|
||||||
|
|
||||||
void log(const std::string& msg);
|
ix::WebSocket _webSocket;
|
||||||
|
|
||||||
|
std::queue<std::string> _receivedQueue;
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocketChat::WebSocketChat(const std::string& url,
|
WebSocketChat::WebSocketChat(const std::string& user) :
|
||||||
const std::string& user) :
|
|
||||||
_url(url),
|
|
||||||
_user(user)
|
_user(user)
|
||||||
{
|
{
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketChat::log(const std::string& msg)
|
|
||||||
{
|
|
||||||
std::cout << msg << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t WebSocketChat::getReceivedMessagesCount() const
|
size_t WebSocketChat::getReceivedMessagesCount() const
|
||||||
{
|
{
|
||||||
return _receivedQueue.size();
|
return _receivedQueue.size();
|
||||||
@ -68,7 +66,7 @@ namespace ix
|
|||||||
|
|
||||||
bool WebSocketChat::isReady() const
|
bool WebSocketChat::isReady() const
|
||||||
{
|
{
|
||||||
return _webSocket.getReadyState() == ix::ReadyState::Open;
|
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Open;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketChat::stop()
|
void WebSocketChat::stop()
|
||||||
@ -78,42 +76,38 @@ namespace ix
|
|||||||
|
|
||||||
void WebSocketChat::start()
|
void WebSocketChat::start()
|
||||||
{
|
{
|
||||||
_webSocket.setUrl(_url);
|
std::string url("ws://localhost:8080/");
|
||||||
|
_webSocket.setUrl(url);
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
log(std::string("Connecting to url: ") + _url);
|
log(std::string("Connecting to url: ") + url);
|
||||||
|
|
||||||
_webSocket.setOnMessageCallback(
|
_webSocket.setOnMessageCallback(
|
||||||
[this](const WebSocketMessagePtr& msg)
|
[this](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketOpenInfo& openInfo,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
{
|
{
|
||||||
log("ws chat: connected");
|
ss << "cmd_websocket_chat: user "
|
||||||
std::cout << "Uri: " << msg->openInfo.uri << std::endl;
|
|
||||||
std::cout << "Handshake Headers:" << std::endl;
|
|
||||||
for (auto it : msg->openInfo.headers)
|
|
||||||
{
|
|
||||||
std::cout << it.first << ": " << it.second << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
ss << "ws chat: user "
|
|
||||||
<< _user
|
<< _user
|
||||||
<< " Connected !";
|
<< " Connected !";
|
||||||
log(ss.str());
|
log(ss.str());
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Close)
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
{
|
{
|
||||||
ss << "ws chat: user "
|
ss << "cmd_websocket_chat: user "
|
||||||
<< _user
|
<< _user
|
||||||
<< " disconnected !"
|
<< " disconnected !";
|
||||||
<< " code " << msg->closeInfo.code
|
|
||||||
<< " reason " << msg->closeInfo.reason;
|
|
||||||
log(ss.str());
|
log(ss.str());
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Message)
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
{
|
{
|
||||||
auto result = decodeMessage(msg->str);
|
auto result = decodeMessage(str);
|
||||||
|
|
||||||
// Our "chat" / "broacast" node.js server does not send us
|
// Our "chat" / "broacast" node.js server does not send us
|
||||||
// the messages we send, so we don't have to filter it out.
|
// the messages we send, so we don't have to filter it out.
|
||||||
@ -121,18 +115,18 @@ namespace ix
|
|||||||
// store text
|
// store text
|
||||||
_receivedQueue.push(result.second);
|
_receivedQueue.push(result.second);
|
||||||
|
|
||||||
ss << std::endl
|
ss << std::endl
|
||||||
<< result.first << "(" << msg->wireSize << " bytes)" << " > " << result.second
|
<< result.first << " > " << result.second
|
||||||
<< std::endl
|
<< std::endl
|
||||||
<< _user << " > ";
|
<< _user << " > ";
|
||||||
log(ss.str());
|
log(ss.str());
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Error)
|
else if (messageType == ix::WebSocket_MessageType_Error)
|
||||||
{
|
{
|
||||||
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
ss << "Connection error: " << error.reason << std::endl;
|
||||||
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
ss << "#retries: " << error.retries << std::endl;
|
||||||
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
ss << "Wait time(ms): " << error.wait_time << std::endl;
|
||||||
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
ss << "HTTP Status: " << error.http_status << std::endl;
|
||||||
log(ss.str());
|
log(ss.str());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -167,14 +161,13 @@ namespace ix
|
|||||||
|
|
||||||
void WebSocketChat::sendMessage(const std::string& text)
|
void WebSocketChat::sendMessage(const std::string& text)
|
||||||
{
|
{
|
||||||
_webSocket.sendText(encodeMessage(text));
|
_webSocket.send(encodeMessage(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
int ws_chat_main(const std::string& url,
|
void interactiveMain(const std::string& user)
|
||||||
const std::string& user)
|
|
||||||
{
|
{
|
||||||
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
||||||
WebSocketChat webSocketChat(url, user);
|
WebSocketChat webSocketChat(user);
|
||||||
webSocketChat.start();
|
webSocketChat.start();
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
@ -193,7 +186,18 @@ namespace ix
|
|||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
webSocketChat.stop();
|
webSocketChat.stop();
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
std::string user("user");
|
||||||
|
if (argc == 2)
|
||||||
|
{
|
||||||
|
user = argv[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
Socket::init();
|
||||||
|
interactiveMain(user);
|
||||||
|
return 0;
|
||||||
|
}
|
31
examples/chat/package-lock.json
generated
Normal file
31
examples/chat/package-lock.json
generated
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"requires": true,
|
||||||
|
"lockfileVersion": 1,
|
||||||
|
"dependencies": {
|
||||||
|
"async-limiter": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg=="
|
||||||
|
},
|
||||||
|
"safe-buffer": {
|
||||||
|
"version": "5.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
||||||
|
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
|
||||||
|
},
|
||||||
|
"ultron": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz",
|
||||||
|
"integrity": "sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og=="
|
||||||
|
},
|
||||||
|
"ws": {
|
||||||
|
"version": "3.3.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/ws/-/ws-3.3.3.tgz",
|
||||||
|
"integrity": "sha512-nnWLa/NwZSt4KQJu51MYlCcSQ5g7INpOrOMt4XV8j4dqTXdmlUmSHQ8/oLC069ckre0fRsgfvsKwbTdtKLCDkA==",
|
||||||
|
"requires": {
|
||||||
|
"async-limiter": "1.0.0",
|
||||||
|
"safe-buffer": "5.1.2",
|
||||||
|
"ultron": "1.1.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
examples/chat/package.json
Normal file
6
examples/chat/package.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"msgpack-js": "^0.3.0",
|
||||||
|
"ws": "^3.3.3"
|
||||||
|
}
|
||||||
|
}
|
3
examples/cobra_publisher/.gitignore
vendored
Normal file
3
examples/cobra_publisher/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
venv
|
||||||
|
build
|
||||||
|
node_modules
|
40
examples/cobra_publisher/CMakeLists.txt
Normal file
40
examples/cobra_publisher/CMakeLists.txt
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
cmake_minimum_required (VERSION 3.4.1)
|
||||||
|
project (cobra_publisher)
|
||||||
|
|
||||||
|
# There's -Weverything too for clang
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32")
|
||||||
|
|
||||||
|
set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
|
option(USE_TLS "Add TLS support" ON)
|
||||||
|
|
||||||
|
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
|
||||||
|
|
||||||
|
include_directories(cobra_publisher ${OPENSSL_PREFIX}/include)
|
||||||
|
include_directories(cobra_publisher .)
|
||||||
|
|
||||||
|
add_executable(cobra_publisher
|
||||||
|
jsoncpp/jsoncpp.cpp
|
||||||
|
ixcrypto/IXHMac.cpp
|
||||||
|
ixcrypto/IXBase64.cpp
|
||||||
|
IXCobraConnection.cpp
|
||||||
|
cobra_publisher.cpp)
|
||||||
|
|
||||||
|
if (APPLE AND USE_TLS)
|
||||||
|
target_link_libraries(cobra_publisher "-framework foundation" "-framework security")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
get_filename_component(crypto_lib_path ${OPENSSL_PREFIX}/lib/libcrypto.a ABSOLUTE)
|
||||||
|
add_library(lib_crypto STATIC IMPORTED)
|
||||||
|
set_target_properties(lib_crypto PROPERTIES IMPORTED_LOCATION ${crypto_lib_path})
|
||||||
|
|
||||||
|
link_directories(/usr/local/opt/openssl/lib)
|
||||||
|
target_link_libraries(cobra_publisher ixwebsocket lib_crypto)
|
||||||
|
install(TARGETS cobra_publisher DESTINATION bin)
|
@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
#include "IXCobraConnection.h"
|
#include "IXCobraConnection.h"
|
||||||
#include <ixcrypto/IXHMac.h>
|
#include <ixcrypto/IXHMac.h>
|
||||||
#include <ixwebsocket/IXWebSocket.h>
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -21,10 +20,9 @@ namespace ix
|
|||||||
constexpr size_t CobraConnection::kQueueMaxSize;
|
constexpr size_t CobraConnection::kQueueMaxSize;
|
||||||
|
|
||||||
CobraConnection::CobraConnection() :
|
CobraConnection::CobraConnection() :
|
||||||
_webSocket(new WebSocket()),
|
|
||||||
_publishMode(CobraConnection_PublishMode_Immediate),
|
|
||||||
_authenticated(false),
|
_authenticated(false),
|
||||||
_eventCallback(nullptr)
|
_eventCallback(nullptr),
|
||||||
|
_publishMode(CobraConnection_PublishMode_Immediate)
|
||||||
{
|
{
|
||||||
_pdu["action"] = "rtm/publish";
|
_pdu["action"] = "rtm/publish";
|
||||||
|
|
||||||
@ -34,7 +32,6 @@ namespace ix
|
|||||||
CobraConnection::~CobraConnection()
|
CobraConnection::~CobraConnection()
|
||||||
{
|
{
|
||||||
disconnect();
|
disconnect();
|
||||||
setEventCallback(nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraConnection::setTrafficTrackerCallback(const TrafficTrackerCallback& callback)
|
void CobraConnection::setTrafficTrackerCallback(const TrafficTrackerCallback& callback)
|
||||||
@ -63,68 +60,69 @@ namespace ix
|
|||||||
|
|
||||||
void CobraConnection::invokeEventCallback(ix::CobraConnectionEventType eventType,
|
void CobraConnection::invokeEventCallback(ix::CobraConnectionEventType eventType,
|
||||||
const std::string& errorMsg,
|
const std::string& errorMsg,
|
||||||
const WebSocketHttpHeaders& headers,
|
const WebSocketHttpHeaders& headers)
|
||||||
const std::string& subscriptionId)
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_eventCallbackMutex);
|
std::lock_guard<std::mutex> lock(_eventCallbackMutex);
|
||||||
if (_eventCallback)
|
if (_eventCallback)
|
||||||
{
|
{
|
||||||
_eventCallback(eventType, errorMsg, headers, subscriptionId);
|
_eventCallback(eventType, errorMsg, headers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraConnection::invokeErrorCallback(const std::string& errorMsg,
|
void CobraConnection::invokeErrorCallback(const std::string& errorMsg)
|
||||||
const std::string& serializedPdu)
|
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
invokeEventCallback(ix::CobraConnection_EventType_Error, errorMsg);
|
||||||
ss << errorMsg << " : received pdu => " << serializedPdu;
|
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Error, ss.str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraConnection::disconnect()
|
void CobraConnection::disconnect()
|
||||||
{
|
{
|
||||||
_authenticated = false;
|
_authenticated = false;
|
||||||
_webSocket->stop();
|
_webSocket.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CobraConnection::initWebSocketOnMessageCallback()
|
void CobraConnection::initWebSocketOnMessageCallback()
|
||||||
{
|
{
|
||||||
_webSocket->setOnMessageCallback(
|
_webSocket.setOnMessageCallback(
|
||||||
[this](const ix::WebSocketMessagePtr& msg)
|
[this](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo,
|
||||||
|
const ix::WebSocketHttpHeaders& headers)
|
||||||
{
|
{
|
||||||
CobraConnection::invokeTrafficTrackerCallback(msg->wireSize, true);
|
CobraConnection::invokeTrafficTrackerCallback(wireSize, true);
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
{
|
{
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Open,
|
invokeEventCallback(ix::CobraConnection_EventType_Open,
|
||||||
std::string(),
|
std::string(),
|
||||||
msg->openInfo.headers);
|
headers);
|
||||||
sendHandshakeMessage();
|
sendHandshakeMessage();
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Close)
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
{
|
{
|
||||||
_authenticated = false;
|
_authenticated = false;
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "Close code " << msg->closeInfo.code;
|
ss << "Close code " << closeInfo.code;
|
||||||
ss << " reason " << msg->closeInfo.reason;
|
ss << " reason " << closeInfo.reason;
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Closed,
|
invokeEventCallback(ix::CobraConnection_EventType_Closed,
|
||||||
ss.str());
|
ss.str());
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Message)
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
{
|
{
|
||||||
Json::Value data;
|
Json::Value data;
|
||||||
Json::Reader reader;
|
Json::Reader reader;
|
||||||
if (!reader.parse(msg->str, data))
|
if (!reader.parse(str, data))
|
||||||
{
|
{
|
||||||
invokeErrorCallback("Invalid json", msg->str);
|
invokeErrorCallback(std::string("Invalid json: ") + str);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.isMember("action"))
|
if (!data.isMember("action"))
|
||||||
{
|
{
|
||||||
invokeErrorCallback("Missing action", msg->str);
|
invokeErrorCallback("Missing action");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,12 +132,12 @@ namespace ix
|
|||||||
{
|
{
|
||||||
if (!handleHandshakeResponse(data))
|
if (!handleHandshakeResponse(data))
|
||||||
{
|
{
|
||||||
invokeErrorCallback("Error extracting nonce from handshake response", msg->str);
|
invokeErrorCallback("Error extracting nonce from handshake response");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (action == "auth/handshake/error")
|
else if (action == "auth/handshake/error")
|
||||||
{
|
{
|
||||||
invokeErrorCallback("Handshake error", msg->str);
|
invokeErrorCallback("Handshake error."); // print full message ?
|
||||||
}
|
}
|
||||||
else if (action == "auth/authenticate/ok")
|
else if (action == "auth/authenticate/ok")
|
||||||
{
|
{
|
||||||
@ -149,47 +147,25 @@ namespace ix
|
|||||||
}
|
}
|
||||||
else if (action == "auth/authenticate/error")
|
else if (action == "auth/authenticate/error")
|
||||||
{
|
{
|
||||||
invokeErrorCallback("Authentication error", msg->str);
|
invokeErrorCallback("Authentication error."); // print full message ?
|
||||||
}
|
}
|
||||||
else if (action == "rtm/subscription/data")
|
else if (action == "rtm/subscription/data")
|
||||||
{
|
{
|
||||||
handleSubscriptionData(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 subscribe response", msg->str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (action == "rtm/unsubscribe/error")
|
|
||||||
{
|
|
||||||
invokeErrorCallback("Unsubscription error", msg->str);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
invokeErrorCallback("Un-handled message type", msg->str);
|
invokeErrorCallback(std::string("Un-handled message type: ") + action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Error)
|
else if (messageType == ix::WebSocket_MessageType_Error)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
ss << "Connection error: " << error.reason << std::endl;
|
||||||
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
ss << "#retries: " << error.retries << std::endl;
|
||||||
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
ss << "Wait time(ms): " << error.wait_time << std::endl;
|
||||||
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
ss << "HTTP Status: " << error.http_status << std::endl;
|
||||||
invokeErrorCallback(ss.str(), std::string());
|
invokeErrorCallback(ss.str());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -200,22 +176,24 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CobraConnection::configure(const std::string& appkey,
|
void CobraConnection::configure(const std::string& appkey,
|
||||||
const std::string& endpoint,
|
const std::string& endpoint,
|
||||||
const std::string& rolename,
|
const std::string& rolename,
|
||||||
const std::string& rolesecret,
|
const std::string& rolesecret,
|
||||||
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions)
|
WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions)
|
||||||
{
|
{
|
||||||
_roleName = rolename;
|
_appkey = appkey;
|
||||||
_roleSecret = rolesecret;
|
_endpoint = endpoint;
|
||||||
|
_role_name = rolename;
|
||||||
|
_role_secret = rolesecret;
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << endpoint;
|
ss << _endpoint;
|
||||||
ss << "/v2?appkey=";
|
ss << "/v2?appkey=";
|
||||||
ss << appkey;
|
ss << _appkey;
|
||||||
|
|
||||||
std::string url = ss.str();
|
std::string url = ss.str();
|
||||||
_webSocket->setUrl(url);
|
_webSocket.setUrl(url);
|
||||||
_webSocket->setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions);
|
_webSocket.setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -235,7 +213,7 @@ namespace ix
|
|||||||
bool CobraConnection::sendHandshakeMessage()
|
bool CobraConnection::sendHandshakeMessage()
|
||||||
{
|
{
|
||||||
Json::Value data;
|
Json::Value data;
|
||||||
data["role"] = _roleName;
|
data["role"] = _role_name;
|
||||||
|
|
||||||
Json::Value body;
|
Json::Value body;
|
||||||
body["data"] = data;
|
body["data"] = data;
|
||||||
@ -248,10 +226,10 @@ namespace ix
|
|||||||
std::string serializedJson = serializeJson(pdu);
|
std::string serializedJson = serializeJson(pdu);
|
||||||
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
||||||
|
|
||||||
return _webSocket->send(serializedJson).success;
|
return _webSocket.send(serializedJson).success;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Extract the nonce from the handshake response
|
// Extract the nonce from the handshake response
|
||||||
// use it to compute a hash during authentication
|
// use it to compute a hash during authentication
|
||||||
//
|
//
|
||||||
@ -297,7 +275,7 @@ namespace ix
|
|||||||
bool CobraConnection::sendAuthMessage(const std::string& nonce)
|
bool CobraConnection::sendAuthMessage(const std::string& nonce)
|
||||||
{
|
{
|
||||||
Json::Value credentials;
|
Json::Value credentials;
|
||||||
credentials["hash"] = hmac(nonce, _roleSecret);
|
credentials["hash"] = hmac(nonce, _role_secret);
|
||||||
|
|
||||||
Json::Value body;
|
Json::Value body;
|
||||||
body["credentials"] = credentials;
|
body["credentials"] = credentials;
|
||||||
@ -310,47 +288,16 @@ namespace ix
|
|||||||
std::string serializedJson = serializeJson(pdu);
|
std::string serializedJson = serializeJson(pdu);
|
||||||
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
||||||
|
|
||||||
return _webSocket->send(serializedJson).success;
|
return _webSocket.send(serializedJson).success;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CobraConnection::handleSubscriptionResponse(const Json::Value& pdu)
|
|
||||||
{
|
|
||||||
if (!pdu.isMember("body")) return false;
|
|
||||||
Json::Value body = pdu["body"];
|
|
||||||
|
|
||||||
if (!body.isMember("subscription_id")) return false;
|
|
||||||
Json::Value subscriptionId = body["subscription_id"];
|
|
||||||
|
|
||||||
if (!subscriptionId.isString()) return false;
|
|
||||||
|
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_Subscribed,
|
|
||||||
std::string(), WebSocketHttpHeaders(),
|
|
||||||
subscriptionId.asString());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CobraConnection::handleUnsubscriptionResponse(const Json::Value& pdu)
|
|
||||||
{
|
|
||||||
if (!pdu.isMember("body")) return false;
|
|
||||||
Json::Value body = pdu["body"];
|
|
||||||
|
|
||||||
if (!body.isMember("subscription_id")) return false;
|
|
||||||
Json::Value subscriptionId = body["subscription_id"];
|
|
||||||
|
|
||||||
if (!subscriptionId.isString()) return false;
|
|
||||||
|
|
||||||
invokeEventCallback(ix::CobraConnection_EventType_UnSubscribed,
|
|
||||||
std::string(), WebSocketHttpHeaders(),
|
|
||||||
subscriptionId.asString());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CobraConnection::handleSubscriptionData(const Json::Value& pdu)
|
bool CobraConnection::handleSubscriptionData(const Json::Value& pdu)
|
||||||
{
|
{
|
||||||
if (!pdu.isMember("body")) return false;
|
if (!pdu.isMember("body")) return false;
|
||||||
Json::Value body = pdu["body"];
|
Json::Value body = pdu["body"];
|
||||||
|
|
||||||
// Identify subscription_id, so that we can find
|
// Identify subscription_id, so that we can find
|
||||||
// which callback to execute
|
// which callback to execute
|
||||||
if (!body.isMember("subscription_id")) return false;
|
if (!body.isMember("subscription_id")) return false;
|
||||||
Json::Value subscriptionId = body["subscription_id"];
|
Json::Value subscriptionId = body["subscription_id"];
|
||||||
@ -373,18 +320,13 @@ namespace ix
|
|||||||
|
|
||||||
bool CobraConnection::connect()
|
bool CobraConnection::connect()
|
||||||
{
|
{
|
||||||
_webSocket->start();
|
_webSocket.start();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CobraConnection::isConnected() const
|
bool CobraConnection::isConnected() const
|
||||||
{
|
{
|
||||||
return _webSocket->getReadyState() == ix::ReadyState::Open;
|
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Open;
|
||||||
}
|
|
||||||
|
|
||||||
bool CobraConnection::isAuthenticated() const
|
|
||||||
{
|
|
||||||
return isConnected() && _authenticated;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string CobraConnection::serializeJson(const Json::Value& value)
|
std::string CobraConnection::serializeJson(const Json::Value& value)
|
||||||
@ -439,7 +381,7 @@ namespace ix
|
|||||||
pdu["action"] = "rtm/subscribe";
|
pdu["action"] = "rtm/subscribe";
|
||||||
pdu["body"] = body;
|
pdu["body"] = body;
|
||||||
|
|
||||||
_webSocket->send(pdu.toStyledString());
|
_webSocket.send(pdu.toStyledString());
|
||||||
|
|
||||||
// Set the callback
|
// Set the callback
|
||||||
std::lock_guard<std::mutex> lock(_cbsMutex);
|
std::lock_guard<std::mutex> lock(_cbsMutex);
|
||||||
@ -458,13 +400,13 @@ namespace ix
|
|||||||
|
|
||||||
// Create and send an unsubscribe pdu
|
// Create and send an unsubscribe pdu
|
||||||
Json::Value body;
|
Json::Value body;
|
||||||
body["subscription_id"] = channel;
|
body["channel"] = channel;
|
||||||
|
|
||||||
Json::Value pdu;
|
Json::Value pdu;
|
||||||
pdu["action"] = "rtm/unsubscribe";
|
pdu["action"] = "rtm/unsubscribe";
|
||||||
pdu["body"] = body;
|
pdu["body"] = body;
|
||||||
|
|
||||||
_webSocket->send(pdu.toStyledString());
|
_webSocket.send(pdu.toStyledString());
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -514,7 +456,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;
|
||||||
@ -529,5 +471,5 @@ namespace ix
|
|||||||
{
|
{
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ix
|
} // namespace ix
|
@ -6,28 +6,24 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
|
|
||||||
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
|
|
||||||
#include <jsoncpp/json/json.h>
|
|
||||||
#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 <jsoncpp/json/json.h>
|
||||||
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
|
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class WebSocket;
|
|
||||||
|
|
||||||
enum CobraConnectionEventType
|
enum CobraConnectionEventType
|
||||||
{
|
{
|
||||||
CobraConnection_EventType_Authenticated = 0,
|
CobraConnection_EventType_Authenticated = 0,
|
||||||
CobraConnection_EventType_Error = 1,
|
CobraConnection_EventType_Error = 1,
|
||||||
CobraConnection_EventType_Open = 2,
|
CobraConnection_EventType_Open = 2,
|
||||||
CobraConnection_EventType_Closed = 3,
|
CobraConnection_EventType_Closed = 3
|
||||||
CobraConnection_EventType_Subscribed = 4,
|
|
||||||
CobraConnection_EventType_UnSubscribed = 5
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum CobraConnectionPublishMode
|
enum CobraConnectionPublishMode
|
||||||
@ -39,8 +35,7 @@ namespace ix
|
|||||||
using SubscriptionCallback = std::function<void(const Json::Value&)>;
|
using SubscriptionCallback = std::function<void(const Json::Value&)>;
|
||||||
using EventCallback = std::function<void(CobraConnectionEventType,
|
using EventCallback = std::function<void(CobraConnectionEventType,
|
||||||
const std::string&,
|
const std::string&,
|
||||||
const WebSocketHttpHeaders&,
|
const WebSocketHttpHeaders&)>;
|
||||||
const std::string&)>;
|
|
||||||
using TrafficTrackerCallback = std::function<void(size_t size, bool incoming)>;
|
using TrafficTrackerCallback = std::function<void(size_t size, bool incoming)>;
|
||||||
|
|
||||||
class CobraConnection
|
class CobraConnection
|
||||||
@ -55,7 +50,7 @@ namespace ix
|
|||||||
const std::string& endpoint,
|
const std::string& endpoint,
|
||||||
const std::string& rolename,
|
const std::string& rolename,
|
||||||
const std::string& rolesecret,
|
const std::string& rolesecret,
|
||||||
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions);
|
WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions);
|
||||||
|
|
||||||
static void setTrafficTrackerCallback(const TrafficTrackerCallback& callback);
|
static void setTrafficTrackerCallback(const TrafficTrackerCallback& callback);
|
||||||
|
|
||||||
@ -71,7 +66,8 @@ namespace ix
|
|||||||
/// Publish a message to a channel
|
/// Publish a message to a channel
|
||||||
///
|
///
|
||||||
/// No-op if the connection is not established
|
/// No-op if the connection is not established
|
||||||
bool publish(const Json::Value& channels, const Json::Value& msg);
|
bool publish(const Json::Value& channels,
|
||||||
|
const Json::Value& msg);
|
||||||
|
|
||||||
// Subscribe to a channel, and execute a callback when an incoming
|
// Subscribe to a channel, and execute a callback when an incoming
|
||||||
// message arrives.
|
// message arrives.
|
||||||
@ -88,10 +84,7 @@ namespace ix
|
|||||||
|
|
||||||
/// Returns true only if we're connected
|
/// Returns true only if we're connected
|
||||||
bool isConnected() const;
|
bool isConnected() const;
|
||||||
|
|
||||||
/// Returns true only if we're authenticated
|
|
||||||
bool isAuthenticated() const;
|
|
||||||
|
|
||||||
/// Flush the publish queue
|
/// Flush the publish queue
|
||||||
bool flushQueue();
|
bool flushQueue();
|
||||||
|
|
||||||
@ -107,8 +100,6 @@ namespace ix
|
|||||||
bool handleHandshakeResponse(const Json::Value& data);
|
bool handleHandshakeResponse(const Json::Value& data);
|
||||||
bool sendAuthMessage(const std::string& nonce);
|
bool sendAuthMessage(const std::string& nonce);
|
||||||
bool handleSubscriptionData(const Json::Value& pdu);
|
bool handleSubscriptionData(const Json::Value& pdu);
|
||||||
bool handleSubscriptionResponse(const Json::Value& pdu);
|
|
||||||
bool handleUnsubscriptionResponse(const Json::Value& pdu);
|
|
||||||
|
|
||||||
void initWebSocketOnMessageCallback();
|
void initWebSocketOnMessageCallback();
|
||||||
|
|
||||||
@ -122,18 +113,19 @@ namespace ix
|
|||||||
/// Invoke event callbacks
|
/// Invoke event callbacks
|
||||||
void invokeEventCallback(CobraConnectionEventType eventType,
|
void invokeEventCallback(CobraConnectionEventType eventType,
|
||||||
const std::string& errorMsg = std::string(),
|
const std::string& errorMsg = std::string(),
|
||||||
const WebSocketHttpHeaders& headers = WebSocketHttpHeaders(),
|
const WebSocketHttpHeaders& headers = WebSocketHttpHeaders());
|
||||||
const std::string& subscriptionId = std::string());
|
void invokeErrorCallback(const std::string& errorMsg);
|
||||||
void invokeErrorCallback(const std::string& errorMsg, const std::string& serializedPdu);
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Member variables
|
/// Member variables
|
||||||
///
|
///
|
||||||
std::unique_ptr<WebSocket> _webSocket;
|
WebSocket _webSocket;
|
||||||
|
|
||||||
/// Configuration data
|
/// Configuration data
|
||||||
std::string _roleName;
|
std::string _appkey;
|
||||||
std::string _roleSecret;
|
std::string _endpoint;
|
||||||
|
std::string _role_name;
|
||||||
|
std::string _role_secret;
|
||||||
std::atomic<CobraConnectionPublishMode> _publishMode;
|
std::atomic<CobraConnectionPublishMode> _publishMode;
|
||||||
|
|
||||||
// Can be set on control+background thread, protecting with an atomic
|
// Can be set on control+background thread, protecting with an atomic
|
||||||
@ -156,10 +148,10 @@ namespace ix
|
|||||||
std::unordered_map<std::string, SubscriptionCallback> _cbs;
|
std::unordered_map<std::string, SubscriptionCallback> _cbs;
|
||||||
mutable std::mutex _cbsMutex;
|
mutable std::mutex _cbsMutex;
|
||||||
|
|
||||||
// Message Queue can be touched on control+background thread,
|
// Message Queue can be touched on control+background thread,
|
||||||
// protecting with a mutex.
|
// protecting with a mutex.
|
||||||
//
|
//
|
||||||
// Message queue is used when there are problems sending messages so
|
// Message queue is used when there are problems sending messages so
|
||||||
// that sending can be retried later.
|
// that sending can be retried later.
|
||||||
std::deque<std::string> _messageQueue;
|
std::deque<std::string> _messageQueue;
|
||||||
mutable std::mutex _queueMutex;
|
mutable std::mutex _queueMutex;
|
||||||
@ -167,5 +159,5 @@ namespace ix
|
|||||||
// Cap the queue size (100 elems so far -> ~100k)
|
// Cap the queue size (100 elems so far -> ~100k)
|
||||||
static constexpr size_t kQueueMaxSize = 256;
|
static constexpr size_t kQueueMaxSize = 256;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ix
|
} // namespace ix
|
6
examples/cobra_publisher/README.md
Normal file
6
examples/cobra_publisher/README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
```
|
||||||
|
mkdir build
|
||||||
|
cd build
|
||||||
|
cmake ..
|
||||||
|
make && (cd .. ; sh cobra_publisher.sh)
|
||||||
|
```
|
123
examples/cobra_publisher/cobra_publisher.cpp
Normal file
123
examples/cobra_publisher/cobra_publisher.cpp
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* cobra_publisher.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <atomic>
|
||||||
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
|
#include "IXCobraConnection.h"
|
||||||
|
#include "jsoncpp/json/json.h"
|
||||||
|
|
||||||
|
void msleep(int ms)
|
||||||
|
{
|
||||||
|
std::chrono::duration<double, std::milli> duration(ms);
|
||||||
|
std::this_thread::sleep_for(duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
if (argc != 7)
|
||||||
|
{
|
||||||
|
std::cerr << "Usage error: need 6 arguments." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string endpoint = argv[1];
|
||||||
|
std::string appkey = argv[2];
|
||||||
|
std::string channel = argv[3];
|
||||||
|
std::string rolename = argv[4];
|
||||||
|
std::string rolesecret = argv[5];
|
||||||
|
std::string path = argv[6];
|
||||||
|
|
||||||
|
std::atomic<size_t> incomingBytes(0);
|
||||||
|
std::atomic<size_t> outgoingBytes(0);
|
||||||
|
ix::CobraConnection::setTrafficTrackerCallback(
|
||||||
|
[&incomingBytes, &outgoingBytes](size_t size, bool incoming)
|
||||||
|
{
|
||||||
|
if (incoming)
|
||||||
|
{
|
||||||
|
incomingBytes += size;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outgoingBytes += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
bool done = false;
|
||||||
|
ix::CobraConnection cobraConnection;
|
||||||
|
ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions(
|
||||||
|
true, false, false, 15, 15);
|
||||||
|
cobraConnection.configure(appkey, endpoint, rolename, rolesecret,
|
||||||
|
webSocketPerMessageDeflateOptions);
|
||||||
|
cobraConnection.connect();
|
||||||
|
cobraConnection.setEventCallback(
|
||||||
|
[&cobraConnection, channel, path, &done]
|
||||||
|
(ix::CobraConnectionEventType eventType,
|
||||||
|
const std::string& errMsg,
|
||||||
|
const ix::WebSocketHttpHeaders& headers)
|
||||||
|
{
|
||||||
|
if (eventType == ix::CobraConnection_EventType_Open)
|
||||||
|
{
|
||||||
|
std::cout << "Handshake Headers:" << std::endl;
|
||||||
|
for (auto it : headers)
|
||||||
|
{
|
||||||
|
std::cout << it.first << ": " << it.second << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (eventType == ix::CobraConnection_EventType_Authenticated)
|
||||||
|
{
|
||||||
|
std::cout << "Authenticated" << std::endl;
|
||||||
|
|
||||||
|
std::string line;
|
||||||
|
std::ifstream f(path);
|
||||||
|
if (!f.is_open())
|
||||||
|
{
|
||||||
|
std::cerr << "Error while opening file: " << path << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = 0;
|
||||||
|
while (getline(f, line))
|
||||||
|
{
|
||||||
|
Json::Value value;
|
||||||
|
Json::Reader reader;
|
||||||
|
reader.parse(line, value);
|
||||||
|
|
||||||
|
cobraConnection.publish(channel, value);
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
std::cerr << "#published messages: " << n << std::endl;
|
||||||
|
|
||||||
|
if (f.bad())
|
||||||
|
{
|
||||||
|
std::cerr << "Error while opening file: " << path << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
else if (eventType == ix::CobraConnection_EventType_Error)
|
||||||
|
{
|
||||||
|
std::cerr << "Cobra Error received: " << errMsg << std::endl;
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
else if (eventType == ix::CobraConnection_EventType_Closed)
|
||||||
|
{
|
||||||
|
std::cerr << "Cobra connection closed" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
while (!done)
|
||||||
|
{
|
||||||
|
msleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Incoming bytes: " << incomingBytes << std::endl;
|
||||||
|
std::cout << "Outgoing bytes: " << outgoingBytes << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
11
examples/cobra_publisher/cobra_publisher.sh
Normal file
11
examples/cobra_publisher/cobra_publisher.sh
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
endpoint="ws://127.0.0.1:8765"
|
||||||
|
endpoint="ws://127.0.0.1:5678"
|
||||||
|
appkey="appkey"
|
||||||
|
channel="foo"
|
||||||
|
rolename="a_role"
|
||||||
|
rolesecret="a_secret"
|
||||||
|
filename=${FILENAME:=events.jsonl}
|
||||||
|
|
||||||
|
build/cobra_publisher $endpoint $appkey $channel $rolename $rolesecret $filename
|
45
examples/cobra_publisher/devnull_server.js
Normal file
45
examples/cobra_publisher/devnull_server.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* devnull_server.js
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
const WebSocket = require('ws');
|
||||||
|
|
||||||
|
let wss = new WebSocket.Server({ port: 5678, perMessageDeflate: true })
|
||||||
|
|
||||||
|
wss.on('connection', (ws) => {
|
||||||
|
|
||||||
|
let handshake = false
|
||||||
|
let authenticated = false
|
||||||
|
|
||||||
|
ws.on('message', (data) => {
|
||||||
|
|
||||||
|
console.log(data.toString('utf-8'))
|
||||||
|
|
||||||
|
if (!handshake) {
|
||||||
|
let response = {
|
||||||
|
"action": "auth/handshake/ok",
|
||||||
|
"body": {
|
||||||
|
"data": {
|
||||||
|
"nonce": "MTI0Njg4NTAyMjYxMzgxMzgzMg==",
|
||||||
|
"version": "0.0.24"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": 1
|
||||||
|
}
|
||||||
|
ws.send(JSON.stringify(response))
|
||||||
|
handshake = true
|
||||||
|
} else if (!authenticated) {
|
||||||
|
let response = {
|
||||||
|
"action": "auth/authenticate/ok",
|
||||||
|
"body": {},
|
||||||
|
"id": 2
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.send(JSON.stringify(response))
|
||||||
|
authenticated = true
|
||||||
|
} else {
|
||||||
|
console.log(data)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
43
examples/cobra_publisher/devnull_server.py
Normal file
43
examples/cobra_publisher/devnull_server.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
import asyncio
|
||||||
|
import websockets
|
||||||
|
|
||||||
|
|
||||||
|
async def echo(websocket, path):
|
||||||
|
handshake = False
|
||||||
|
authenticated = False
|
||||||
|
|
||||||
|
async for message in websocket:
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
if not handshake:
|
||||||
|
response = {
|
||||||
|
"action": "auth/handshake/ok",
|
||||||
|
"body": {
|
||||||
|
"data": {
|
||||||
|
"nonce": "MTI0Njg4NTAyMjYxMzgxMzgzMg==",
|
||||||
|
"version": "0.0.24"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": 1
|
||||||
|
}
|
||||||
|
await websocket.send(json.dumps(response))
|
||||||
|
handshake = True
|
||||||
|
|
||||||
|
elif not authenticated:
|
||||||
|
response = {
|
||||||
|
"action": "auth/authenticate/ok",
|
||||||
|
"body": {},
|
||||||
|
"id": 2
|
||||||
|
}
|
||||||
|
|
||||||
|
await websocket.send(json.dumps(response))
|
||||||
|
authenticated = True
|
||||||
|
|
||||||
|
|
||||||
|
asyncio.get_event_loop().run_until_complete(
|
||||||
|
websockets.serve(echo, 'localhost', 5678))
|
||||||
|
asyncio.get_event_loop().run_forever()
|
3
examples/cobra_publisher/events.jsonl
Normal file
3
examples/cobra_publisher/events.jsonl
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{"array":[1,2,3],"boolean":true,"color":"#82b92c","null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Foo"}
|
||||||
|
{"array":[1,2,3],"boolean":true,"color":"#82b92c","null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Bar"}
|
||||||
|
{"array":[1,2,3],"boolean":true,"color":"#82b92c","null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Baz"}
|
@ -1,39 +1,39 @@
|
|||||||
/*
|
/*
|
||||||
base64.cpp and base64.h
|
base64.cpp and base64.h
|
||||||
|
|
||||||
Copyright (C) 2004-2008 René Nyffenegger
|
Copyright (C) 2004-2008 René Nyffenegger
|
||||||
|
|
||||||
This source code is provided 'as-is', without any express or implied
|
This source code is provided 'as-is', without any express or implied
|
||||||
warranty. In no event will the author be held liable for any damages
|
warranty. In no event will the author be held liable for any damages
|
||||||
arising from the use of this software.
|
arising from the use of this software.
|
||||||
|
|
||||||
Permission is granted to anyone to use this software for any purpose,
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
including commercial applications, and to alter it and redistribute it
|
including commercial applications, and to alter it and redistribute it
|
||||||
freely, subject to the following restrictions:
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
1. The origin of this source code must not be misrepresented; you must not
|
1. The origin of this source code must not be misrepresented; you must not
|
||||||
claim that you wrote the original source code. If you use this source code
|
claim that you wrote the original source code. If you use this source code
|
||||||
in a product, an acknowledgment in the product documentation would be
|
in a product, an acknowledgment in the product documentation would be
|
||||||
appreciated but is not required.
|
appreciated but is not required.
|
||||||
|
|
||||||
2. Altered source versions must be plainly marked as such, and must not be
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
misrepresented as being the original source code.
|
misrepresented as being the original source code.
|
||||||
|
|
||||||
3. This notice may not be removed or altered from any source distribution.
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
|
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "IXBase64.h"
|
#include "IXBase64.h"
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
std::string ret;
|
std::string ret;
|
||||||
@ -41,9 +41,9 @@ namespace ix
|
|||||||
int j = 0;
|
int j = 0;
|
||||||
unsigned char char_array_3[3];
|
unsigned char char_array_3[3];
|
||||||
unsigned char char_array_4[4];
|
unsigned char char_array_4[4];
|
||||||
|
|
||||||
const char* bytes_to_encode = data.c_str();
|
const char* bytes_to_encode = data.c_str();
|
||||||
|
|
||||||
while(len--)
|
while(len--)
|
||||||
{
|
{
|
||||||
char_array_3[i++] = *(bytes_to_encode++);
|
char_array_3[i++] = *(bytes_to_encode++);
|
||||||
@ -53,83 +53,32 @@ namespace ix
|
|||||||
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;
|
||||||
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(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool is_base64(unsigned char c)
|
|
||||||
{
|
|
||||||
return (isalnum(c) || (c == '+') || (c == '/'));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string base64_decode(const std::string& encoded_string)
|
|
||||||
{
|
|
||||||
int in_len = (int)encoded_string.size();
|
|
||||||
int i = 0;
|
|
||||||
int j = 0;
|
|
||||||
int in_ = 0;
|
|
||||||
unsigned char char_array_4[4], char_array_3[3];
|
|
||||||
std::string ret;
|
|
||||||
|
|
||||||
while(in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
|
|
||||||
{
|
|
||||||
char_array_4[i++] = encoded_string[in_]; in_++;
|
|
||||||
if(i ==4)
|
|
||||||
{
|
|
||||||
for(i = 0; i <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[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];
|
|
||||||
|
|
||||||
for(i = 0; (i < 3); i++)
|
|
||||||
ret += char_array_3[i];
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(i)
|
|
||||||
{
|
|
||||||
for(j = i; j <4; j++)
|
|
||||||
char_array_4[j] = 0;
|
|
||||||
|
|
||||||
for(j = 0; j <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[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];
|
|
||||||
|
|
||||||
for(j = 0; (j < i - 1); j++) ret += char_array_3[j];
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,5 +11,4 @@
|
|||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
std::string base64_encode(const std::string& data, size_t len);
|
std::string base64_encode(const std::string& data, size_t len);
|
||||||
std::string base64_decode(const std::string& encoded_string);
|
}
|
||||||
} // namespace ix
|
|
@ -3,17 +3,10 @@
|
|||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2018 Machine Zone. All rights reserved.
|
* Copyright (c) 2018 Machine Zone. All rights reserved.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "IXHMac.h"
|
#include "IXHMac.h"
|
||||||
#include "IXBase64.h"
|
#include "IXBase64.h"
|
||||||
|
|
||||||
#if defined(IXWEBSOCKET_USE_MBED_TLS)
|
#include <openssl/hmac.h>
|
||||||
# include <mbedtls/md.h>
|
|
||||||
#elif defined(__APPLE__)
|
|
||||||
# include <CommonCrypto/CommonHMAC.h>
|
|
||||||
#else
|
|
||||||
# include <openssl/hmac.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
@ -22,22 +15,10 @@ namespace ix
|
|||||||
constexpr size_t hashSize = 16;
|
constexpr size_t hashSize = 16;
|
||||||
unsigned char hash[hashSize];
|
unsigned char hash[hashSize];
|
||||||
|
|
||||||
#if defined(IXWEBSOCKET_USE_MBED_TLS)
|
|
||||||
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_MD5),
|
|
||||||
(unsigned char *) key.c_str(), key.size(),
|
|
||||||
(unsigned char *) data.c_str(), data.size(),
|
|
||||||
(unsigned char *) &hash);
|
|
||||||
#elif defined(__APPLE__)
|
|
||||||
CCHmac(kCCHmacAlgMD5,
|
|
||||||
key.c_str(), key.size(),
|
|
||||||
data.c_str(), data.size(),
|
|
||||||
&hash);
|
|
||||||
#else
|
|
||||||
HMAC(EVP_md5(),
|
HMAC(EVP_md5(),
|
||||||
key.c_str(), (int) key.size(),
|
key.c_str(), (int) key.size(),
|
||||||
(unsigned char *) data.c_str(), (int) data.size(),
|
(unsigned char *) data.c_str(), (int) data.size(),
|
||||||
(unsigned char *) hash, nullptr);
|
(unsigned char *) hash, nullptr);
|
||||||
#endif
|
|
||||||
|
|
||||||
std::string hashString(reinterpret_cast<char*>(hash), hashSize);
|
std::string hashString(reinterpret_cast<char*>(hash), hashSize);
|
||||||
|
|
@ -11,13 +11,13 @@ The JsonCpp library's source code, including accompanying documentation,
|
|||||||
tests and demonstration applications, are licensed under the following
|
tests and demonstration applications, are licensed under the following
|
||||||
conditions...
|
conditions...
|
||||||
|
|
||||||
The author (Baptiste Lepilleur) explicitly disclaims copyright in all
|
Baptiste Lepilleur and The JsonCpp Authors explicitly disclaim copyright in all
|
||||||
jurisdictions which recognize such a disclaimer. In such jurisdictions,
|
jurisdictions which recognize such a disclaimer. In such jurisdictions,
|
||||||
this software is released into the Public Domain.
|
this software is released into the Public Domain.
|
||||||
|
|
||||||
In jurisdictions which do not recognize Public Domain property (e.g. Germany as of
|
In jurisdictions which do not recognize Public Domain property (e.g. Germany as of
|
||||||
2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is
|
2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur and
|
||||||
released under the terms of the MIT License (see below).
|
The JsonCpp Authors, and is released under the terms of the MIT License (see below).
|
||||||
|
|
||||||
In jurisdictions which recognize Public Domain property, the user of this
|
In jurisdictions which recognize Public Domain property, the user of this
|
||||||
software may choose to accept it either as 1) Public Domain, 2) under the
|
software may choose to accept it either as 1) Public Domain, 2) under the
|
||||||
@ -32,7 +32,7 @@ described in clear, concise terms at:
|
|||||||
The full text of the MIT License follows:
|
The full text of the MIT License follows:
|
||||||
|
|
||||||
========================================================================
|
========================================================================
|
||||||
Copyright (c) 2007-2010 Baptiste Lepilleur
|
Copyright (c) 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person
|
Permission is hereby granted, free of charge, to any person
|
||||||
obtaining a copy of this software and associated documentation
|
obtaining a copy of this software and associated documentation
|
||||||
@ -83,13 +83,16 @@ license you like.
|
|||||||
// Beginning of content of file: include/json/config.h
|
// Beginning of content of file: include/json/config.h
|
||||||
// //////////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Copyright 2007-2010 Baptiste Lepilleur
|
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
|
||||||
// Distributed under MIT license, or public domain if desired and
|
// Distributed under MIT license, or public domain if desired and
|
||||||
// recognized in your jurisdiction.
|
// recognized in your jurisdiction.
|
||||||
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
||||||
|
|
||||||
#ifndef JSON_CONFIG_H_INCLUDED
|
#ifndef JSON_CONFIG_H_INCLUDED
|
||||||
#define JSON_CONFIG_H_INCLUDED
|
#define JSON_CONFIG_H_INCLUDED
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <string> //typedef String
|
||||||
|
#include <stdint.h> //typedef int64_t, uint64_t
|
||||||
|
|
||||||
/// If defined, indicates that json library is embedded in CppTL library.
|
/// If defined, indicates that json library is embedded in CppTL library.
|
||||||
//# define JSON_IN_CPPTL 1
|
//# define JSON_IN_CPPTL 1
|
||||||
@ -122,12 +125,12 @@ license you like.
|
|||||||
#ifdef JSON_IN_CPPTL
|
#ifdef JSON_IN_CPPTL
|
||||||
#define JSON_API CPPTL_API
|
#define JSON_API CPPTL_API
|
||||||
#elif defined(JSON_DLL_BUILD)
|
#elif defined(JSON_DLL_BUILD)
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||||
#define JSON_API __declspec(dllexport)
|
#define JSON_API __declspec(dllexport)
|
||||||
#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
|
#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
|
||||||
#endif // if defined(_MSC_VER)
|
#endif // if defined(_MSC_VER)
|
||||||
#elif defined(JSON_DLL)
|
#elif defined(JSON_DLL)
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||||
#define JSON_API __declspec(dllimport)
|
#define JSON_API __declspec(dllimport)
|
||||||
#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
|
#define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
|
||||||
#endif // if defined(_MSC_VER)
|
#endif // if defined(_MSC_VER)
|
||||||
@ -136,49 +139,101 @@ license you like.
|
|||||||
#define JSON_API
|
#define JSON_API
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(JSON_HAS_UNIQUE_PTR)
|
|
||||||
#if __cplusplus >= 201103L
|
|
||||||
#define JSON_HAS_UNIQUE_PTR (1)
|
|
||||||
#elif _MSC_VER >= 1600
|
|
||||||
#define JSON_HAS_UNIQUE_PTR (1)
|
|
||||||
#else
|
|
||||||
#define JSON_HAS_UNIQUE_PTR (0)
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for
|
// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for
|
||||||
// integer
|
// integer
|
||||||
// Storages, and 64 bits integer support is disabled.
|
// Storages, and 64 bits integer support is disabled.
|
||||||
// #define JSON_NO_INT64 1
|
// #define JSON_NO_INT64 1
|
||||||
|
|
||||||
#if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6
|
#if defined(_MSC_VER) // MSVC
|
||||||
// Microsoft Visual Studio 6 only support conversion from __int64 to double
|
# if _MSC_VER <= 1200 // MSVC 6
|
||||||
// (no conversion from unsigned __int64).
|
// Microsoft Visual Studio 6 only support conversion from __int64 to double
|
||||||
#define JSON_USE_INT64_DOUBLE_CONVERSION 1
|
// (no conversion from unsigned __int64).
|
||||||
// Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255'
|
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
|
||||||
// characters in the debug information)
|
// Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255'
|
||||||
// All projects I've ever seen with VS6 were using this globally (not bothering
|
// characters in the debug information)
|
||||||
// with pragma push/pop).
|
// All projects I've ever seen with VS6 were using this globally (not bothering
|
||||||
#pragma warning(disable : 4786)
|
// with pragma push/pop).
|
||||||
#endif // if defined(_MSC_VER) && _MSC_VER < 1200 // MSVC 6
|
# pragma warning(disable : 4786)
|
||||||
|
# endif // MSVC 6
|
||||||
|
|
||||||
#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008
|
# if _MSC_VER >= 1500 // MSVC 2008
|
||||||
/// Indicates that the following function is deprecated.
|
/// Indicates that the following function is deprecated.
|
||||||
#define JSONCPP_DEPRECATED(message) __declspec(deprecated(message))
|
# define JSONCPP_DEPRECATED(message) __declspec(deprecated(message))
|
||||||
#elif defined(__clang__) && defined(__has_feature)
|
# endif
|
||||||
#if __has_feature(attribute_deprecated_with_message)
|
|
||||||
#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
|
#endif // defined(_MSC_VER)
|
||||||
|
|
||||||
|
// In c++11 the override keyword allows you to explicity define that a function
|
||||||
|
// is intended to override the base-class version. This makes the code more
|
||||||
|
// managable and fixes a set of common hard-to-find bugs.
|
||||||
|
#if __cplusplus >= 201103L
|
||||||
|
# define JSONCPP_OVERRIDE override
|
||||||
|
# define JSONCPP_NOEXCEPT noexcept
|
||||||
|
#elif defined(_MSC_VER) && _MSC_VER > 1600 && _MSC_VER < 1900
|
||||||
|
# define JSONCPP_OVERRIDE override
|
||||||
|
# define JSONCPP_NOEXCEPT throw()
|
||||||
|
#elif defined(_MSC_VER) && _MSC_VER >= 1900
|
||||||
|
# define JSONCPP_OVERRIDE override
|
||||||
|
# define JSONCPP_NOEXCEPT noexcept
|
||||||
|
#else
|
||||||
|
# define JSONCPP_OVERRIDE
|
||||||
|
# define JSONCPP_NOEXCEPT throw()
|
||||||
#endif
|
#endif
|
||||||
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
|
|
||||||
#define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
|
#ifndef JSON_HAS_RVALUE_REFERENCES
|
||||||
#elif defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
|
|
||||||
#define JSONCPP_DEPRECATED(message) __attribute__((__deprecated__))
|
#if defined(_MSC_VER) && _MSC_VER >= 1600 // MSVC >= 2010
|
||||||
|
#define JSON_HAS_RVALUE_REFERENCES 1
|
||||||
|
#endif // MSVC >= 2010
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
#if __has_feature(cxx_rvalue_references)
|
||||||
|
#define JSON_HAS_RVALUE_REFERENCES 1
|
||||||
|
#endif // has_feature
|
||||||
|
|
||||||
|
#elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc)
|
||||||
|
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L)
|
||||||
|
#define JSON_HAS_RVALUE_REFERENCES 1
|
||||||
|
#endif // GXX_EXPERIMENTAL
|
||||||
|
|
||||||
|
#endif // __clang__ || __GNUC__
|
||||||
|
|
||||||
|
#endif // not defined JSON_HAS_RVALUE_REFERENCES
|
||||||
|
|
||||||
|
#ifndef JSON_HAS_RVALUE_REFERENCES
|
||||||
|
#define JSON_HAS_RVALUE_REFERENCES 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
# if __has_extension(attribute_deprecated_with_message)
|
||||||
|
# define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
|
||||||
|
# endif
|
||||||
|
#elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc)
|
||||||
|
# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
|
||||||
|
# define JSONCPP_DEPRECATED(message) __attribute__ ((deprecated(message)))
|
||||||
|
# elif (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
|
||||||
|
# define JSONCPP_DEPRECATED(message) __attribute__((__deprecated__))
|
||||||
|
# endif // GNUC version
|
||||||
|
#endif // __clang__ || __GNUC__
|
||||||
|
|
||||||
#if !defined(JSONCPP_DEPRECATED)
|
#if !defined(JSONCPP_DEPRECATED)
|
||||||
#define JSONCPP_DEPRECATED(message)
|
#define JSONCPP_DEPRECATED(message)
|
||||||
#endif // if !defined(JSONCPP_DEPRECATED)
|
#endif // if !defined(JSONCPP_DEPRECATED)
|
||||||
|
|
||||||
|
#if __GNUC__ >= 6
|
||||||
|
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(JSON_IS_AMALGAMATION)
|
||||||
|
|
||||||
|
# include "version.h"
|
||||||
|
|
||||||
|
# if JSONCPP_USING_SECURE_MEMORY
|
||||||
|
# include "allocator.h" //typedef Allocator
|
||||||
|
# endif
|
||||||
|
|
||||||
|
#endif // if !defined(JSON_IS_AMALGAMATION)
|
||||||
|
|
||||||
namespace Json {
|
namespace Json {
|
||||||
typedef int Int;
|
typedef int Int;
|
||||||
typedef unsigned int UInt;
|
typedef unsigned int UInt;
|
||||||
@ -192,13 +247,26 @@ typedef unsigned int LargestUInt;
|
|||||||
typedef __int64 Int64;
|
typedef __int64 Int64;
|
||||||
typedef unsigned __int64 UInt64;
|
typedef unsigned __int64 UInt64;
|
||||||
#else // if defined(_MSC_VER) // Other platforms, use long long
|
#else // if defined(_MSC_VER) // Other platforms, use long long
|
||||||
typedef long long int Int64;
|
typedef int64_t Int64;
|
||||||
typedef unsigned long long int UInt64;
|
typedef uint64_t UInt64;
|
||||||
#endif // if defined(_MSC_VER)
|
#endif // if defined(_MSC_VER)
|
||||||
typedef Int64 LargestInt;
|
typedef Int64 LargestInt;
|
||||||
typedef UInt64 LargestUInt;
|
typedef UInt64 LargestUInt;
|
||||||
#define JSON_HAS_INT64
|
#define JSON_HAS_INT64
|
||||||
#endif // if defined(JSON_NO_INT64)
|
#endif // if defined(JSON_NO_INT64)
|
||||||
|
#if JSONCPP_USING_SECURE_MEMORY
|
||||||
|
#define JSONCPP_STRING std::basic_string<char, std::char_traits<char>, Json::SecureAllocator<char> >
|
||||||
|
#define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
|
||||||
|
#define JSONCPP_OSTREAM std::basic_ostream<char, std::char_traits<char>>
|
||||||
|
#define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, Json::SecureAllocator<char> >
|
||||||
|
#define JSONCPP_ISTREAM std::istream
|
||||||
|
#else
|
||||||
|
#define JSONCPP_STRING std::string
|
||||||
|
#define JSONCPP_OSTRINGSTREAM std::ostringstream
|
||||||
|
#define JSONCPP_OSTREAM std::ostream
|
||||||
|
#define JSONCPP_ISTRINGSTREAM std::istringstream
|
||||||
|
#define JSONCPP_ISTREAM std::istream
|
||||||
|
#endif // if JSONCPP_USING_SECURE_MEMORY
|
||||||
} // end namespace Json
|
} // end namespace Json
|
||||||
|
|
||||||
#endif // JSON_CONFIG_H_INCLUDED
|
#endif // JSON_CONFIG_H_INCLUDED
|
||||||
@ -216,7 +284,7 @@ typedef UInt64 LargestUInt;
|
|||||||
// Beginning of content of file: include/json/forwards.h
|
// Beginning of content of file: include/json/forwards.h
|
||||||
// //////////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Copyright 2007-2010 Baptiste Lepilleur
|
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
|
||||||
// Distributed under MIT license, or public domain if desired and
|
// Distributed under MIT license, or public domain if desired and
|
||||||
// recognized in your jurisdiction.
|
// recognized in your jurisdiction.
|
||||||
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -8,9 +8,9 @@
|
|||||||
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg=="
|
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg=="
|
||||||
},
|
},
|
||||||
"ws": {
|
"ws": {
|
||||||
"version": "6.2.0",
|
"version": "6.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/ws/-/ws-6.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/ws/-/ws-6.1.0.tgz",
|
||||||
"integrity": "sha512-deZYUNlt2O4buFCa3t5bKLf8A7FPP/TVjwOeVNpw818Ma5nk4MLXls2eoEGS39o8119QIYxTrTDoPQ5B/gTD6w==",
|
"integrity": "sha512-H3dGVdGvW2H8bnYpIDc3u3LH8Wue3Qh+Zto6aXXFzvESkTVT6rAfKR6tR/+coaUvxs8yHtmNV0uioBF62ZGSTg==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"async-limiter": "1.0.0"
|
"async-limiter": "1.0.0"
|
||||||
}
|
}
|
30
examples/echo_server/CMakeLists.txt
Normal file
30
examples/echo_server/CMakeLists.txt
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
cmake_minimum_required (VERSION 3.4.1)
|
||||||
|
project (echo_server)
|
||||||
|
|
||||||
|
# There's -Weverything too for clang
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32")
|
||||||
|
|
||||||
|
set (OPENSSL_PREFIX /usr/local/opt/openssl) # Homebrew openssl
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
|
option(USE_TLS "Add TLS support" ON)
|
||||||
|
|
||||||
|
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
|
||||||
|
|
||||||
|
include_directories(echo_server .)
|
||||||
|
|
||||||
|
add_executable(echo_server
|
||||||
|
echo_server.cpp)
|
||||||
|
|
||||||
|
if (APPLE AND USE_TLS)
|
||||||
|
target_link_libraries(echo_server "-framework foundation" "-framework security")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(echo_server ixwebsocket)
|
||||||
|
install(TARGETS echo_server DESTINATION bin)
|
68
examples/echo_server/echo_server.cpp
Normal file
68
examples/echo_server/echo_server.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* echo_server.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <ixwebsocket/IXWebSocketServer.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
int port = 8080;
|
||||||
|
if (argc == 2)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << argv[1];
|
||||||
|
ss >> port;
|
||||||
|
}
|
||||||
|
|
||||||
|
ix::WebSocketServer server(port);
|
||||||
|
|
||||||
|
server.setOnConnectionCallback(
|
||||||
|
[&server](std::shared_ptr<ix::WebSocket> webSocket)
|
||||||
|
{
|
||||||
|
webSocket->setOnMessageCallback(
|
||||||
|
[webSocket, &server](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketOpenInfo& openInfo,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo)
|
||||||
|
{
|
||||||
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
|
{
|
||||||
|
std::cerr << "New connection" << std::endl;
|
||||||
|
std::cerr << "Uri: " << openInfo.uri << std::endl;
|
||||||
|
std::cerr << "Headers:" << std::endl;
|
||||||
|
for (auto it : openInfo.headers)
|
||||||
|
{
|
||||||
|
std::cerr << it.first << ": " << it.second << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
|
{
|
||||||
|
std::cerr << "Closed connection" << std::endl;
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
|
{
|
||||||
|
webSocket->send(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
auto res = server.listen();
|
||||||
|
if (!res.first)
|
||||||
|
{
|
||||||
|
std::cerr << res.second << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
server.wait();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
2
.gitignore → examples/ping_pong/.gitignore
vendored
2
.gitignore → examples/ping_pong/.gitignore
vendored
@ -1,2 +1,2 @@
|
|||||||
|
venv
|
||||||
build
|
build
|
||||||
*.pyc
|
|
27
examples/ping_pong/CMakeLists.txt
Normal file
27
examples/ping_pong/CMakeLists.txt
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
cmake_minimum_required (VERSION 3.4.1)
|
||||||
|
project (ping_pong)
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
|
option(USE_TLS "Add TLS support" ON)
|
||||||
|
|
||||||
|
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
|
||||||
|
|
||||||
|
add_executable(ping_pong ping_pong.cpp)
|
||||||
|
|
||||||
|
if (APPLE AND USE_TLS)
|
||||||
|
target_link_libraries(ping_pong "-framework foundation" "-framework security")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (WIN32)
|
||||||
|
target_link_libraries(ping_pong wsock32 ws2_32)
|
||||||
|
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(ping_pong ixwebsocket)
|
||||||
|
install(TARGETS ping_pong DESTINATION bin)
|
15
examples/ping_pong/build_linux.sh
Normal file
15
examples/ping_pong/build_linux.sh
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
# 'manual' way of building. You can also use cmake.
|
||||||
|
|
||||||
|
g++ --std=c++11 \
|
||||||
|
../../ixwebsocket/IXSocket.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocketTransport.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocket.cpp \
|
||||||
|
-I ../.. \
|
||||||
|
cmd_websocket_chat.cpp \
|
||||||
|
-o cmd_websocket_chat
|
17
examples/ping_pong/client.py
Normal file
17
examples/ping_pong/client.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import websockets
|
||||||
|
|
||||||
|
async def hello(uri):
|
||||||
|
async with websockets.connect(uri) as websocket:
|
||||||
|
await websocket.send("Hello world!")
|
||||||
|
response = await websocket.recv()
|
||||||
|
print(response)
|
||||||
|
|
||||||
|
pong_waiter = await websocket.ping('coucou')
|
||||||
|
ret = await pong_waiter # only if you want to wait for the pong
|
||||||
|
print(ret)
|
||||||
|
|
||||||
|
asyncio.get_event_loop().run_until_complete(
|
||||||
|
hello('ws://localhost:5678'))
|
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* ws_ping_pong.cpp
|
* ping_pong.cpp
|
||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -9,8 +9,15 @@
|
|||||||
#include <ixwebsocket/IXWebSocket.h>
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
#include <ixwebsocket/IXSocket.h>
|
#include <ixwebsocket/IXSocket.h>
|
||||||
|
|
||||||
namespace ix
|
using namespace ix;
|
||||||
|
|
||||||
|
namespace
|
||||||
{
|
{
|
||||||
|
void log(const std::string& msg)
|
||||||
|
{
|
||||||
|
std::cout << msg << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
class WebSocketPingPong
|
class WebSocketPingPong
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -26,8 +33,6 @@ namespace ix
|
|||||||
private:
|
private:
|
||||||
std::string _url;
|
std::string _url;
|
||||||
ix::WebSocket _webSocket;
|
ix::WebSocket _webSocket;
|
||||||
|
|
||||||
void log(const std::string& msg);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocketPingPong::WebSocketPingPong(const std::string& url) :
|
WebSocketPingPong::WebSocketPingPong(const std::string& url) :
|
||||||
@ -36,11 +41,6 @@ namespace ix
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketPingPong::log(const std::string& msg)
|
|
||||||
{
|
|
||||||
std::cout << msg << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebSocketPingPong::stop()
|
void WebSocketPingPong::stop()
|
||||||
{
|
{
|
||||||
_webSocket.stop();
|
_webSocket.stop();
|
||||||
@ -54,54 +54,50 @@ namespace ix
|
|||||||
log(std::string("Connecting to url: ") + _url);
|
log(std::string("Connecting to url: ") + _url);
|
||||||
|
|
||||||
_webSocket.setOnMessageCallback(
|
_webSocket.setOnMessageCallback(
|
||||||
[this](const ix::WebSocketMessagePtr& msg)
|
[this](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketOpenInfo& openInfo,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo)
|
||||||
{
|
{
|
||||||
std::cerr << "Received " << msg->wireSize << " bytes" << std::endl;
|
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
if (msg->type == ix::WebSocketMessageType::Open)
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
{
|
{
|
||||||
log("ping_pong: connected");
|
log("ping_pong: connected");
|
||||||
|
|
||||||
std::cout << "Uri: " << msg->openInfo.uri << std::endl;
|
|
||||||
std::cout << "Handshake Headers:" << std::endl;
|
|
||||||
for (auto it : msg->openInfo.headers)
|
|
||||||
{
|
|
||||||
std::cout << it.first << ": " << it.second << std::endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Close)
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
{
|
{
|
||||||
ss << "ping_pong: disconnected:"
|
ss << "ping_pong: disconnected:"
|
||||||
<< " code " << msg->closeInfo.code
|
<< " code " << closeInfo.code
|
||||||
<< " reason " << msg->closeInfo.reason
|
<< " reason " << closeInfo.reason
|
||||||
<< msg->str;
|
<< str;
|
||||||
log(ss.str());
|
log(ss.str());
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Message)
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
{
|
{
|
||||||
ss << "ping_pong: received message: "
|
ss << "ping_pong: received message: "
|
||||||
<< msg->str;
|
<< str;
|
||||||
log(ss.str());
|
log(ss.str());
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Ping)
|
else if (messageType == ix::WebSocket_MessageType_Ping)
|
||||||
{
|
{
|
||||||
ss << "ping_pong: received ping message: "
|
ss << "ping_pong: received ping message: "
|
||||||
<< msg->str;
|
<< str;
|
||||||
log(ss.str());
|
log(ss.str());
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Pong)
|
else if (messageType == ix::WebSocket_MessageType_Pong)
|
||||||
{
|
{
|
||||||
ss << "ping_pong: received pong message: "
|
ss << "ping_pong: received pong message: "
|
||||||
<< msg->str;
|
<< str;
|
||||||
log(ss.str());
|
log(ss.str());
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Error)
|
else if (messageType == ix::WebSocket_MessageType_Error)
|
||||||
{
|
{
|
||||||
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
ss << "Connection error: " << error.reason << std::endl;
|
||||||
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
ss << "#retries: " << error.retries << std::endl;
|
||||||
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
ss << "Wait time(ms): " << error.wait_time << std::endl;
|
||||||
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
ss << "HTTP Status: " << error.http_status << std::endl;
|
||||||
log(ss.str());
|
log(ss.str());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -128,7 +124,7 @@ namespace ix
|
|||||||
_webSocket.send(text);
|
_webSocket.send(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ws_ping_pong_main(const std::string& url)
|
void interactiveMain(const std::string& url)
|
||||||
{
|
{
|
||||||
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
||||||
WebSocketPingPong webSocketPingPong(url);
|
WebSocketPingPong webSocketPingPong(url);
|
||||||
@ -157,7 +153,19 @@ namespace ix
|
|||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
webSocketPingPong.stop();
|
webSocketPingPong.stop();
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
std::cerr << "Usage: ping_pong <url>" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::string url = argv[1];
|
||||||
|
|
||||||
|
Socket::init();
|
||||||
|
interactiveMain(url);
|
||||||
|
return 0;
|
||||||
|
}
|
21
examples/ping_pong/server.py
Normal file
21
examples/ping_pong/server.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import os
|
||||||
|
import asyncio
|
||||||
|
import websockets
|
||||||
|
|
||||||
|
async def echo(websocket, path):
|
||||||
|
async for message in websocket:
|
||||||
|
print(message)
|
||||||
|
await websocket.send(message)
|
||||||
|
|
||||||
|
if os.getenv('TEST_CLOSE'):
|
||||||
|
print('Closing')
|
||||||
|
# breakpoint()
|
||||||
|
await websocket.close(1001, 'close message')
|
||||||
|
# await websocket.close()
|
||||||
|
break
|
||||||
|
|
||||||
|
asyncio.get_event_loop().run_until_complete(
|
||||||
|
websockets.serve(echo, 'localhost', 5678))
|
||||||
|
asyncio.get_event_loop().run_forever()
|
9
examples/ping_pong/test.sh
Normal file
9
examples/ping_pong/test.sh
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
test -d build || {
|
||||||
|
mkdir -p build
|
||||||
|
cd build
|
||||||
|
cmake ..
|
||||||
|
}
|
||||||
|
(cd build ; make)
|
||||||
|
./build/ping_pong ws://localhost:5678
|
3
examples/ws_connect/.gitignore
vendored
Normal file
3
examples/ws_connect/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
build
|
||||||
|
venv
|
||||||
|
node_modules
|
22
examples/ws_connect/CMakeLists.txt
Normal file
22
examples/ws_connect/CMakeLists.txt
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
cmake_minimum_required (VERSION 3.4.1)
|
||||||
|
project (ws_connect)
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
|
option(USE_TLS "Add TLS support" ON)
|
||||||
|
|
||||||
|
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket)
|
||||||
|
|
||||||
|
add_executable(ws_connect ws_connect.cpp)
|
||||||
|
|
||||||
|
if (APPLE AND USE_TLS)
|
||||||
|
target_link_libraries(ws_connect "-framework foundation" "-framework security")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(ws_connect ixwebsocket)
|
||||||
|
install(TARGETS ws_connect DESTINATION bin)
|
11
examples/ws_connect/README.md
Normal file
11
examples/ws_connect/README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Building
|
||||||
|
|
||||||
|
1. mkdir build
|
||||||
|
2. cd build
|
||||||
|
3. cmake ..
|
||||||
|
4. make
|
||||||
|
|
||||||
|
## Disable TLS
|
||||||
|
|
||||||
|
* Enable: `cmake -DUSE_TLS=OFF ..`
|
||||||
|
* Disable: `cmake -DUSE_TLS=ON ..`
|
25
examples/ws_connect/build_linux.sh
Normal file
25
examples/ws_connect/build_linux.sh
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# Author: Benjamin Sergeant
|
||||||
|
# Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
#
|
||||||
|
|
||||||
|
# 'manual' way of building. You can also use cmake.
|
||||||
|
|
||||||
|
g++ --std=c++11 \
|
||||||
|
-DIXWEBSOCKET_USE_TLS \
|
||||||
|
-g \
|
||||||
|
../../ixwebsocket/IXEventFd.cpp \
|
||||||
|
../../ixwebsocket/IXSocket.cpp \
|
||||||
|
../../ixwebsocket/IXSetThreadName.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocketTransport.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocket.cpp \
|
||||||
|
../../ixwebsocket/IXDNSLookup.cpp \
|
||||||
|
../../ixwebsocket/IXSocketConnect.cpp \
|
||||||
|
../../ixwebsocket/IXSocketOpenSSL.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocketPerMessageDeflate.cpp \
|
||||||
|
../../ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp \
|
||||||
|
-I ../.. \
|
||||||
|
ws_connect.cpp \
|
||||||
|
-o ws_connect \
|
||||||
|
-lcrypto -lssl -lz -lpthread
|
165
examples/ws_connect/ws_connect.cpp
Normal file
165
examples/ws_connect/ws_connect.cpp
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
/*
|
||||||
|
* ws_connect.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
|
#include <ixwebsocket/IXSocket.h>
|
||||||
|
|
||||||
|
using namespace ix;
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
void log(const std::string& msg)
|
||||||
|
{
|
||||||
|
std::cout << msg << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
class WebSocketConnect
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WebSocketConnect(const std::string& _url);
|
||||||
|
|
||||||
|
void subscribe(const std::string& channel);
|
||||||
|
void start();
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
void sendMessage(const std::string& text);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string _url;
|
||||||
|
ix::WebSocket _webSocket;
|
||||||
|
};
|
||||||
|
|
||||||
|
WebSocketConnect::WebSocketConnect(const std::string& url) :
|
||||||
|
_url(url)
|
||||||
|
{
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebSocketConnect::stop()
|
||||||
|
{
|
||||||
|
_webSocket.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebSocketConnect::start()
|
||||||
|
{
|
||||||
|
_webSocket.setUrl(_url);
|
||||||
|
|
||||||
|
ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions(
|
||||||
|
true, false, false, 15, 15);
|
||||||
|
_webSocket.setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions);
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
log(std::string("Connecting to url: ") + _url);
|
||||||
|
|
||||||
|
_webSocket.setOnMessageCallback(
|
||||||
|
[this](ix::WebSocketMessageType messageType,
|
||||||
|
const std::string& str,
|
||||||
|
size_t wireSize,
|
||||||
|
const ix::WebSocketErrorInfo& error,
|
||||||
|
const ix::WebSocketOpenInfo& openInfo,
|
||||||
|
const ix::WebSocketCloseInfo& closeInfo)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
if (messageType == ix::WebSocket_MessageType_Open)
|
||||||
|
{
|
||||||
|
log("ws_connect: connected");
|
||||||
|
std::cout << "Uri: " << openInfo.uri << std::endl;
|
||||||
|
std::cout << "Handshake Headers:" << std::endl;
|
||||||
|
for (auto it : openInfo.headers)
|
||||||
|
{
|
||||||
|
std::cout << it.first << ": " << it.second << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
||||||
|
{
|
||||||
|
ss << "ws_connect: connection closed:";
|
||||||
|
ss << " code " << closeInfo.code;
|
||||||
|
ss << " reason " << closeInfo.reason << std::endl;
|
||||||
|
log(ss.str());
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
||||||
|
{
|
||||||
|
ss << "ws_connect: received message: "
|
||||||
|
<< str;
|
||||||
|
log(ss.str());
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocket_MessageType_Error)
|
||||||
|
{
|
||||||
|
ss << "Connection error: " << error.reason << std::endl;
|
||||||
|
ss << "#retries: " << error.retries << std::endl;
|
||||||
|
ss << "Wait time(ms): " << error.wait_time << std::endl;
|
||||||
|
ss << "HTTP Status: " << error.http_status << std::endl;
|
||||||
|
log(ss.str());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ss << "Invalid ix::WebSocketMessageType";
|
||||||
|
log(ss.str());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
_webSocket.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebSocketConnect::sendMessage(const std::string& text)
|
||||||
|
{
|
||||||
|
_webSocket.send(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void interactiveMain(const std::string& url)
|
||||||
|
{
|
||||||
|
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
||||||
|
WebSocketConnect webSocketChat(url);
|
||||||
|
webSocketChat.start();
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
std::string text;
|
||||||
|
std::cout << "> " << std::flush;
|
||||||
|
std::getline(std::cin, text);
|
||||||
|
|
||||||
|
if (text == "/stop")
|
||||||
|
{
|
||||||
|
std::cout << "Stopping connection..." << std::endl;
|
||||||
|
webSocketChat.stop();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (text == "/start")
|
||||||
|
{
|
||||||
|
std::cout << "Starting connection..." << std::endl;
|
||||||
|
webSocketChat.start();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!std::cin)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
webSocketChat.sendMessage(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
webSocketChat.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
std::cerr << "Usage: ws_connect <url>" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::string url = argv[1];
|
||||||
|
|
||||||
|
Socket::init();
|
||||||
|
interactiveMain(url);
|
||||||
|
return 0;
|
||||||
|
}
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
CancellationRequest makeCancellationRequestWithTimeout(int secs,
|
CancellationRequest makeCancellationRequestWithTimeout(int secs,
|
||||||
std::atomic<bool>& requestInitCancellation)
|
std::atomic<bool>& requestInitCancellation)
|
||||||
@ -20,7 +20,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
// Was an explicit cancellation requested ?
|
// Was an explicit cancellation requested ?
|
||||||
if (requestInitCancellation) return true;
|
if (requestInitCancellation) return true;
|
||||||
|
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
if ((now - start) > timeout) return true;
|
if ((now - start) > timeout) return true;
|
||||||
|
|
||||||
|
@ -6,13 +6,14 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
using CancellationRequest = std::function<bool()>;
|
using CancellationRequest = std::function<bool()>;
|
||||||
|
|
||||||
CancellationRequest makeCancellationRequestWithTimeout(
|
CancellationRequest makeCancellationRequestWithTimeout(int seconds,
|
||||||
int seconds, std::atomic<bool>& requestInitCancellation);
|
std::atomic<bool>& requestInitCancellation);
|
||||||
} // namespace ix
|
}
|
||||||
|
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXConnectionState.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "IXConnectionState.h"
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
std::atomic<uint64_t> ConnectionState::_globalId(0);
|
|
||||||
|
|
||||||
ConnectionState::ConnectionState() : _terminated(false)
|
|
||||||
{
|
|
||||||
computeId();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionState::computeId()
|
|
||||||
{
|
|
||||||
_id = std::to_string(_globalId++);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& ConnectionState::getId() const
|
|
||||||
{
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<ConnectionState> ConnectionState::createConnectionState()
|
|
||||||
{
|
|
||||||
return std::make_shared<ConnectionState>();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ConnectionState::isTerminated() const
|
|
||||||
{
|
|
||||||
return _terminated;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionState::setTerminated()
|
|
||||||
{
|
|
||||||
_terminated = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXConnectionState.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <memory>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
class ConnectionState
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ConnectionState();
|
|
||||||
virtual ~ConnectionState() = default;
|
|
||||||
|
|
||||||
virtual void computeId();
|
|
||||||
virtual const std::string& getId() const;
|
|
||||||
|
|
||||||
void setTerminated();
|
|
||||||
bool isTerminated() const;
|
|
||||||
|
|
||||||
static std::shared_ptr<ConnectionState> createConnectionState();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::atomic<bool> _terminated;
|
|
||||||
std::string _id;
|
|
||||||
|
|
||||||
static std::atomic<uint64_t> _globalId;
|
|
||||||
};
|
|
||||||
} // namespace ix
|
|
@ -10,7 +10,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
const int64_t DNSLookup::kDefaultWait = 10; // ms
|
const int64_t DNSLookup::kDefaultWait = 10; // ms
|
||||||
|
|
||||||
@ -19,23 +19,24 @@ namespace ix
|
|||||||
std::mutex DNSLookup::_activeJobsMutex;
|
std::mutex DNSLookup::_activeJobsMutex;
|
||||||
|
|
||||||
DNSLookup::DNSLookup(const std::string& hostname, int port, int64_t wait) :
|
DNSLookup::DNSLookup(const std::string& hostname, int port, int64_t wait) :
|
||||||
|
_hostname(hostname),
|
||||||
_port(port),
|
_port(port),
|
||||||
_wait(wait),
|
_wait(wait),
|
||||||
_res(nullptr),
|
_res(nullptr),
|
||||||
_done(false),
|
_done(false),
|
||||||
_id(_nextId++)
|
_id(_nextId++)
|
||||||
{
|
{
|
||||||
setHostname(hostname);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DNSLookup::~DNSLookup()
|
DNSLookup::~DNSLookup()
|
||||||
{
|
{
|
||||||
// Remove this job from the active jobs list
|
// Remove this job from the active jobs list
|
||||||
std::lock_guard<std::mutex> lock(_activeJobsMutex);
|
std::unique_lock<std::mutex> lock(_activeJobsMutex);
|
||||||
_activeJobs.erase(_id);
|
_activeJobs.erase(_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct addrinfo* DNSLookup::getAddrInfo(const std::string& hostname,
|
struct addrinfo* DNSLookup::getAddrInfo(const std::string& hostname,
|
||||||
int port,
|
int port,
|
||||||
std::string& errMsg)
|
std::string& errMsg)
|
||||||
{
|
{
|
||||||
@ -48,7 +49,7 @@ namespace ix
|
|||||||
std::string sport = std::to_string(port);
|
std::string sport = std::to_string(port);
|
||||||
|
|
||||||
struct addrinfo* res;
|
struct addrinfo* res;
|
||||||
int getaddrinfo_result = getaddrinfo(hostname.c_str(), sport.c_str(),
|
int getaddrinfo_result = getaddrinfo(hostname.c_str(), sport.c_str(),
|
||||||
&hints, &res);
|
&hints, &res);
|
||||||
if (getaddrinfo_result)
|
if (getaddrinfo_result)
|
||||||
{
|
{
|
||||||
@ -72,13 +73,13 @@ namespace ix
|
|||||||
errMsg = "no error";
|
errMsg = "no error";
|
||||||
|
|
||||||
// Maybe a cancellation request got in before the background thread terminated ?
|
// Maybe a cancellation request got in before the background thread terminated ?
|
||||||
if (isCancellationRequested && isCancellationRequested())
|
if (isCancellationRequested())
|
||||||
{
|
{
|
||||||
errMsg = "cancellation requested";
|
errMsg = "cancellation requested";
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return getAddrInfo(getHostname(), _port, errMsg);
|
return getAddrInfo(_hostname, _port, errMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct addrinfo* DNSLookup::resolveAsync(std::string& errMsg,
|
struct addrinfo* DNSLookup::resolveAsync(std::string& errMsg,
|
||||||
@ -96,15 +97,15 @@ namespace ix
|
|||||||
|
|
||||||
// Record job in the active Job set
|
// Record job in the active Job set
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_activeJobsMutex);
|
std::unique_lock<std::mutex> lock(_activeJobsMutex);
|
||||||
_activeJobs.insert(_id);
|
_activeJobs.insert(_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Good resource on thread forced termination
|
// Good resource on thread forced termination
|
||||||
// https://www.bo-yang.net/2017/11/19/cpp-kill-detached-thread
|
// https://www.bo-yang.net/2017/11/19/cpp-kill-detached-thread
|
||||||
//
|
//
|
||||||
_thread = std::thread(&DNSLookup::run, this, _id, getHostname(), _port);
|
_thread = std::thread(&DNSLookup::run, this, _id, _hostname, _port);
|
||||||
_thread.detach();
|
_thread.detach();
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(_conditionVariableMutex);
|
std::unique_lock<std::mutex> lock(_conditionVariableMutex);
|
||||||
@ -120,7 +121,7 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Were we cancelled ?
|
// Were we cancelled ?
|
||||||
if (isCancellationRequested && isCancellationRequested())
|
if (isCancellationRequested())
|
||||||
{
|
{
|
||||||
errMsg = "cancellation requested";
|
errMsg = "cancellation requested";
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -128,74 +129,36 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Maybe a cancellation request got in before the bg terminated ?
|
// Maybe a cancellation request got in before the bg terminated ?
|
||||||
if (isCancellationRequested && isCancellationRequested())
|
if (isCancellationRequested())
|
||||||
{
|
{
|
||||||
errMsg = "cancellation requested";
|
errMsg = "cancellation requested";
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
errMsg = getErrMsg();
|
return _res;
|
||||||
return getRes();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DNSLookup::run(uint64_t id, const std::string& hostname, int port) // thread runner
|
void DNSLookup::run(uint64_t id, const std::string& hostname, int port) // thread runner
|
||||||
{
|
{
|
||||||
// We don't want to read or write into members variables of an object that could be
|
// We don't want to read or write into members variables of an object that could be
|
||||||
// gone, so we use temporary variables (res) or we pass in by copy everything that
|
// gone, so we use temporary variables (res) or we pass in by copy everything that
|
||||||
// getAddrInfo needs to work.
|
// getAddrInfo needs to work.
|
||||||
std::string errMsg;
|
std::string errMsg;
|
||||||
struct addrinfo* res = getAddrInfo(hostname, port, errMsg);
|
struct addrinfo* res = getAddrInfo(hostname, port, errMsg);
|
||||||
|
|
||||||
// if this isn't an active job, and the control thread is gone
|
// if this isn't an active job, and the control thread is gone
|
||||||
// there is nothing to do, and we don't want to touch the defunct
|
// there is not thing to do, and we don't want to touch the defunct
|
||||||
// object data structure such as _errMsg or _condition
|
// object data structure such as _errMsg or _condition
|
||||||
std::lock_guard<std::mutex> lock(_activeJobsMutex);
|
std::unique_lock<std::mutex> lock(_activeJobsMutex);
|
||||||
if (_activeJobs.count(id) == 0)
|
if (_activeJobs.count(id) == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy result into the member variables
|
// Copy result into the member variables
|
||||||
setRes(res);
|
_res = res;
|
||||||
setErrMsg(errMsg);
|
_errMsg = errMsg;
|
||||||
|
|
||||||
_condition.notify_one();
|
_condition.notify_one();
|
||||||
_done = true;
|
_done = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DNSLookup::setHostname(const std::string& hostname)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_hostnameMutex);
|
|
||||||
_hostname = hostname;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& DNSLookup::getHostname()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_hostnameMutex);
|
|
||||||
return _hostname;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DNSLookup::setErrMsg(const std::string& errMsg)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_errMsgMutex);
|
|
||||||
_errMsg = errMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& DNSLookup::getErrMsg()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_errMsgMutex);
|
|
||||||
return _errMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DNSLookup::setRes(struct addrinfo* addr)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_resMutex);
|
|
||||||
_res = addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct addrinfo* DNSLookup::getRes()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_resMutex);
|
|
||||||
return _res;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
* Author: Benjamin Sergeant
|
* Author: Benjamin Sergeant
|
||||||
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
*
|
*
|
||||||
* Resolve a hostname+port to a struct addrinfo obtained with getaddrinfo
|
* Resolve a hostname+port to a struct addrinfo obtained with getaddrinfo
|
||||||
* Does this in a background thread so that it can be cancelled, since
|
* Does this in a background thread so that it can be cancelled, since
|
||||||
* getaddrinfo is a blocking call, and we don't want to block the main thread on Mobile.
|
* getaddrinfo is a blocking call, and we don't want to block the main thread on Mobile.
|
||||||
*/
|
*/
|
||||||
@ -11,20 +11,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IXCancellationRequest.h"
|
#include "IXCancellationRequest.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
struct addrinfo;
|
struct addrinfo;
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class DNSLookup
|
class DNSLookup {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
DNSLookup(const std::string& hostname, int port, int64_t wait = DNSLookup::kDefaultWait);
|
DNSLookup(const std::string& hostname,
|
||||||
|
int port,
|
||||||
|
int64_t wait = DNSLookup::kDefaultWait);
|
||||||
~DNSLookup();
|
~DNSLookup();
|
||||||
|
|
||||||
struct addrinfo* resolve(std::string& errMsg,
|
struct addrinfo* resolve(std::string& errMsg,
|
||||||
@ -37,32 +39,17 @@ namespace ix
|
|||||||
struct addrinfo* resolveBlocking(std::string& errMsg,
|
struct addrinfo* resolveBlocking(std::string& errMsg,
|
||||||
const CancellationRequest& isCancellationRequested);
|
const CancellationRequest& isCancellationRequested);
|
||||||
|
|
||||||
static struct addrinfo* getAddrInfo(const std::string& hostname,
|
static struct addrinfo* getAddrInfo(const std::string& hostname,
|
||||||
int port,
|
int port,
|
||||||
std::string& errMsg);
|
std::string& errMsg);
|
||||||
|
|
||||||
void run(uint64_t id, const std::string& hostname, int port); // thread runner
|
void run(uint64_t id, const std::string& hostname, int port); // thread runner
|
||||||
|
|
||||||
void setHostname(const std::string& hostname);
|
|
||||||
const std::string& getHostname();
|
|
||||||
|
|
||||||
void setErrMsg(const std::string& errMsg);
|
|
||||||
const std::string& getErrMsg();
|
|
||||||
|
|
||||||
void setRes(struct addrinfo* addr);
|
|
||||||
struct addrinfo* getRes();
|
|
||||||
|
|
||||||
std::string _hostname;
|
std::string _hostname;
|
||||||
std::mutex _hostnameMutex;
|
|
||||||
int _port;
|
int _port;
|
||||||
|
|
||||||
int64_t _wait;
|
int64_t _wait;
|
||||||
|
|
||||||
struct addrinfo* _res;
|
|
||||||
std::mutex _resMutex;
|
|
||||||
|
|
||||||
std::string _errMsg;
|
std::string _errMsg;
|
||||||
std::mutex _errMsgMutex;
|
struct addrinfo* _res;
|
||||||
|
|
||||||
std::atomic<bool> _done;
|
std::atomic<bool> _done;
|
||||||
std::thread _thread;
|
std::thread _thread;
|
||||||
@ -76,4 +63,4 @@ namespace ix
|
|||||||
|
|
||||||
const static int64_t kDefaultWait;
|
const static int64_t kDefaultWait;
|
||||||
};
|
};
|
||||||
} // namespace ix
|
}
|
||||||
|
82
ixwebsocket/IXEventFd.cpp
Normal file
82
ixwebsocket/IXEventFd.cpp
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* IXEventFd.cpp
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "IXEventFd.h"
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
# include <sys/eventfd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include <unistd.h> // for write
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
EventFd::EventFd() :
|
||||||
|
_eventfd(-1)
|
||||||
|
{
|
||||||
|
#ifdef __linux__
|
||||||
|
_eventfd = eventfd(0, 0);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
EventFd::~EventFd()
|
||||||
|
{
|
||||||
|
#ifdef __linux__
|
||||||
|
::close(_eventfd);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EventFd::notify()
|
||||||
|
{
|
||||||
|
#if defined(__linux__)
|
||||||
|
if (_eventfd == -1) return false;
|
||||||
|
|
||||||
|
// select will wake up when a non-zero value is written to our eventfd
|
||||||
|
uint64_t value = 1;
|
||||||
|
|
||||||
|
// we should write 8 bytes for an uint64_t
|
||||||
|
return write(_eventfd, &value, sizeof(value)) == 8;
|
||||||
|
#else
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EventFd::clear()
|
||||||
|
{
|
||||||
|
#if defined(__linux__)
|
||||||
|
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;
|
||||||
|
#else
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
int EventFd::getFd()
|
||||||
|
{
|
||||||
|
return _eventfd;
|
||||||
|
}
|
||||||
|
}
|
23
ixwebsocket/IXEventFd.h
Normal file
23
ixwebsocket/IXEventFd.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* IXEventFd.h
|
||||||
|
* Author: Benjamin Sergeant
|
||||||
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace ix
|
||||||
|
{
|
||||||
|
class EventFd {
|
||||||
|
public:
|
||||||
|
EventFd();
|
||||||
|
virtual ~EventFd();
|
||||||
|
|
||||||
|
bool notify();
|
||||||
|
bool clear();
|
||||||
|
int getFd();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int _eventfd;
|
||||||
|
};
|
||||||
|
}
|
@ -1,570 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXHttpClient.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "IXHttpClient.h"
|
|
||||||
#include "IXUrlParser.h"
|
|
||||||
#include "IXWebSocketHttpHeaders.h"
|
|
||||||
#include "IXSocketFactory.h"
|
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <vector>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
#include <zlib.h>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
const std::string HttpClient::kPost = "POST";
|
|
||||||
const std::string HttpClient::kGet = "GET";
|
|
||||||
const std::string HttpClient::kHead = "HEAD";
|
|
||||||
const std::string HttpClient::kDel = "DEL";
|
|
||||||
const std::string HttpClient::kPut = "PUT";
|
|
||||||
|
|
||||||
HttpClient::HttpClient(bool async) : _async(async), _stop(false)
|
|
||||||
{
|
|
||||||
if (!_async) return;
|
|
||||||
|
|
||||||
_thread = std::thread(&HttpClient::run, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpClient::~HttpClient()
|
|
||||||
{
|
|
||||||
if (!_thread.joinable()) return;
|
|
||||||
|
|
||||||
_stop = true;
|
|
||||||
_condition.notify_one();
|
|
||||||
_thread.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpRequestArgsPtr HttpClient::createRequest(const std::string& url,
|
|
||||||
const std::string& verb)
|
|
||||||
{
|
|
||||||
auto request = std::make_shared<HttpRequestArgs>();
|
|
||||||
request->url = url;
|
|
||||||
request->verb = verb;
|
|
||||||
return request;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HttpClient::performRequest(HttpRequestArgsPtr args,
|
|
||||||
const OnResponseCallback& onResponseCallback)
|
|
||||||
{
|
|
||||||
if (!_async) return false;
|
|
||||||
|
|
||||||
// Enqueue the task
|
|
||||||
{
|
|
||||||
// acquire lock
|
|
||||||
std::unique_lock<std::mutex> lock(_queueMutex);
|
|
||||||
|
|
||||||
// add the task
|
|
||||||
_queue.push(std::make_pair(args, onResponseCallback));
|
|
||||||
} // release lock
|
|
||||||
|
|
||||||
// wake up one thread
|
|
||||||
_condition.notify_one();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void HttpClient::run()
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
HttpRequestArgsPtr args;
|
|
||||||
OnResponseCallback onResponseCallback;
|
|
||||||
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(_queueMutex);
|
|
||||||
|
|
||||||
while (!_stop && _queue.empty())
|
|
||||||
{
|
|
||||||
_condition.wait(lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_stop) return;
|
|
||||||
|
|
||||||
auto p = _queue.front();
|
|
||||||
_queue.pop();
|
|
||||||
|
|
||||||
args = p.first;
|
|
||||||
onResponseCallback = p.second;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_stop) return;
|
|
||||||
|
|
||||||
HttpResponsePtr response = request(args->url, args->verb, args->body, args);
|
|
||||||
onResponseCallback(response);
|
|
||||||
|
|
||||||
if (_stop) return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::request(
|
|
||||||
const std::string& url,
|
|
||||||
const std::string& verb,
|
|
||||||
const std::string& body,
|
|
||||||
HttpRequestArgsPtr args,
|
|
||||||
int redirects)
|
|
||||||
{
|
|
||||||
// We only have one socket connection, so we cannot
|
|
||||||
// make multiple requests concurrently.
|
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
|
|
||||||
uint64_t uploadSize = 0;
|
|
||||||
uint64_t downloadSize = 0;
|
|
||||||
int code = 0;
|
|
||||||
WebSocketHttpHeaders headers;
|
|
||||||
std::string payload;
|
|
||||||
|
|
||||||
std::string protocol, host, path, query;
|
|
||||||
int port;
|
|
||||||
|
|
||||||
if (!UrlParser::parse(url, protocol, host, path, query, port))
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "Cannot parse url: " << url;
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::UrlMalformed,
|
|
||||||
headers, payload, ss.str(),
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool tls = protocol == "https";
|
|
||||||
std::string errorMsg;
|
|
||||||
_socket = createSocket(tls, errorMsg);
|
|
||||||
|
|
||||||
if (!_socket)
|
|
||||||
{
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::CannotCreateSocket,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build request string
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << verb << " " << path << " HTTP/1.1\r\n";
|
|
||||||
ss << "Host: " << host << "\r\n";
|
|
||||||
|
|
||||||
if (args->compress)
|
|
||||||
{
|
|
||||||
ss << "Accept-Encoding: gzip" << "\r\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append extra headers
|
|
||||||
for (auto&& it : args->extraHeaders)
|
|
||||||
{
|
|
||||||
ss << it.first << ": " << it.second << "\r\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set a default Accept header if none is present
|
|
||||||
if (headers.find("Accept") == headers.end())
|
|
||||||
{
|
|
||||||
ss << "Accept: */*" << "\r\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set a default User agent if none is present
|
|
||||||
if (headers.find("User-Agent") == headers.end())
|
|
||||||
{
|
|
||||||
ss << "User-Agent: ixwebsocket" << "\r\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (verb == kPost || verb == kPut)
|
|
||||||
{
|
|
||||||
ss << "Content-Length: " << body.size() << "\r\n";
|
|
||||||
|
|
||||||
// Set default Content-Type if unspecified
|
|
||||||
if (args->extraHeaders.find("Content-Type") == args->extraHeaders.end())
|
|
||||||
{
|
|
||||||
ss << "Content-Type: application/x-www-form-urlencoded" << "\r\n";
|
|
||||||
}
|
|
||||||
ss << "\r\n";
|
|
||||||
ss << body;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ss << "\r\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string req(ss.str());
|
|
||||||
std::string errMsg;
|
|
||||||
std::atomic<bool> requestInitCancellation(false);
|
|
||||||
|
|
||||||
// Make a cancellation object dealing with connection timeout
|
|
||||||
auto isCancellationRequested =
|
|
||||||
makeCancellationRequestWithTimeout(args->connectTimeout, requestInitCancellation);
|
|
||||||
|
|
||||||
bool success = _socket->connect(host, port, errMsg, isCancellationRequested);
|
|
||||||
if (!success)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "Cannot connect to url: " << url << " / error : " << errMsg;
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::CannotConnect,
|
|
||||||
headers, payload, ss.str(),
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make a new cancellation object dealing with transfer timeout
|
|
||||||
isCancellationRequested =
|
|
||||||
makeCancellationRequestWithTimeout(args->transferTimeout, requestInitCancellation);
|
|
||||||
|
|
||||||
if (args->verbose)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "Sending " << verb << " request "
|
|
||||||
<< "to " << host << ":" << port << std::endl
|
|
||||||
<< "request size: " << req.size() << " bytes" << std::endl
|
|
||||||
<< "=============" << std::endl
|
|
||||||
<< req
|
|
||||||
<< "=============" << std::endl
|
|
||||||
<< std::endl;
|
|
||||||
|
|
||||||
log(ss.str(), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_socket->writeBytes(req, isCancellationRequested))
|
|
||||||
{
|
|
||||||
std::string errorMsg("Cannot send request");
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::SendError,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
uploadSize = req.size();
|
|
||||||
|
|
||||||
auto lineResult = _socket->readLine(isCancellationRequested);
|
|
||||||
auto lineValid = lineResult.first;
|
|
||||||
auto line = lineResult.second;
|
|
||||||
|
|
||||||
if (!lineValid)
|
|
||||||
{
|
|
||||||
std::string errorMsg("Cannot retrieve status line");
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::CannotReadStatusLine,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args->verbose)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "Status line " << line;
|
|
||||||
log(ss.str(), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sscanf(line.c_str(), "HTTP/1.1 %d", &code) != 1)
|
|
||||||
{
|
|
||||||
std::string errorMsg("Cannot parse response code from status line");
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::MissingStatus,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto result = parseHttpHeaders(_socket, isCancellationRequested);
|
|
||||||
auto headersValid = result.first;
|
|
||||||
headers = result.second;
|
|
||||||
|
|
||||||
if (!headersValid)
|
|
||||||
{
|
|
||||||
std::string errorMsg("Cannot parse http headers");
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::HeaderParsingError,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Redirect ?
|
|
||||||
if ((code >= 301 && code <= 308) && args->followRedirects)
|
|
||||||
{
|
|
||||||
if (headers.find("Location") == headers.end())
|
|
||||||
{
|
|
||||||
std::string errorMsg("Missing location header for redirect");
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::MissingLocation,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (redirects >= args->maxRedirects)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "Too many redirects: " << redirects;
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::TooManyRedirects,
|
|
||||||
headers, payload, ss.str(),
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recurse
|
|
||||||
std::string location = headers["Location"];
|
|
||||||
return request(location, verb, body, args, redirects+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (verb == "HEAD")
|
|
||||||
{
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::Ok,
|
|
||||||
headers, payload, std::string(),
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse response:
|
|
||||||
if (headers.find("Content-Length") != headers.end())
|
|
||||||
{
|
|
||||||
ssize_t contentLength = -1;
|
|
||||||
ss.str("");
|
|
||||||
ss << headers["Content-Length"];
|
|
||||||
ss >> contentLength;
|
|
||||||
|
|
||||||
payload.reserve(contentLength);
|
|
||||||
|
|
||||||
auto chunkResult = _socket->readBytes(contentLength,
|
|
||||||
args->onProgressCallback,
|
|
||||||
isCancellationRequested);
|
|
||||||
if (!chunkResult.first)
|
|
||||||
{
|
|
||||||
errorMsg = "Cannot read chunk";
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::ChunkReadError,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
payload += chunkResult.second;
|
|
||||||
}
|
|
||||||
else if (headers.find("Transfer-Encoding") != headers.end() &&
|
|
||||||
headers["Transfer-Encoding"] == "chunked")
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
lineResult = _socket->readLine(isCancellationRequested);
|
|
||||||
line = lineResult.second;
|
|
||||||
|
|
||||||
if (!lineResult.first)
|
|
||||||
{
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::ChunkReadError,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t chunkSize;
|
|
||||||
ss.str("");
|
|
||||||
ss << std::hex << line;
|
|
||||||
ss >> chunkSize;
|
|
||||||
|
|
||||||
if (args->verbose)
|
|
||||||
{
|
|
||||||
std::stringstream oss;
|
|
||||||
oss << "Reading " << chunkSize << " bytes"
|
|
||||||
<< std::endl;
|
|
||||||
log(oss.str(), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
payload.reserve(payload.size() + (size_t) chunkSize);
|
|
||||||
|
|
||||||
// Read a chunk
|
|
||||||
auto chunkResult = _socket->readBytes((size_t) chunkSize,
|
|
||||||
args->onProgressCallback,
|
|
||||||
isCancellationRequested);
|
|
||||||
if (!chunkResult.first)
|
|
||||||
{
|
|
||||||
errorMsg = "Cannot read chunk";
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::ChunkReadError,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
payload += chunkResult.second;
|
|
||||||
|
|
||||||
// Read the line that terminates the chunk (\r\n)
|
|
||||||
lineResult = _socket->readLine(isCancellationRequested);
|
|
||||||
|
|
||||||
if (!lineResult.first)
|
|
||||||
{
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::ChunkReadError,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (chunkSize == 0) break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (code == 204)
|
|
||||||
{
|
|
||||||
; // 204 is NoContent response code
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::string errorMsg("Cannot read http body");
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::CannotReadBody,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
downloadSize = payload.size();
|
|
||||||
|
|
||||||
// If the content was compressed with gzip, decode it
|
|
||||||
if (headers["Content-Encoding"] == "gzip")
|
|
||||||
{
|
|
||||||
std::string decompressedPayload;
|
|
||||||
if (!gzipInflate(payload, decompressedPayload))
|
|
||||||
{
|
|
||||||
std::string errorMsg("Error decompressing payload");
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::Gzip,
|
|
||||||
headers, payload, errorMsg,
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
payload = decompressedPayload;
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::make_shared<HttpResponse>(code, HttpErrorCode::Ok,
|
|
||||||
headers, payload, std::string(),
|
|
||||||
uploadSize, downloadSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::get(const std::string& url,
|
|
||||||
HttpRequestArgsPtr args)
|
|
||||||
{
|
|
||||||
return request(url, kGet, std::string(), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::head(const std::string& url,
|
|
||||||
HttpRequestArgsPtr args)
|
|
||||||
{
|
|
||||||
return request(url, kHead, std::string(), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::del(const std::string& url,
|
|
||||||
HttpRequestArgsPtr args)
|
|
||||||
{
|
|
||||||
return request(url, kDel, std::string(), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::post(const std::string& url,
|
|
||||||
const HttpParameters& httpParameters,
|
|
||||||
HttpRequestArgsPtr args)
|
|
||||||
{
|
|
||||||
return request(url, kPost, serializeHttpParameters(httpParameters), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::post(const std::string& url,
|
|
||||||
const std::string& body,
|
|
||||||
HttpRequestArgsPtr args)
|
|
||||||
{
|
|
||||||
return request(url, kPost, body, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::put(const std::string& url,
|
|
||||||
const HttpParameters& httpParameters,
|
|
||||||
HttpRequestArgsPtr args)
|
|
||||||
{
|
|
||||||
return request(url, kPut, serializeHttpParameters(httpParameters), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpResponsePtr HttpClient::put(const std::string& url,
|
|
||||||
const std::string& body,
|
|
||||||
const HttpRequestArgsPtr args)
|
|
||||||
{
|
|
||||||
return request(url, kPut, body, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string HttpClient::urlEncode(const std::string& value)
|
|
||||||
{
|
|
||||||
std::ostringstream escaped;
|
|
||||||
escaped.fill('0');
|
|
||||||
escaped << std::hex;
|
|
||||||
|
|
||||||
for (std::string::const_iterator i = value.begin(), n = value.end();
|
|
||||||
i != n; ++i)
|
|
||||||
{
|
|
||||||
std::string::value_type c = (*i);
|
|
||||||
|
|
||||||
// Keep alphanumeric and other accepted characters intact
|
|
||||||
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
|
|
||||||
{
|
|
||||||
escaped << c;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Any other characters are percent-encoded
|
|
||||||
escaped << std::uppercase;
|
|
||||||
escaped << '%' << std::setw(2) << int((unsigned char) c);
|
|
||||||
escaped << std::nouppercase;
|
|
||||||
}
|
|
||||||
|
|
||||||
return escaped.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string HttpClient::serializeHttpParameters(const HttpParameters& httpParameters)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
size_t count = httpParameters.size();
|
|
||||||
size_t i = 0;
|
|
||||||
|
|
||||||
for (auto&& it : httpParameters)
|
|
||||||
{
|
|
||||||
ss << urlEncode(it.first)
|
|
||||||
<< "="
|
|
||||||
<< urlEncode(it.second);
|
|
||||||
|
|
||||||
if (i++ < (count-1))
|
|
||||||
{
|
|
||||||
ss << "&";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HttpClient::gzipInflate(
|
|
||||||
const std::string& in,
|
|
||||||
std::string& out)
|
|
||||||
{
|
|
||||||
z_stream inflateState;
|
|
||||||
std::memset(&inflateState, 0, sizeof(inflateState));
|
|
||||||
|
|
||||||
inflateState.zalloc = Z_NULL;
|
|
||||||
inflateState.zfree = Z_NULL;
|
|
||||||
inflateState.opaque = Z_NULL;
|
|
||||||
inflateState.avail_in = 0;
|
|
||||||
inflateState.next_in = Z_NULL;
|
|
||||||
|
|
||||||
if (inflateInit2(&inflateState, 16+MAX_WBITS) != Z_OK)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
inflateState.avail_in = (uInt) in.size();
|
|
||||||
inflateState.next_in = (unsigned char *)(const_cast<char *>(in.data()));
|
|
||||||
|
|
||||||
const int kBufferSize = 1 << 14;
|
|
||||||
|
|
||||||
std::unique_ptr<unsigned char[]> compressBuffer =
|
|
||||||
std::make_unique<unsigned char[]>(kBufferSize);
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
inflateState.avail_out = (uInt) kBufferSize;
|
|
||||||
inflateState.next_out = compressBuffer.get();
|
|
||||||
|
|
||||||
int ret = inflate(&inflateState, Z_SYNC_FLUSH);
|
|
||||||
|
|
||||||
if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR)
|
|
||||||
{
|
|
||||||
inflateEnd(&inflateState);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
out.append(
|
|
||||||
reinterpret_cast<char *>(compressBuffer.get()),
|
|
||||||
kBufferSize - inflateState.avail_out
|
|
||||||
);
|
|
||||||
} while (inflateState.avail_out == 0);
|
|
||||||
|
|
||||||
inflateEnd(&inflateState);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void HttpClient::log(const std::string& msg,
|
|
||||||
HttpRequestArgsPtr args)
|
|
||||||
{
|
|
||||||
if (args->logger)
|
|
||||||
{
|
|
||||||
args->logger(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,161 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXHttpClient.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "IXSocket.h"
|
|
||||||
#include "IXWebSocketHttpHeaders.h"
|
|
||||||
#include <algorithm>
|
|
||||||
#include <atomic>
|
|
||||||
#include <condition_variable>
|
|
||||||
#include <functional>
|
|
||||||
#include <map>
|
|
||||||
#include <memory>
|
|
||||||
#include <mutex>
|
|
||||||
#include <queue>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
enum class HttpErrorCode : int
|
|
||||||
{
|
|
||||||
Ok = 0,
|
|
||||||
CannotConnect = 1,
|
|
||||||
Timeout = 2,
|
|
||||||
Gzip = 3,
|
|
||||||
UrlMalformed = 4,
|
|
||||||
CannotCreateSocket = 5,
|
|
||||||
SendError = 6,
|
|
||||||
ReadError = 7,
|
|
||||||
CannotReadStatusLine = 8,
|
|
||||||
MissingStatus = 9,
|
|
||||||
HeaderParsingError = 10,
|
|
||||||
MissingLocation = 11,
|
|
||||||
TooManyRedirects = 12,
|
|
||||||
ChunkReadError = 13,
|
|
||||||
CannotReadBody = 14,
|
|
||||||
Invalid = 100
|
|
||||||
};
|
|
||||||
|
|
||||||
struct HttpResponse
|
|
||||||
{
|
|
||||||
int statusCode;
|
|
||||||
HttpErrorCode errorCode;
|
|
||||||
WebSocketHttpHeaders headers;
|
|
||||||
std::string payload;
|
|
||||||
std::string errorMsg;
|
|
||||||
uint64_t uploadSize;
|
|
||||||
uint64_t downloadSize;
|
|
||||||
|
|
||||||
HttpResponse(int s = 0,
|
|
||||||
const HttpErrorCode& c = HttpErrorCode::Ok,
|
|
||||||
const WebSocketHttpHeaders& h = WebSocketHttpHeaders(),
|
|
||||||
const std::string& p = std::string(),
|
|
||||||
const std::string& e = std::string(),
|
|
||||||
uint64_t u = 0,
|
|
||||||
uint64_t d = 0)
|
|
||||||
: statusCode(s)
|
|
||||||
, errorCode(c)
|
|
||||||
, headers(h)
|
|
||||||
, payload(p)
|
|
||||||
, errorMsg(e)
|
|
||||||
, uploadSize(u)
|
|
||||||
, downloadSize(d)
|
|
||||||
{
|
|
||||||
;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
using HttpResponsePtr = std::shared_ptr<HttpResponse>;
|
|
||||||
using HttpParameters = std::map<std::string, std::string>;
|
|
||||||
using Logger = std::function<void(const std::string&)>;
|
|
||||||
using OnResponseCallback = std::function<void(const HttpResponsePtr&)>;
|
|
||||||
|
|
||||||
struct HttpRequestArgs
|
|
||||||
{
|
|
||||||
std::string url;
|
|
||||||
std::string verb;
|
|
||||||
WebSocketHttpHeaders extraHeaders;
|
|
||||||
std::string body;
|
|
||||||
int connectTimeout;
|
|
||||||
int transferTimeout;
|
|
||||||
bool followRedirects;
|
|
||||||
int maxRedirects;
|
|
||||||
bool verbose;
|
|
||||||
bool compress;
|
|
||||||
Logger logger;
|
|
||||||
OnProgressCallback onProgressCallback;
|
|
||||||
};
|
|
||||||
|
|
||||||
using HttpRequestArgsPtr = std::shared_ptr<HttpRequestArgs>;
|
|
||||||
|
|
||||||
class HttpClient
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
HttpClient(bool async = false);
|
|
||||||
~HttpClient();
|
|
||||||
|
|
||||||
HttpResponsePtr get(const std::string& url, HttpRequestArgsPtr args);
|
|
||||||
HttpResponsePtr head(const std::string& url, HttpRequestArgsPtr args);
|
|
||||||
HttpResponsePtr del(const std::string& url, HttpRequestArgsPtr args);
|
|
||||||
|
|
||||||
HttpResponsePtr post(const std::string& url,
|
|
||||||
const HttpParameters& httpParameters,
|
|
||||||
HttpRequestArgsPtr args);
|
|
||||||
HttpResponsePtr post(const std::string& url,
|
|
||||||
const std::string& body,
|
|
||||||
HttpRequestArgsPtr args);
|
|
||||||
|
|
||||||
HttpResponsePtr put(const std::string& url,
|
|
||||||
const HttpParameters& httpParameters,
|
|
||||||
HttpRequestArgsPtr args);
|
|
||||||
HttpResponsePtr put(const std::string& url,
|
|
||||||
const std::string& body,
|
|
||||||
HttpRequestArgsPtr args);
|
|
||||||
|
|
||||||
HttpResponsePtr request(const std::string& url,
|
|
||||||
const std::string& verb,
|
|
||||||
const std::string& body,
|
|
||||||
HttpRequestArgsPtr args,
|
|
||||||
int redirects = 0);
|
|
||||||
|
|
||||||
// Async API
|
|
||||||
HttpRequestArgsPtr createRequest(const std::string& url = std::string(),
|
|
||||||
const std::string& verb = HttpClient::kGet);
|
|
||||||
|
|
||||||
bool performRequest(HttpRequestArgsPtr request,
|
|
||||||
const OnResponseCallback& onResponseCallback);
|
|
||||||
|
|
||||||
std::string serializeHttpParameters(const HttpParameters& httpParameters);
|
|
||||||
|
|
||||||
std::string urlEncode(const std::string& value);
|
|
||||||
|
|
||||||
const static std::string kPost;
|
|
||||||
const static std::string kGet;
|
|
||||||
const static std::string kHead;
|
|
||||||
const static std::string kDel;
|
|
||||||
const static std::string kPut;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void log(const std::string& msg, HttpRequestArgsPtr args);
|
|
||||||
|
|
||||||
bool gzipInflate(const std::string& in, std::string& out);
|
|
||||||
|
|
||||||
// Async API background thread runner
|
|
||||||
void run();
|
|
||||||
|
|
||||||
// Async API
|
|
||||||
bool _async;
|
|
||||||
std::queue<std::pair<HttpRequestArgsPtr, OnResponseCallback>> _queue;
|
|
||||||
mutable std::mutex _queueMutex;
|
|
||||||
std::condition_variable _condition;
|
|
||||||
std::atomic<bool> _stop;
|
|
||||||
std::thread _thread;
|
|
||||||
|
|
||||||
std::shared_ptr<Socket> _socket;
|
|
||||||
std::mutex _mutex; // to protect accessing the _socket (only one socket per client)
|
|
||||||
};
|
|
||||||
} // namespace ix
|
|
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXNetSystem.cpp
|
|
||||||
* Author: Korchynskyi Dmytro
|
|
||||||
* Copyright (c) 2019 Machine Zone. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "IXNetSystem.h"
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
bool initNetSystem()
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
WORD wVersionRequested;
|
|
||||||
WSADATA wsaData;
|
|
||||||
int err;
|
|
||||||
|
|
||||||
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
|
|
||||||
wVersionRequested = MAKEWORD(2, 2);
|
|
||||||
|
|
||||||
err = WSAStartup(wVersionRequested, &wsaData);
|
|
||||||
|
|
||||||
return err == 0;
|
|
||||||
#else
|
|
||||||
return true;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
bool uninitNetSystem()
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
int err = WSACleanup();
|
|
||||||
|
|
||||||
return err == 0;
|
|
||||||
#else
|
|
||||||
return true;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
@ -7,25 +7,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <WS2tcpip.h>
|
# include <WS2tcpip.h>
|
||||||
#include <WinSock2.h>
|
# include <WinSock2.h>
|
||||||
#include <basetsd.h>
|
# include <basetsd.h>
|
||||||
#include <io.h>
|
# include <io.h>
|
||||||
#include <ws2def.h>
|
# include <ws2def.h>
|
||||||
#else
|
#else
|
||||||
#include <arpa/inet.h>
|
# include <arpa/inet.h>
|
||||||
#include <errno.h>
|
# include <errno.h>
|
||||||
#include <netdb.h>
|
# include <netdb.h>
|
||||||
#include <netinet/tcp.h>
|
# include <netinet/tcp.h>
|
||||||
#include <sys/select.h>
|
# include <sys/select.h>
|
||||||
#include <sys/socket.h>
|
# include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
# include <sys/stat.h>
|
||||||
#include <sys/time.h>
|
# include <sys/time.h>
|
||||||
#include <unistd.h>
|
# include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
bool initNetSystem();
|
|
||||||
bool uninitNetSystem();
|
|
||||||
} // namespace ix
|
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXProgressCallback.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
using OnProgressCallback = std::function<bool(int current, int total)>;
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSelectInterrupt.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "IXSelectInterrupt.h"
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
SelectInterrupt::SelectInterrupt()
|
|
||||||
{
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
SelectInterrupt::~SelectInterrupt()
|
|
||||||
{
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SelectInterrupt::init(std::string& /*errorMsg*/)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SelectInterrupt::notify(uint64_t /*value*/)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t SelectInterrupt::read()
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SelectInterrupt::clear()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SelectInterrupt::getFd() const
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSelectInterrupt.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
class SelectInterrupt
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
SelectInterrupt();
|
|
||||||
virtual ~SelectInterrupt();
|
|
||||||
|
|
||||||
virtual bool init(std::string& errorMsg);
|
|
||||||
|
|
||||||
virtual bool notify(uint64_t value);
|
|
||||||
virtual bool clear();
|
|
||||||
virtual uint64_t read();
|
|
||||||
virtual int getFd() const;
|
|
||||||
};
|
|
||||||
} // namespace ix
|
|
@ -1,116 +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 <sys/eventfd.h>
|
|
||||||
|
|
||||||
#include <unistd.h> // for write
|
|
||||||
#include <string.h> // for strerror
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -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
|
|
@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSelectInterruptFactory.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "IXSelectInterruptFactory.h"
|
|
||||||
|
|
||||||
#if defined(__linux__) || defined(__APPLE__)
|
|
||||||
# include <ixwebsocket/IXSelectInterruptPipe.h>
|
|
||||||
#else
|
|
||||||
# include <ixwebsocket/IXSelectInterrupt.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
std::shared_ptr<SelectInterrupt> createSelectInterrupt()
|
|
||||||
{
|
|
||||||
#if defined(__linux__) || defined(__APPLE__)
|
|
||||||
return std::make_shared<SelectInterruptPipe>();
|
|
||||||
#else
|
|
||||||
return std::make_shared<SelectInterrupt>();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSelectInterruptFactory.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
class SelectInterrupt;
|
|
||||||
std::shared_ptr<SelectInterrupt> createSelectInterrupt();
|
|
||||||
} // namespace ix
|
|
@ -1,146 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSelectInterruptPipe.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//
|
|
||||||
// On macOS we use UNIX pipes to wake up select.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "IXSelectInterruptPipe.h"
|
|
||||||
|
|
||||||
#include <unistd.h> // for write
|
|
||||||
#include <string.h> // for strerror
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
// File descriptor at index 0 in _fildes is the read end of the pipe
|
|
||||||
// File descriptor at index 1 in _fildes is the write end of the pipe
|
|
||||||
const int SelectInterruptPipe::kPipeReadIndex = 0;
|
|
||||||
const int SelectInterruptPipe::kPipeWriteIndex = 1;
|
|
||||||
|
|
||||||
SelectInterruptPipe::SelectInterruptPipe()
|
|
||||||
{
|
|
||||||
_fildes[kPipeReadIndex] = -1;
|
|
||||||
_fildes[kPipeWriteIndex] = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
SelectInterruptPipe::~SelectInterruptPipe()
|
|
||||||
{
|
|
||||||
::close(_fildes[kPipeReadIndex]);
|
|
||||||
::close(_fildes[kPipeWriteIndex]);
|
|
||||||
_fildes[kPipeReadIndex] = -1;
|
|
||||||
_fildes[kPipeWriteIndex] = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SelectInterruptPipe::init(std::string& errorMsg)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_fildesMutex);
|
|
||||||
|
|
||||||
// calling init twice is a programming error
|
|
||||||
assert(_fildes[kPipeReadIndex] == -1);
|
|
||||||
assert(_fildes[kPipeWriteIndex] == -1);
|
|
||||||
|
|
||||||
if (pipe(_fildes) < 0)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "SelectInterruptPipe::init() failed in pipe() call"
|
|
||||||
<< " : " << strerror(errno);
|
|
||||||
errorMsg = ss.str();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fcntl(_fildes[kPipeReadIndex], F_SETFL, O_NONBLOCK) == -1)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "SelectInterruptPipe::init() failed in fcntl(..., O_NONBLOCK) call"
|
|
||||||
<< " : " << strerror(errno);
|
|
||||||
errorMsg = ss.str();
|
|
||||||
|
|
||||||
_fildes[kPipeReadIndex] = -1;
|
|
||||||
_fildes[kPipeWriteIndex] = -1;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fcntl(_fildes[kPipeWriteIndex], F_SETFL, O_NONBLOCK) == -1)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "SelectInterruptPipe::init() failed in fcntl(..., O_NONBLOCK) call"
|
|
||||||
<< " : " << strerror(errno);
|
|
||||||
errorMsg = ss.str();
|
|
||||||
|
|
||||||
_fildes[kPipeReadIndex] = -1;
|
|
||||||
_fildes[kPipeWriteIndex] = -1;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef F_SETNOSIGPIPE
|
|
||||||
if (fcntl(_fildes[kPipeWriteIndex], F_SETNOSIGPIPE, 1) == -1)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "SelectInterruptPipe::init() failed in fcntl(.... F_SETNOSIGPIPE) call"
|
|
||||||
<< " : " << strerror(errno);
|
|
||||||
errorMsg = ss.str();
|
|
||||||
|
|
||||||
_fildes[kPipeReadIndex] = -1;
|
|
||||||
_fildes[kPipeWriteIndex] = -1;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fcntl(_fildes[kPipeWriteIndex], F_SETNOSIGPIPE, 1) == -1)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "SelectInterruptPipe::init() failed in fcntl(..., F_SETNOSIGPIPE) call"
|
|
||||||
<< " : " << strerror(errno);
|
|
||||||
errorMsg = ss.str();
|
|
||||||
|
|
||||||
_fildes[kPipeReadIndex] = -1;
|
|
||||||
_fildes[kPipeWriteIndex] = -1;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SelectInterruptPipe::notify(uint64_t value)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_fildesMutex);
|
|
||||||
|
|
||||||
int fd = _fildes[kPipeWriteIndex];
|
|
||||||
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 SelectInterruptPipe::read()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_fildesMutex);
|
|
||||||
|
|
||||||
int fd = _fildes[kPipeReadIndex];
|
|
||||||
|
|
||||||
uint64_t value = 0;
|
|
||||||
::read(fd, &value, sizeof(value));
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SelectInterruptPipe::clear()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SelectInterruptPipe::getFd() const
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_fildesMutex);
|
|
||||||
|
|
||||||
return _fildes[kPipeReadIndex];
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSelectInterruptPipe.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "IXSelectInterrupt.h"
|
|
||||||
#include <mutex>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
class SelectInterruptPipe final : public SelectInterrupt
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
SelectInterruptPipe();
|
|
||||||
virtual ~SelectInterruptPipe();
|
|
||||||
|
|
||||||
bool init(std::string& errorMsg) final;
|
|
||||||
|
|
||||||
bool notify(uint64_t value) final;
|
|
||||||
bool clear() final;
|
|
||||||
uint64_t read() final;
|
|
||||||
int getFd() const final;
|
|
||||||
|
|
||||||
private:
|
|
||||||
// Store file descriptors used by the communication pipe. Communication
|
|
||||||
// happens between a control thread and a background thread, which is
|
|
||||||
// blocked on select.
|
|
||||||
int _fildes[2];
|
|
||||||
mutable std::mutex _fildesMutex;
|
|
||||||
|
|
||||||
// Used to identify the read/write idx
|
|
||||||
static const int kPipeReadIndex;
|
|
||||||
static const int kPipeWriteIndex;
|
|
||||||
};
|
|
||||||
} // namespace ix
|
|
@ -10,3 +10,4 @@ namespace ix
|
|||||||
{
|
{
|
||||||
void setThreadName(const std::string& name);
|
void setThreadName(const std::string& name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
#include "IXSocket.h"
|
#include "IXSocket.h"
|
||||||
#include "IXSocketConnect.h"
|
#include "IXSocketConnect.h"
|
||||||
#include "IXNetSystem.h"
|
#include "IXNetSystem.h"
|
||||||
#include "IXSelectInterrupt.h"
|
|
||||||
#include "IXSelectInterruptFactory.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -17,26 +15,20 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <poll.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#ifdef min
|
namespace ix
|
||||||
#undef min
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
{
|
||||||
const int Socket::kDefaultPollNoTimeout = -1; // No poll timeout by default
|
const int Socket::kDefaultPollNoTimeout = -1; // No poll timeout by default
|
||||||
const int Socket::kDefaultPollTimeout = kDefaultPollNoTimeout;
|
const int Socket::kDefaultPollTimeout = kDefaultPollNoTimeout;
|
||||||
const uint64_t Socket::kSendRequest = 1;
|
|
||||||
const uint64_t Socket::kCloseRequest = 2;
|
|
||||||
constexpr size_t Socket::kChunkSize;
|
|
||||||
|
|
||||||
Socket::Socket(int fd) :
|
Socket::Socket(int fd) :
|
||||||
_sockfd(fd),
|
_sockfd(fd)
|
||||||
_selectInterrupt(createSelectInterrupt())
|
|
||||||
{
|
{
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket::~Socket()
|
Socket::~Socket()
|
||||||
@ -44,102 +36,48 @@ namespace ix
|
|||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
PollResultType Socket::poll(int timeoutMs)
|
void Socket::poll(const OnPollCallback& onPollCallback, int timeoutSecs)
|
||||||
{
|
{
|
||||||
return isReadyToRead(timeoutMs);
|
if (_sockfd == -1)
|
||||||
}
|
|
||||||
|
|
||||||
PollResultType Socket::select(bool readyToRead, int timeoutMs)
|
|
||||||
{
|
|
||||||
fd_set rfds;
|
|
||||||
fd_set wfds;
|
|
||||||
FD_ZERO(&rfds);
|
|
||||||
FD_ZERO(&wfds);
|
|
||||||
|
|
||||||
fd_set* fds = (readyToRead) ? &rfds : & wfds;
|
|
||||||
if (_sockfd != -1)
|
|
||||||
{
|
{
|
||||||
FD_SET(_sockfd, fds);
|
onPollCallback(PollResultType_Error);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// File descriptor used to interrupt select when needed
|
#ifdef __linux__
|
||||||
int interruptFd = _selectInterrupt->getFd();
|
constexpr int nfds = 2;
|
||||||
if (interruptFd != -1)
|
#else
|
||||||
{
|
constexpr int nfds = 1;
|
||||||
FD_SET(interruptFd, fds);
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
struct timeval timeout;
|
struct pollfd fds[nfds];
|
||||||
timeout.tv_sec = timeoutMs / 1000;
|
fds[0].fd = _sockfd;
|
||||||
timeout.tv_usec = 1000 * (timeoutMs % 1000);
|
fds[0].events = POLLIN;
|
||||||
|
|
||||||
// Compute the highest fd.
|
#ifdef __linux__
|
||||||
int sockfd = _sockfd;
|
fds[1].fd = _eventfd.getFd();
|
||||||
int nfds = (std::max)(sockfd, interruptFd);
|
fds[1].events = POLLIN;
|
||||||
|
#endif
|
||||||
|
int ret = ::poll(fds, nfds, timeoutSecs * 1000);
|
||||||
|
|
||||||
int ret = ::select(nfds + 1, &rfds, &wfds, nullptr,
|
PollResultType pollResult = PollResultType_ReadyForRead;
|
||||||
(timeoutMs < 0) ? nullptr : &timeout);
|
|
||||||
|
|
||||||
PollResultType pollResult = PollResultType::ReadyForRead;
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
pollResult = PollResultType::Error;
|
pollResult = PollResultType_Error;
|
||||||
}
|
}
|
||||||
else if (ret == 0)
|
else if (ret == 0)
|
||||||
{
|
{
|
||||||
pollResult = PollResultType::Timeout;
|
pollResult = PollResultType_Timeout;
|
||||||
}
|
|
||||||
else if (interruptFd != -1 && FD_ISSET(interruptFd, &rfds))
|
|
||||||
{
|
|
||||||
uint64_t value = _selectInterrupt->read();
|
|
||||||
|
|
||||||
if (value == kSendRequest)
|
|
||||||
{
|
|
||||||
pollResult = PollResultType::SendRequest;
|
|
||||||
}
|
|
||||||
else if (value == kCloseRequest)
|
|
||||||
{
|
|
||||||
pollResult = PollResultType::CloseRequest;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (sockfd != -1 && readyToRead && FD_ISSET(sockfd, &rfds))
|
|
||||||
{
|
|
||||||
pollResult = PollResultType::ReadyForRead;
|
|
||||||
}
|
|
||||||
else if (sockfd != -1 && !readyToRead && FD_ISSET(sockfd, &wfds))
|
|
||||||
{
|
|
||||||
pollResult = PollResultType::ReadyForWrite;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pollResult;
|
onPollCallback(pollResult);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PollResultType Socket::isReadyToRead(int timeoutMs)
|
void Socket::wakeUpFromPoll()
|
||||||
{
|
{
|
||||||
if (_sockfd == -1)
|
// this will wake up the thread blocked on select, only needed on Linux
|
||||||
{
|
_eventfd.notify();
|
||||||
return PollResultType::Error;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool readyToRead = true;
|
|
||||||
return select(readyToRead, timeoutMs);
|
|
||||||
}
|
|
||||||
|
|
||||||
PollResultType Socket::isReadyToWrite(int timeoutMs)
|
|
||||||
{
|
|
||||||
if (_sockfd == -1)
|
|
||||||
{
|
|
||||||
return PollResultType::Error;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool readyToRead = false;
|
|
||||||
return select(readyToRead, timeoutMs);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wake up from poll/select by writing to the pipe which is watched by select
|
|
||||||
bool Socket::wakeUpFromPoll(uint64_t wakeUpCode)
|
|
||||||
{
|
|
||||||
return _selectInterrupt->notify(wakeUpCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Socket::connect(const std::string& host,
|
bool Socket::connect(const std::string& host,
|
||||||
@ -149,7 +87,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_socketMutex);
|
std::lock_guard<std::mutex> lock(_socketMutex);
|
||||||
|
|
||||||
if (!_selectInterrupt->clear()) return false;
|
if (!_eventfd.clear()) return false;
|
||||||
|
|
||||||
_sockfd = SocketConnect::connect(host, port, errMsg, isCancellationRequested);
|
_sockfd = SocketConnect::connect(host, port, errMsg, isCancellationRequested);
|
||||||
return _sockfd != -1;
|
return _sockfd != -1;
|
||||||
@ -192,27 +130,11 @@ namespace ix
|
|||||||
|
|
||||||
int Socket::getErrno()
|
int Socket::getErrno()
|
||||||
{
|
{
|
||||||
int err;
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
err = WSAGetLastError();
|
return WSAGetLastError();
|
||||||
#else
|
#else
|
||||||
err = errno;
|
return errno;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Socket::isWaitNeeded()
|
|
||||||
{
|
|
||||||
int err = getErrno();
|
|
||||||
|
|
||||||
if (err == EWOULDBLOCK || err == EAGAIN || err == EINPROGRESS)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::closeSocket(int fd)
|
void Socket::closeSocket(int fd)
|
||||||
@ -224,9 +146,69 @@ namespace ix
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Socket::init(std::string& errorMsg)
|
bool Socket::init()
|
||||||
{
|
{
|
||||||
return _selectInterrupt->init(errorMsg);
|
#ifdef _WIN32
|
||||||
|
INT rc;
|
||||||
|
WSADATA wsaData;
|
||||||
|
|
||||||
|
rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||||
|
return rc != 0;
|
||||||
|
#else
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void Socket::cleanup()
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
WSACleanup();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Socket::readByte(void* buffer,
|
||||||
|
const CancellationRequest& isCancellationRequested)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (isCancellationRequested()) return false;
|
||||||
|
|
||||||
|
ssize_t ret;
|
||||||
|
ret = recv(buffer, 1);
|
||||||
|
|
||||||
|
// We read one byte, as needed, all good.
|
||||||
|
if (ret == 1)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// There is possibly something to be read, try again
|
||||||
|
else if (ret < 0 && (getErrno() == EWOULDBLOCK ||
|
||||||
|
getErrno() == EAGAIN))
|
||||||
|
{
|
||||||
|
// Wait with a timeout until something is written.
|
||||||
|
// This way we are not busy looping
|
||||||
|
fd_set rfds;
|
||||||
|
struct timeval timeout;
|
||||||
|
timeout.tv_sec = 0;
|
||||||
|
timeout.tv_usec = 1 * 1000; // 1ms timeout
|
||||||
|
|
||||||
|
FD_ZERO(&rfds);
|
||||||
|
FD_SET(_sockfd, &rfds);
|
||||||
|
|
||||||
|
if (select(_sockfd + 1, &rfds, nullptr, nullptr, &timeout) < 0 &&
|
||||||
|
(errno == EBADF || errno == EINVAL))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// There was an error during the read, abort
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Socket::writeBytes(const std::string& str,
|
bool Socket::writeBytes(const std::string& str,
|
||||||
@ -234,7 +216,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (isCancellationRequested && isCancellationRequested()) return false;
|
if (isCancellationRequested()) return false;
|
||||||
|
|
||||||
char* buffer = const_cast<char*>(str.c_str());
|
char* buffer = const_cast<char*>(str.c_str());
|
||||||
int len = (int) str.size();
|
int len = (int) str.size();
|
||||||
@ -246,8 +228,9 @@ namespace ix
|
|||||||
{
|
{
|
||||||
return ret == len;
|
return ret == len;
|
||||||
}
|
}
|
||||||
// There is possibly something to be writen, try again
|
// There is possibly something to be write, try again
|
||||||
else if (ret < 0 && Socket::isWaitNeeded())
|
else if (ret < 0 && (getErrno() == EWOULDBLOCK ||
|
||||||
|
getErrno() == EAGAIN))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -259,41 +242,7 @@ namespace ix
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Socket::readByte(void* buffer,
|
std::pair<bool, std::string> Socket::readLine(const CancellationRequest& isCancellationRequested)
|
||||||
const CancellationRequest& isCancellationRequested)
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (isCancellationRequested && isCancellationRequested()) return false;
|
|
||||||
|
|
||||||
ssize_t ret;
|
|
||||||
ret = recv(buffer, 1);
|
|
||||||
|
|
||||||
// We read one byte, as needed, all good.
|
|
||||||
if (ret == 1)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// There is possibly something to be read, try again
|
|
||||||
else if (ret < 0 && Socket::isWaitNeeded())
|
|
||||||
{
|
|
||||||
// Wait with a 1ms timeout until the socket is ready to read.
|
|
||||||
// This way we are not busy looping
|
|
||||||
if (isReadyToRead(1) == PollResultType::Error)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// There was an error during the read, abort
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<bool, std::string> Socket::readLine(
|
|
||||||
const CancellationRequest& isCancellationRequested)
|
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
std::string line;
|
std::string line;
|
||||||
@ -303,8 +252,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
if (!readByte(&c, isCancellationRequested))
|
if (!readByte(&c, isCancellationRequested))
|
||||||
{
|
{
|
||||||
// Return what we were able to read
|
return std::make_pair(false, std::string());
|
||||||
return std::make_pair(false, line);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
line += c;
|
line += c;
|
||||||
@ -312,51 +260,4 @@ namespace ix
|
|||||||
|
|
||||||
return std::make_pair(true, line);
|
return std::make_pair(true, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<bool, std::string> Socket::readBytes(
|
|
||||||
size_t length,
|
|
||||||
const OnProgressCallback& onProgressCallback,
|
|
||||||
const CancellationRequest& isCancellationRequested)
|
|
||||||
{
|
|
||||||
if (_readBuffer.empty())
|
|
||||||
{
|
|
||||||
_readBuffer.resize(kChunkSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<uint8_t> output;
|
|
||||||
while (output.size() != length)
|
|
||||||
{
|
|
||||||
if (isCancellationRequested && isCancellationRequested())
|
|
||||||
{
|
|
||||||
return std::make_pair(false, std::string());
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size = std::min(kChunkSize, length - output.size());
|
|
||||||
ssize_t ret = recv((char*)&_readBuffer[0], size);
|
|
||||||
|
|
||||||
if (ret <= 0 && !Socket::isWaitNeeded())
|
|
||||||
{
|
|
||||||
// Error
|
|
||||||
return std::make_pair(false, std::string());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
output.insert(output.end(),
|
|
||||||
_readBuffer.begin(),
|
|
||||||
_readBuffer.begin() + ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (onProgressCallback) onProgressCallback((int) output.size(), (int) length);
|
|
||||||
|
|
||||||
// Wait with a 1ms timeout until the socket is ready to read.
|
|
||||||
// This way we are not busy looping
|
|
||||||
if (isReadyToRead(1) == PollResultType::Error)
|
|
||||||
{
|
|
||||||
return std::make_pair(false, std::string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::make_pair(true, std::string(output.begin(),
|
|
||||||
output.end()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -6,65 +6,43 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
|
||||||
#include <mutex>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <functional>
|
||||||
|
#include <mutex>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <BaseTsd.h>
|
#include <BaseTsd.h>
|
||||||
typedef SSIZE_T ssize_t;
|
typedef SSIZE_T ssize_t;
|
||||||
|
|
||||||
#undef EWOULDBLOCK
|
|
||||||
#undef EAGAIN
|
|
||||||
#undef EINPROGRESS
|
|
||||||
#undef EBADF
|
|
||||||
#undef EINVAL
|
|
||||||
|
|
||||||
// map to WSA error codes
|
|
||||||
#define EWOULDBLOCK WSAEWOULDBLOCK
|
|
||||||
#define EAGAIN WSATRY_AGAIN
|
|
||||||
#define EINPROGRESS WSAEINPROGRESS
|
|
||||||
#define EBADF WSAEBADF
|
|
||||||
#define EINVAL WSAEINVAL
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "IXEventFd.h"
|
||||||
#include "IXCancellationRequest.h"
|
#include "IXCancellationRequest.h"
|
||||||
#include "IXProgressCallback.h"
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class SelectInterrupt;
|
enum PollResultType
|
||||||
|
|
||||||
enum class PollResultType
|
|
||||||
{
|
{
|
||||||
ReadyForRead = 0,
|
PollResultType_ReadyForRead = 0,
|
||||||
ReadyForWrite = 1,
|
PollResultType_Timeout = 1,
|
||||||
Timeout = 2,
|
PollResultType_Error = 2
|
||||||
Error = 3,
|
|
||||||
SendRequest = 4,
|
|
||||||
CloseRequest = 5
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Socket
|
class Socket {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
|
using OnPollCallback = std::function<void(PollResultType)>;
|
||||||
|
|
||||||
Socket(int fd = -1);
|
Socket(int fd = -1);
|
||||||
virtual ~Socket();
|
virtual ~Socket();
|
||||||
bool init(std::string& errorMsg);
|
|
||||||
|
|
||||||
// Functions to check whether there is activity on the socket
|
void configure();
|
||||||
PollResultType poll(int timeoutMs = kDefaultPollTimeout);
|
|
||||||
bool wakeUpFromPoll(uint64_t wakeUpCode);
|
|
||||||
|
|
||||||
PollResultType isReadyToWrite(int timeoutMs);
|
virtual void poll(const OnPollCallback& onPollCallback,
|
||||||
PollResultType isReadyToRead(int timeoutMs);
|
int timeoutSecs = kDefaultPollTimeout);
|
||||||
|
virtual void wakeUpFromPoll();
|
||||||
|
|
||||||
// Virtual methods
|
// Virtual methods
|
||||||
virtual bool connect(const std::string& url,
|
virtual bool connect(const std::string& url,
|
||||||
int port,
|
int port,
|
||||||
std::string& errMsg,
|
std::string& errMsg,
|
||||||
const CancellationRequest& isCancellationRequested);
|
const CancellationRequest& isCancellationRequested);
|
||||||
@ -76,36 +54,25 @@ namespace ix
|
|||||||
|
|
||||||
// Blocking and cancellable versions, working with socket that can be set
|
// Blocking and cancellable versions, working with socket that can be set
|
||||||
// to non blocking mode. Used during HTTP upgrade.
|
// to non blocking mode. Used during HTTP upgrade.
|
||||||
bool readByte(void* buffer, const CancellationRequest& isCancellationRequested);
|
bool readByte(void* buffer,
|
||||||
bool writeBytes(const std::string& str, const CancellationRequest& isCancellationRequested);
|
const CancellationRequest& isCancellationRequested);
|
||||||
|
bool writeBytes(const std::string& str,
|
||||||
|
const CancellationRequest& isCancellationRequested);
|
||||||
std::pair<bool, std::string> readLine(const CancellationRequest& isCancellationRequested);
|
std::pair<bool, std::string> readLine(const CancellationRequest& isCancellationRequested);
|
||||||
std::pair<bool, std::string> readBytes(size_t length,
|
|
||||||
const OnProgressCallback& onProgressCallback,
|
|
||||||
const CancellationRequest& isCancellationRequested);
|
|
||||||
|
|
||||||
static int getErrno();
|
static int getErrno();
|
||||||
static bool isWaitNeeded();
|
static bool init(); // Required on Windows to initialize WinSocket
|
||||||
static void closeSocket(int fd);
|
static void cleanup(); // Required on Windows to cleanup WinSocket
|
||||||
|
|
||||||
// Used as special codes for pipe communication
|
|
||||||
static const uint64_t kSendRequest;
|
|
||||||
static const uint64_t kCloseRequest;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void closeSocket(int fd);
|
||||||
|
|
||||||
std::atomic<int> _sockfd;
|
std::atomic<int> _sockfd;
|
||||||
std::mutex _socketMutex;
|
std::mutex _socketMutex;
|
||||||
|
EventFd _eventfd;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PollResultType select(bool readyToRead, int timeoutMs);
|
|
||||||
|
|
||||||
static const int kDefaultPollTimeout;
|
static const int kDefaultPollTimeout;
|
||||||
static const int kDefaultPollNoTimeout;
|
static const int kDefaultPollNoTimeout;
|
||||||
|
|
||||||
// Buffer for reading from our socket. That buffer is never resized.
|
|
||||||
std::vector<uint8_t> _readBuffer;
|
|
||||||
static constexpr size_t kChunkSize = 1 << 15;
|
|
||||||
|
|
||||||
std::shared_ptr<SelectInterrupt> _selectInterrupt;
|
|
||||||
};
|
};
|
||||||
} // namespace ix
|
}
|
||||||
|
@ -20,6 +20,8 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#define socketerrno errno
|
#define socketerrno errno
|
||||||
|
|
||||||
@ -48,7 +50,7 @@ OSStatus read_from_socket(SSLConnectionRef connection, void *data, size_t *len)
|
|||||||
else
|
else
|
||||||
return noErr;
|
return noErr;
|
||||||
}
|
}
|
||||||
else if (0 == status)
|
else if (0 == status)
|
||||||
{
|
{
|
||||||
*len = 0;
|
*len = 0;
|
||||||
return errSSLClosedGraceful;
|
return errSSLClosedGraceful;
|
||||||
@ -100,7 +102,7 @@ OSStatus write_to_socket(SSLConnectionRef connection, const void *data, size_t *
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
*len = 0;
|
*len = 0;
|
||||||
if (EAGAIN == errno)
|
if (EAGAIN == errno)
|
||||||
{
|
{
|
||||||
return errSSLWouldBlock;
|
return errSSLWouldBlock;
|
||||||
}
|
}
|
||||||
@ -139,7 +141,7 @@ std::string getSSLErrorDescription(OSStatus status)
|
|||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
SocketAppleSSL::SocketAppleSSL(int fd) : Socket(fd),
|
SocketAppleSSL::SocketAppleSSL(int fd) : Socket(fd),
|
||||||
_sslContext(nullptr)
|
_sslContext(nullptr)
|
||||||
@ -174,11 +176,11 @@ namespace ix
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
status = SSLHandshake(_sslContext);
|
status = SSLHandshake(_sslContext);
|
||||||
} while (errSSLWouldBlock == status ||
|
} while (errSSLWouldBlock == status ||
|
||||||
errSSLServerAuthCompleted == status);
|
errSSLServerAuthCompleted == status);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (noErr != status)
|
if (noErr != status)
|
||||||
{
|
{
|
||||||
errMsg = getSSLErrorDescription(status);
|
errMsg = getSSLErrorDescription(status);
|
||||||
close();
|
close();
|
||||||
@ -228,7 +230,7 @@ namespace ix
|
|||||||
ssize_t SocketAppleSSL::recv(void* buf, size_t nbyte)
|
ssize_t SocketAppleSSL::recv(void* buf, size_t nbyte)
|
||||||
{
|
{
|
||||||
OSStatus status = errSSLWouldBlock;
|
OSStatus status = errSSLWouldBlock;
|
||||||
while (errSSLWouldBlock == status)
|
while (errSSLWouldBlock == status)
|
||||||
{
|
{
|
||||||
size_t processed = 0;
|
size_t processed = 0;
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
@ -237,7 +239,7 @@ namespace ix
|
|||||||
if (processed > 0)
|
if (processed > 0)
|
||||||
return (ssize_t) processed;
|
return (ssize_t) processed;
|
||||||
|
|
||||||
// The connection was reset, inform the caller that this
|
// The connection was reset, inform the caller that this
|
||||||
// Socket should close
|
// Socket should close
|
||||||
if (status == errSSLClosedGraceful ||
|
if (status == errSSLClosedGraceful ||
|
||||||
status == errSSLClosedNoNotify ||
|
status == errSSLClosedNoNotify ||
|
||||||
|
@ -6,21 +6,23 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IXCancellationRequest.h"
|
|
||||||
#include "IXSocket.h"
|
#include "IXSocket.h"
|
||||||
#include <Security/SecureTransport.h>
|
#include "IXCancellationRequest.h"
|
||||||
|
|
||||||
#include <Security/Security.h>
|
#include <Security/Security.h>
|
||||||
|
#include <Security/SecureTransport.h>
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class SocketAppleSSL final : public Socket
|
class SocketAppleSSL : public Socket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SocketAppleSSL(int fd = -1);
|
SocketAppleSSL(int fd = -1);
|
||||||
~SocketAppleSSL();
|
~SocketAppleSSL();
|
||||||
|
|
||||||
virtual bool connect(const std::string& host,
|
virtual bool connect(const std::string& host,
|
||||||
int port,
|
int port,
|
||||||
std::string& errMsg,
|
std::string& errMsg,
|
||||||
const CancellationRequest& isCancellationRequested) final;
|
const CancellationRequest& isCancellationRequested) final;
|
||||||
@ -32,7 +34,7 @@ namespace ix
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
SSLContextRef _sslContext;
|
SSLContextRef _sslContext;
|
||||||
mutable std::mutex _mutex; // AppleSSL routines are not thread-safe
|
mutable std::mutex _mutex; // AppleSSL routines are not thread-safe
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ix
|
}
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
#include "IXSocketConnect.h"
|
#include "IXSocketConnect.h"
|
||||||
#include "IXDNSLookup.h"
|
#include "IXDNSLookup.h"
|
||||||
#include "IXNetSystem.h"
|
#include "IXNetSystem.h"
|
||||||
#include "IXSocket.h"
|
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -19,7 +18,19 @@
|
|||||||
# include <linux/tcp.h>
|
# include <linux/tcp.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace ix
|
namespace
|
||||||
|
{
|
||||||
|
void closeSocket(int fd)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
closesocket(fd);
|
||||||
|
#else
|
||||||
|
::close(fd);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace ix
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// This function can be cancelled every 50 ms
|
// This function can be cancelled every 50 ms
|
||||||
@ -31,7 +42,7 @@ namespace ix
|
|||||||
const CancellationRequest& isCancellationRequested)
|
const CancellationRequest& isCancellationRequested)
|
||||||
{
|
{
|
||||||
errMsg = "no error";
|
errMsg = "no error";
|
||||||
|
|
||||||
int fd = socket(address->ai_family,
|
int fd = socket(address->ai_family,
|
||||||
address->ai_socktype,
|
address->ai_socktype,
|
||||||
address->ai_protocol);
|
address->ai_protocol);
|
||||||
@ -45,30 +56,27 @@ namespace ix
|
|||||||
// block us for too long
|
// block us for too long
|
||||||
SocketConnect::configure(fd);
|
SocketConnect::configure(fd);
|
||||||
|
|
||||||
int res = ::connect(fd, address->ai_addr, address->ai_addrlen);
|
if (::connect(fd, address->ai_addr, address->ai_addrlen) == -1
|
||||||
|
&& errno != EINPROGRESS)
|
||||||
if (res == -1 && !Socket::isWaitNeeded())
|
|
||||||
{
|
{
|
||||||
errMsg = strerror(Socket::getErrno());
|
closeSocket(fd);
|
||||||
Socket::closeSocket(fd);
|
errMsg = strerror(errno);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
if (isCancellationRequested && isCancellationRequested()) // Must handle timeout as well
|
if (isCancellationRequested()) // Must handle timeout as well
|
||||||
{
|
{
|
||||||
Socket::closeSocket(fd);
|
closeSocket(fd);
|
||||||
errMsg = "Cancelled";
|
errMsg = "Cancelled";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// On Linux the timeout needs to be re-initialized everytime
|
// Use select to check the status of the new connection
|
||||||
// http://man7.org/linux/man-pages/man2/select.2.html
|
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
timeout.tv_sec = 0;
|
timeout.tv_sec = 0;
|
||||||
timeout.tv_usec = 10 * 1000; // 10ms timeout
|
timeout.tv_usec = 10 * 1000; // 10ms timeout
|
||||||
|
|
||||||
fd_set wfds;
|
fd_set wfds;
|
||||||
fd_set efds;
|
fd_set efds;
|
||||||
|
|
||||||
@ -77,13 +85,11 @@ namespace ix
|
|||||||
FD_ZERO(&efds);
|
FD_ZERO(&efds);
|
||||||
FD_SET(fd, &efds);
|
FD_SET(fd, &efds);
|
||||||
|
|
||||||
// Use select to check the status of the new connection
|
if (select(fd + 1, nullptr, &wfds, &efds, &timeout) < 0 &&
|
||||||
res = select(fd + 1, nullptr, &wfds, &efds, &timeout);
|
(errno == EBADF || errno == EINVAL))
|
||||||
|
|
||||||
if (res < 0 && (Socket::getErrno() == EBADF || Socket::getErrno() == EINVAL))
|
|
||||||
{
|
{
|
||||||
Socket::closeSocket(fd);
|
closeSocket(fd);
|
||||||
errMsg = std::string("Connect error, select error: ") + strerror(Socket::getErrno());
|
errMsg = std::string("Connect error, select error: ") + strerror(errno);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +110,7 @@ namespace ix
|
|||||||
optval != 0)
|
optval != 0)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
Socket::closeSocket(fd);
|
closeSocket(fd);
|
||||||
errMsg = strerror(optval);
|
errMsg = strerror(optval);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -115,7 +121,7 @@ namespace ix
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket::closeSocket(fd);
|
closeSocket(fd);
|
||||||
errMsg = "connect timed out after 60 seconds";
|
errMsg = "connect timed out after 60 seconds";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -173,7 +179,7 @@ namespace ix
|
|||||||
// 3. (apple) prevent SIGPIPE from being emitted when the remote end disconnect
|
// 3. (apple) prevent SIGPIPE from being emitted when the remote end disconnect
|
||||||
#ifdef SO_NOSIGPIPE
|
#ifdef SO_NOSIGPIPE
|
||||||
int value = 1;
|
int value = 1;
|
||||||
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE,
|
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE,
|
||||||
(void *)&value, sizeof(value));
|
(void *)&value, sizeof(value));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IXCancellationRequest.h"
|
#include "IXCancellationRequest.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct addrinfo;
|
struct addrinfo;
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
class SocketConnect
|
class SocketConnect {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
static int connect(const std::string& hostname,
|
static int connect(const std::string& hostname,
|
||||||
int port,
|
int port,
|
||||||
@ -24,8 +24,9 @@ namespace ix
|
|||||||
static void configure(int sockfd);
|
static void configure(int sockfd);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static int connectToAddress(const struct addrinfo* address,
|
static int connectToAddress(const struct addrinfo *address,
|
||||||
std::string& errMsg,
|
std::string& errMsg,
|
||||||
const CancellationRequest& isCancellationRequested);
|
const CancellationRequest& isCancellationRequested);
|
||||||
};
|
};
|
||||||
} // namespace ix
|
}
|
||||||
|
|
||||||
|
@ -1,78 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSocketFactory.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "IXSocketFactory.h"
|
|
||||||
|
|
||||||
#ifdef IXWEBSOCKET_USE_TLS
|
|
||||||
|
|
||||||
# ifdef IXWEBSOCKET_USE_MBED_TLS
|
|
||||||
# include <ixwebsocket/IXSocketMbedTLS.h>
|
|
||||||
# elif __APPLE__
|
|
||||||
# include <ixwebsocket/IXSocketAppleSSL.h>
|
|
||||||
# elif defined(_WIN32)
|
|
||||||
# include <ixwebsocket/IXSocketSChannel.h>
|
|
||||||
# elif defined(IXWEBSOCKET_USE_OPEN_SSL)
|
|
||||||
# include <ixwebsocket/IXSocketOpenSSL.h>
|
|
||||||
# endif
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#include <ixwebsocket/IXSocket.h>
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
std::shared_ptr<Socket> createSocket(bool tls,
|
|
||||||
std::string& errorMsg)
|
|
||||||
{
|
|
||||||
errorMsg.clear();
|
|
||||||
std::shared_ptr<Socket> socket;
|
|
||||||
|
|
||||||
if (!tls)
|
|
||||||
{
|
|
||||||
socket = std::make_shared<Socket>();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
#ifdef IXWEBSOCKET_USE_TLS
|
|
||||||
# if defined(IXWEBSOCKET_USE_MBED_TLS)
|
|
||||||
socket = std::make_shared<SocketMbedTLS>();
|
|
||||||
# elif defined(__APPLE__)
|
|
||||||
socket = std::make_shared<SocketAppleSSL>();
|
|
||||||
# elif defined(_WIN32)
|
|
||||||
socket = std::make_shared<SocketSChannel>();
|
|
||||||
# else
|
|
||||||
socket = std::make_shared<SocketOpenSSL>();
|
|
||||||
# endif
|
|
||||||
#else
|
|
||||||
errorMsg = "TLS support is not enabled on this platform.";
|
|
||||||
return nullptr;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!socket->init(errorMsg))
|
|
||||||
{
|
|
||||||
socket.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
return socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Socket> createSocket(int fd,
|
|
||||||
std::string& errorMsg)
|
|
||||||
{
|
|
||||||
errorMsg.clear();
|
|
||||||
|
|
||||||
std::shared_ptr<Socket> socket = std::make_shared<Socket>(fd);
|
|
||||||
if (!socket->init(errorMsg))
|
|
||||||
{
|
|
||||||
socket.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
return socket;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
|
|
||||||
/*
|
|
||||||
* IXSocketFactory.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
class Socket;
|
|
||||||
std::shared_ptr<Socket> createSocket(bool tls, std::string& errorMsg);
|
|
||||||
|
|
||||||
std::shared_ptr<Socket> createSocket(int fd, std::string& errorMsg);
|
|
||||||
} // namespace ix
|
|
@ -1,178 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSocketMbedTLS.cpp
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*
|
|
||||||
* Some code taken from
|
|
||||||
* https://github.com/rottor12/WsClientLib/blob/master/lib/src/WsClientLib.cpp
|
|
||||||
* and mini_client.c example from mbedtls
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "IXSocketMbedTLS.h"
|
|
||||||
#include "IXSocketConnect.h"
|
|
||||||
#include "IXNetSystem.h"
|
|
||||||
#include "IXSocket.h"
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
SocketMbedTLS::~SocketMbedTLS()
|
|
||||||
{
|
|
||||||
close();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SocketMbedTLS::init(const std::string& host, std::string& errMsg)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
|
|
||||||
mbedtls_ssl_init(&_ssl);
|
|
||||||
mbedtls_ssl_config_init(&_conf);
|
|
||||||
mbedtls_ctr_drbg_init(&_ctr_drbg);
|
|
||||||
|
|
||||||
const char *pers = "IXSocketMbedTLS";
|
|
||||||
|
|
||||||
mbedtls_entropy_init(&_entropy);
|
|
||||||
if (mbedtls_ctr_drbg_seed(&_ctr_drbg,
|
|
||||||
mbedtls_entropy_func,
|
|
||||||
&_entropy,
|
|
||||||
(const unsigned char *) pers,
|
|
||||||
strlen(pers)) != 0)
|
|
||||||
{
|
|
||||||
errMsg = "Setting entropy seed failed";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mbedtls_ssl_config_defaults(&_conf,
|
|
||||||
MBEDTLS_SSL_IS_CLIENT,
|
|
||||||
MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
||||||
MBEDTLS_SSL_PRESET_DEFAULT ) != 0)
|
|
||||||
{
|
|
||||||
errMsg = "Setting config default failed";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
mbedtls_ssl_conf_rng(&_conf, mbedtls_ctr_drbg_random, &_ctr_drbg);
|
|
||||||
|
|
||||||
// FIXME: cert verification is disabled
|
|
||||||
mbedtls_ssl_conf_authmode(&_conf, MBEDTLS_SSL_VERIFY_NONE);
|
|
||||||
|
|
||||||
if (mbedtls_ssl_setup(&_ssl, &_conf) != 0)
|
|
||||||
{
|
|
||||||
errMsg = "SSL setup failed";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mbedtls_ssl_set_hostname(&_ssl, host.c_str()) != 0)
|
|
||||||
{
|
|
||||||
errMsg = "SNI setup failed";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SocketMbedTLS::connect(const std::string& host,
|
|
||||||
int port,
|
|
||||||
std::string& errMsg,
|
|
||||||
const CancellationRequest& isCancellationRequested)
|
|
||||||
{
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
_sockfd = SocketConnect::connect(host, port, errMsg, isCancellationRequested);
|
|
||||||
if (_sockfd == -1) return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!init(host, errMsg))
|
|
||||||
{
|
|
||||||
close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
mbedtls_ssl_set_bio(&_ssl, &_sockfd, mbedtls_net_send, mbedtls_net_recv, NULL);
|
|
||||||
|
|
||||||
int res;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
res = mbedtls_ssl_handshake(&_ssl);
|
|
||||||
}
|
|
||||||
while (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE);
|
|
||||||
|
|
||||||
if (res != 0)
|
|
||||||
{
|
|
||||||
char buf[256];
|
|
||||||
mbedtls_strerror(res, buf, sizeof(buf));
|
|
||||||
|
|
||||||
errMsg = "error in handshake : ";
|
|
||||||
errMsg += buf;
|
|
||||||
|
|
||||||
close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SocketMbedTLS::close()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
|
|
||||||
mbedtls_ssl_free(&_ssl);
|
|
||||||
mbedtls_ssl_config_free(&_conf);
|
|
||||||
mbedtls_ctr_drbg_free(&_ctr_drbg);
|
|
||||||
mbedtls_entropy_free(&_entropy);
|
|
||||||
|
|
||||||
Socket::close();
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t SocketMbedTLS::send(char* buf, size_t nbyte)
|
|
||||||
{
|
|
||||||
ssize_t sent = 0;
|
|
||||||
|
|
||||||
while (nbyte > 0)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
|
|
||||||
ssize_t res = mbedtls_ssl_write(&_ssl, (unsigned char*) buf, nbyte);
|
|
||||||
|
|
||||||
if (res > 0) {
|
|
||||||
nbyte -= res;
|
|
||||||
sent += res;
|
|
||||||
} else if (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
||||||
errno = EWOULDBLOCK;
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sent;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t SocketMbedTLS::send(const std::string& buffer)
|
|
||||||
{
|
|
||||||
return send((char*)&buffer[0], buffer.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t SocketMbedTLS::recv(void* buf, size_t nbyte)
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(_mutex);
|
|
||||||
|
|
||||||
ssize_t res = mbedtls_ssl_read(&_ssl, (unsigned char*) buf, (int) nbyte);
|
|
||||||
|
|
||||||
if (res > 0)
|
|
||||||
{
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
||||||
{
|
|
||||||
errno = EWOULDBLOCK;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
/*
|
|
||||||
* IXSocketMbedTLS.h
|
|
||||||
* Author: Benjamin Sergeant
|
|
||||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "IXSocket.h"
|
|
||||||
#include <mbedtls/ctr_drbg.h>
|
|
||||||
#include <mbedtls/debug.h>
|
|
||||||
#include <mbedtls/entropy.h>
|
|
||||||
#include <mbedtls/error.h>
|
|
||||||
#include <mbedtls/net.h>
|
|
||||||
#include <mbedtls/platform.h>
|
|
||||||
#include <mutex>
|
|
||||||
|
|
||||||
namespace ix
|
|
||||||
{
|
|
||||||
class SocketMbedTLS final : public Socket
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
SocketMbedTLS() = default;
|
|
||||||
~SocketMbedTLS();
|
|
||||||
|
|
||||||
virtual bool connect(const std::string& host,
|
|
||||||
int port,
|
|
||||||
std::string& errMsg,
|
|
||||||
const CancellationRequest& isCancellationRequested) final;
|
|
||||||
virtual void close() final;
|
|
||||||
|
|
||||||
virtual ssize_t send(char* buffer, size_t length) final;
|
|
||||||
virtual ssize_t send(const std::string& buffer) final;
|
|
||||||
virtual ssize_t recv(void* buffer, size_t length) final;
|
|
||||||
|
|
||||||
private:
|
|
||||||
mbedtls_ssl_context _ssl;
|
|
||||||
mbedtls_ssl_config _conf;
|
|
||||||
mbedtls_entropy_context _entropy;
|
|
||||||
mbedtls_ctr_drbg_context _ctr_drbg;
|
|
||||||
|
|
||||||
std::mutex _mutex;
|
|
||||||
|
|
||||||
bool init(const std::string& host, std::string& errMsg);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace ix
|
|
@ -10,6 +10,7 @@
|
|||||||
#include "IXSocketConnect.h"
|
#include "IXSocketConnect.h"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include <openssl/x509v3.h>
|
#include <openssl/x509v3.h>
|
||||||
|
|
||||||
@ -17,13 +18,12 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#define socketerrno errno
|
#define socketerrno errno
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
std::atomic<bool> SocketOpenSSL::_openSSLInitializationSuccessful(false);
|
std::atomic<bool> SocketOpenSSL::_openSSLInitializationSuccessful(false);
|
||||||
std::once_flag SocketOpenSSL::_openSSLInitFlag;
|
|
||||||
|
|
||||||
SocketOpenSSL::SocketOpenSSL(int fd) : Socket(fd),
|
SocketOpenSSL::SocketOpenSSL(int fd) : Socket(fd),
|
||||||
_ssl_connection(nullptr),
|
_ssl_connection(nullptr),
|
||||||
_ssl_context(nullptr)
|
_ssl_context(nullptr)
|
||||||
{
|
{
|
||||||
std::call_once(_openSSLInitFlag, &SocketOpenSSL::openSSLInitialize, this);
|
std::call_once(_openSSLInitFlag, &SocketOpenSSL::openSSLInitialize, this);
|
||||||
@ -80,7 +80,7 @@ namespace ix
|
|||||||
return "OpenSSL failed - underlying BIO reported an I/O error";
|
return "OpenSSL failed - underlying BIO reported an I/O error";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (err == SSL_ERROR_SSL)
|
else if (err == SSL_ERROR_SSL)
|
||||||
{
|
{
|
||||||
e = ERR_get_error();
|
e = ERR_get_error();
|
||||||
std::string errMsg("OpenSSL failed - ");
|
std::string errMsg("OpenSSL failed - ");
|
||||||
@ -149,7 +149,7 @@ namespace ix
|
|||||||
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
||||||
// Check server name
|
// Check server name
|
||||||
bool hostname_verifies_ok = false;
|
bool hostname_verifies_ok = false;
|
||||||
STACK_OF(GENERAL_NAME) *san_names =
|
STACK_OF(GENERAL_NAME) *san_names =
|
||||||
(STACK_OF(GENERAL_NAME)*) X509_get_ext_d2i((X509 *)server_cert,
|
(STACK_OF(GENERAL_NAME)*) X509_get_ext_d2i((X509 *)server_cert,
|
||||||
NID_subject_alt_name, NULL, NULL);
|
NID_subject_alt_name, NULL, NULL);
|
||||||
if (san_names)
|
if (san_names)
|
||||||
@ -160,8 +160,8 @@ namespace ix
|
|||||||
if (sk_name->type == GEN_DNS)
|
if (sk_name->type == GEN_DNS)
|
||||||
{
|
{
|
||||||
char *name = (char *)ASN1_STRING_data(sk_name->d.dNSName);
|
char *name = (char *)ASN1_STRING_data(sk_name->d.dNSName);
|
||||||
if ((size_t)ASN1_STRING_length(sk_name->d.dNSName) == strlen(name) &&
|
if ((size_t)ASN1_STRING_length(sk_name->d.dNSName) == strlen(name) &&
|
||||||
checkHost(hostname, name))
|
checkHost(hostname, name))
|
||||||
{
|
{
|
||||||
hostname_verifies_ok = true;
|
hostname_verifies_ok = true;
|
||||||
break;
|
break;
|
||||||
@ -185,8 +185,8 @@ namespace ix
|
|||||||
ASN1_STRING *cn_asn1 = X509_NAME_ENTRY_get_data(cn_entry);
|
ASN1_STRING *cn_asn1 = X509_NAME_ENTRY_get_data(cn_entry);
|
||||||
char *cn = (char *)ASN1_STRING_data(cn_asn1);
|
char *cn = (char *)ASN1_STRING_data(cn_asn1);
|
||||||
|
|
||||||
if ((size_t)ASN1_STRING_length(cn_asn1) == strlen(cn) &&
|
if ((size_t)ASN1_STRING_length(cn_asn1) == strlen(cn) &&
|
||||||
checkHost(hostname, cn))
|
checkHost(hostname, cn))
|
||||||
{
|
{
|
||||||
hostname_verifies_ok = true;
|
hostname_verifies_ok = true;
|
||||||
}
|
}
|
||||||
@ -205,7 +205,7 @@ namespace ix
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SocketOpenSSL::openSSLHandshake(const std::string& host, std::string& errMsg)
|
bool SocketOpenSSL::openSSLHandshake(const std::string& host, std::string& errMsg)
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -240,6 +240,7 @@ namespace ix
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No wait support
|
||||||
bool SocketOpenSSL::connect(const std::string& host,
|
bool SocketOpenSSL::connect(const std::string& host,
|
||||||
int port,
|
int port,
|
||||||
std::string& errMsg,
|
std::string& errMsg,
|
||||||
@ -340,7 +341,7 @@ namespace ix
|
|||||||
|
|
||||||
ERR_clear_error();
|
ERR_clear_error();
|
||||||
ssize_t write_result = SSL_write(_ssl_connection, buf + sent, (int) nbyte);
|
ssize_t write_result = SSL_write(_ssl_connection, buf + sent, (int) nbyte);
|
||||||
int reason = SSL_get_error(_ssl_connection, (int) write_result);
|
int reason = SSL_get_error(_ssl_connection, write_result);
|
||||||
|
|
||||||
if (reason == SSL_ERROR_NONE) {
|
if (reason == SSL_ERROR_NONE) {
|
||||||
nbyte -= write_result;
|
nbyte -= write_result;
|
||||||
@ -360,6 +361,7 @@ namespace ix
|
|||||||
return send((char*)&buffer[0], buffer.size());
|
return send((char*)&buffer[0], buffer.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No wait support
|
||||||
ssize_t SocketOpenSSL::recv(void* buf, size_t nbyte)
|
ssize_t SocketOpenSSL::recv(void* buf, size_t nbyte)
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
@ -379,7 +381,7 @@ namespace ix
|
|||||||
return read_result;
|
return read_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int reason = SSL_get_error(_ssl_connection, (int) read_result);
|
int reason = SSL_get_error(_ssl_connection, read_result);
|
||||||
|
|
||||||
if (reason == SSL_ERROR_WANT_READ || reason == SSL_ERROR_WANT_WRITE)
|
if (reason == SSL_ERROR_WANT_READ || reason == SSL_ERROR_WANT_WRITE)
|
||||||
{
|
{
|
||||||
|
@ -6,24 +6,26 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IXCancellationRequest.h"
|
|
||||||
#include "IXSocket.h"
|
#include "IXSocket.h"
|
||||||
#include <mutex>
|
#include "IXCancellationRequest.h"
|
||||||
|
|
||||||
#include <openssl/bio.h>
|
#include <openssl/bio.h>
|
||||||
|
#include <openssl/hmac.h>
|
||||||
#include <openssl/conf.h>
|
#include <openssl/conf.h>
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
#include <openssl/hmac.h>
|
|
||||||
#include <openssl/ssl.h>
|
#include <openssl/ssl.h>
|
||||||
|
|
||||||
namespace ix
|
#include <mutex>
|
||||||
|
|
||||||
|
namespace ix
|
||||||
{
|
{
|
||||||
class SocketOpenSSL final : public Socket
|
class SocketOpenSSL : public Socket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SocketOpenSSL(int fd = -1);
|
SocketOpenSSL(int fd = -1);
|
||||||
~SocketOpenSSL();
|
~SocketOpenSSL();
|
||||||
|
|
||||||
virtual bool connect(const std::string& host,
|
virtual bool connect(const std::string& host,
|
||||||
int port,
|
int port,
|
||||||
std::string& errMsg,
|
std::string& errMsg,
|
||||||
const CancellationRequest& isCancellationRequested) final;
|
const CancellationRequest& isCancellationRequested) final;
|
||||||
@ -38,16 +40,18 @@ namespace ix
|
|||||||
std::string getSSLError(int ret);
|
std::string getSSLError(int ret);
|
||||||
SSL_CTX* openSSLCreateContext(std::string& errMsg);
|
SSL_CTX* openSSLCreateContext(std::string& errMsg);
|
||||||
bool openSSLHandshake(const std::string& hostname, std::string& errMsg);
|
bool openSSLHandshake(const std::string& hostname, std::string& errMsg);
|
||||||
bool openSSLCheckServerCert(SSL* ssl, const std::string& hostname, std::string& errMsg);
|
bool openSSLCheckServerCert(SSL *ssl,
|
||||||
bool checkHost(const std::string& host, const char* pattern);
|
const std::string& hostname,
|
||||||
|
std::string& errMsg);
|
||||||
|
bool checkHost(const std::string& host, const char *pattern);
|
||||||
|
|
||||||
SSL* _ssl_connection;
|
SSL* _ssl_connection;
|
||||||
SSL_CTX* _ssl_context;
|
SSL_CTX* _ssl_context;
|
||||||
const SSL_METHOD* _ssl_method;
|
const SSL_METHOD* _ssl_method;
|
||||||
mutable std::mutex _mutex; // OpenSSL routines are not thread-safe
|
mutable std::mutex _mutex; // OpenSSL routines are not thread-safe
|
||||||
|
|
||||||
static std::once_flag _openSSLInitFlag;
|
std::once_flag _openSSLInitFlag;
|
||||||
static std::atomic<bool> _openSSLInitializationSuccessful;
|
static std::atomic<bool> _openSSLInitializationSuccessful;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // 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