From a3d2fa4b7ee65a40cdabbe6258db75c3866d5908 Mon Sep 17 00:00:00 2001 From: Anton Ivlev Date: Sat, 19 Mar 2022 21:41:40 +0300 Subject: [PATCH] Export static symbols when building ws with shared library (#370) --- CMakeLists.txt | 6 ++++ ixwebsocket/IXDNSLookup.h | 3 +- ixwebsocket/IXHttpClient.h | 13 +++++---- ixwebsocket/IXHttpServer.h | 3 +- ixwebsocket/IXSocketServer.h | 11 +++---- ixwebsocket/IXWebSocketCloseConstants.h | 39 +++++++++++++------------ ixwebsocket/IXWebSocketServer.h | 5 ++-- ixwebsocket/IXWebsocketExport.h | 33 +++++++++++++++++++++ 8 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 ixwebsocket/IXWebsocketExport.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e7341bc0..ef68f819 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,7 @@ set( IXWEBSOCKET_HEADERS ixwebsocket/IXWebSocketCloseConstants.h ixwebsocket/IXWebSocketCloseInfo.h ixwebsocket/IXWebSocketErrorInfo.h + ixwebsocket/IXWebsocketExport.h ixwebsocket/IXWebSocketHandshake.h ixwebsocket/IXWebSocketHandshakeKeyGen.h ixwebsocket/IXWebSocketHttpHeaders.h @@ -173,6 +174,10 @@ else() ${IXWEBSOCKET_SOURCES} ${IXWEBSOCKET_HEADERS} ) + + if( MSVC ) + target_compile_definitions(ixwebsocket PUBLIC IXWEBSOCKET_STATIC_DEFINE) + endif() endif() if (USE_TLS) @@ -286,6 +291,7 @@ if (IXWEBSOCKET_INSTALL) install(TARGETS ixwebsocket EXPORT ixwebsocket ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ixwebsocket/ ) diff --git a/ixwebsocket/IXDNSLookup.h b/ixwebsocket/IXDNSLookup.h index fcdd103d..89dcac36 100644 --- a/ixwebsocket/IXDNSLookup.h +++ b/ixwebsocket/IXDNSLookup.h @@ -11,6 +11,7 @@ #pragma once #include "IXCancellationRequest.h" +#include "IXWebsocketExport.h" #include #include #include @@ -54,7 +55,7 @@ namespace ix std::string _hostname; int _port; int64_t _wait; - const static int64_t kDefaultWait; + IXWEBSOCKET_EXPORT const static int64_t kDefaultWait; struct addrinfo* _res; std::mutex _resMutex; diff --git a/ixwebsocket/IXHttpClient.h b/ixwebsocket/IXHttpClient.h index c4b05845..969b4be6 100644 --- a/ixwebsocket/IXHttpClient.h +++ b/ixwebsocket/IXHttpClient.h @@ -10,6 +10,7 @@ #include "IXSocket.h" #include "IXSocketTLSOptions.h" #include "IXWebSocketHttpHeaders.h" +#include "IXWebsocketExport.h" #include #include #include @@ -91,12 +92,12 @@ namespace ix 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 kDelete; - const static std::string kPut; - const static std::string kPatch; + IXWEBSOCKET_EXPORT const static std::string kPost; + IXWEBSOCKET_EXPORT const static std::string kGet; + IXWEBSOCKET_EXPORT const static std::string kHead; + IXWEBSOCKET_EXPORT const static std::string kDelete; + IXWEBSOCKET_EXPORT const static std::string kPut; + IXWEBSOCKET_EXPORT const static std::string kPatch; private: void log(const std::string& msg, HttpRequestArgsPtr args); diff --git a/ixwebsocket/IXHttpServer.h b/ixwebsocket/IXHttpServer.h index 7de67631..afab27ab 100644 --- a/ixwebsocket/IXHttpServer.h +++ b/ixwebsocket/IXHttpServer.h @@ -9,6 +9,7 @@ #include "IXHttp.h" #include "IXSocketServer.h" #include "IXWebSocket.h" +#include "IXWebsocketExport.h" #include #include #include @@ -46,7 +47,7 @@ namespace ix OnConnectionCallback _onConnectionCallback; std::atomic _connectedClientsCount; - const static int kDefaultTimeoutSecs; + IXWEBSOCKET_EXPORT const static int kDefaultTimeoutSecs; int _timeoutSecs; // Methods diff --git a/ixwebsocket/IXSocketServer.h b/ixwebsocket/IXSocketServer.h index fe0f7e28..0ccd93d7 100644 --- a/ixwebsocket/IXSocketServer.h +++ b/ixwebsocket/IXSocketServer.h @@ -10,6 +10,7 @@ #include "IXNetSystem.h" #include "IXSelectInterrupt.h" #include "IXSocketTLSOptions.h" +#include "IXWebsocketExport.h" #include #include #include @@ -48,11 +49,11 @@ namespace ix // that inherits from ConnectionState but has its own methods. void setConnectionStateFactory(const ConnectionStateFactory& connectionStateFactory); - const static int kDefaultPort; - const static std::string kDefaultHost; - const static int kDefaultTcpBacklog; - const static size_t kDefaultMaxConnections; - const static int kDefaultAddressFamily; + IXWEBSOCKET_EXPORT const static int kDefaultPort; + IXWEBSOCKET_EXPORT const static std::string kDefaultHost; + IXWEBSOCKET_EXPORT const static int kDefaultTcpBacklog; + IXWEBSOCKET_EXPORT const static size_t kDefaultMaxConnections; + IXWEBSOCKET_EXPORT const static int kDefaultAddressFamily; void start(); std::pair listen(); diff --git a/ixwebsocket/IXWebSocketCloseConstants.h b/ixwebsocket/IXWebSocketCloseConstants.h index 145777b9..35ef3886 100644 --- a/ixwebsocket/IXWebSocketCloseConstants.h +++ b/ixwebsocket/IXWebSocketCloseConstants.h @@ -6,6 +6,7 @@ #pragma once +#include "IXWebsocketExport.h" #include #include @@ -13,25 +14,25 @@ namespace ix { struct WebSocketCloseConstants { - static const uint16_t kNormalClosureCode; - static const uint16_t kInternalErrorCode; - static const uint16_t kAbnormalCloseCode; - static const uint16_t kProtocolErrorCode; - static const uint16_t kNoStatusCodeErrorCode; - static const uint16_t kInvalidFramePayloadData; + IXWEBSOCKET_EXPORT static const uint16_t kNormalClosureCode; + IXWEBSOCKET_EXPORT static const uint16_t kInternalErrorCode; + IXWEBSOCKET_EXPORT static const uint16_t kAbnormalCloseCode; + IXWEBSOCKET_EXPORT static const uint16_t kProtocolErrorCode; + IXWEBSOCKET_EXPORT static const uint16_t kNoStatusCodeErrorCode; + IXWEBSOCKET_EXPORT static const uint16_t kInvalidFramePayloadData; - static const std::string kNormalClosureMessage; - static const std::string kInternalErrorMessage; - static const std::string kAbnormalCloseMessage; - static const std::string kPingTimeoutMessage; - static const std::string kProtocolErrorMessage; - static const std::string kNoStatusCodeErrorMessage; - static const std::string kProtocolErrorReservedBitUsed; - static const std::string kProtocolErrorPingPayloadOversized; - static const std::string kProtocolErrorCodeControlMessageFragmented; - static const std::string kProtocolErrorCodeDataOpcodeOutOfSequence; - static const std::string kProtocolErrorCodeContinuationOpCodeOutOfSequence; - static const std::string kInvalidFramePayloadDataMessage; - static const std::string kInvalidCloseCodeMessage; + IXWEBSOCKET_EXPORT static const std::string kNormalClosureMessage; + IXWEBSOCKET_EXPORT static const std::string kInternalErrorMessage; + IXWEBSOCKET_EXPORT static const std::string kAbnormalCloseMessage; + IXWEBSOCKET_EXPORT static const std::string kPingTimeoutMessage; + IXWEBSOCKET_EXPORT static const std::string kProtocolErrorMessage; + IXWEBSOCKET_EXPORT static const std::string kNoStatusCodeErrorMessage; + IXWEBSOCKET_EXPORT static const std::string kProtocolErrorReservedBitUsed; + IXWEBSOCKET_EXPORT static const std::string kProtocolErrorPingPayloadOversized; + IXWEBSOCKET_EXPORT static const std::string kProtocolErrorCodeControlMessageFragmented; + IXWEBSOCKET_EXPORT static const std::string kProtocolErrorCodeDataOpcodeOutOfSequence; + IXWEBSOCKET_EXPORT static const std::string kProtocolErrorCodeContinuationOpCodeOutOfSequence; + IXWEBSOCKET_EXPORT static const std::string kInvalidFramePayloadDataMessage; + IXWEBSOCKET_EXPORT static const std::string kInvalidCloseCodeMessage; }; } // namespace ix diff --git a/ixwebsocket/IXWebSocketServer.h b/ixwebsocket/IXWebSocketServer.h index 6cae6331..d4382c88 100644 --- a/ixwebsocket/IXWebSocketServer.h +++ b/ixwebsocket/IXWebSocketServer.h @@ -8,6 +8,7 @@ #include "IXSocketServer.h" #include "IXWebSocket.h" +#include "IXWebsocketExport.h" #include #include #include @@ -50,7 +51,7 @@ namespace ix void makeBroadcastServer(); bool listenAndStart(); - const static int kDefaultHandShakeTimeoutSecs; + IXWEBSOCKET_EXPORT const static int kDefaultHandShakeTimeoutSecs; int getHandshakeTimeoutSecs(); bool isPongEnabled(); @@ -67,7 +68,7 @@ namespace ix std::mutex _clientsMutex; std::set> _clients; - const static bool kDefaultEnablePong; + IXWEBSOCKET_EXPORT const static bool kDefaultEnablePong; // Methods virtual void handleConnection(std::unique_ptr socket, diff --git a/ixwebsocket/IXWebsocketExport.h b/ixwebsocket/IXWebsocketExport.h new file mode 100644 index 00000000..9de83a93 --- /dev/null +++ b/ixwebsocket/IXWebsocketExport.h @@ -0,0 +1,33 @@ +#ifndef IXWEBSOCKET_EXPORT_H +#define IXWEBSOCKET_EXPORT_H + +#ifdef _WIN32 + +#ifdef IXWEBSOCKET_STATIC_DEFINE + /* Building and using static library */ +# define IXWEBSOCKET_EXPORT +# define IXWEBSOCKET_NO_EXPORT +#else +# ifndef IXWEBSOCKET_EXPORT +# ifdef ixwebsocket_EXPORTS /* Comes from cmake, documented in DEFINE_SYMBOL */ + /* Building dynamic library */ +# define IXWEBSOCKET_EXPORT __declspec(dllexport) +# else + /* Using dynamic library */ +# define IXWEBSOCKET_EXPORT __declspec(dllimport) +# endif +# endif + +# ifndef IXWEBSOCKET_NO_EXPORT +# define IXWEBSOCKET_NO_EXPORT +# endif +#endif + +#else /* _WIN32 */ + +# define IXWEBSOCKET_EXPORT +# define IXWEBSOCKET_NO_EXPORT + +#endif /* _WIN32 */ + +#endif /* IXWEBSOCKET_EXPORT_H */