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
This commit is contained in:
Dimon4eg 2019-04-16 18:51:57 +03:00 committed by Benjamin Sergeant
parent dac18fcabf
commit fbb7c012a3
12 changed files with 80 additions and 40 deletions

View File

@ -37,7 +37,6 @@ set( IXWEBSOCKET_SOURCES
ixwebsocket/IXHttpClient.cpp ixwebsocket/IXHttpClient.cpp
ixwebsocket/IXUrlParser.cpp ixwebsocket/IXUrlParser.cpp
ixwebsocket/IXSelectInterrupt.cpp ixwebsocket/IXSelectInterrupt.cpp
ixwebsocket/IXSelectInterruptPipe.cpp
ixwebsocket/IXSelectInterruptFactory.cpp ixwebsocket/IXSelectInterruptFactory.cpp
ixwebsocket/IXConnectionState.cpp ixwebsocket/IXConnectionState.cpp
) )
@ -65,11 +64,16 @@ set( IXWEBSOCKET_HEADERS
ixwebsocket/IXHttpClient.h ixwebsocket/IXHttpClient.h
ixwebsocket/IXUrlParser.h ixwebsocket/IXUrlParser.h
ixwebsocket/IXSelectInterrupt.h ixwebsocket/IXSelectInterrupt.h
ixwebsocket/IXSelectInterruptPipe.h
ixwebsocket/IXSelectInterruptFactory.h ixwebsocket/IXSelectInterruptFactory.h
ixwebsocket/IXConnectionState.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 # 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)
@ -81,6 +85,7 @@ else()
list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSelectInterruptEventFd.h) list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSelectInterruptEventFd.h)
endif() endif()
set(USE_OPEN_SSL FALSE)
if (USE_TLS) if (USE_TLS)
add_definitions(-DIXWEBSOCKET_USE_TLS) add_definitions(-DIXWEBSOCKET_USE_TLS)
@ -91,6 +96,7 @@ if (USE_TLS)
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()
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()
@ -101,18 +107,16 @@ add_library( ixwebsocket STATIC
${IXWEBSOCKET_HEADERS} ${IXWEBSOCKET_HEADERS}
) )
# gcc/Linux needs -pthread
find_package(Threads)
if (APPLE AND USE_TLS) if (APPLE AND USE_TLS)
target_link_libraries(ixwebsocket "-framework foundation" "-framework security") target_link_libraries(ixwebsocket "-framework foundation" "-framework security")
endif() endif()
if(UNIX AND NOT APPLE) if(USE_OPEN_SSL)
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 (WIN32) if (WIN32)
@ -129,13 +133,19 @@ if (WIN32)
add_definitions(-D_CRT_SECURE_NO_WARNINGS) add_definitions(-D_CRT_SECURE_NO_WARNINGS)
else() else()
# gcc/Linux needs -pthread
find_package(Threads)
target_link_libraries(ixwebsocket target_link_libraries(ixwebsocket
z ${OPENSSL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) z ${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} ) target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} )
if (NOT WIN32)
add_subdirectory(ws) add_subdirectory(ws)
endif()

View File

@ -21,6 +21,10 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#ifdef min
#undef min
#endif
namespace ix namespace ix
{ {
const int Socket::kDefaultPollNoTimeout = -1; // No poll timeout by default const int Socket::kDefaultPollNoTimeout = -1; // No poll timeout by default

View File

@ -6,12 +6,20 @@
#include "IXSocketFactory.h" #include "IXSocketFactory.h"
#if defined(__APPLE__) or defined(__linux__) #ifdef IXWEBSOCKET_USE_TLS
# ifdef __APPLE__ # ifdef __APPLE__
# include <ixwebsocket/IXSocketAppleSSL.h> # include <ixwebsocket/IXSocketAppleSSL.h>
# elif defined(_WIN32)
# include <ixwebsocket/IXSocketSChannel.h>
# else # else
# include <ixwebsocket/IXSocketOpenSSL.h> # include <ixwebsocket/IXSocketOpenSSL.h>
# endif # endif
#else
#include <ixwebsocket/IXSocket.h>
#endif #endif
namespace ix namespace ix
@ -31,6 +39,8 @@ namespace ix
#ifdef IXWEBSOCKET_USE_TLS #ifdef IXWEBSOCKET_USE_TLS
# ifdef __APPLE__ # ifdef __APPLE__
socket = std::make_shared<SocketAppleSSL>(); socket = std::make_shared<SocketAppleSSL>();
# elif defined(_WIN32)
socket = std::make_shared<SocketSChannel>();
# else # else
socket = std::make_shared<SocketOpenSSL>(); socket = std::make_shared<SocketOpenSSL>();
# endif # endif

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <string>
namespace ix namespace ix
{ {

View File

@ -18,7 +18,7 @@
# include <ws2def.h> # include <ws2def.h>
# include <WS2tcpip.h> # include <WS2tcpip.h>
# include <schannel.h> # include <schannel.h>
# include <sslsock.h> //# include <sslsock.h>
# include <io.h> # include <io.h>
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
@ -75,7 +75,7 @@ namespace ix
int port, int port,
std::string& errMsg) std::string& errMsg)
{ {
return Socket::connect(host, port, errMsg); return Socket::connect(host, port, errMsg, nullptr);
} }
@ -89,17 +89,17 @@ namespace ix
Socket::close(); Socket::close();
} }
int SocketSChannel::send(char* buf, size_t nbyte) ssize_t SocketSChannel::send(char* buf, size_t nbyte)
{ {
return Socket::send(buf, 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); 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); return Socket::recv(buf, nbyte);
} }

View File

@ -24,9 +24,9 @@ namespace ix
// The important override // The important override
virtual void secureSocket() final; virtual void secureSocket() final;
virtual int send(char* buffer, size_t length) final; virtual ssize_t send(char* buffer, size_t length) final;
virtual int send(const std::string& buffer) final; virtual ssize_t send(const std::string& buffer) final;
virtual int recv(void* buffer, size_t length) final; virtual ssize_t recv(void* buffer, size_t length) final;
private: private:
}; };

View File

@ -6,12 +6,28 @@
#include "IXWebSocketHttpHeaders.h" #include "IXWebSocketHttpHeaders.h"
#include "IXSocket.h" #include "IXSocket.h"
#include <algorithm>
#include <string> #include <locale>
#include <unordered_map>
namespace ix 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<bool, WebSocketHttpHeaders> parseHttpHeaders( std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders(
std::shared_ptr<Socket> socket, std::shared_ptr<Socket> socket,
const CancellationRequest& isCancellationRequested) const CancellationRequest& isCancellationRequested)

View File

@ -11,7 +11,6 @@
#include <string> #include <string>
#include <map> #include <map>
#include <memory> #include <memory>
#include <algorithm>
namespace ix namespace ix
{ {
@ -22,19 +21,10 @@ namespace ix
// Case Insensitive compare_less binary function // Case Insensitive compare_less binary function
struct NocaseCompare struct NocaseCompare
{ {
bool operator() (const unsigned char& c1, const unsigned char& c2) const bool operator() (const unsigned char& c1, const unsigned char& c2) const;
{
return std::tolower(c1) < std::tolower(c2);
}
}; };
bool operator() (const std::string & s1, const std::string & s2) 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
}
}; };
using WebSocketHttpHeaders = std::map<std::string, std::string, CaseInsensitiveLess>; using WebSocketHttpHeaders = std::map<std::string, std::string, CaseInsensitiveLess>;

View File

@ -73,7 +73,7 @@ TEST_CASE("socket", "[socket]")
testSocket(host, port, request, socket, expectedStatus, timeoutSecs); 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") SECTION("Connect to google HTTPS server. Send GET request without header. Should return 200")
{ {
std::string errMsg; std::string errMsg;

View File

@ -109,7 +109,7 @@ namespace ix
} }
struct sockaddr_in sa; // server address information struct sockaddr_in sa; // server address information
unsigned int len; socklen_t len;
if (getsockname(sockfd, (struct sockaddr *) &sa, &len) < 0) if (getsockname(sockfd, (struct sockaddr *) &sa, &len) < 0)
{ {
log("Cannot compute a free port. getsockname error."); log("Cannot compute a free port. getsockname error.");

View File

@ -7,8 +7,6 @@
#define CATCH_CONFIG_RUNNER #define CATCH_CONFIG_RUNNER
#include "catch.hpp" #include "catch.hpp"
#include <ixwebsocket/IXSocket.h>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int result = Catch::Session().run(argc, argv); int result = Catch::Session().run(argc, argv);

View File

@ -7,7 +7,9 @@ cmake_minimum_required (VERSION 3.4.1)
project (ws) project (ws)
# There's -Weverything too for clang # There's -Weverything too for clang
if (NOT WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
endif()
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
#set(CMAKE_LD_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") #set(CMAKE_LD_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
@ -51,4 +53,13 @@ add_executable(ws
ws.cpp) ws.cpp)
target_link_libraries(ws ixwebsocket) 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) install(TARGETS ws RUNTIME DESTINATION bin)