Skip to content

Commit 9cbb2e7

Browse files
committed
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 <[email protected]>
1 parent 7222078 commit 9cbb2e7

File tree

6 files changed

+70
-29
lines changed

6 files changed

+70
-29
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@ if(CLSOCKET_SHARED)
7575
else()
7676
ADD_LIBRARY(clsocket SHARED ${CLSOCKET_SOURCES})
7777
endif()
78+
# linking against shared library requires the symbols
79+
target_compile_definitions(clsocket PRIVATE EXPORT_CLSOCKET_SYMBOLS)
80+
# have internal symbols hidden by default
81+
set_target_properties(clsocket PROPERTIES CXX_VISIBILITY_PRESET hidden)
7882
else()
7983
if(CLSOCKET_DEP_ONLY)
8084
ADD_LIBRARY(clsocket STATIC EXCLUDE_FROM_ALL ${CLSOCKET_SOURCES})
8185
else()
8286
ADD_LIBRARY(clsocket STATIC ${CLSOCKET_SOURCES})
8387
endif()
88+
# no need for export symbols with a static library
8489

8590
if(MSVC)
8691
# Special MSVC stuff here

src/Host.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,35 @@ extern "C"
251251
#endif
252252

253253
#ifdef _MSC_VER
254-
#define EXPORT __declspec(dllexport)
255-
#else
256-
#define EXPORT
254+
#ifdef EXPORT_CLSOCKET_SYMBOLS
255+
#define CLSOCKET_API __declspec(dllexport)
256+
#else
257+
#define CLSOCKET_API __declspec(dllimport)
258+
#endif
259+
#elif defined(_LINUX) || defined(_DARWIN)
260+
#ifdef EXPORT_CLSOCKET_SYMBOLS
261+
#define CLSOCKET_API __attribute__ ((visibility("default")))
262+
#endif
263+
#endif
264+
265+
#ifndef CLSOCKET_API
266+
#define CLSOCKET_API
267+
#endif
268+
269+
270+
#if defined(_LINUX) || defined(_DARWIN)
271+
#ifdef EXPORT_CLSOCKET_SYMBOLS
272+
#define CLSOCKET_NO_EXPORT __attribute__ ((visibility("hidden")))
273+
#endif
274+
#endif
275+
276+
#ifndef CLSOCKET_NO_EXPORT
277+
#define CLSOCKET_NO_EXPORT
257278
#endif
258279

280+
281+
282+
259283
#ifdef __cplusplus
260284
}
261285
#endif

src/PassiveSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
/// in a similar fashion. The big difference is that the method
5353
/// CPassiveSocket::Accept should not be called on the latter two socket
5454
/// types.
55-
class EXPORT CPassiveSocket : public CSimpleSocket {
55+
class CLSOCKET_API CPassiveSocket : public CSimpleSocket {
5656
public:
5757
CPassiveSocket(CSocketType type = SocketTypeTcp);
5858
virtual ~CPassiveSocket();

src/SimpleSocket.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,26 @@ uint16 CSimpleSocket::GetPeerPort() const
10721072
return (m_bIsServerSide) ? GetClientPort() : GetServerPort();
10731073
}
10741074

1075+
uint32 CSimpleSocket::GetReceiveWindowSize()
1076+
{
1077+
return GetWindowSize(SO_RCVBUF);
1078+
}
1079+
1080+
uint32 CSimpleSocket::GetSendWindowSize()
1081+
{
1082+
return GetWindowSize(SO_SNDBUF);
1083+
}
1084+
1085+
uint32 CSimpleSocket::SetReceiveWindowSize(uint32 nWindowSize)
1086+
{
1087+
return SetWindowSize(SO_RCVBUF, nWindowSize);
1088+
}
1089+
1090+
uint32 CSimpleSocket::SetSendWindowSize(uint32 nWindowSize)
1091+
{
1092+
return SetWindowSize(SO_SNDBUF, nWindowSize);
1093+
}
1094+
10751095

10761096
//------------------------------------------------------------------------------
10771097
//

src/SimpleSocket.h

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
/// - Socket types
118118
/// -# CActiveSocket Class
119119
/// -# CPassiveSocket Class
120-
class EXPORT CSimpleSocket {
120+
class CLSOCKET_API CSimpleSocket {
121121
friend class CPassiveSocket;
122122
public:
123123
/// Defines the three possible states for shuting down a socket.
@@ -207,11 +207,11 @@ class EXPORT CSimpleSocket {
207207

208208
inline bool CloseForReads() {
209209
return Shutdown( CSimpleSocket::Receives );
210-
};
210+
}
211211

212212
inline bool CloseForWrites() {
213213
return Shutdown( CSimpleSocket::Sends );
214-
};
214+
}
215215

216216
/// Examine the socket descriptor sets currently owned by the instance of
217217
/// the socket class (the readfds, writefds, and errorfds parameters) to
@@ -616,30 +616,22 @@ class EXPORT CSimpleSocket {
616616
/// Get the TCP receive buffer window size for the current socket object.
617617
/// <br><br>\b NOTE: Linux will set the receive buffer to twice the value passed.
618618
/// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful.
619-
uint32 GetReceiveWindowSize() {
620-
return GetWindowSize(SO_RCVBUF);
621-
};
619+
uint32 GetReceiveWindowSize();
622620

623621
/// Get the TCP send buffer window size for the current socket object.
624622
/// <br><br>\b NOTE: Linux will set the send buffer to twice the value passed.
625623
/// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful.
626-
uint32 GetSendWindowSize() {
627-
return GetWindowSize(SO_SNDBUF);
628-
};
624+
uint32 GetSendWindowSize();
629625

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

637631
/// Set the TCP send buffer window size for the current socket object.
638632
/// <br><br>\b NOTE: Linux will set the send buffer to twice the value passed.
639633
/// @return zero on failure else the number of bytes of the TCP send buffer window size if successful.
640-
uint32 SetSendWindowSize(uint32 nWindowSize) {
641-
return SetWindowSize(SO_SNDBUF, nWindowSize);
642-
};
634+
uint32 SetSendWindowSize(uint32 nWindowSize);
643635

644636
/// Disable the Nagle algorithm (Set TCP_NODELAY to true)
645637
/// @return false if failed to set socket option otherwise return true;
@@ -672,11 +664,11 @@ class EXPORT CSimpleSocket {
672664
private:
673665
/// Generic function used to get the send/receive window size
674666
/// @return zero on failure else the number of bytes of the TCP window size if successful.
675-
uint32 GetWindowSize(uint32 nOptionName);
667+
CLSOCKET_NO_EXPORT uint32 GetWindowSize(uint32 nOptionName);
676668

677669
/// Generic function used to set the send/receive window size
678670
/// @return zero on failure else the number of bytes of the TCP window size if successful.
679-
uint32 SetWindowSize(uint32 nOptionName, uint32 nWindowSize);
671+
CLSOCKET_NO_EXPORT uint32 SetWindowSize(uint32 nOptionName, uint32 nWindowSize);
680672

681673

682674
/// Attempts to send at most nNumItem blocks described by sendVector
@@ -688,29 +680,29 @@ class EXPORT CSimpleSocket {
688680
/// @return number of bytes actually sent, return of zero means the
689681
/// connection has been shutdown on the other side, and a return of -1
690682
/// means that an error has occurred.
691-
int32 Writev(const struct iovec *pVector, size_t nCount);
683+
CLSOCKET_NO_EXPORT int32 Writev(const struct iovec *pVector, size_t nCount);
692684

693685
/// Flush the socket descriptor owned by the object.
694686
/// @return true data was successfully sent, else return false;
695-
bool Flush();
687+
CLSOCKET_NO_EXPORT bool Flush();
696688

697-
CSimpleSocket *operator=(CSimpleSocket &socket);
689+
CLSOCKET_NO_EXPORT CSimpleSocket *operator=(CSimpleSocket &socket);
698690

699691
static bool GetAddrInfoStatic(const char *pAddr, uint16 nPort, struct in_addr * pOutIpAddress, CSocketType nSocketType = SocketTypeTcp );
700692

701-
bool GetAddrInfo(const char *pAddr, uint16 nPort, struct in_addr * pOutIpAddress );
693+
CLSOCKET_NO_EXPORT bool GetAddrInfo(const char *pAddr, uint16 nPort, struct in_addr * pOutIpAddress );
702694

703695
/// Utility function used to create a TCP connection, called from Open().
704696
/// @return true if successful connection made, otherwise false.
705-
bool ConnectTCP(const char *pAddr, uint16 nPort);
697+
CLSOCKET_NO_EXPORT bool ConnectTCP(const char *pAddr, uint16 nPort);
706698

707699
/// Utility function used to create a UDP connection, called from Open().
708700
/// @return true if successful connection made, otherwise false.
709-
bool ConnectUDP(const char *pAddr, uint16 nPort);
701+
CLSOCKET_NO_EXPORT bool ConnectUDP(const char *pAddr, uint16 nPort);
710702

711703
/// Utility function used to create a RAW connection, called from Open().
712704
/// @return true if successful connection made, otherwise false.
713-
bool ConnectRAW(const char *pAddr, uint16 nPort);
705+
CLSOCKET_NO_EXPORT bool ConnectRAW(const char *pAddr, uint16 nPort);
714706

715707
protected:
716708
SOCKET m_socket; /// socket handle

src/StatTimer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
/// Class to abstract socket communications in a cross platform manner.
7070
/// This class is designed
71-
class EXPORT CStatTimer {
71+
class CLSOCKET_API CStatTimer {
7272
public:
7373
CStatTimer()
7474
{

0 commit comments

Comments
 (0)