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

Merge branch 'develop' of https://github.com/yse/easy_profiler into develop

This commit is contained in:
Victor Zarubkin 2016-09-20 22:57:52 +03:00
commit d6269f17fe
5 changed files with 176 additions and 268 deletions

View File

@ -40,6 +40,13 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
class PROFILER_API EasySocket
{
public:
#ifdef _WIN32
typedef SOCKET socket_t;
#else
typedef int socket_t;
#endif
enum ConnectionState
{
CONNECTION_STATE_UNKNOWN,
@ -47,15 +54,13 @@ public:
CONNECTION_STATE_DISCONNECTED
};
private:
void checkResult(int result);
#ifdef _WIN32
typedef SOCKET socket_t;
#else
typedef int socket_t;
#endif
bool checkSocket(socket_t s) const;
static int _close(socket_t s);
void setBlocking(socket_t s, bool blocking);
socket_t m_socket = 0;
socket_t m_replySocket = 0;
@ -63,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();

View File

@ -121,11 +121,11 @@ EasyMainWindow::EasyMainWindow() : Parent()
auto toolbar = addToolBar("MainToolBar");
toolbar->setObjectName("ProfilerGUI_MainToolBar");
m_captureAction = toolbar->addAction(QIcon(":/Start"), "Capture", this, &This::onCaptureClicked);
m_captureAction = toolbar->addAction(QIcon(":/Start"), tr("Capture"), this, SLOT(onCaptureClicked(bool)));
m_captureAction->setEnabled(false);
toolbar->addSeparator();
m_connectAction = toolbar->addAction(QIcon(":/Connection"), "Connect", this, &This::onConnectClicked);
m_connectAction = toolbar->addAction(QIcon(":/Connection"), tr("Connect"), this, SLOT(onConnectClicked(bool)));
toolbar->addWidget(new QLabel(" IP:"));
m_ipEdit = new QLineEdit();
@ -191,17 +191,17 @@ EasyMainWindow::EasyMainWindow() : Parent()
auto menu = menuBar()->addMenu("&File");
menu->addAction(QIcon(":/Open"), "&Open", this, &This::onOpenFileClicked);
menu->addAction(QIcon(":/Reload"), "&Reload", this, &This::onReloadFileClicked);
menu->addAction(QIcon(":/Open"), "&Open", this, SLOT(onOpenFileClicked(bool)));
menu->addAction(QIcon(":/Reload"), "&Reload", this, SLOT(onReloadFileClicked(bool)));
menu->addSeparator();
menu->addAction(QIcon(":/Exit"), "&Exit", this, &This::onExitClicked);
menu->addAction(QIcon(":/Exit"), "&Exit", this, SLOT(onExitClicked(bool)));
menu = menuBar()->addMenu("&View");
menu->addAction(QIcon(":/Expand"), "Expand all", this, &This::onExpandAllClicked);
menu->addAction(QIcon(":/Collapse"), "Collapse all", this, &This::onCollapseAllClicked);
menu->addAction(QIcon(":/Expand"), "Expand all", this, SLOT(onExpandAllClicked(bool)));
menu->addAction(QIcon(":/Collapse"), "Collapse all", this,SLOT(onCollapseAllClicked(bool)));
menu->addSeparator();
@ -257,7 +257,7 @@ EasyMainWindow::EasyMainWindow() : Parent()
menu = menuBar()->addMenu("&Edit");
m_editBlocksAction = menu->addAction("Edit blocks", this, &This::onEditBlocksClicked);
m_editBlocksAction = menu->addAction(tr("Edit blocks"), this, SLOT(onEditBlocksClicked(bool)));
m_editBlocksAction->setEnabled(false);
@ -337,7 +337,7 @@ void EasyMainWindow::listen()
//std::this_thread::sleep_for(std::chrono::seconds(2));
static const int buffer_size = 8 * 1024 * 1024;
char buffer[buffer_size] = {};
char*buffer = new char[buffer_size];
int seek = 0;
int bytes = 0;
@ -481,6 +481,8 @@ void EasyMainWindow::listen()
}
}
}
delete [] buffer;
}
EasyMainWindow::~EasyMainWindow()
@ -1135,4 +1137,4 @@ void EasyMainWindow::onNewConnection()
void EasyMainWindow::onDisconnection()
{
//m_client = nullptr;
}
}

View File

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

View File

@ -18,60 +18,171 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
#include "profiler/easy_socket.h"
#include <string.h>
#ifndef _WIN32
#include <strings.h>
#ifdef _WIN32
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#else
#include <errno.h>
#include <sys/ioctl.h>
#endif
bool EasySocket::checkSocket(socket_t s) const
{
return s > 0;
}
int EasySocket::_close(EasySocket::socket_t s)
{
#ifdef _WIN32
return ::closesocket(s);
#else
//TODO
//return close(s);
#endif
}
void EasySocket::setBlocking(EasySocket::socket_t s, bool blocking)
{
#ifdef _WIN32
u_long iMode = blocking ? 0 : 1;//0 - blocking, 1 - non blocking
ioctlsocket(s, FIONBIO, &iMode);
#else
const int iMode = blocking ? 0 : 1;//0 - blocking, 1 - non blocking
ioctl(s, FIONBIO, (char *)&iMode);
#endif
}
int EasySocket::bind(uint16_t portno)
{
if(m_socket < 0 ) return -1;
if (!checkSocket(m_socket)) return -1;
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
return ::bind(m_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
auto res = ::bind(m_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
return res;
}
void EasySocket::flush()
{
if (m_socket){
close(m_socket);
_close(m_socket);
}
if (m_replySocket != m_socket){
close(m_replySocket);
_close(m_replySocket);
}
#ifdef _WIN32
m_socket = 0;
m_replySocket = 0;
#endif
}
void EasySocket::checkResult(int result)
{
// printf("Errno: %s\n", strerror(errno));
if(result >= 0){
m_state = CONNECTION_STATE_SUCCESS;
return;
}else if(result == -1){
int error_code = 0;
#ifdef _WIN32
error_code = WSAGetLastError();
const int CONNECTION_ABORTED = WSAECONNABORTED;
const int CONNECTION_RESET = WSAECONNRESET;
#else
error_code = errno;
const int CONNECTION_ABORTED = ECONNABORTED;
const int CONNECTION_RESET = ECONNRESET;
#endif
switch(error_code)
{
case CONNECTION_ABORTED:
case CONNECTION_RESET:
m_state = CONNECTION_STATE_DISCONNECTED;
break;
default:
break;
}
}
}
void EasySocket::init()
{
m_socket = socket(AF_INET, SOCK_STREAM, 0);
if (wsaret == 0)
{
int protocol = 0;
#ifdef _WIN32
protocol = IPPROTO_TCP;
#endif
m_socket = socket(AF_INET, SOCK_STREAM, protocol);
if (!checkSocket(m_socket)) {
return;
}
}else
return;
setBlocking(m_socket,true);
int opt = 1;
setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt));
}
EasySocket::EasySocket()
{
#ifdef _WIN32
WSADATA wsaData;
wsaret = WSAStartup(0x101, &wsaData);
#else
wsaret = 0;
#endif
init();
#ifndef _WIN32
wsaret = 1;
#endif
}
EasySocket::~EasySocket()
{
flush();
#ifdef _WIN32
if (wsaret == 0)
WSACleanup();
#endif
}
int EasySocket::send(const void *buf, size_t nbyte)
{
if(m_replySocket <= 0) return -1;
int res = ::write(m_replySocket,buf,nbyte);
if(!checkSocket(m_replySocket)) return -1;
int res = 0;
#ifdef _WIN32
res = ::send(m_replySocket, (const char*)buf, nbyte, 0);
#else
res = ::write(m_replySocket,buf,nbyte);
#endif
checkResult(res);
return res;
}
int EasySocket::receive(void *buf, size_t nbyte)
{
if(m_replySocket <= 0) return -1;
int res = ::read(m_replySocket,buf,nbyte);
if(!checkSocket(m_replySocket)) return -1;
int res = 0;
#ifdef _WIN32
res = ::recv(m_replySocket, (char*)buf, nbyte, 0);
#else
res = ::read(m_replySocket,buf,nbyte);
#endif
checkResult(res);
if (res == 0){
m_state = CONNECTION_STATE_DISCONNECTED;
@ -81,7 +192,7 @@ int EasySocket::receive(void *buf, size_t nbyte)
int EasySocket::listen(int count)
{
if(m_socket < 0 ) return -1;
if(!checkSocket(m_socket)) return -1;
int res = ::listen(m_socket,count);
checkResult(res);
return res;
@ -89,11 +200,21 @@ int EasySocket::listen(int count)
int EasySocket::accept()
{
if(m_socket < 0 ) return -1;
if(!checkSocket(m_socket)) return -1;
m_replySocket = ::accept(m_socket,nullptr,nullptr);
checkResult(m_replySocket);
if(checkSocket(m_replySocket))
{
int send_buffer = 64*1024*1024;
int send_buffer_sizeof = sizeof(int);
setsockopt(m_replySocket, SOL_SOCKET, SO_SNDBUF, (char*)&send_buffer, send_buffer_sizeof);
//int flag = 1;
//int result = setsockopt(m_replySocket,IPPROTO_TCP,TCP_NODELAY,(char *)&flag,sizeof(int));
//setBlocking(m_replySocket,true);
}
return m_replySocket;
}
@ -104,11 +225,10 @@ bool EasySocket::setAddress(const char *serv, uint16_t portno)
return false;
//fprintf(stderr,"ERROR, no such host\n");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length);
serv_addr.sin_port = htons(portno);
return true;
@ -121,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;
@ -134,222 +255,3 @@ int EasySocket::connect()
}
return res;
}
#include <string.h>
void EasySocket::checkResult(int result)
{
if(result >= 0){
m_state = CONNECTION_STATE_SUCCESS;
return;
}else if(result == -1){
//printf("Errno: %s\n", strerror(errno));
switch(errno){
case ECONNABORTED:
case ECONNRESET:
m_state = CONNECTION_STATE_DISCONNECTED;
break;
default:
break;
}
}
}
#else
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
void EasySocket::checkResult(int result)
{
if (result >= 0){
m_state = CONNECTION_STATE_SUCCESS;
return;
}
else if (result == -1){
int error_code = WSAGetLastError();
//printf("Errno: %s\n", strerror(errno));
switch (error_code){
case WSAECONNABORTED:
case WSAECONNRESET:
m_state = CONNECTION_STATE_DISCONNECTED;
break;
default:
break;
}
}
}
void EasySocket::flush()
{
if (m_socket){
closesocket(m_socket);
}
if (m_replySocket != m_socket){
closesocket(m_replySocket);
}
m_socket = INVALID_SOCKET;
m_replySocket = INVALID_SOCKET;
}
void EasySocket::init()
{
if (wsaret == 0)
{
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);;
if (m_socket == INVALID_SOCKET) {
return;
}
}
u_long iMode = 0;//0 - blocking, 1 - non blocking
ioctlsocket(m_socket, FIONBIO, &iMode);
int opt = 1;
setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt));
}
EasySocket::EasySocket()
{
// socket
WSADATA wsaData;
wsaret = WSAStartup(0x101, &wsaData);
init();
}
EasySocket::~EasySocket()
{
flush();
if (wsaret == 0)
WSACleanup();
}
int EasySocket::send(const void *buf, size_t nbyte)
{
if (m_replySocket <= 0){
return -1;
}
int res = ::send(m_replySocket, (const char*)buf, nbyte, 0);
checkResult(res);
return res;
}
#include <stdio.h>
int EasySocket::receive(void *buf, size_t nbyte)
{
if (m_replySocket <= 0){
return -1;
}
int res = ::recv(m_replySocket, (char*)buf, nbyte, 0);
checkResult(res);
if (res == 0){
m_state = CONNECTION_STATE_DISCONNECTED;
}
/**
if (res == SOCKET_ERROR)
{
LPWSTR *s = NULL;
int err = WSAGetLastError();
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&s, 0, NULL);
printf("%S\n", s);
LocalFree(s);
}
/**/
return res;
}
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;
}
int EasySocket::listen(int count)
{
if (m_socket < 0) return -1;
int res = ::listen(m_socket, count);
checkResult(res);
return res;
}
int EasySocket::accept()
{
if (m_socket < 0) return -1;
m_replySocket = ::accept(m_socket, nullptr, nullptr);
int send_buffer = 64 * 1024*1024; // 64 MB
int send_buffer_sizeof = sizeof(int);
setsockopt(m_replySocket, SOL_SOCKET, SO_SNDBUF, (char*)&send_buffer, send_buffer_sizeof);
//int flag = 1;
//int result = setsockopt(m_replySocket,IPPROTO_TCP,TCP_NODELAY,(char *)&flag,sizeof(int));
u_long iMode = 0;//0 - blocking, 1 - non blocking
ioctlsocket(m_replySocket, FIONBIO, &iMode);
return (int)m_replySocket;
}
int EasySocket::bind(uint16_t portno)
{
if (m_socket < 0) return -1;
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
auto res = ::bind(m_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (res == SOCKET_ERROR)
{
printf("bind failed with error %u\n", WSAGetLastError());
return -1;
}
return res;
}
#endif

View File

@ -709,9 +709,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];
@ -723,16 +720,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: