From 4c738862337ce46b123ae9cf5ba1cbefa3fb99ff Mon Sep 17 00:00:00 2001 From: Sergey Yagovtsev Date: Tue, 20 Sep 2016 00:54:56 +0300 Subject: [PATCH] common code for linux and windows --- include/profiler/easy_socket.h | 15 +++---- src/CMakeLists.txt | 7 +++ src/easy_socket.cpp | 81 +++++++--------------------------- src/profile_manager.cpp | 10 ++--- 4 files changed, 30 insertions(+), 83 deletions(-) diff --git a/include/profiler/easy_socket.h b/include/profiler/easy_socket.h index 96d584c..5d72119 100644 --- a/include/profiler/easy_socket.h +++ b/include/profiler/easy_socket.h @@ -54,10 +54,9 @@ public: CONNECTION_STATE_DISCONNECTED }; + private: - - - + void checkResult(int result); bool checkSocket(socket_t s) const; static int _close(socket_t s); @@ -69,17 +68,13 @@ private: int wsaret = -1; -#ifndef _WIN32 + struct hostent * server; struct sockaddr_in serv_addr; - struct hostent *server = nullptr; -#else - struct addrinfo *result = NULL; - struct addrinfo hints; -#endif - ConnectionState m_state = CONNECTION_STATE_UNKNOWN; + public: + EasySocket(); ~EasySocket(); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ce4e7ac..133797d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,6 +26,13 @@ add_definitions( -D_BUILD_PROFILER ) +if(WIN32) +add_definitions( + -D_WINSOCK_DEPRECATED_NO_WARNINGS +) +endif(WIN32) + + add_library(${PROJECT_NAME} SHARED ${SOURCES}) if(UNIX) diff --git a/src/easy_socket.cpp b/src/easy_socket.cpp index 82ae7ef..f10118d 100644 --- a/src/easy_socket.cpp +++ b/src/easy_socket.cpp @@ -218,10 +218,21 @@ int EasySocket::accept() return m_replySocket; } -#ifndef _WIN32 -#include -#include +bool EasySocket::setAddress(const char *serv, uint16_t portno) +{ + server = gethostbyname(serv); + if (server == NULL) { + return false; + //fprintf(stderr,"ERROR, no such host\n"); + } + memset((char *)&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length); + serv_addr.sin_port = htons(portno); + + return true; +} int EasySocket::connect() { @@ -230,6 +241,7 @@ int EasySocket::connect() //fprintf(stderr,"ERROR, no such host\n"); } int res = ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); + checkResult(res); if(res == 0){ struct timeval tv; @@ -243,66 +255,3 @@ int EasySocket::connect() } return res; } - -bool EasySocket::setAddress(const char *serv, uint16_t portno) -{ - server = gethostbyname(serv); - if (server == NULL) { - return false; - //fprintf(stderr,"ERROR, no such host\n"); - } - bzero((char *) &serv_addr, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - bcopy((char *)server->h_addr, - (char *)&serv_addr.sin_addr.s_addr, - server->h_length); - serv_addr.sin_port = htons(portno); - - return true; -} - -#else - -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) { - return false; - } - - return true; -} - -int EasySocket::connect() -{ - if (!m_socket || !result){ - return -1; - } - - // Connect to server. - auto iResult = ::connect(m_socket, result->ai_addr, (int)result->ai_addrlen); - checkResult(iResult); - if (iResult == SOCKET_ERROR) { - return iResult; - } - /**/ - struct timeval tv; - - tv.tv_sec = 1; - tv.tv_usec = 0; - - setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); - - m_replySocket = m_socket; - - return iResult; -} - -#endif diff --git a/src/profile_manager.cpp b/src/profile_manager.cpp index b4a7379..c3b9b28 100644 --- a/src/profile_manager.cpp +++ b/src/profile_manager.cpp @@ -640,9 +640,6 @@ void ProfileManager::startListen() dumpBlocksToStream(os); dm.size = (uint32_t)os.stream().str().length(); - - //dm.size = 8192*4; - int packet_size = int(sizeof(dm)) + int(dm.size); char *sendbuf = new char[packet_size]; @@ -654,16 +651,15 @@ void ProfileManager::startListen() bytes = socket.send(sendbuf, packet_size); hasConnect = bytes > 0; - std::string tempfilename = "test_snd.prof"; + /*std::string tempfilename = "test_snd.prof"; std::ofstream of(tempfilename, std::fstream::binary); of.write((const char*)os.stream().str().c_str(), dm.size); - of.close(); + of.close();*/ delete[] sendbuf; - //std::this_thread::sleep_for(std::chrono::seconds(2)); replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_END_SEND_BLOCKS; bytes = socket.send(&replyMessage, sizeof(replyMessage)); - //hasConnect = bytes > 0; + hasConnect = bytes > 0; } break; default: