From 401fc3987992f358988e2715f489a8910332d57e Mon Sep 17 00:00:00 2001 From: Dimon4eg Date: Tue, 16 Apr 2019 18:51:57 +0300 Subject: [PATCH] fix windows build (#29) * fix windows build * fix for Unix * Fix build if TLS is OFF * add OpenSSL req to ws * Fix build on Mac * fix tests for windows --- CMakeLists.txt | 32 +++++++++++++++++--------- ixwebsocket/IXSocket.cpp | 4 ++++ ixwebsocket/IXSocketFactory.cpp | 12 +++++++++- ixwebsocket/IXSocketFactory.h | 1 + ixwebsocket/IXSocketSChannel.cpp | 10 ++++---- ixwebsocket/IXSocketSChannel.h | 6 ++--- ixwebsocket/IXWebSocketHttpHeaders.cpp | 22 +++++++++++++++--- ixwebsocket/IXWebSocketHttpHeaders.h | 14 ++--------- test/IXSocketTest.cpp | 2 +- test/IXTest.cpp | 2 +- test/test_runner.cpp | 2 -- ws/CMakeLists.txt | 13 ++++++++++- 12 files changed, 80 insertions(+), 40 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba09c7d6..558ffe97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,6 @@ set( IXWEBSOCKET_SOURCES ixwebsocket/IXHttpClient.cpp ixwebsocket/IXUrlParser.cpp ixwebsocket/IXSelectInterrupt.cpp - ixwebsocket/IXSelectInterruptPipe.cpp ixwebsocket/IXSelectInterruptFactory.cpp ixwebsocket/IXConnectionState.cpp ) @@ -65,11 +64,16 @@ set( IXWEBSOCKET_HEADERS ixwebsocket/IXHttpClient.h ixwebsocket/IXUrlParser.h ixwebsocket/IXSelectInterrupt.h - ixwebsocket/IXSelectInterruptPipe.h ixwebsocket/IXSelectInterruptFactory.h ixwebsocket/IXConnectionState.h ) +if (UNIX) + # Linux, Mac, iOS, Android + list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSelectInterruptPipe.cpp ) + list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSelectInterruptPipe.h ) +endif() + # Platform specific code if (APPLE) list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/apple/IXSetThreadName_apple.cpp) @@ -81,6 +85,7 @@ else() list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSelectInterruptEventFd.h) endif() +set(USE_OPEN_SSL FALSE) if (USE_TLS) add_definitions(-DIXWEBSOCKET_USE_TLS) @@ -91,6 +96,7 @@ if (USE_TLS) list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketSChannel.h) list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketSChannel.cpp) else() + set(USE_OPEN_SSL TRUE) list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketOpenSSL.h) list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketOpenSSL.cpp) endif() @@ -101,18 +107,16 @@ add_library( ixwebsocket STATIC ${IXWEBSOCKET_HEADERS} ) -# gcc/Linux needs -pthread -find_package(Threads) - if (APPLE AND USE_TLS) - target_link_libraries(ixwebsocket "-framework foundation" "-framework security") + target_link_libraries(ixwebsocket "-framework foundation" "-framework security") endif() -if(UNIX AND NOT APPLE) +if(USE_OPEN_SSL) find_package(OpenSSL REQUIRED) add_definitions(${OPENSSL_DEFINITIONS}) message(STATUS "OpenSSL: " ${OPENSSL_VERSION}) include_directories(${OPENSSL_INCLUDE_DIR}) + target_link_libraries(ixwebsocket ${OPENSSL_LIBRARIES}) endif() if (WIN32) @@ -127,15 +131,21 @@ if (WIN32) target_link_libraries(ixwebsocket libz wsock32 ws2_32) add_definitions(-D_CRT_SECURE_NO_WARNINGS) - + else() + # gcc/Linux needs -pthread + find_package(Threads) + target_link_libraries(ixwebsocket - z ${OPENSSL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) + z ${CMAKE_THREAD_LIBS_INIT}) endif() set( IXWEBSOCKET_INCLUDE_DIRS . - ../../shared/OpenSSL/include) +) + target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} ) -add_subdirectory(ws) +if (NOT WIN32) + add_subdirectory(ws) +endif() diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index 224d8bfe..461f85fe 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -21,6 +21,10 @@ #include #include +#ifdef min +#undef min +#endif + namespace ix { const int Socket::kDefaultPollNoTimeout = -1; // No poll timeout by default diff --git a/ixwebsocket/IXSocketFactory.cpp b/ixwebsocket/IXSocketFactory.cpp index 2c574757..40c23f96 100644 --- a/ixwebsocket/IXSocketFactory.cpp +++ b/ixwebsocket/IXSocketFactory.cpp @@ -6,12 +6,20 @@ #include "IXSocketFactory.h" -#if defined(__APPLE__) or defined(__linux__) +#ifdef IXWEBSOCKET_USE_TLS + # ifdef __APPLE__ # include +# elif defined(_WIN32) +# include # else # include # endif + +#else + +#include + #endif namespace ix @@ -31,6 +39,8 @@ namespace ix #ifdef IXWEBSOCKET_USE_TLS # ifdef __APPLE__ socket = std::make_shared(); +# elif defined(_WIN32) + socket = std::make_shared(); # else socket = std::make_shared(); # endif diff --git a/ixwebsocket/IXSocketFactory.h b/ixwebsocket/IXSocketFactory.h index c10145d1..396bc457 100644 --- a/ixwebsocket/IXSocketFactory.h +++ b/ixwebsocket/IXSocketFactory.h @@ -8,6 +8,7 @@ #pragma once #include +#include namespace ix { diff --git a/ixwebsocket/IXSocketSChannel.cpp b/ixwebsocket/IXSocketSChannel.cpp index 2dd2dee0..57002953 100644 --- a/ixwebsocket/IXSocketSChannel.cpp +++ b/ixwebsocket/IXSocketSChannel.cpp @@ -18,7 +18,7 @@ # include # include # include -# include +//# include # include #define WIN32_LEAN_AND_MEAN @@ -75,7 +75,7 @@ namespace ix int port, std::string& errMsg) { - return Socket::connect(host, port, errMsg); + return Socket::connect(host, port, errMsg, nullptr); } @@ -89,17 +89,17 @@ namespace ix Socket::close(); } - int SocketSChannel::send(char* buf, size_t nbyte) + ssize_t SocketSChannel::send(char* buf, size_t nbyte) { return Socket::send(buf, nbyte); } - int SocketSChannel::send(const std::string& buffer) + ssize_t SocketSChannel::send(const std::string& buffer) { return Socket::send(buffer); } - int SocketSChannel::recv(void* buf, size_t nbyte) + ssize_t SocketSChannel::recv(void* buf, size_t nbyte) { return Socket::recv(buf, nbyte); } diff --git a/ixwebsocket/IXSocketSChannel.h b/ixwebsocket/IXSocketSChannel.h index 4da6c881..10e4132f 100644 --- a/ixwebsocket/IXSocketSChannel.h +++ b/ixwebsocket/IXSocketSChannel.h @@ -24,9 +24,9 @@ namespace ix // The important override virtual void secureSocket() final; - virtual int send(char* buffer, size_t length) final; - virtual int send(const std::string& buffer) final; - virtual int recv(void* buffer, size_t length) 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: }; diff --git a/ixwebsocket/IXWebSocketHttpHeaders.cpp b/ixwebsocket/IXWebSocketHttpHeaders.cpp index 68c3902a..00c93393 100644 --- a/ixwebsocket/IXWebSocketHttpHeaders.cpp +++ b/ixwebsocket/IXWebSocketHttpHeaders.cpp @@ -6,12 +6,28 @@ #include "IXWebSocketHttpHeaders.h" #include "IXSocket.h" - -#include -#include +#include +#include namespace ix { + bool CaseInsensitiveLess::NocaseCompare::operator()(const unsigned char & c1, const unsigned char & c2) const + { +#ifdef _WIN32 + return std::tolower(c1, std::locale()) < std::tolower(c2, std::locale()); +#else + return std::tolower(c1) < std::tolower(c2); +#endif + } + + bool CaseInsensitiveLess::operator()(const std::string & s1, const std::string & s2) const + { + return std::lexicographical_compare + (s1.begin(), s1.end(), // source range + s2.begin(), s2.end(), // dest range + NocaseCompare()); // comparison + } + std::pair parseHttpHeaders( std::shared_ptr socket, const CancellationRequest& isCancellationRequested) diff --git a/ixwebsocket/IXWebSocketHttpHeaders.h b/ixwebsocket/IXWebSocketHttpHeaders.h index 6e6b4cc4..b8422e56 100644 --- a/ixwebsocket/IXWebSocketHttpHeaders.h +++ b/ixwebsocket/IXWebSocketHttpHeaders.h @@ -11,7 +11,6 @@ #include #include #include -#include namespace ix { @@ -22,19 +21,10 @@ namespace ix // Case Insensitive compare_less binary function struct NocaseCompare { - bool operator() (const unsigned char& c1, const unsigned char& c2) const - { - return std::tolower(c1) < std::tolower(c2); - } + bool operator() (const unsigned char& c1, const unsigned char& c2) const; }; - bool operator() (const std::string & s1, const std::string & s2) const - { - return std::lexicographical_compare - (s1.begin(), s1.end(), // source range - s2.begin(), s2.end(), // dest range - NocaseCompare()); // comparison - } + bool operator() (const std::string & s1, const std::string & s2) const; }; using WebSocketHttpHeaders = std::map; diff --git a/test/IXSocketTest.cpp b/test/IXSocketTest.cpp index e03bcb7a..6306c7ed 100644 --- a/test/IXSocketTest.cpp +++ b/test/IXSocketTest.cpp @@ -73,7 +73,7 @@ TEST_CASE("socket", "[socket]") testSocket(host, port, request, socket, expectedStatus, timeoutSecs); } -#if defined(__APPLE__) or defined(__linux__) +#if defined(__APPLE__) || defined(__linux__) SECTION("Connect to google HTTPS server. Send GET request without header. Should return 200") { std::string errMsg; diff --git a/test/IXTest.cpp b/test/IXTest.cpp index 5abc9d59..8fea889e 100644 --- a/test/IXTest.cpp +++ b/test/IXTest.cpp @@ -109,7 +109,7 @@ namespace ix } struct sockaddr_in sa; // server address information - unsigned int len; + socklen_t len; if (getsockname(sockfd, (struct sockaddr *) &sa, &len) < 0) { log("Cannot compute a free port. getsockname error."); diff --git a/test/test_runner.cpp b/test/test_runner.cpp index 77d71dfa..51f0e398 100644 --- a/test/test_runner.cpp +++ b/test/test_runner.cpp @@ -7,8 +7,6 @@ #define CATCH_CONFIG_RUNNER #include "catch.hpp" -#include - int main(int argc, char* argv[]) { int result = Catch::Session().run(argc, argv); diff --git a/ws/CMakeLists.txt b/ws/CMakeLists.txt index 5b1478d7..d9fe3661 100644 --- a/ws/CMakeLists.txt +++ b/ws/CMakeLists.txt @@ -7,7 +7,9 @@ cmake_minimum_required (VERSION 3.4.1) project (ws) # There's -Weverything too for clang -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic") +if (NOT WIN32) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic") +endif() #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") #set(CMAKE_LD_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") @@ -51,4 +53,13 @@ add_executable(ws ws.cpp) target_link_libraries(ws ixwebsocket) + +if(NOT APPLE) + find_package(OpenSSL REQUIRED) + add_definitions(${OPENSSL_DEFINITIONS}) + message(STATUS "OpenSSL: " ${OPENSSL_VERSION}) + include_directories(${OPENSSL_INCLUDE_DIR}) + target_link_libraries(ws ${OPENSSL_LIBRARIES}) +endif() + install(TARGETS ws RUNTIME DESTINATION bin)