/************************************************************************ * file name : main_window.cpp * ----------------- : * creation time : 2016/06/26 * author : Victor Zarubkin * email : v.s.zarubkin@gmail.com * ----------------- : * description : The file contains implementation of MainWindow for easy_profiler GUI. * ----------------- : * change log : * 2016/06/26 Victor Zarubkin: Initial commit. * : * : * 2016/06/27 Victor Zarubkin: Passing blocks number to EasyTreeWidget::setTree(). * : * : * 2016/06/29 Victor Zarubkin: Added menu with tests. * : * : * 2016/06/30 Sergey Yagovtsev: Open file by command line argument * : * : * * ----------------- : * license : Lightweight profiler library for c++ * : Copyright(C) 2016 Sergey Yagovtsev, Victor Zarubkin * : * : 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 . ************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "main_window.h" #include "blocks_tree_widget.h" #include "blocks_graphics_view.h" #include "descriptors_tree_widget.h" #include "globals.h" #include "easy/easy_net.h" #ifdef max #undef max #endif #ifdef min #undef min #endif ////////////////////////////////////////////////////////////////////////// const int LOADER_TIMER_INTERVAL = 40; const auto NETWORK_CACHE_FILE = "easy_profiler_stream.cache"; ////////////////////////////////////////////////////////////////////////// EasyMainWindow::EasyMainWindow() : Parent(), m_lastAddress("127.0.0.1"), m_lastPort(::profiler::DEFAULT_PORT) { { QIcon icon(":/logo"); if (!icon.isNull()) QApplication::setWindowIcon(icon); } setObjectName("ProfilerGUI_MainWindow"); setWindowTitle("EasyProfiler Reader beta"); setDockNestingEnabled(true); resize(800, 600); setStatusBar(new QStatusBar()); auto graphicsView = new EasyGraphicsViewWidget(); m_graphicsView = new QDockWidget("Diagram"); m_graphicsView->setObjectName("ProfilerGUI_Diagram"); m_graphicsView->setMinimumHeight(50); m_graphicsView->setAllowedAreas(Qt::AllDockWidgetAreas); m_graphicsView->setWidget(graphicsView); auto treeWidget = new EasyTreeWidget(); m_treeWidget = new QDockWidget("Hierarchy"); m_treeWidget->setObjectName("ProfilerGUI_Hierarchy"); m_treeWidget->setMinimumHeight(50); m_treeWidget->setAllowedAreas(Qt::AllDockWidgetAreas); m_treeWidget->setWidget(treeWidget); addDockWidget(Qt::TopDockWidgetArea, m_graphicsView); addDockWidget(Qt::BottomDockWidgetArea, m_treeWidget); #if EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW != 0 auto descTree = new EasyDescWidget(); m_descTreeWidget = new QDockWidget("Blocks"); m_descTreeWidget->setObjectName("ProfilerGUI_Blocks"); m_descTreeWidget->setMinimumHeight(50); m_descTreeWidget->setAllowedAreas(Qt::AllDockWidgetAreas); m_descTreeWidget->setWidget(descTree); addDockWidget(Qt::BottomDockWidgetArea, m_descTreeWidget); #endif loadSettings(); auto toolbar = addToolBar("FileToolbar"); toolbar->setObjectName("ProfilerGUI_FileToolbar"); toolbar->setContentsMargins(1, 0, 1, 0); toolbar->addAction(QIcon(":/Open"), tr("Open"), this, SLOT(onOpenFileClicked(bool))); toolbar->addAction(QIcon(":/Reopen"), tr("Reload last file"), this, SLOT(onReloadFileClicked(bool))); m_saveAction = toolbar->addAction(QIcon(":/Save"), tr("Save"), this, SLOT(onSaveFileClicked(bool))); m_deleteAction = toolbar->addAction(QIcon(":/Delete"), tr("Clear all"), this, SLOT(onDeleteClicked(bool))); m_saveAction->setEnabled(false); m_deleteAction->setEnabled(false); toolbar = addToolBar("ProfileToolbar"); toolbar->setObjectName("ProfilerGUI_ProfileToolbar"); toolbar->setContentsMargins(1, 0, 1, 0); toolbar->addAction(QIcon(":/List"), tr("Blocks"), this, SLOT(onEditBlocksClicked(bool))); m_captureAction = toolbar->addAction(QIcon(":/Start"), tr("Capture"), this, SLOT(onCaptureClicked(bool))); m_captureAction->setEnabled(false); toolbar->addSeparator(); m_connectAction = toolbar->addAction(QIcon(":/Connection"), tr("Connect"), this, SLOT(onConnectClicked(bool))); auto lbl = new QLabel("IP:", toolbar); lbl->setContentsMargins(5, 0, 1, 0); toolbar->addWidget(lbl); m_ipEdit = new QLineEdit(); QRegExp rx("^0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})(\\.0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})){3}$"); m_ipEdit->setValidator(new QRegExpValidator(rx, m_ipEdit)); m_ipEdit->setText(m_lastAddress); m_ipEdit->setFixedWidth(m_ipEdit->fontMetrics().width(QString("255.255.255.255")) + 20); toolbar->addWidget(m_ipEdit); lbl = new QLabel("Port:", toolbar); lbl->setContentsMargins(5, 0, 1, 0); toolbar->addWidget(lbl); m_portEdit = new QLineEdit(); m_portEdit->setValidator(new QIntValidator(1, 65535, m_portEdit)); m_portEdit->setText(QString::number(m_lastPort)); m_portEdit->setFixedWidth(m_portEdit->fontMetrics().width(QString("000000")) + 10); toolbar->addWidget(m_portEdit); connect(m_ipEdit, &QLineEdit::returnPressed, [this](){ onConnectClicked(true); }); connect(m_portEdit, &QLineEdit::returnPressed, [this](){ onConnectClicked(true); }); toolbar = addToolBar("SetupToolbar"); toolbar->setObjectName("ProfilerGUI_SetupToolbar"); toolbar->setContentsMargins(1, 0, 1, 0); toolbar->addAction(QIcon(":/Expand"), "Expand all", this, SLOT(onExpandAllClicked(bool))); toolbar->addAction(QIcon(":/Collapse"), "Collapse all", this, SLOT(onCollapseAllClicked(bool))); toolbar->addSeparator(); auto menu = new QMenu("Settings", this); QToolButton* toolButton = new QToolButton(toolbar); toolButton->setIcon(QIcon(":/Settings")); toolButton->setMenu(menu); toolButton->setPopupMode(QToolButton::InstantPopup); toolbar->addWidget(toolButton); auto action = menu->addAction("Statistics enabled"); action->setCheckable(true); action->setChecked(EASY_GLOBALS.enable_statistics); connect(action, &QAction::triggered, this, &This::onEnableDisableStatistics); if (EASY_GLOBALS.enable_statistics) { auto f = action->font(); f.setBold(true); action->setFont(f); SET_ICON(action, ":/Stats"); } else { action->setText("Statistics disabled"); SET_ICON(action, ":/Stats-off"); } menu->addSeparator(); auto submenu = menu->addMenu("View"); action = submenu->addAction("Draw items' borders"); action->setCheckable(true); action->setChecked(EASY_GLOBALS.draw_graphics_items_borders); connect(action, &QAction::triggered, this, &This::onDrawBordersChanged); action = submenu->addAction("Hide narrow children"); action->setCheckable(true); action->setChecked(EASY_GLOBALS.hide_narrow_children); connect(action, &QAction::triggered, this, &This::onHideNarrowChildrenChanged); action = submenu->addAction("Collapse items on tree reset"); action->setCheckable(true); action->setChecked(EASY_GLOBALS.collapse_items_on_tree_close); connect(action, &QAction::triggered, this, &This::onCollapseItemsAfterCloseChanged); action = submenu->addAction("Expand all on file open"); action->setCheckable(true); action->setChecked(EASY_GLOBALS.all_items_expanded_by_default); connect(action, &QAction::triggered, this, &This::onAllItemsExpandedByDefaultChange); action = submenu->addAction("Bind scene and tree expand"); action->setCheckable(true); action->setChecked(EASY_GLOBALS.bind_scene_and_tree_expand_status); connect(action, &QAction::triggered, this, &This::onBindExpandStatusChange); action = submenu->addAction("Draw event indicators"); action->setCheckable(true); action->setChecked(EASY_GLOBALS.enable_event_indicators); connect(action, &QAction::triggered, this, &This::onEventIndicatorsChange); submenu->addSeparator(); auto actionGroup = new QActionGroup(this); actionGroup->setExclusive(true); action = new QAction("Chrono text at top", actionGroup); action->setCheckable(true); action->setData(static_cast(::profiler_gui::ChronoTextPosition_Top)); if (EASY_GLOBALS.chrono_text_position == ::profiler_gui::ChronoTextPosition_Top) action->setChecked(true); submenu->addAction(action); connect(action, &QAction::triggered, this, &This::onChronoTextPosChanged); action = new QAction("Chrono text at center", actionGroup); action->setCheckable(true); action->setData(static_cast(::profiler_gui::ChronoTextPosition_Center)); if (EASY_GLOBALS.chrono_text_position == ::profiler_gui::ChronoTextPosition_Center) action->setChecked(true); submenu->addAction(action); connect(action, &QAction::triggered, this, &This::onChronoTextPosChanged); action = new QAction("Chrono text at bottom", actionGroup); action->setCheckable(true); action->setData(static_cast(::profiler_gui::ChronoTextPosition_Bottom)); if (EASY_GLOBALS.chrono_text_position == ::profiler_gui::ChronoTextPosition_Bottom) action->setChecked(true); submenu->addAction(action); connect(action, &QAction::triggered, this, &This::onChronoTextPosChanged); submenu = menu->addMenu("Remote"); m_eventTracingEnableAction = submenu->addAction("Event tracing enabled"); m_eventTracingEnableAction->setCheckable(true); m_eventTracingEnableAction->setEnabled(false); connect(m_eventTracingEnableAction, &QAction::triggered, this, &This::onEventTracingEnableChange); m_eventTracingPriorityAction = submenu->addAction("Low priority event tracing"); m_eventTracingPriorityAction->setCheckable(true); m_eventTracingPriorityAction->setChecked(EASY_LOW_PRIORITY_EVENT_TRACING); m_eventTracingPriorityAction->setEnabled(false); connect(m_eventTracingPriorityAction, &QAction::triggered, this, &This::onEventTracingPriorityChange); submenu = menu->addMenu("Encoding"); actionGroup = new QActionGroup(this); actionGroup->setExclusive(true); auto default_codec_mib = QTextCodec::codecForLocale()->mibEnum(); foreach(int mib, QTextCodec::availableMibs()) { auto codec = QTextCodec::codecForMib(mib)->name(); action = new QAction(codec, actionGroup); action->setCheckable(true); if (mib == default_codec_mib) action->setChecked(true); submenu->addAction(action); connect(action, &QAction::triggered, this, &This::onEncodingChanged); } connect(graphicsView->view(), &EasyGraphicsView::intervalChanged, treeWidget, &EasyTreeWidget::setTreeBlocks); connect(&m_readerTimer, &QTimer::timeout, this, &This::onFileReaderTimeout); connect(&m_listenerTimer, &QTimer::timeout, this, &This::onListenerTimerTimeout); m_progress = new QProgressDialog("Loading file...", "Cancel", 0, 100, this); m_progress->setFixedWidth(300); m_progress->setWindowTitle("EasyProfiler"); m_progress->setModal(true); m_progress->setValue(100); //m_progress->hide(); connect(m_progress, &QProgressDialog::canceled, this, &This::onFileReaderCancel); loadGeometry(); if(QCoreApplication::arguments().size() > 1) { auto opened_filename = QCoreApplication::arguments().at(1); loadFile(opened_filename); } connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::blockStatusChanged, this, &This::onBlockStatusChange); connect(&EASY_GLOBALS.events, &::profiler_gui::EasyGlobalSignals::blocksRefreshRequired, this, &This::onGetBlockDescriptionsClicked); } EasyMainWindow::~EasyMainWindow() { delete m_progress; } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onOpenFileClicked(bool) { auto filename = QFileDialog::getOpenFileName(this, "Open profiler log", m_lastFile, "Profiler Log File (*.prof);;All Files (*.*)"); if (!filename.isEmpty()) loadFile(filename); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::loadFile(const QString& filename) { const auto i = filename.lastIndexOf(QChar('/')); const auto j = filename.lastIndexOf(QChar('\\')); m_progress->setLabelText(QString("Loading %1...").arg(filename.mid(::std::max(i, j) + 1))); m_progress->setValue(0); m_progress->show(); m_readerTimer.start(LOADER_TIMER_INTERVAL); m_reader.load(filename); } void EasyMainWindow::readStream(::std::stringstream& data) { m_progress->setLabelText(tr("Reading from stream...")); m_progress->setValue(0); m_progress->show(); m_readerTimer.start(LOADER_TIMER_INTERVAL); m_reader.load(data); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onReloadFileClicked(bool) { if (m_lastFile.isEmpty()) return; loadFile(m_lastFile); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onSaveFileClicked(bool) { if (m_serializedBlocks.empty()) return; const auto i = m_lastFile.lastIndexOf(QChar('/')); const auto j = m_lastFile.lastIndexOf(QChar('\\')); auto k = ::std::max(i, j); QString dir; if (k > 0) dir = m_lastFile.mid(0, ++k); auto filename = QFileDialog::getSaveFileName(this, "Save profiler log", dir, "Profiler Log File (*.prof);;All Files (*.*)"); if (!filename.isEmpty()) { bool inOk = false, outOk = false; int8_t retry1 = -1; while (++retry1 < 4) { ::std::ifstream inFile(m_bNetworkFileRegime ? NETWORK_CACHE_FILE : m_lastFile.toStdString().c_str(), ::std::fstream::binary); if (!inFile.is_open()) { ::std::this_thread::sleep_for(::std::chrono::milliseconds(500)); continue; } inOk = true; int8_t retry2 = -1; while (++retry2 < 4) { ::std::ofstream outFile(filename.toStdString(), ::std::fstream::binary); if (!outFile.is_open()) { ::std::this_thread::sleep_for(::std::chrono::milliseconds(500)); continue; } outFile << inFile.rdbuf(); outOk = true; break; } break; } if (outOk) { if (m_bNetworkFileRegime) QFile::remove(QString(NETWORK_CACHE_FILE)); m_lastFile = filename; m_bNetworkFileRegime = false; } else if (inOk) { QMessageBox::warning(this, "Warning", "Can not open destination file.\nSaving incomplete.", QMessageBox::Close); } else { if (m_bNetworkFileRegime) QMessageBox::warning(this, "Warning", "Can not open network cache file.\nSaving incomplete.", QMessageBox::Close); else QMessageBox::warning(this, "Warning", "Can not open source file.\nSaving incomplete.", QMessageBox::Close); } } } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::clear() { static_cast(m_treeWidget->widget())->clearSilent(true); static_cast(m_graphicsView->widget())->clear(); #if EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW != 0 static_cast(m_descTreeWidget->widget())->clear(); #endif if (m_dialogDescTree != nullptr) m_dialogDescTree->clear(); EASY_GLOBALS.selected_thread = 0; ::profiler_gui::set_max(EASY_GLOBALS.selected_block); EASY_GLOBALS.profiler_blocks.clear(); EASY_GLOBALS.descriptors.clear(); EASY_GLOBALS.gui_blocks.clear(); m_serializedBlocks.clear(); m_serializedDescriptors.clear(); m_saveAction->setEnabled(false); m_deleteAction->setEnabled(false); m_bNetworkFileRegime = false; } void EasyMainWindow::onDeleteClicked(bool) { auto button = QMessageBox::question(this, "Clear all profiled data", "All profiled data is going to be deleted!\nContinue?", QMessageBox::Yes, QMessageBox::No); if (button == QMessageBox::Yes) clear(); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onExitClicked(bool) { close(); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onEncodingChanged(bool) { auto _sender = qobject_cast(sender()); auto name = _sender->text(); QTextCodec *codec = QTextCodec::codecForName(name.toStdString().c_str()); QTextCodec::setCodecForLocale(codec); } void EasyMainWindow::onChronoTextPosChanged(bool) { auto _sender = qobject_cast(sender()); EASY_GLOBALS.chrono_text_position = static_cast<::profiler_gui::ChronometerTextPosition>(_sender->data().toInt()); static_cast(m_graphicsView->widget())->view()->scene()->update(); } void EasyMainWindow::onEventIndicatorsChange(bool _checked) { EASY_GLOBALS.enable_event_indicators = _checked; static_cast(m_graphicsView->widget())->view()->scene()->update(); } void EasyMainWindow::onEnableDisableStatistics(bool _checked) { EASY_GLOBALS.enable_statistics = _checked; auto action = qobject_cast(sender()); if (action != nullptr) { auto f = action->font(); f.setBold(_checked); action->setFont(f); if (_checked) { action->setText("Statistics enabled"); SET_ICON(action, ":/Stats"); } else { action->setText("Statistics disabled"); SET_ICON(action, ":/Stats-off"); } } } void EasyMainWindow::onDrawBordersChanged(bool _checked) { EASY_GLOBALS.draw_graphics_items_borders = _checked; static_cast(m_graphicsView->widget())->view()->scene()->update(); } void EasyMainWindow::onHideNarrowChildrenChanged(bool _checked) { EASY_GLOBALS.hide_narrow_children = _checked; static_cast(m_graphicsView->widget())->view()->scene()->update(); } void EasyMainWindow::onCollapseItemsAfterCloseChanged(bool _checked) { EASY_GLOBALS.collapse_items_on_tree_close = _checked; } void EasyMainWindow::onAllItemsExpandedByDefaultChange(bool _checked) { EASY_GLOBALS.all_items_expanded_by_default = _checked; } void EasyMainWindow::onBindExpandStatusChange(bool _checked) { EASY_GLOBALS.bind_scene_and_tree_expand_status = _checked; } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onExpandAllClicked(bool) { for (auto& block : EASY_GLOBALS.gui_blocks) block.expanded = true; emit EASY_GLOBALS.events.itemsExpandStateChanged(); auto tree = static_cast(m_treeWidget->widget()); const QSignalBlocker b(tree); tree->expandAll(); } void EasyMainWindow::onCollapseAllClicked(bool) { for (auto& block : EASY_GLOBALS.gui_blocks) block.expanded = false; emit EASY_GLOBALS.events.itemsExpandStateChanged(); auto tree = static_cast(m_treeWidget->widget()); const QSignalBlocker b(tree); tree->collapseAll(); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onEditBlocksClicked(bool) { if (m_descTreeDialog != nullptr) { m_descTreeDialog->raise(); return; } m_descTreeDialog = new QDialog(); m_descTreeDialog->setAttribute(Qt::WA_DeleteOnClose, true); m_descTreeDialog->setWindowTitle("EasyProfiler"); m_descTreeDialog->resize(800, 600); connect(m_descTreeDialog, &QDialog::finished, this, &This::onDescTreeDialogClose); auto l = new QVBoxLayout(m_descTreeDialog); m_dialogDescTree = new EasyDescWidget(m_descTreeDialog); l->addWidget(m_dialogDescTree); m_descTreeDialog->setLayout(l); m_dialogDescTree->build(); m_descTreeDialog->show(); } void EasyMainWindow::onDescTreeDialogClose(int) { disconnect(m_descTreeDialog, &QDialog::finished, this, &This::onDescTreeDialogClose); m_dialogDescTree = nullptr; m_descTreeDialog = nullptr; } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::closeEvent(QCloseEvent* close_event) { saveSettingsAndGeometry(); if (m_descTreeDialog != nullptr) { m_descTreeDialog->reject(); m_descTreeDialog = nullptr; m_dialogDescTree = nullptr; } Parent::closeEvent(close_event); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::loadSettings() { QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); settings.beginGroup("main"); auto last_file = settings.value("last_file"); if (!last_file.isNull()) m_lastFile = last_file.toString(); auto last_addr = settings.value("ip_address"); if (!last_addr.isNull()) m_lastAddress = last_addr.toString(); auto last_port = settings.value("port"); if (!last_port.isNull()) m_lastPort = (uint16_t)last_port.toUInt(); auto val = settings.value("chrono_text_position"); if (!val.isNull()) EASY_GLOBALS.chrono_text_position = static_cast<::profiler_gui::ChronometerTextPosition>(val.toInt()); auto flag = settings.value("draw_graphics_items_borders"); if (!flag.isNull()) EASY_GLOBALS.draw_graphics_items_borders = flag.toBool(); flag = settings.value("hide_narrow_children"); if (!flag.isNull()) EASY_GLOBALS.hide_narrow_children = flag.toBool(); flag = settings.value("collapse_items_on_tree_close"); if (!flag.isNull()) EASY_GLOBALS.collapse_items_on_tree_close = flag.toBool(); flag = settings.value("all_items_expanded_by_default"); if (!flag.isNull()) EASY_GLOBALS.all_items_expanded_by_default = flag.toBool(); flag = settings.value("bind_scene_and_tree_expand_status"); if (!flag.isNull()) EASY_GLOBALS.bind_scene_and_tree_expand_status = flag.toBool(); flag = settings.value("enable_event_indicators"); if (!flag.isNull()) EASY_GLOBALS.enable_event_indicators = flag.toBool(); flag = settings.value("enable_statistics"); if (!flag.isNull()) EASY_GLOBALS.enable_statistics = flag.toBool(); QString encoding = settings.value("encoding", "UTF-8").toString(); auto default_codec_mib = QTextCodec::codecForName(encoding.toStdString().c_str())->mibEnum(); auto default_codec = QTextCodec::codecForMib(default_codec_mib); QTextCodec::setCodecForLocale(default_codec); settings.endGroup(); } void EasyMainWindow::loadGeometry() { QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); settings.beginGroup("main"); auto geometry = settings.value("geometry").toByteArray(); if (!geometry.isEmpty()) restoreGeometry(geometry); auto state = settings.value("windowState").toByteArray(); if (!state.isEmpty()) restoreState(state); settings.endGroup(); } void EasyMainWindow::saveSettingsAndGeometry() { QSettings settings(::profiler_gui::ORGANAZATION_NAME, ::profiler_gui::APPLICATION_NAME); settings.beginGroup("main"); settings.setValue("geometry", this->saveGeometry()); settings.setValue("windowState", this->saveState()); settings.setValue("last_file", m_lastFile); settings.setValue("ip_address", m_lastAddress); settings.setValue("port", (quint32)m_lastPort); settings.setValue("chrono_text_position", static_cast(EASY_GLOBALS.chrono_text_position)); settings.setValue("draw_graphics_items_borders", EASY_GLOBALS.draw_graphics_items_borders); settings.setValue("hide_narrow_children", EASY_GLOBALS.hide_narrow_children); settings.setValue("collapse_items_on_tree_close", EASY_GLOBALS.collapse_items_on_tree_close); settings.setValue("all_items_expanded_by_default", EASY_GLOBALS.all_items_expanded_by_default); settings.setValue("bind_scene_and_tree_expand_status", EASY_GLOBALS.bind_scene_and_tree_expand_status); settings.setValue("enable_event_indicators", EASY_GLOBALS.enable_event_indicators); settings.setValue("enable_statistics", EASY_GLOBALS.enable_statistics); settings.setValue("encoding", QTextCodec::codecForLocale()->name()); settings.endGroup(); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onListenerTimerTimeout() { if (!m_listener.connected()) m_listenerDialog->reject(); } void EasyMainWindow::onListenerDialogClose(int) { m_listenerTimer.stop(); disconnect(m_listenerDialog, &QDialog::finished, this, &This::onListenerDialogClose); m_listenerDialog = nullptr; switch (m_listener.regime()) { case LISTENER_CAPTURE: { m_listenerDialog = new QMessageBox(QMessageBox::Information, "Receiving data...", "This process may take some time.", QMessageBox::NoButton, this); m_listenerDialog->setAttribute(Qt::WA_DeleteOnClose, true); m_listenerDialog->show(); m_listener.stopCapture(); m_listenerDialog->reject(); m_listenerDialog = nullptr; if (m_listener.size() != 0) { readStream(m_listener.data()); m_listener.clearData(); } break; } case LISTENER_DESCRIBE: { break; } default: return; } 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); } } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onFileReaderTimeout() { if (m_reader.done()) { auto nblocks = m_reader.size(); if (nblocks != 0) { static_cast(m_treeWidget->widget())->clearSilent(true); ::profiler::SerializedData serialized_blocks; ::profiler::SerializedData serialized_descriptors; ::profiler::descriptors_list_t descriptors; ::profiler::blocks_t blocks; ::profiler::thread_blocks_tree_t threads_map; QString filename; uint32_t descriptorsNumberInFile = 0; m_reader.get(serialized_blocks, serialized_descriptors, descriptors, blocks, threads_map, descriptorsNumberInFile, filename); if (threads_map.size() > 0xff) { if (m_reader.isFile()) qWarning() << "Warning: file " << filename << " contains " << threads_map.size() << " threads!"; else qWarning() << "Warning: input stream contains " << threads_map.size() << " threads!"; qWarning() << "Warning: Currently, maximum number of displayed threads is 255! Some threads will not be displayed."; } m_bNetworkFileRegime = !m_reader.isFile(); if (!m_bNetworkFileRegime) m_lastFile = ::std::move(filename); m_serializedBlocks = ::std::move(serialized_blocks); m_serializedDescriptors = ::std::move(serialized_descriptors); m_descriptorsNumberInFile = descriptorsNumberInFile; EASY_GLOBALS.selected_thread = 0; ::profiler_gui::set_max(EASY_GLOBALS.selected_block); EASY_GLOBALS.profiler_blocks.swap(threads_map); EASY_GLOBALS.descriptors.swap(descriptors); EASY_GLOBALS.gui_blocks.clear(); EASY_GLOBALS.gui_blocks.resize(nblocks); memset(EASY_GLOBALS.gui_blocks.data(), 0, sizeof(::profiler_gui::EasyBlock) * nblocks); for (decltype(nblocks) i = 0; i < nblocks; ++i) { auto& guiblock = EASY_GLOBALS.gui_blocks[i]; guiblock.tree = ::std::move(blocks[i]); ::profiler_gui::set_max(guiblock.tree_item); } static_cast(m_graphicsView->widget())->view()->setTree(EASY_GLOBALS.profiler_blocks); #if EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW != 0 static_cast(m_descTreeWidget->widget())->build(); #endif if (m_dialogDescTree != nullptr) m_dialogDescTree->build(); m_saveAction->setEnabled(true); m_deleteAction->setEnabled(true); } else { QMessageBox::warning(this, "Warning", QString("Can not read profiled blocks.\n\nReason:\n%1").arg(m_reader.getError()), QMessageBox::Close); } m_reader.interrupt(); m_readerTimer.stop(); m_progress->setValue(100); //m_progress->hide(); if (EASY_GLOBALS.all_items_expanded_by_default) { onExpandAllClicked(true); } } else { m_progress->setValue(m_reader.progress()); } } void EasyMainWindow::onFileReaderCancel() { m_readerTimer.stop(); m_reader.interrupt(); m_progress->setValue(100); //m_progress->hide(); } ////////////////////////////////////////////////////////////////////////// EasyFileReader::EasyFileReader() { } EasyFileReader::~EasyFileReader() { interrupt(); } const bool EasyFileReader::isFile() const { return m_isFile; } bool EasyFileReader::done() const { return m_bDone.load(::std::memory_order_acquire); } int EasyFileReader::progress() const { return m_progress.load(::std::memory_order_acquire); } unsigned int EasyFileReader::size() const { return m_size.load(::std::memory_order_acquire); } const QString& EasyFileReader::filename() const { return m_filename; } void EasyFileReader::load(const QString& _filename) { interrupt(); m_isFile = true; m_filename = _filename; m_thread = ::std::move(::std::thread([this](bool _enableStatistics) { m_size.store(fillTreesFromFile(m_progress, m_filename.toStdString().c_str(), m_serializedBlocks, m_serializedDescriptors, m_descriptors, m_blocks, m_blocksTree, m_descriptorsNumberInFile, _enableStatistics, m_errorMessage), ::std::memory_order_release); m_progress.store(100, ::std::memory_order_release); m_bDone.store(true, ::std::memory_order_release); }, EASY_GLOBALS.enable_statistics)); } void EasyFileReader::load(::std::stringstream& _stream) { interrupt(); m_isFile = false; m_filename.clear(); m_stream.swap(_stream); m_thread = ::std::move(::std::thread([this](bool _enableStatistics) { ::std::ofstream cache_file(NETWORK_CACHE_FILE, ::std::fstream::binary); if (cache_file.is_open()) { cache_file << m_stream.str(); cache_file.close(); } m_size.store(fillTreesFromStream(m_progress, m_stream, m_serializedBlocks, m_serializedDescriptors, m_descriptors, m_blocks, m_blocksTree, m_descriptorsNumberInFile, _enableStatistics, m_errorMessage), ::std::memory_order_release); m_progress.store(100, ::std::memory_order_release); m_bDone.store(true, ::std::memory_order_release); }, EASY_GLOBALS.enable_statistics)); } void EasyFileReader::interrupt() { m_progress.store(-100, ::std::memory_order_release); if (m_thread.joinable()) m_thread.join(); m_bDone.store(false, ::std::memory_order_release); m_progress.store(0, ::std::memory_order_release); m_size.store(0, ::std::memory_order_release); m_serializedBlocks.clear(); m_serializedDescriptors.clear(); m_descriptors.clear(); m_blocks.clear(); m_blocksTree.clear(); m_descriptorsNumberInFile = 0; { decltype(m_stream) dummy; dummy.swap(m_stream); } { decltype(m_errorMessage) dummy; dummy.swap(m_errorMessage); } } void EasyFileReader::get(::profiler::SerializedData& _serializedBlocks, ::profiler::SerializedData& _serializedDescriptors, ::profiler::descriptors_list_t& _descriptors, ::profiler::blocks_t& _blocks, ::profiler::thread_blocks_tree_t& _tree, uint32_t& _descriptorsNumberInFile, QString& _filename) { if (done()) { m_serializedBlocks.swap(_serializedBlocks); m_serializedDescriptors.swap(_serializedDescriptors); ::profiler::descriptors_list_t(::std::move(m_descriptors)).swap(_descriptors); m_blocks.swap(_blocks); m_blocksTree.swap(_tree); m_filename.swap(_filename); _descriptorsNumberInFile = m_descriptorsNumberInFile; } } QString EasyFileReader::getError() { return QString(m_errorMessage.str().c_str()); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onEventTracingPriorityChange(bool _checked) { if (EASY_GLOBALS.connected) m_listener.send(profiler::net::BoolMessage(profiler::net::MESSAGE_TYPE_EVENT_TRACING_PRIORITY, _checked)); } void EasyMainWindow::onEventTracingEnableChange(bool _checked) { if (EASY_GLOBALS.connected) m_listener.send(profiler::net::BoolMessage(profiler::net::MESSAGE_TYPE_EVENT_TRACING_STATUS, _checked)); } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onConnectClicked(bool) { if(EASY_GLOBALS.connected) return; auto text = m_ipEdit->text(); auto parts = text.split(QChar('.')); if (parts.size() != 4) { QMessageBox::warning(this, "Warning", "Invalid IP-Address", QMessageBox::Close); return; } for (auto& part : parts) { int i = 0; for (; i < part.size(); ++i) { if (part[i] != QChar('0')) break; } if (i < part.size()) part = part.mid(i); else part = "0"; } m_lastAddress = parts.join(QChar('.')); m_lastPort = m_portEdit->text().toUShort(); m_ipEdit->setText(m_lastAddress); profiler::net::EasyProfilerStatus reply(false, false, false); if (!m_listener.connect(m_lastAddress.toStdString().c_str(), m_lastPort, reply)) { QMessageBox::warning(this, "Warning", "Cannot connect with application", QMessageBox::Close); return; } qInfo() << "Connected successfully"; EASY_GLOBALS.connected = true; m_captureAction->setEnabled(true); SET_ICON(m_connectAction, ":/Connection-on"); disconnect(m_eventTracingEnableAction, &QAction::triggered, this, &This::onEventTracingEnableChange); disconnect(m_eventTracingPriorityAction, &QAction::triggered, this, &This::onEventTracingPriorityChange); m_eventTracingEnableAction->setEnabled(true); m_eventTracingPriorityAction->setEnabled(true); m_eventTracingEnableAction->setChecked(reply.isEventTracingEnabled); m_eventTracingPriorityAction->setChecked(reply.isLowPriorityEventTracing); connect(m_eventTracingEnableAction, &QAction::triggered, this, &This::onEventTracingEnableChange); connect(m_eventTracingPriorityAction, &QAction::triggered, this, &This::onEventTracingPriorityChange); emit EASY_GLOBALS.events.connectionChanged(true); } void EasyMainWindow::onCaptureClicked(bool) { if (!EASY_GLOBALS.connected) { QMessageBox::warning(this, "Warning", "No connection with profiling app", QMessageBox::Close); return; } if (m_listener.regime() != LISTENER_IDLE) { if (m_listener.regime() == LISTENER_CAPTURE) QMessageBox::warning(this, "Warning", "Already capturing frames.\nFinish old capturing session first.", QMessageBox::Close); else QMessageBox::warning(this, "Warning", "Capturing blocks description.\nFinish old capturing session first.", QMessageBox::Close); return; } m_listener.startCapture(); m_listenerTimer.start(250); m_listenerDialog = new QMessageBox(QMessageBox::Information, "Capturing frames...", "Close this dialog to stop capturing.", QMessageBox::Close, this); m_listenerDialog->setAttribute(Qt::WA_DeleteOnClose, true); connect(m_listenerDialog, &QDialog::finished, this, &This::onListenerDialogClose); m_listenerDialog->show(); } void EasyMainWindow::onGetBlockDescriptionsClicked(bool) { if (!EASY_GLOBALS.connected) { QMessageBox::warning(this, "Warning", "No connection with profiling app", QMessageBox::Close); return; } if (m_listener.regime() != LISTENER_IDLE) { if (m_listener.regime() == LISTENER_DESCRIBE) QMessageBox::warning(this, "Warning", "Already capturing blocks description.\nFinish old capturing session first.", QMessageBox::Close); else QMessageBox::warning(this, "Warning", "Capturing capturing frames.\nFinish old capturing session first.", QMessageBox::Close); return; } m_listenerDialog = new QMessageBox(QMessageBox::Information, "Waiting for blocks...", "This may take some time.", QMessageBox::NoButton, this); m_listenerDialog->setAttribute(Qt::WA_DeleteOnClose, true); m_listenerDialog->show(); m_listener.requestBlocksDescription(); m_listenerDialog->reject(); m_listenerDialog = nullptr; if (m_listener.size() != 0) { // Read descriptions from stream decltype(EASY_GLOBALS.descriptors) descriptors; decltype(m_serializedDescriptors) serializedDescriptors; ::std::stringstream errorMessage; if (readDescriptionsFromStream(m_listener.data(), serializedDescriptors, descriptors, errorMessage)) { // Merge old and new descriptions bool cancel = false; const bool doFlush = m_descriptorsNumberInFile > descriptors.size(); if (doFlush && !m_serializedBlocks.empty()) { auto button = QMessageBox::question(this, "Information", QString("New blocks description number = %1\nis less than the old one = %2.\nTo avoid possible conflicts\nall profiled data will be deleted.\nContinue?") .arg(descriptors.size()) .arg(m_descriptorsNumberInFile), QMessageBox::Yes, QMessageBox::No); if (button == QMessageBox::Yes) clear(); // Clear all contents because new descriptors list conflicts with old one else cancel = true; } if (!cancel) { if (!doFlush && m_descriptorsNumberInFile < EASY_GLOBALS.descriptors.size()) { // There are dynamically added descriptors, add them to the new list too auto newnumber = static_cast(descriptors.size()); auto size = static_cast(EASY_GLOBALS.descriptors.size()); auto diff = newnumber - size; decltype(newnumber) failnumber = 0; descriptors.reserve(descriptors.size() + EASY_GLOBALS.descriptors.size() - m_descriptorsNumberInFile); for (auto i = m_descriptorsNumberInFile; i < size; ++i) { auto id = EASY_GLOBALS.descriptors[i]->id(); if (id < newnumber) descriptors.push_back(descriptors[id]); else ++failnumber; } if (failnumber != 0) { // There are some errors... // revert changes descriptors.resize(newnumber); // clear all profiled data to avoid conflicts auto button = QMessageBox::question(this, "Information", "There are errors while merging block descriptions lists.\nTo avoid possible conflicts\nall profiled data will be deleted.\nContinue?", QMessageBox::Yes, QMessageBox::No); if (button == QMessageBox::Yes) clear(); // Clear all contents because new descriptors list conflicts with old one else cancel = true; } if (!cancel && diff != 0) { for (auto& b : EASY_GLOBALS.gui_blocks) { if (b.tree.node->id() >= m_descriptorsNumberInFile) b.tree.node->setId(b.tree.node->id() + diff); } m_descriptorsNumberInFile = newnumber; } } if (!cancel) { EASY_GLOBALS.descriptors.swap(descriptors); m_serializedDescriptors.swap(serializedDescriptors); if (m_descTreeDialog != nullptr) { #if EASY_GUI_USE_DESCRIPTORS_DOCK_WINDOW != 0 static_cast(m_descTreeWidget->widget())->build(); #endif m_dialogDescTree->build(); m_descTreeDialog->raise(); } else { onEditBlocksClicked(true); } } } } else { QMessageBox::warning(this, "Warning", QString("Can not read blocks description from stream.\n\nReason:\n%1").arg(errorMessage.str().c_str()), QMessageBox::Close); } m_listener.clearData(); } 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); } } ////////////////////////////////////////////////////////////////////////// void EasyMainWindow::onBlockStatusChange(::profiler::block_id_t _id, ::profiler::EasyBlockStatus _status) { if (EASY_GLOBALS.connected) m_listener.send(profiler::net::BlockStatusMessage(_id, static_cast(_status))); } ////////////////////////////////////////////////////////////////////////// EasySocketListener::EasySocketListener() : m_receivedSize(0), m_regime(LISTENER_IDLE) { m_bInterrupt = ATOMIC_VAR_INIT(false); m_bConnected = ATOMIC_VAR_INIT(false); } EasySocketListener::~EasySocketListener() { m_bInterrupt.store(true, ::std::memory_order_release); if (m_thread.joinable()) m_thread.join(); } bool EasySocketListener::connected() const { return m_bConnected.load(::std::memory_order_acquire); } EasyListenerRegime EasySocketListener::regime() const { return m_regime; } uint64_t EasySocketListener::size() const { return m_receivedSize; } ::std::stringstream& EasySocketListener::data() { return m_receivedData; } void EasySocketListener::clearData() { decltype(m_receivedData) dummy; dummy.swap(m_receivedData); m_receivedSize = 0; } bool EasySocketListener::connect(const char* _ipaddress, uint16_t _port, profiler::net::EasyProfilerStatus& _reply) { if (connected()) return true; m_easySocket.flush(); m_easySocket.init(); int res = m_easySocket.setAddress(_ipaddress, _port); res = m_easySocket.connect(); const bool isConnected = res == 0; if (isConnected) { static const size_t buffer_size = sizeof(profiler::net::EasyProfilerStatus) << 1; char buffer[buffer_size] = {}; int bytes = 0; while (true) { bytes = m_easySocket.receive(buffer, buffer_size); if (bytes == -1) { if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) return false; bytes = 0; continue; } break; } if (bytes == 0) { m_bConnected.store(isConnected, ::std::memory_order_release); return isConnected; } int seek = bytes; while (seek < sizeof(profiler::net::EasyProfilerStatus)) { bytes = m_easySocket.receive(buffer + seek, buffer_size - seek); if (bytes == -1) { if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) return false; break; } seek += bytes; } auto message = reinterpret_cast(buffer); if (message->isEasyNetMessage() && message->type == profiler::net::MESSAGE_TYPE_ACCEPTED_CONNECTION) _reply = *message; } m_bConnected.store(isConnected, ::std::memory_order_release); return isConnected; } void EasySocketListener::startCapture() { clearData(); profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_START_CAPTURE); m_easySocket.send(&request, sizeof(request)); m_regime = LISTENER_CAPTURE; m_thread = ::std::move(::std::thread(&EasySocketListener::listenCapture, this)); } void EasySocketListener::stopCapture() { if (!m_thread.joinable() || m_regime != LISTENER_CAPTURE) return; profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_STOP_CAPTURE); m_easySocket.send(&request, sizeof(request)); m_thread.join(); m_regime = LISTENER_IDLE; } void EasySocketListener::requestBlocksDescription() { clearData(); profiler::net::Message request(profiler::net::MESSAGE_TYPE_REQUEST_BLOCKS_DESCRIPTION); m_easySocket.send(&request, sizeof(request)); m_regime = LISTENER_DESCRIBE; listenDescription(); m_regime = LISTENER_IDLE; } ////////////////////////////////////////////////////////////////////////// void EasySocketListener::listenCapture() { // TODO: Merge functions listenCapture() and listenDescription() static const int buffer_size = 8 * 1024 * 1024; char* buffer = new char[buffer_size]; int seek = 0, bytes = 0; auto timeBegin = ::std::chrono::system_clock::now(); bool isListen = true, disconnected = false; while (isListen && !m_bInterrupt.load(::std::memory_order_acquire)) { if ((bytes - seek) == 0) { bytes = m_easySocket.receive(buffer, buffer_size); if (bytes == -1) { if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) { m_bConnected.store(false, ::std::memory_order_release); isListen = false; disconnected = true; } seek = 0; bytes = 0; continue; } seek = 0; } if (bytes == 0) { isListen = false; break; } char* buf = buffer + seek; if (bytes > 0) { auto message = reinterpret_cast(buf); if (!message->isEasyNetMessage()) continue; switch (message->type) { case profiler::net::MESSAGE_TYPE_ACCEPTED_CONNECTION: { qInfo() << "Receive MESSAGE_TYPE_ACCEPTED_CONNECTION"; //m_easySocket.send(&request, sizeof(request)); seek += sizeof(profiler::net::Message); break; } case profiler::net::MESSAGE_TYPE_REPLY_START_CAPTURING: { qInfo() << "Receive MESSAGE_TYPE_REPLY_START_CAPTURING"; seek += sizeof(profiler::net::Message); break; } case profiler::net::MESSAGE_TYPE_REPLY_BLOCKS_END: { qInfo() << "Receive MESSAGE_TYPE_REPLY_BLOCKS_END"; seek += sizeof(profiler::net::Message); const auto dt = ::std::chrono::duration_cast(::std::chrono::system_clock::now() - timeBegin); const auto bytesNumber = m_receivedData.str().size(); qInfo() << "recieved " << bytesNumber << " bytes, " << dt.count() << " ms, average speed = " << double(bytesNumber) * 1e3 / double(dt.count()) / 1024. << " kBytes/sec"; seek = 0; bytes = 0; isListen = false; break; } case profiler::net::MESSAGE_TYPE_REPLY_BLOCKS: { qInfo() << "Receive MESSAGE_TYPE_REPLY_BLOCKS"; seek += sizeof(profiler::net::DataMessage); profiler::net::DataMessage* dm = (profiler::net::DataMessage*)message; timeBegin = std::chrono::system_clock::now(); int neededSize = dm->size; buf = buffer + seek; auto bytesNumber = ::std::min((int)dm->size, bytes - seek); m_receivedSize += bytesNumber; m_receivedData.write(buf, bytesNumber); neededSize -= bytesNumber; if (neededSize == 0) seek += bytesNumber; else { seek = 0; bytes = 0; } int loaded = 0; while (neededSize > 0) { bytes = m_easySocket.receive(buffer, buffer_size); if (bytes == -1) { if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) { m_bConnected.store(false, ::std::memory_order_release); isListen = false; disconnected = true; neededSize = 0; } break; } buf = buffer; int toWrite = ::std::min(bytes, neededSize); m_receivedSize += toWrite; m_receivedData.write(buf, toWrite); neededSize -= toWrite; loaded += toWrite; seek = toWrite; } break; } default: //qInfo() << "Receive unknown " << message->type; break; } } } if (disconnected) clearData(); delete [] buffer; } void EasySocketListener::listenDescription() { // TODO: Merge functions listenDescription() and listenCapture() static const int buffer_size = 8 * 1024 * 1024; char* buffer = new char[buffer_size]; int seek = 0, bytes = 0; bool isListen = true, disconnected = false; while (isListen && !m_bInterrupt.load(::std::memory_order_acquire)) { if ((bytes - seek) == 0) { bytes = m_easySocket.receive(buffer, buffer_size); if (bytes == -1) { if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) { m_bConnected.store(false, ::std::memory_order_release); isListen = false; disconnected = true; } seek = 0; bytes = 0; continue; } seek = 0; } if (bytes == 0) { isListen = false; break; } char* buf = buffer + seek; if (bytes > 0) { auto message = reinterpret_cast(buf); if (!message->isEasyNetMessage()) continue; switch (message->type) { case profiler::net::MESSAGE_TYPE_ACCEPTED_CONNECTION: { qInfo() << "Receive MESSAGE_TYPE_ACCEPTED_CONNECTION"; seek += sizeof(profiler::net::Message); break; } case profiler::net::MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION_END: { qInfo() << "Receive MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION_END"; seek += sizeof(profiler::net::Message); seek = 0; bytes = 0; isListen = false; break; } case profiler::net::MESSAGE_TYPE_REPLY_BLOCKS_DESCRIPTION: { qInfo() << "Receive MESSAGE_TYPE_REPLY_BLOCKS"; seek += sizeof(profiler::net::DataMessage); profiler::net::DataMessage* dm = (profiler::net::DataMessage*)message; int neededSize = dm->size; buf = buffer + seek; auto bytesNumber = ::std::min((int)dm->size, bytes - seek); m_receivedSize += bytesNumber; m_receivedData.write(buf, bytesNumber); neededSize -= bytesNumber; if (neededSize == 0) seek += bytesNumber; else{ seek = 0; bytes = 0; } int loaded = 0; while (neededSize > 0) { bytes = m_easySocket.receive(buffer, buffer_size); if (bytes == -1) { if (m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED) { m_bConnected.store(false, ::std::memory_order_release); isListen = false; disconnected = true; neededSize = 0; } break; } buf = buffer; int toWrite = ::std::min(bytes, neededSize); m_receivedSize += toWrite; m_receivedData.write(buf, toWrite); neededSize -= toWrite; loaded += toWrite; seek = toWrite; } break; } default: break; } } } if (disconnected) clearData(); delete[] buffer; } //////////////////////////////////////////////////////////////////////////