0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-28 01:04:41 +08:00

Add interface for EasySocket

This commit is contained in:
Sergey Yagovtsev 2016-09-08 23:34:04 +03:00
parent a422f69deb
commit f95d88bb1a
4 changed files with 146 additions and 45 deletions

View File

@ -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

78
src/easy_socket.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
**/
#include "easy_socket.h"
#include <strings.h>
#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

51
src/easy_socket.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
**/
#ifndef EASY________SOCKET_________H
#define EASY________SOCKET_________H
#include <stdint.h>
#ifndef _WIN32
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#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

View File

@ -29,6 +29,8 @@
#include "profiler/easy_net.h"
#include "easy_socket.h"
#include <thread>
#include <string.h>
@ -437,6 +439,7 @@ const char* ProfileManager::setThreadName(const char* name, const char* filename
//////////////////////////////////////////////////////////////////////////
/*
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
@ -444,59 +447,26 @@ const char* ProfileManager::setThreadName(const char* name, const char* filename
#include <unistd.h>
#include <fcntl.h>
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: