Skip to content

Commit

Permalink
define export/visibility - and it's import for shared library
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hayguen committed Aug 12, 2020
1 parent 7222078 commit 9cbb2e7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 29 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 27 additions & 3 deletions src/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/PassiveSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions src/SimpleSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


//------------------------------------------------------------------------------
//
Expand Down
40 changes: 16 additions & 24 deletions src/SimpleSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -616,30 +616,22 @@ class EXPORT CSimpleSocket {
/// Get the TCP receive buffer window size for the current socket object.
/// <br><br>\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.
/// <br><br>\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.
/// <br><br>\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.
/// <br><br>\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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/StatTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 9cbb2e7

Please sign in to comment.