From 0fe6690545dd59d6c7ee020656cbcb8d7b10b773 Mon Sep 17 00:00:00 2001 From: Sergey Yagovtsev Date: Mon, 12 Sep 2016 21:28:15 +0300 Subject: [PATCH] Add server function for EasySock --- CMakeLists.txt | 2 +- include/profiler/profiler.h | 2 ++ profiler_gui/main_window.cpp | 41 ++++++++++++++++---------------- profiler_gui/main_window.h | 2 +- src/easy_socket.cpp | 46 ++++++++++++++++++++++++++---------- src/easy_socket.h | 10 ++++++-- src/profile_manager.cpp | 29 +++++++++++++---------- 7 files changed, 82 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 598a2cd..78876ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ include_directories( if(UNIX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -Wno-reorder -pedantic -g -ggdb" ) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -Wno-reorder -pedantic -O3" ) else() add_definitions( -D_CRT_SECURE_NO_WARNINGS diff --git a/include/profiler/profiler.h b/include/profiler/profiler.h index ec4ca1f..d5ed4a2 100644 --- a/include/profiler/profiler.h +++ b/include/profiler/profiler.h @@ -233,6 +233,8 @@ class ThreadStorage; namespace profiler { + const uint32_t DEFAULT_PORT = 28077; + class Block; typedef uint64_t timestamp_t; diff --git a/profiler_gui/main_window.cpp b/profiler_gui/main_window.cpp index 1d53841..96e8cf8 100644 --- a/profiler_gui/main_window.cpp +++ b/profiler_gui/main_window.cpp @@ -95,12 +95,12 @@ EasyMainWindow::EasyMainWindow() : Parent(), m_treeWidget(nullptr), m_graphicsVi fileToolBar->addAction(newAct); connect(newAct, &QAction::triggered, this, &This::onCaptureClicked); - m_server = new QTcpServer( this ); + m_server = new QTcpSocket( this ); - //m_server->connectToHost("127.0.0.1",28077); - //connect( m_server, SIGNAL(readyRead()), SLOT(readTcpData()) ); + m_server->connectToHost("127.0.0.1",profiler::DEFAULT_PORT); + connect( m_server, SIGNAL(readyRead()), SLOT(readTcpData()) ); - if (!m_server->listen(QHostAddress(QHostAddress::Any), 28077)) { + /*if (!m_server->listen(QHostAddress(QHostAddress::Any), 28077)) { QMessageBox::critical(0, "Server Error", "Unable to start the server:" @@ -110,9 +110,9 @@ EasyMainWindow::EasyMainWindow() : Parent(), m_treeWidget(nullptr), m_graphicsVi } connect(m_server, SIGNAL(newConnection()), - this, SLOT(onNewConnection()) + this, SLOT(onNewConnection()) ); - + */ loadSettings(); @@ -547,6 +547,7 @@ void EasyMainWindow::onCaptureClicked(bool) void EasyMainWindow::readTcpData() { QTcpSocket* pClientSocket = (QTcpSocket*)sender(); + m_client = pClientSocket; static int necessarySize = 0; static int loadedSize = 0; while(pClientSocket->bytesAvailable()) @@ -563,20 +564,11 @@ void EasyMainWindow::readTcpData() loadedSize += data.size(); if (loadedSize == necessarySize) { - qInfo() << "Write FILE"; - std::string tempfilename = "test_rec.prof"; - std::ofstream of(tempfilename, std::fstream::binary); - of << m_receivedProfileData.str(); - of.close(); - m_receivedProfileData.str(std::string()); - m_receivedProfileData.clear(); - loadFile(QString(tempfilename.c_str())); - m_recFrames = false; } //qInfo() << necessarySize << " " << loadedSize; - if (m_recFrames) - continue; + //if (m_recFrames) + // continue; } @@ -597,10 +589,19 @@ void EasyMainWindow::readTcpData() case profiler::net::MESSAGE_TYPE_REPLY_END_SEND_BLOCKS: { qInfo() << "Receive MESSAGE_TYPE_REPLY_END_SEND_BLOCKS"; - //m_recFrames = false; + m_recFrames = false; - + qInfo() << "Write FILE"; + std::string tempfilename = "test_rec.prof"; + std::ofstream of(tempfilename, std::fstream::binary); + of << m_receivedProfileData.str(); + of.close(); + + m_receivedProfileData.str(std::string()); + m_receivedProfileData.clear(); + loadFile(QString(tempfilename.c_str())); + m_recFrames = false; } break; @@ -628,7 +629,7 @@ void EasyMainWindow::readTcpData() void EasyMainWindow::onNewConnection() { - m_client = m_server->nextPendingConnection(); + //m_client = m_server->nextPendingConnection(); qInfo() << "New connection!" << m_client; diff --git a/profiler_gui/main_window.h b/profiler_gui/main_window.h index 563c9a6..d7a5acb 100644 --- a/profiler_gui/main_window.h +++ b/profiler_gui/main_window.h @@ -97,7 +97,7 @@ protected: ::profiler::SerializedData m_serializedDescriptors; EasyFileReader m_reader; - QTcpServer* m_server = nullptr; + QTcpSocket* m_server = nullptr; QTcpSocket* m_client = nullptr; std::stringstream m_receivedProfileData; bool m_recFrames = false; diff --git a/src/easy_socket.cpp b/src/easy_socket.cpp index 26c745f..4b2d0af 100644 --- a/src/easy_socket.cpp +++ b/src/easy_socket.cpp @@ -22,6 +22,17 @@ along with this program.If not, see . #ifndef _WIN32 #include +int EasySocket::bind(uint16_t portno) +{ + if(m_socket < 0 ) return -1; + struct sockaddr_in serv_addr; + bzero((char *) &serv_addr, 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)); +} + EasySocket::EasySocket() { m_socket = socket(AF_INET, SOCK_STREAM, 0); @@ -30,27 +41,36 @@ EasySocket::EasySocket() tv.tv_sec = 1; tv.tv_usec = 0; - setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); + //setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); } EasySocket::~EasySocket() { } -int EasySocket::write(const void *buf, size_t nbyte) +int EasySocket::send(const void *buf, size_t nbyte) { - if(m_socket <= 0){ - return -1; - } - return ::write(m_socket,buf,nbyte); + if(m_replySocket <= 0) return -1; + return ::write(m_replySocket,buf,nbyte); } -int EasySocket::read(void *buf, size_t nbyte) +int EasySocket::receive(void *buf, size_t nbyte) { - if(m_socket <= 0){ - return -1; - } - return ::read(m_socket,buf,nbyte); + if(m_replySocket <= 0) return -1; + return ::read(m_replySocket,buf,nbyte); +} + +int EasySocket::listen(int count) +{ + if(m_socket < 0 ) return -1; + return ::listen(m_socket,count); +} + +int EasySocket::accept() +{ + if(m_socket < 0 ) return -1; + m_replySocket = ::accept(m_socket,nullptr,nullptr); + return m_replySocket; } bool EasySocket::setAddress(const char *serv, uint16_t portno) @@ -111,7 +131,7 @@ EasySocket::~EasySocket() WSACleanup(); } -int EasySocket::write(const void *buf, size_t nbyte) +int EasySocket::send(const void *buf, size_t nbyte) { if (m_socket <= 0){ return -1; @@ -121,7 +141,7 @@ int EasySocket::write(const void *buf, size_t nbyte) #include -int EasySocket::read(void *buf, size_t nbyte) +int EasySocket::receive(void *buf, size_t nbyte) { if (m_socket <= 0){ return -1; diff --git a/src/easy_socket.h b/src/easy_socket.h index 06f78b4..4073519 100644 --- a/src/easy_socket.h +++ b/src/easy_socket.h @@ -41,6 +41,7 @@ class EasySocket { #ifndef _WIN32 int m_socket = 0; + int m_replySocket = 0; uint16_t m_port = 0; struct sockaddr_in serv_addr; struct hostent *server = nullptr; @@ -50,12 +51,17 @@ class EasySocket struct addrinfo hints; #endif + + public: EasySocket(); ~EasySocket(); - int write(const void *buf, size_t nbyte); - int read(void *buf, size_t nbyte); + int send(const void *buf, size_t nbyte); + int receive(void *buf, size_t nbyte); + int listen(int count=5); + int accept(); + int bind(uint16_t portno); bool setAddress(const char* serv, uint16_t port); int connect(); diff --git a/src/profile_manager.cpp b/src/profile_manager.cpp index 26933a8..b258cec 100644 --- a/src/profile_manager.cpp +++ b/src/profile_manager.cpp @@ -453,8 +453,17 @@ void ProfileManager::startListen() EasySocket socket; - socket.setAddress("192.224.4.115",28077); - while(!m_stopListen.load() && (socket.connect() < 0 ) ) {} + socket.bind(profiler::DEFAULT_PORT); + + socket.listen(); + + socket.accept(); + + int foo = 0; + socket.send(&foo,sizeof(foo)); + + //socket.setAddress("192.224.4.115",28077); + //while(!m_stopListen.load() && (socket.connect() < 0 ) ) {} profiler::net::Message replyMessage(profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING); @@ -462,16 +471,10 @@ void ProfileManager::startListen() //bzero(buffer,256); while(!m_stopListen.load()) { - auto bytes = socket.read(buffer,255); + auto bytes = socket.receive(buffer,255); char *buf = &buffer[0]; - if (bytes == 0) - { - while (!m_stopListen.load() && (socket.connect() < 0)) {} - continue; - } - if(bytes > 0) { profiler::net::Message* message = (profiler::net::Message*)buf; @@ -486,7 +489,7 @@ void ProfileManager::startListen() profiler::setEnabled(true); replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING; - socket.write(&replyMessage,sizeof(replyMessage)); + socket.send(&replyMessage,sizeof(replyMessage)); } break; case profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE: @@ -495,7 +498,7 @@ void ProfileManager::startListen() profiler::setEnabled(false); replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_PREPARE_BLOCKS; - auto send_bytes = socket.write(&replyMessage,sizeof(replyMessage)); + auto send_bytes = socket.send(&replyMessage,sizeof(replyMessage)); profiler::net::DataMessage dm; @@ -511,7 +514,7 @@ void ProfileManager::startListen() memcpy(sendbuf,&dm,sizeof(dm)); memcpy(sendbuf + sizeof(dm),os.stream().str().c_str(),dm.size); - send_bytes = socket.write(sendbuf,packet_size); + send_bytes = socket.send(sendbuf,packet_size); /*std::string tempfilename = "test_snd.prof"; @@ -522,7 +525,7 @@ void ProfileManager::startListen() delete [] sendbuf; //std::this_thread::sleep_for(std::chrono::seconds(2)); replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_END_SEND_BLOCKS; - //send_bytes = socket.write(&replyMessage,sizeof(replyMessage)); + send_bytes = socket.send(&replyMessage,sizeof(replyMessage)); } break; default: