diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ad61c9..429ba62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,9 +7,7 @@ set(BUILD_MINOR "4") set(BUILD_VERSION "3") set(BUILD_VERSION ${BUILD_MAJOR}.${BUILD_MINOR}.${BUILD_VERSION}) -include_directories(src) - -SET(CLSOCKET_HEADERS +set(CLSOCKET_HEADERS src/ActiveSocket.h src/Host.h src/PassiveSocket.h @@ -17,87 +15,53 @@ src/SimpleSocket.h src/StatTimer.h ) -SET(CLSOCKET_SOURCES +set(CLSOCKET_SOURCES src/SimpleSocket.cpp src/ActiveSocket.cpp src/PassiveSocket.cpp ) -# mark headers as headers... -SET_SOURCE_FILES_PROPERTIES( ${CLSOCKET_HEADERS} PROPERTIES HEADER_FILE_ONLY TRUE ) -# append to sources so that dependency checks work on headers -LIST(APPEND CLSOCKET_SOURCES ${CLSOCKET_HEADERS}) - -# OS and compiler checks. -if(UNIX) - # linux / normal unix - add_definitions(-D_LINUX) - if(CYGWIN) - # Special Cygwin stuff here - elseif(APPLE) - # Special Apple stuff here - remove_definitions(-D_LINUX) - add_definitions(-D_DARWIN) - endif() -elseif(WIN32) - add_definitions(-DWIN32) - SET(PROJECT_LIBS Ws2_32.lib) - if(MINGW) - # Special MINGW stuff here - elseif(MSVC) - # Special MSVC stuff here - add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS) - else() - # No idea what it is, but there's a good chance it's too weird. - MESSAGE( FATAL_ERROR "Using unknown WIN32 compiler... NOT. Please add to build system." ) - endif() +option(BUILD_SHARED_LIBS "Build libs as shared" ON) +if(DEFINED CLSOCKET_SHARED) + message(FATAL_ERROR "The CLSOCKET_SHARED option is deprecated, instead use BUILD_SHARED_LIBS.") +endif() +if(DEFINED CLSOCKET_DEP_ONLY) + message(FATAL_ERROR "The CLSOCKET_DEP_ONLY option is deprecated, instead use add_subdirectory() with EXCLUDE_FROM_ALL.") endif() - -OPTION(CLSOCKET_SHARED "Build clsocket lib as shared." ON) -OPTION(CLSOCKET_DEP_ONLY "Build for use inside other CMake projects as dependency." OFF) # make the lib -if(CLSOCKET_SHARED) - if(CLSOCKET_DEP_ONLY) - ADD_LIBRARY(clsocket SHARED EXCLUDE_FROM_ALL ${CLSOCKET_SOURCES}) - else() - ADD_LIBRARY(clsocket SHARED ${CLSOCKET_SOURCES}) - endif() -else() - if(CLSOCKET_DEP_ONLY) - ADD_LIBRARY(clsocket STATIC EXCLUDE_FROM_ALL ${CLSOCKET_SOURCES}) - else() - ADD_LIBRARY(clsocket STATIC ${CLSOCKET_SOURCES}) - endif() +# add headers so that they appear in IDEs +add_library(clsocket ${CLSOCKET_SOURCES} ${CLSOCKET_HEADERS}) +if(WIN32) + add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS) + target_link_libraries(clsocket PRIVATE Ws2_32) endif() -TARGET_LINK_LIBRARIES(clsocket ${PROJECT_LIBS}) +# target_include_directories was added in 2.8.11 +set_target_properties(clsocket PROPERTIES + INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/src + INTERFACE_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/src +) # install into configured prefix -if(NOT CLSOCKET_DEP_ONLY) - install(TARGETS clsocket ARCHIVE DESTINATION lib LIBRARY DESTINATION lib) - install(FILES ${CLSOCKET_HEADERS} DESTINATION include) -else() - -endif() +install(TARGETS clsocket DESTINATION lib) +install(FILES ${CLSOCKET_HEADERS} DESTINATION include) set_target_properties(clsocket PROPERTIES VERSION ${BUILD_VERSION} SOVERSION ${BUILD_MAJOR}) if(UNIX) - OPTION(CLSOCKET_EXAMPLES "Build the examples" OFF) + option(CLSOCKET_EXAMPLES "Build the examples" OFF) if(CLSOCKET_EXAMPLES) - ADD_EXECUTABLE(clsocket-example examples/RecvAsync.cpp) - TARGET_LINK_LIBRARIES(clsocket-example clsocket pthread) - if(NOT CLSOCKET_DEP_ONLY) - install(TARGETS clsocket-example DESTINATION bin) - endif() + add_executable(clsocket-example examples/RecvAsync.cpp) + target_link_libraries(clsocket-example clsocket pthread) + install(TARGETS clsocket-example DESTINATION bin) - ADD_EXECUTABLE(querydaytime-example examples/QueryDayTime.cpp) - TARGET_LINK_LIBRARIES(querydaytime-example clsocket) + add_executable(querydaytime-example examples/QueryDayTime.cpp) + target_link_libraries(querydaytime-example clsocket) - ADD_EXECUTABLE(echoserver-example examples/EchoServer.cpp) - TARGET_LINK_LIBRARIES(echoserver-example clsocket) + add_executable(echoserver-example examples/EchoServer.cpp) + target_link_libraries(echoserver-example clsocket) endif() endif() diff --git a/README b/README index 9de2cab..cc9aa03 100644 --- a/README +++ b/README @@ -21,13 +21,10 @@ This is a very small library and is very easy to build and configure. To build make sure you are logged in as a user who has access to the recommend GNU installation directories. Then type -make -BUILD=Release && make install +cmake -DCMAKE_BUILD_TYPE=Release && make && make install That is it now you are off and running. -NOTE: When using the library with WINDOWS you must define _WIN32 and when using with LINUX - you must define _LINUX. - ------------------------------------------------------------------------------------------ * SimpleSocket Class Overview ------------------------------------------------------------------------------------------ diff --git a/examples/RecvAsync.cpp b/examples/RecvAsync.cpp index d4e415f..647c039 100644 --- a/examples/RecvAsync.cpp +++ b/examples/RecvAsync.cpp @@ -1,7 +1,7 @@ #include #include "PassiveSocket.h" -#ifdef WIN32 +#ifdef _WIN32 #include // usually defined with #include diff --git a/src/ActiveSocket.cpp b/src/ActiveSocket.cpp index 3dd5b12..b9054dd 100644 --- a/src/ActiveSocket.cpp +++ b/src/ActiveSocket.cpp @@ -64,7 +64,7 @@ bool CActiveSocket::ConnectTCP(const char *pAddr, uint16 nPort) if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL) { -#ifdef WIN32 +#ifdef _WIN32 TranslateSocketError(); #else if (h_errno == HOST_NOT_FOUND) @@ -144,7 +144,7 @@ bool CActiveSocket::ConnectUDP(const char *pAddr, uint16 nPort) if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL) { -#ifdef WIN32 +#ifdef _WIN32 TranslateSocketError(); #else if (h_errno == HOST_NOT_FOUND) @@ -202,7 +202,7 @@ bool CActiveSocket::ConnectRAW(const char *pAddr, uint16 nPort) if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL) { -#ifdef WIN32 +#ifdef _WIN32 TranslateSocketError(); #else if (h_errno == HOST_NOT_FOUND) diff --git a/src/Host.h b/src/Host.h index 91b3d16..cbdd09c 100644 --- a/src/Host.h +++ b/src/Host.h @@ -61,7 +61,7 @@ extern "C" #define __WORDSIZE 32 #endif -#if defined(_LINUX) || defined(_DARWIN) +#if defined(__unix__) || defined(__APPLE__) typedef unsigned char uint8; typedef char int8; typedef unsigned short uint16; @@ -71,7 +71,7 @@ extern "C" typedef int SOCKET; #endif -#ifdef WIN32 +#ifdef _WIN32 struct iovec { void *iov_base; size_t iov_len; @@ -85,11 +85,11 @@ extern "C" typedef int int32; #endif -#ifdef WIN32 +#ifdef _WIN32 typedef int socklen_t; #endif -#if defined(WIN32) +#if defined(_WIN32) typedef unsigned long long int uint64; typedef long long int int64; #elif (__WORDSIZE == 32) @@ -102,7 +102,7 @@ extern "C" typedef long int int64; #endif -#ifdef WIN32 +#ifdef _WIN32 #ifndef UINT8_MAX #define UINT8_MAX (UCHAR_MAX) @@ -123,7 +123,7 @@ extern "C" #endif #endif -#if defined(WIN32) +#if defined(_WIN32) #define ssize_t size_t #endif @@ -150,7 +150,7 @@ extern "C" /* Socket Macros */ /* */ /*---------------------------------------------------------------------------*/ -#ifdef WIN32 +#ifdef _WIN32 #define SHUT_RD 0 #define SHUT_WR 1 #define SHUT_RDWR 2 @@ -176,7 +176,7 @@ extern "C" #define GETHOSTBYNAME(a) gethostbyname(a) #endif -#if defined(_LINUX) || defined(_DARWIN) +#if defined(__unix__) || defined(__APPLE__) #define ACCEPT(a,b,c) accept(a,b,c) #define CONNECT(a,b,c) connect(a,b,c) #define CLOSE(a) close(a) @@ -226,19 +226,19 @@ extern "C" /* Misc Macros */ /* */ /*---------------------------------------------------------------------------*/ -#if defined(WIN32) +#if defined(_WIN32) #define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x) #else #define GET_CLOCK_COUNT(x) gettimeofday(x, NULL) #endif -#if defined(WIN32) +#if defined(_WIN32) #define STRTOULL(x) _atoi64(x) #else #define STRTOULL(x) strtoull(x, NULL, 10) #endif -#if defined(WIN32) +#if defined(_WIN32) #define SNPRINTF _snprintf #define PRINTF printf #define VPRINTF vprintf diff --git a/src/PassiveSocket.cpp b/src/PassiveSocket.cpp index b9dce77..f74762f 100644 --- a/src/PassiveSocket.cpp +++ b/src/PassiveSocket.cpp @@ -51,7 +51,7 @@ CPassiveSocket::CPassiveSocket(CSocketType nType) : CSimpleSocket(nType) bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, uint16 nPort) { bool bRetVal = false; -#ifdef WIN32 +#ifdef _WIN32 ULONG inAddr; #else in_addr_t inAddr; @@ -130,7 +130,7 @@ bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, u bool CPassiveSocket::Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog) { bool bRetVal = false; -#ifdef WIN32 +#ifdef _WIN32 ULONG inAddr; #else in_addr_t inAddr; diff --git a/src/SimpleSocket.cpp b/src/SimpleSocket.cpp index 3058e65..db284e8 100644 --- a/src/SimpleSocket.cpp +++ b/src/SimpleSocket.cpp @@ -92,7 +92,7 @@ CSimpleSocket::CSimpleSocket(CSocketType nType) : //---------------------------------------------------------------------- case CSimpleSocket::SocketTypeRaw: { -#if defined(_LINUX) && !defined(_DARWIN) +#if defined(__unix__) && !defined(__APPLE__) m_nSocketDomain = AF_PACKET; m_nSocketType = CSimpleSocket::SocketTypeRaw; #endif @@ -137,7 +137,7 @@ bool CSimpleSocket::Initialize() { errno = CSimpleSocket::SocketSuccess; -#ifdef WIN32 +#ifdef _WIN32 //------------------------------------------------------------------------- // Data structure containing general Windows Sockets Info //------------------------------------------------------------------------- @@ -831,7 +831,7 @@ bool CSimpleSocket::SetNonblocking(void) { int32 nCurFlags; -#if WIN32 +#if _WIN32 nCurFlags = 1; if (ioctlsocket(m_socket, FIONBIO, (ULONG *)&nCurFlags) != 0) @@ -870,7 +870,7 @@ bool CSimpleSocket::SetBlocking(void) { int32 nCurFlags; -#if WIN32 +#if _WIN32 nCurFlags = 0; if (ioctlsocket(m_socket, FIONBIO, (ULONG *)&nCurFlags) != 0) @@ -947,7 +947,7 @@ int32 CSimpleSocket::SendFile(int32 nOutFd, int32 nInFd, off_t *pOffset, int32 n //------------------------------------------------------------------------------ void CSimpleSocket::TranslateSocketError(void) { -#if defined(_LINUX) || defined(_DARWIN) +#if defined(__unix__) || defined(__APPLE__) switch (errno) { case EXIT_SUCCESS: @@ -1010,7 +1010,7 @@ void CSimpleSocket::TranslateSocketError(void) break; } #endif -#ifdef WIN32 +#ifdef _WIN32 int32 nError = WSAGetLastError(); switch (nError) { diff --git a/src/SimpleSocket.h b/src/SimpleSocket.h index 4a5a9be..d84e83f 100644 --- a/src/SimpleSocket.h +++ b/src/SimpleSocket.h @@ -49,7 +49,7 @@ #include #include -#if defined(_LINUX) || defined (_DARWIN) +#if defined(__unix__) || defined (__APPLE__) #include #include #include @@ -57,16 +57,16 @@ #include #include #endif -#ifdef _LINUX +#ifdef __unix__ #include #include #include #include #endif -#ifdef _DARWIN +#ifdef __APPLE__ #include #endif -#if defined(_LINUX) || defined (_DARWIN) +#if defined(__unix__) || defined (__APPLE__) #include #include #include @@ -570,7 +570,7 @@ class EXPORT CSimpleSocket { struct sockaddr_in m_stMulticastGroup; /// multicast group to bind to struct linger m_stLinger; /// linger flag CStatTimer m_timer; /// internal statistics. -#ifdef WIN32 +#ifdef _WIN32 WSADATA m_hWSAData; /// Windows #endif fd_set m_writeFds; /// write file descriptor set diff --git a/src/StatTimer.h b/src/StatTimer.h index c8097b1..94ae7a7 100644 --- a/src/StatTimer.h +++ b/src/StatTimer.h @@ -45,19 +45,19 @@ #include -#if WIN32 +#if _WIN32 #include #include #endif -#ifdef _LINUX +#ifdef __unix__ #include #include #endif #include "Host.h" -#if defined(WIN32) +#if defined(_WIN32) #define GET_CLOCK_COUNT(x) QueryPerformanceCounter((LARGE_INTEGER *)x) #else #define GET_CLOCK_COUNT(x) gettimeofday(x, NULL)