diff --git a/profiler_gui/main_window.cpp b/profiler_gui/main_window.cpp index 95758a6..a3f6384 100644 --- a/profiler_gui/main_window.cpp +++ b/profiler_gui/main_window.cpp @@ -347,10 +347,29 @@ void EasyMainWindow::onCollapseAllClicked(bool) void EasyMainWindow::onCaptureClicked(bool) { + if(m_client != nullptr) + { + profiler::net::Message requestMessage(profiler::net::MESSAGE_TYPE_REQUEST_START_CAPTURE); + m_client->write((const char*)&requestMessage, sizeof(requestMessage)); + }else + { + QMessageBox::warning(this,"Warning" ,"No connection with profiling app",QMessageBox::Close); + return; + } + + QMessageBox::information(this,"Capturing frames..." ,"Close this window to stop capturing.",QMessageBox::Close); - profiler::net::Message requestMessage(profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE); - m_client->write((const char*)&requestMessage, sizeof(requestMessage)); + if(m_client != nullptr) + { + profiler::net::Message requestMessage(profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE); + m_client->write((const char*)&requestMessage, sizeof(requestMessage)); + }else + { + QMessageBox::warning(this,"Warning" ,"Application was disconnected",QMessageBox::Close); + return; + } + /* //int sock = nn_socket (AF_SP, NN_BUS); //assert (sock >= 0); @@ -536,7 +555,7 @@ void EasyMainWindow::readTcpData() profiler::net::Message* message = (profiler::net::Message*)data.data(); - qInfo() << "rec size: " << data.size() << " " << QString(data);; + //qInfo() << "rec size: " << data.size() << " " << QString(data);; if(!m_recFrames && !message->isEasyNetMessage()){ return; }else if(m_recFrames){ @@ -569,6 +588,8 @@ void EasyMainWindow::readTcpData() of << m_receivedProfileData.str(); of.close(); + m_receivedProfileData.str(std::string()); + m_receivedProfileData.clear(); loadFile(QString(tempfilename.c_str())); } @@ -582,7 +603,7 @@ void EasyMainWindow::readTcpData() } break; default: - qInfo() << "Receive unknown " << message->type; + //qInfo() << "Receive unknown " << message->type; break; } @@ -599,11 +620,15 @@ void EasyMainWindow::onNewConnection() qInfo() << "New connection!" << m_client; - connect(m_client, &QAbstractSocket::disconnected, m_client, &QObject::deleteLater) ; + connect(m_client, SIGNAL(disconnected()), this, SLOT(onDisconnection())) ; connect(m_client, SIGNAL(readyRead()), this, SLOT(readTcpData()) ); - profiler::net::Message requestMessage(profiler::net::MESSAGE_TYPE_REQUEST_START_CAPTURE); - m_client->write((const char*)&requestMessage, sizeof(requestMessage)); + +} + +void EasyMainWindow::onDisconnection() +{ + m_client = nullptr; } ////////////////////////////////////////////////////////////////////////// diff --git a/profiler_gui/main_window.h b/profiler_gui/main_window.h index 031af36..7b966c3 100644 --- a/profiler_gui/main_window.h +++ b/profiler_gui/main_window.h @@ -96,8 +96,8 @@ protected: ::profiler::SerializedData m_serializedDescriptors; EasyFileReader m_reader; - QTcpServer* m_server; - QTcpSocket* m_client; + QTcpServer* m_server = nullptr; + QTcpSocket* m_client = nullptr; std::stringstream m_receivedProfileData; bool m_recFrames = false; public: @@ -128,6 +128,7 @@ protected slots: void readTcpData(); void onNewConnection(); + void onDisconnection(); private: // Private non-virtual methods diff --git a/sample/main.cpp b/sample/main.cpp index d9369e0..9607584 100644 --- a/sample/main.cpp +++ b/sample/main.cpp @@ -240,7 +240,7 @@ int main(int argc, char* argv[]) std::cout << "Resource loading count: " << RESOURCE_LOADING_COUNT << std::endl; auto start = std::chrono::system_clock::now(); - EASY_PROFILER_ENABLE; + //EASY_PROFILER_ENABLE; EASY_MAIN_THREAD; profiler::startListenSignalToCapture(); //one(); @@ -285,5 +285,7 @@ int main(int argc, char* argv[]) std::cout << "Blocks count: " << blocks_count << std::endl; + + profiler::stopListenSignalToCapture(); return 0; } diff --git a/src/profile_manager.cpp b/src/profile_manager.cpp index 56e15b6..61e6e1d 100644 --- a/src/profile_manager.cpp +++ b/src/profile_manager.cpp @@ -183,6 +183,8 @@ ProfileManager::ProfileManager() // #ifdef _WIN32 // PREVIOUS_FILTER = SetUnhandledExceptionFilter(easyTopLevelExceptionFilter); // #endif + + m_stopListen = ATOMIC_VAR_INIT(false); } ProfileManager::~ProfileManager() @@ -266,14 +268,17 @@ void ProfileManager::startListenSignalToCapture() { if(!m_isAlreadyListened) { + m_stopListen.store(false); m_listenThread = std::thread(&ProfileManager::startListen, this); m_isAlreadyListened = true; + } } void ProfileManager::stopListenSignalToCapture() { - + m_stopListen.store(true); + m_isAlreadyListened = false; } void ProfileManager::setEnabled(bool isEnable) @@ -436,6 +441,8 @@ const char* ProfileManager::setThreadName(const char* name, const char* filename #include #include #include +#include +#include void error(const char *msg) @@ -466,14 +473,25 @@ void ProfileManager::startListen() (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); - if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) - error("ERROR connecting"); + + 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"); + + profiler::net::Message replyMessage(profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING); char buffer[256]; bzero(buffer,256); - while(1) + while(!m_stopListen.load()) { n = read(sockfd,buffer,255); diff --git a/src/profile_manager.h b/src/profile_manager.h index 1d09ad8..b7e8f04 100644 --- a/src/profile_manager.h +++ b/src/profile_manager.h @@ -32,6 +32,7 @@ along with this program.If not, see . #include #include #include +#include ////////////////////////////////////////////////////////////////////////// @@ -220,7 +221,7 @@ class ProfileManager final int m_socket = 0;//TODO crossplatform uint32_t dumpBlocksToStream(StreamWriter& _stream); - + std::atomic_bool m_stopListen; public: static ProfileManager& instance();