From f95d88bb1a41e1095040417071e15f243460be27 Mon Sep 17 00:00:00 2001 From: Sergey Yagovtsev Date: Thu, 8 Sep 2016 23:34:04 +0300 Subject: [PATCH] Add interface for EasySocket --- src/CMakeLists.txt | 2 ++ src/easy_socket.cpp | 78 +++++++++++++++++++++++++++++++++++++++++ src/easy_socket.h | 51 +++++++++++++++++++++++++++ src/profile_manager.cpp | 60 ++++++++----------------------- 4 files changed, 146 insertions(+), 45 deletions(-) create mode 100644 src/easy_socket.cpp create mode 100644 src/easy_socket.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bf1bce0..be059c2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ set(CPP_FILES profile_manager.cpp reader.cpp event_trace_win.cpp + easy_socket.cpp ) set(H_FILES @@ -15,6 +16,7 @@ set(H_FILES profile_manager.h spin_lock.h event_trace_win.h + easy_socket.h ) set(SOURCES diff --git a/src/easy_socket.cpp b/src/easy_socket.cpp new file mode 100644 index 0000000..32e3a89 --- /dev/null +++ b/src/easy_socket.cpp @@ -0,0 +1,78 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016 Sergey Yagovtsev + +This program is free software : you can redistribute it and / or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program.If not, see . +**/ + +#include "easy_socket.h" +#include + +#ifndef _WIN32 + +EasySocket::EasySocket() +{ + m_socket = socket(AF_INET, SOCK_STREAM, 0); + struct timeval tv; + + tv.tv_sec = 1; + tv.tv_usec = 0; + + setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); +} + +size_t EasySocket::write(const void *buf, size_t nbyte) +{ + if(m_socket <= 0){ + return -1; + } + return ::write(m_socket,buf,nbyte); +} + +size_t EasySocket::read(void *buf, size_t nbyte) +{ + if(m_socket <= 0){ + return -1; + } + return ::read(m_socket,buf,nbyte); +} + +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; +} + +int EasySocket::connect() +{ + if (server == NULL || m_socket <=0 ) { + return -1; + //fprintf(stderr,"ERROR, no such host\n"); + } + + return ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr)); +} + +#endif diff --git a/src/easy_socket.h b/src/easy_socket.h new file mode 100644 index 0000000..53f91a4 --- /dev/null +++ b/src/easy_socket.h @@ -0,0 +1,51 @@ +/** +Lightweight profiler library for c++ +Copyright(C) 2016 Sergey Yagovtsev + +This program is free software : you can redistribute it and / or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program.If not, see . +**/ +#ifndef EASY________SOCKET_________H +#define EASY________SOCKET_________H + +#include + +#ifndef _WIN32 +#include +#include +#include +#include +#include +#include +#endif + +class EasySocket +{ +#ifndef _WIN32 + int m_socket = 0; + uint16_t m_port = 0; + struct sockaddr_in serv_addr; + struct hostent *server = nullptr; +#endif + +public: + EasySocket(); + + size_t write(const void *buf, size_t nbyte); + size_t read(void *buf, size_t nbyte); + + bool setAddress(const char* serv, uint16_t port); + int connect(); +}; + +#endif // EASY________SOCKET_________H diff --git a/src/profile_manager.cpp b/src/profile_manager.cpp index 61e6e1d..c0d0f37 100644 --- a/src/profile_manager.cpp +++ b/src/profile_manager.cpp @@ -29,6 +29,8 @@ #include "profiler/easy_net.h" +#include "easy_socket.h" + #include #include @@ -437,6 +439,7 @@ const char* ProfileManager::setThreadName(const char* name, const char* filename ////////////////////////////////////////////////////////////////////////// +/* #include #include #include @@ -444,59 +447,26 @@ const char* ProfileManager::setThreadName(const char* name, const char* filename #include #include - -void error(const char *msg) -{ - perror(msg); - exit(0); -} +*/ void ProfileManager::startListen() { - int sockfd, portno, n; - struct sockaddr_in serv_addr; - struct hostent *server; - - portno = 28077; - - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) - error("ERROR opening socket"); - server = gethostbyname("127.0.0.1"); - if (server == NULL) { - fprintf(stderr,"ERROR, no such host\n"); - exit(0); - } - 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); - - struct timeval tv; - - tv.tv_sec = 1; /* 30 Secs Timeout */ - tv.tv_usec = 0; // Not init'ing this can cause strange errors - - setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); - - while(!m_stopListen.load() && (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0 ) ) {} - //if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) - // error("ERROR connecting"); + EasySocket socket; + socket.setAddress("127.0.0.1",28077); + while(!m_stopListen.load() && (socket.connect() < 0 ) ) {} profiler::net::Message replyMessage(profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING); - char buffer[256]; - bzero(buffer,256); + char buffer[256] = {}; + //bzero(buffer,256); while(!m_stopListen.load()) { - n = read(sockfd,buffer,255); + int bytes = socket.read(buffer,255); char *buf = &buffer[0]; - int bytes = n; + if(bytes > 0) { profiler::net::Message* message = (profiler::net::Message*)buf; @@ -511,7 +481,7 @@ void ProfileManager::startListen() profiler::setEnabled(true); replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING; - write(sockfd,&replyMessage,sizeof(replyMessage)); + socket.write(&replyMessage,sizeof(replyMessage)); } break; case profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE: @@ -520,7 +490,7 @@ void ProfileManager::startListen() profiler::setEnabled(false); replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_PREPARE_BLOCKS; - int send_bytes = write(sockfd,&replyMessage,sizeof(replyMessage)); + int send_bytes = socket.write(&replyMessage,sizeof(replyMessage)); profiler::net::DataMessage dm; @@ -536,12 +506,12 @@ void ProfileManager::startListen() memcpy(sendbuf,&dm,sizeof(dm)); memcpy(sendbuf + sizeof(dm),os.stream().str().c_str(),dm.size); - send_bytes = write(sockfd,sendbuf,packet_size); + send_bytes = socket.write(sendbuf,packet_size); delete [] sendbuf; replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_END_SEND_BLOCKS; - send_bytes = write(sockfd,&replyMessage,sizeof(replyMessage)); + send_bytes = socket.write(&replyMessage,sizeof(replyMessage)); } break; default: