From 03765b08bc8b5dc986e2cb41efe7dc81e3bf1dca Mon Sep 17 00:00:00 2001 From: Sergey Yagovtsev Date: Fri, 9 Sep 2016 06:14:34 +0300 Subject: [PATCH] Not compiling version of windows sock --- profiler_gui/CMakeLists.txt | 2 +- src/CMakeLists.txt | 5 +- src/easy_socket.cpp | 93 ++++++++++++++++++++++++++++++++++++- src/easy_socket.h | 20 ++++++++ src/event_trace_win.h | 2 +- src/profile_manager.h | 3 +- 6 files changed, 120 insertions(+), 5 deletions(-) diff --git a/profiler_gui/CMakeLists.txt b/profiler_gui/CMakeLists.txt index e7d7187..6860222 100644 --- a/profiler_gui/CMakeLists.txt +++ b/profiler_gui/CMakeLists.txt @@ -8,7 +8,7 @@ set(CMAKE_AUTOMOC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) find_package(Qt5Widgets REQUIRED) -find_package(Qt5Network REQUIRED) +#find_package(Qt5Network REQUIRED) include_directories(${ROOT}/3rdparty/nanomsg/src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index be059c2..8da331f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,5 +29,8 @@ add_definitions( add_library(${PROJECT_NAME} SHARED ${SOURCES}) +if(UNIX) +set(PLATFORM_LIBS thread) +endif(UNIX) -target_link_libraries(${PROJECT_NAME} pthread) +target_link_libraries(${PROJECT_NAME} ${PLATFORM_LIBS}) diff --git a/src/easy_socket.cpp b/src/easy_socket.cpp index 32e3a89..629fd41 100644 --- a/src/easy_socket.cpp +++ b/src/easy_socket.cpp @@ -17,9 +17,10 @@ along with this program.If not, see . **/ #include "easy_socket.h" -#include + #ifndef _WIN32 +#include EasySocket::EasySocket() { @@ -32,6 +33,10 @@ EasySocket::EasySocket() setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); } +EasySocket::~EasySocket() +{ +} + size_t EasySocket::write(const void *buf, size_t nbyte) { if(m_socket <= 0){ @@ -74,5 +79,91 @@ int EasySocket::connect() return ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); } +#else + +#pragma comment (lib, "Ws2_32.lib") +#pragma comment (lib, "Mswsock.lib") +#pragma comment (lib, "AdvApi32.lib") + +EasySocket::EasySocket() +{ + // socket + WSADATA wsaData; + int wsaret = WSAStartup(0x101, &wsaData); + + if (wsaret == 0) + { + m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);; + if (m_socket == INVALID_SOCKET) { + WSACleanup(); + } + } + + +} + +EasySocket::~EasySocket() +{ + if (m_socket) + WSACleanup(); +} + +size_t EasySocket::write(const void *buf, size_t nbyte) +{ + if (m_socket <= 0){ + return -1; + } + return send(m_socket, (const char*)buf, nbyte,0); +} + +size_t EasySocket::read(void *buf, size_t nbyte) +{ + if (m_socket <= 0){ + return -1; + } + return recv(m_socket, (char*)buf, nbyte,0); +} + +bool EasySocket::setAddress(const char *serv, uint16_t portno) +{ + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + int iResult; + char buffer[20] = {}; + _itoa(portno, buffer, 10); + iResult = getaddrinfo(serv, buffer, &hints, &result); + if (iResult != 0) { + WSACleanup(); + return false; + } + + return false; +} + +int EasySocket::connect() +{ + if (!m_socket || !result){ + return -1; + } + + SOCKET ConnectSocket = socket(result->ai_family, result->ai_socktype, + result->ai_protocol); + if (ConnectSocket == INVALID_SOCKET) { + return -1; + } + + // Connect to server. + auto iResult = ::connect(ConnectSocket, result->ai_addr, (int)result->ai_addrlen); + if (iResult == SOCKET_ERROR) { + closesocket(ConnectSocket); + ConnectSocket = INVALID_SOCKET; + } + + return iResult; +} + + #endif diff --git a/src/easy_socket.h b/src/easy_socket.h index 53f91a4..0afbb69 100644 --- a/src/easy_socket.h +++ b/src/easy_socket.h @@ -27,6 +27,21 @@ along with this program.If not, see . #include #include #include +#else + +#define WIN32_LEAN_AND_MEAN + +#include +#include + +#include + + + +#include +#include + + #endif class EasySocket @@ -36,10 +51,15 @@ class EasySocket uint16_t m_port = 0; struct sockaddr_in serv_addr; struct hostent *server = nullptr; +#else + SOCKET m_socket = 0; + struct addrinfo *result = NULL; + struct addrinfo hints; #endif public: EasySocket(); + ~EasySocket(); size_t write(const void *buf, size_t nbyte); size_t read(void *buf, size_t nbyte); diff --git a/src/event_trace_win.h b/src/event_trace_win.h index a8aa11a..d787fbb 100644 --- a/src/event_trace_win.h +++ b/src/event_trace_win.h @@ -6,7 +6,7 @@ #ifdef _WIN32 #define INITGUID // This is to enable using SystemTraceControlGuid in evntrace.h. -#include +//#include //#include #include #include diff --git a/src/profile_manager.h b/src/profile_manager.h index b7e8f04..0c665c3 100644 --- a/src/profile_manager.h +++ b/src/profile_manager.h @@ -32,13 +32,14 @@ along with this program.If not, see . #include #include #include +#include #include ////////////////////////////////////////////////////////////////////////// #ifdef _WIN32 -#include +//#include #else #include #include