From b1e6bef05658457659569ead0ad5f636eae1f89e Mon Sep 17 00:00:00 2001 From: Sergey Yagovtsev Date: Fri, 18 Nov 2016 22:58:44 +0300 Subject: [PATCH] Check connection in GUI --- include/easy/easy_socket.h | 6 ++++ profiler_gui/main_window.cpp | 63 +++++++++++++++++++++--------------- profiler_gui/main_window.h | 2 ++ src/easy_socket.cpp | 8 +++-- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/include/easy/easy_socket.h b/include/easy/easy_socket.h index 9651128..6f166d6 100644 --- a/include/easy/easy_socket.h +++ b/include/easy/easy_socket.h @@ -107,6 +107,12 @@ public: void setState(ConnectionState state){m_state=state;} ConnectionState state() const{return m_state;} + + bool isDisconnected() const + { + return m_state == CONNECTION_STATE_UNKNOWN || + m_state == CONNECTION_STATE_DISCONNECTED; + } }; #endif // EASY________SOCKET_________H diff --git a/profiler_gui/main_window.cpp b/profiler_gui/main_window.cpp index 8d125ac..a5b190d 100644 --- a/profiler_gui/main_window.cpp +++ b/profiler_gui/main_window.cpp @@ -67,7 +67,7 @@ #include #include #include -#include +#include #include #include #include @@ -790,6 +790,20 @@ void EasyMainWindow::saveSettingsAndGeometry() settings.endGroup(); } +void EasyMainWindow::setDisconnected() +{ + QMessageBox::warning(this, "Warning", "Application was disconnected", QMessageBox::Close); + EASY_GLOBALS.connected = false; + m_captureAction->setEnabled(false); + SET_ICON(m_connectAction, ":/Connection"); + + m_eventTracingEnableAction->setEnabled(false); + m_eventTracingPriorityAction->setEnabled(false); + + emit EASY_GLOBALS.events.connectionChanged(false); + +} + ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onListenerTimerTimeout() @@ -837,18 +851,11 @@ void EasyMainWindow::onListenerDialogClose(int) if (!m_listener.connected()) { - QMessageBox::warning(this, "Warning", "Application was disconnected", QMessageBox::Close); - EASY_GLOBALS.connected = false; - m_captureAction->setEnabled(false); - SET_ICON(m_connectAction, ":/Connection"); - - m_eventTracingEnableAction->setEnabled(false); - m_eventTracingPriorityAction->setEnabled(false); - - emit EASY_GLOBALS.events.connectionChanged(false); + setDisconnected(); } } + ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onFileReaderTimeout() @@ -860,7 +867,7 @@ void EasyMainWindow::onFileReaderTimeout() { static_cast(m_treeWidget->widget())->clearSilent(true); - ::profiler::SerializedData serialized_blocks; + ::profiler::SerializedData serialized_blocks; ::profiler::SerializedData serialized_descriptors; ::profiler::descriptors_list_t descriptors; ::profiler::blocks_t blocks; @@ -1311,15 +1318,7 @@ void EasyMainWindow::onGetBlockDescriptionsClicked(bool) if (!m_listener.connected()) { - QMessageBox::warning(this, "Warning", "Application was disconnected", QMessageBox::Close); - EASY_GLOBALS.connected = false; - m_captureAction->setEnabled(false); - SET_ICON(m_connectAction, ":/Connection"); - - m_eventTracingEnableAction->setEnabled(false); - m_eventTracingPriorityAction->setEnabled(false); - - emit EASY_GLOBALS.events.connectionChanged(false); + setDisconnected(); } } @@ -1395,7 +1394,7 @@ bool EasySocketListener::connect(const char* _ipaddress, uint16_t _port, profile if (bytes == -1) { - if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) + if (m_easySocket.isDisconnected()) return false; bytes = 0; continue; @@ -1417,7 +1416,7 @@ bool EasySocketListener::connect(const char* _ipaddress, uint16_t _port, profile if (bytes == -1) { - if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) + if (m_easySocket.isDisconnected()) return false; break; } @@ -1441,6 +1440,10 @@ void EasySocketListener::startCapture() profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_START_CAPTURE); m_easySocket.send(&request, sizeof(request)); + if(m_easySocket.isDisconnected() ){ + m_bConnected.store(false, ::std::memory_order_release); + } + m_regime = LISTENER_CAPTURE; m_thread = ::std::move(::std::thread(&EasySocketListener::listenCapture, this)); } @@ -1453,6 +1456,10 @@ void EasySocketListener::stopCapture() profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE); m_easySocket.send(&request, sizeof(request)); + if(m_easySocket.isDisconnected() ){ + m_bConnected.store(false, ::std::memory_order_release); + } + m_thread.join(); m_regime = LISTENER_IDLE; @@ -1465,6 +1472,10 @@ void EasySocketListener::requestBlocksDescription() profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_BLOCKS_DESCRIPTION); m_easySocket.send(&request, sizeof(request)); + if(m_easySocket.isDisconnected() ){ + m_bConnected.store(false, ::std::memory_order_release); + } + m_regime = LISTENER_DESCRIBE; listenDescription(); m_regime = LISTENER_IDLE; @@ -1490,7 +1501,7 @@ void EasySocketListener::listenCapture() if (bytes == -1) { - if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) + if (m_easySocket.isDisconnected()) { m_bConnected.store(false, ::std::memory_order_release); isListen = false; @@ -1587,7 +1598,7 @@ void EasySocketListener::listenCapture() if (bytes == -1) { - if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) + if (m_easySocket.isDisconnected()) { m_bConnected.store(false, ::std::memory_order_release); isListen = false; @@ -1640,7 +1651,7 @@ void EasySocketListener::listenDescription() if (bytes == -1) { - if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) + if (m_easySocket.isDisconnected()) { m_bConnected.store(false, ::std::memory_order_release); isListen = false; @@ -1720,7 +1731,7 @@ void EasySocketListener::listenDescription() if (bytes == -1) { - if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) + if (m_easySocket.isDisconnected()) { m_bConnected.store(false, ::std::memory_order_release); isListen = false; diff --git a/profiler_gui/main_window.h b/profiler_gui/main_window.h index fb9d84a..f722fc8 100644 --- a/profiler_gui/main_window.h +++ b/profiler_gui/main_window.h @@ -263,6 +263,8 @@ private: void loadGeometry(); void saveSettingsAndGeometry(); + void setDisconnected(); + }; // END of class EasyMainWindow. ////////////////////////////////////////////////////////////////////////// diff --git a/src/easy_socket.cpp b/src/easy_socket.cpp index a4e0f4d..647e51c 100644 --- a/src/easy_socket.cpp +++ b/src/easy_socket.cpp @@ -103,7 +103,7 @@ void EasySocket::flush() void EasySocket::checkResult(int result) { -// printf("Errno: %s\n", strerror(errno)); + //printf("Errno: %s\n", strerror(errno)); if(result >= 0){ m_state = CONNECTION_STATE_SUCCESS; return; @@ -117,23 +117,25 @@ void EasySocket::checkResult(int result) const int CONNECTION_ABORTED = WSAECONNABORTED; const int CONNECTION_RESET = WSAECONNRESET; const int CONNECTION_IN_PROGRESS = WSAEINPROGRESS; + const int CONNECTION_BROKEN_PIPE = WSAECONNABORTED; #else error_code = errno; const int CONNECTION_ABORTED = ECONNABORTED; const int CONNECTION_RESET = ECONNRESET; const int CONNECTION_IN_PROGRESS = EINPROGRESS; + const int CONNECTION_BROKEN_PIPE = EPIPE; #endif switch(error_code) { case CONNECTION_ABORTED: case CONNECTION_RESET: + case CONNECTION_BROKEN_PIPE: m_state = CONNECTION_STATE_DISCONNECTED; break; case CONNECTION_IN_PROGRESS: m_state = CONNECTION_STATE_IN_PROGRESS; break; - default: break; } @@ -193,7 +195,7 @@ int EasySocket::send(const void *buf, size_t nbyte) #ifdef _WIN32 res = ::send(m_replySocket, (const char*)buf, (int)nbyte, 0); #else - res = ::write(m_replySocket,buf,nbyte); + res = ::send(m_replySocket,buf,nbyte,MSG_NOSIGNAL); #endif checkResult(res); return res;