From 9cbb2e75467748a454307930a9c84713d0458406 Mon Sep 17 00:00:00 2001 From: hayati ayguen Date: Wed, 12 Aug 2020 23:30:05 +0200 Subject: [PATCH] define export/visibility - and it's import for shared library * renamed EXPORT to CLSOCKET_API, it's for export and import * added definition CLSOCKET_NO_EXPORT for not exporting private methods * cmake: export symbols only for shared library: EXPORT_CLSOCKET_SYMBOLS * have default visibility hidden - also on linux .. Signed-off-by: hayati ayguen --- CMakeLists.txt | 5 +++++ src/Host.h | 30 +++++++++++++++++++++++++++--- src/PassiveSocket.h | 2 +- src/SimpleSocket.cpp | 20 ++++++++++++++++++++ src/SimpleSocket.h | 40 ++++++++++++++++------------------------ src/StatTimer.h | 2 +- 6 files changed, 70 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd8103f..269d400 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,12 +75,17 @@ if(CLSOCKET_SHARED) else() ADD_LIBRARY(clsocket SHARED ${CLSOCKET_SOURCES}) endif() + # linking against shared library requires the symbols + target_compile_definitions(clsocket PRIVATE EXPORT_CLSOCKET_SYMBOLS) + # have internal symbols hidden by default + set_target_properties(clsocket PROPERTIES CXX_VISIBILITY_PRESET hidden) else() if(CLSOCKET_DEP_ONLY) ADD_LIBRARY(clsocket STATIC EXCLUDE_FROM_ALL ${CLSOCKET_SOURCES}) else() ADD_LIBRARY(clsocket STATIC ${CLSOCKET_SOURCES}) endif() + # no need for export symbols with a static library if(MSVC) # Special MSVC stuff here diff --git a/src/Host.h b/src/Host.h index 91b3d16..8aaa568 100644 --- a/src/Host.h +++ b/src/Host.h @@ -251,11 +251,35 @@ extern "C" #endif #ifdef _MSC_VER - #define EXPORT __declspec(dllexport) -#else - #define EXPORT + #ifdef EXPORT_CLSOCKET_SYMBOLS + #define CLSOCKET_API __declspec(dllexport) + #else + #define CLSOCKET_API __declspec(dllimport) + #endif +#elif defined(_LINUX) || defined(_DARWIN) + #ifdef EXPORT_CLSOCKET_SYMBOLS + #define CLSOCKET_API __attribute__ ((visibility("default"))) + #endif +#endif + +#ifndef CLSOCKET_API + #define CLSOCKET_API +#endif + + +#if defined(_LINUX) || defined(_DARWIN) + #ifdef EXPORT_CLSOCKET_SYMBOLS + #define CLSOCKET_NO_EXPORT __attribute__ ((visibility("hidden"))) + #endif +#endif + +#ifndef CLSOCKET_NO_EXPORT + #define CLSOCKET_NO_EXPORT #endif + + + #ifdef __cplusplus } #endif diff --git a/src/PassiveSocket.h b/src/PassiveSocket.h index 308c555..8023b42 100644 --- a/src/PassiveSocket.h +++ b/src/PassiveSocket.h @@ -52,7 +52,7 @@ /// in a similar fashion. The big difference is that the method /// CPassiveSocket::Accept should not be called on the latter two socket /// types. -class EXPORT CPassiveSocket : public CSimpleSocket { +class CLSOCKET_API CPassiveSocket : public CSimpleSocket { public: CPassiveSocket(CSocketType type = SocketTypeTcp); virtual ~CPassiveSocket(); diff --git a/src/SimpleSocket.cpp b/src/SimpleSocket.cpp index 8060c02..c74ba6e 100644 --- a/src/SimpleSocket.cpp +++ b/src/SimpleSocket.cpp @@ -1072,6 +1072,26 @@ uint16 CSimpleSocket::GetPeerPort() const return (m_bIsServerSide) ? GetClientPort() : GetServerPort(); } +uint32 CSimpleSocket::GetReceiveWindowSize() +{ + return GetWindowSize(SO_RCVBUF); +} + +uint32 CSimpleSocket::GetSendWindowSize() +{ + return GetWindowSize(SO_SNDBUF); +} + +uint32 CSimpleSocket::SetReceiveWindowSize(uint32 nWindowSize) +{ + return SetWindowSize(SO_RCVBUF, nWindowSize); +} + +uint32 CSimpleSocket::SetSendWindowSize(uint32 nWindowSize) +{ + return SetWindowSize(SO_SNDBUF, nWindowSize); +} + //------------------------------------------------------------------------------ // diff --git a/src/SimpleSocket.h b/src/SimpleSocket.h index 3a965e1..82d2107 100644 --- a/src/SimpleSocket.h +++ b/src/SimpleSocket.h @@ -117,7 +117,7 @@ /// - Socket types /// -# CActiveSocket Class /// -# CPassiveSocket Class -class EXPORT CSimpleSocket { +class CLSOCKET_API CSimpleSocket { friend class CPassiveSocket; public: /// Defines the three possible states for shuting down a socket. @@ -207,11 +207,11 @@ class EXPORT CSimpleSocket { inline bool CloseForReads() { return Shutdown( CSimpleSocket::Receives ); - }; + } inline bool CloseForWrites() { return Shutdown( CSimpleSocket::Sends ); - }; + } /// Examine the socket descriptor sets currently owned by the instance of /// the socket class (the readfds, writefds, and errorfds parameters) to @@ -616,30 +616,22 @@ class EXPORT CSimpleSocket { /// Get the TCP receive buffer window size for the current socket object. ///

\b NOTE: Linux will set the receive buffer to twice the value passed. /// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful. - uint32 GetReceiveWindowSize() { - return GetWindowSize(SO_RCVBUF); - }; + uint32 GetReceiveWindowSize(); /// Get the TCP send buffer window size for the current socket object. ///

\b NOTE: Linux will set the send buffer to twice the value passed. /// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful. - uint32 GetSendWindowSize() { - return GetWindowSize(SO_SNDBUF); - }; + uint32 GetSendWindowSize(); /// Set the TCP receive buffer window size for the current socket object. ///

\b NOTE: Linux will set the receive buffer to twice the value passed. /// @return zero on failure else the number of bytes of the TCP send buffer window size if successful. - uint32 SetReceiveWindowSize(uint32 nWindowSize) { - return SetWindowSize(SO_RCVBUF, nWindowSize); - }; + uint32 SetReceiveWindowSize(uint32 nWindowSize); /// Set the TCP send buffer window size for the current socket object. ///

\b NOTE: Linux will set the send buffer to twice the value passed. /// @return zero on failure else the number of bytes of the TCP send buffer window size if successful. - uint32 SetSendWindowSize(uint32 nWindowSize) { - return SetWindowSize(SO_SNDBUF, nWindowSize); - }; + uint32 SetSendWindowSize(uint32 nWindowSize); /// Disable the Nagle algorithm (Set TCP_NODELAY to true) /// @return false if failed to set socket option otherwise return true; @@ -672,11 +664,11 @@ class EXPORT CSimpleSocket { private: /// Generic function used to get the send/receive window size /// @return zero on failure else the number of bytes of the TCP window size if successful. - uint32 GetWindowSize(uint32 nOptionName); + CLSOCKET_NO_EXPORT uint32 GetWindowSize(uint32 nOptionName); /// Generic function used to set the send/receive window size /// @return zero on failure else the number of bytes of the TCP window size if successful. - uint32 SetWindowSize(uint32 nOptionName, uint32 nWindowSize); + CLSOCKET_NO_EXPORT uint32 SetWindowSize(uint32 nOptionName, uint32 nWindowSize); /// Attempts to send at most nNumItem blocks described by sendVector @@ -688,29 +680,29 @@ class EXPORT CSimpleSocket { /// @return number of bytes actually sent, return of zero means the /// connection has been shutdown on the other side, and a return of -1 /// means that an error has occurred. - int32 Writev(const struct iovec *pVector, size_t nCount); + CLSOCKET_NO_EXPORT int32 Writev(const struct iovec *pVector, size_t nCount); /// Flush the socket descriptor owned by the object. /// @return true data was successfully sent, else return false; - bool Flush(); + CLSOCKET_NO_EXPORT bool Flush(); - CSimpleSocket *operator=(CSimpleSocket &socket); + CLSOCKET_NO_EXPORT CSimpleSocket *operator=(CSimpleSocket &socket); static bool GetAddrInfoStatic(const char *pAddr, uint16 nPort, struct in_addr * pOutIpAddress, CSocketType nSocketType = SocketTypeTcp ); - bool GetAddrInfo(const char *pAddr, uint16 nPort, struct in_addr * pOutIpAddress ); + CLSOCKET_NO_EXPORT bool GetAddrInfo(const char *pAddr, uint16 nPort, struct in_addr * pOutIpAddress ); /// Utility function used to create a TCP connection, called from Open(). /// @return true if successful connection made, otherwise false. - bool ConnectTCP(const char *pAddr, uint16 nPort); + CLSOCKET_NO_EXPORT bool ConnectTCP(const char *pAddr, uint16 nPort); /// Utility function used to create a UDP connection, called from Open(). /// @return true if successful connection made, otherwise false. - bool ConnectUDP(const char *pAddr, uint16 nPort); + CLSOCKET_NO_EXPORT bool ConnectUDP(const char *pAddr, uint16 nPort); /// Utility function used to create a RAW connection, called from Open(). /// @return true if successful connection made, otherwise false. - bool ConnectRAW(const char *pAddr, uint16 nPort); + CLSOCKET_NO_EXPORT bool ConnectRAW(const char *pAddr, uint16 nPort); protected: SOCKET m_socket; /// socket handle diff --git a/src/StatTimer.h b/src/StatTimer.h index c8097b1..9f30e04 100644 --- a/src/StatTimer.h +++ b/src/StatTimer.h @@ -68,7 +68,7 @@ /// Class to abstract socket communications in a cross platform manner. /// This class is designed -class EXPORT CStatTimer { +class CLSOCKET_API CStatTimer { public: CStatTimer() {