From bf7197048b105facedb77a942401cedec6f95670 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Thu, 4 Jan 2024 18:07:50 +0800 Subject: [PATCH] update --- .vscode/settings.json | 3 +- CMakeLists.txt | 6 +++- libxsync | 2 +- mainwindow.cpp | 33 ++++++++++++++++++ mainwindow.h | 27 +++++++++++++-- src/main.cpp | 5 +-- src/xsync_udp_factory_impl.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++ src/xsync_udp_factory_impl.hpp | 26 ++++++++++++++ src/zqthread.cpp | 12 +++++++ src/zqthread.hpp | 33 ++++++++++++++++++ 10 files changed, 217 insertions(+), 7 deletions(-) create mode 100644 src/xsync_udp_factory_impl.cpp create mode 100644 src/xsync_udp_factory_impl.hpp create mode 100644 src/zqthread.cpp create mode 100644 src/zqthread.hpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 9ef7e98..8f8faba 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -80,7 +80,8 @@ "future": "cpp", "qcoreapplication": "cpp", "qcategoryaxis": "cpp", - "qaction": "cpp" + "qaction": "cpp", + "qthread": "cpp" }, "files.autoGuessEncoding": true, } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d99683..0030f36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,9 +18,13 @@ find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Charts REQUIRED) set(PROJECT_SOURCES src/logger.cpp src/main.cpp + src/zqthread.cpp mainwindow.cpp mainwindow.h mainwindow.ui + libxsync/xsync.cpp + libxsync/iflytop_xsync_protocol/iflytop_xsync_protocol.c + src/xsync_udp_factory_impl.cpp ) @@ -28,7 +32,7 @@ add_executable(xsync ${PROJECT_SOURCES} ) -target_link_libraries(xsync PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Charts ) +target_link_libraries(xsync PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Charts wsock32) set_target_properties(xsync PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com diff --git a/libxsync b/libxsync index 362cd3a..5e99b7c 160000 --- a/libxsync +++ b/libxsync @@ -1 +1 @@ -Subproject commit 362cd3a9013bfbee034a508bd7b36f819ce28ced +Subproject commit 5e99b7c334eae1657ea128754db0d5cd04849c7d diff --git a/mainwindow.cpp b/mainwindow.cpp index 438e3d8..c302ac1 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,6 +1,25 @@ #include "mainwindow.h" +#include + #include "./ui_mainwindow.h" +#include "src/logger.hpp" + +using namespace iflytop; +using namespace std; + +static MainWindow *m_mainWindow; +#define TAG "MainWindow" + +void MainWindow::log_output(QtMsgType type, const QMessageLogContext &context, const QString &msg) { + QString text; + text.append(msg); + m_mainWindow->append_log_signal(text); +} + +void MainWindow::append_log_slot(QString text) { + ui->logbrowser->append(text); +} void MainWindow::push_reg(QWidget *parent, int off, const char *regname, int32_t regadd, int32_t rwflag) { DispalyRegIterm *regitem = new DispalyRegIterm(); @@ -80,7 +99,21 @@ void MainWindow::construct_reg_table() { // // MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); + m_mainWindow = this; construct_reg_table(); + connect(this, SIGNAL(append_log_signal(QString)), this, SLOT(append_log_slot(QString))); + qInstallMessageHandler(log_output); + + m_thread.reset(new ZQThread("test", [this]() { + while (true) { + ZLOGI(TAG, "start1"); + ZLOGI(TAG, "start2"); + ZLOGI(TAG, "start3"); + ZLOGI(TAG, "start4"); + QThread::sleep(1); + } + })); + m_thread->start(); } MainWindow::~MainWindow() { delete ui; } diff --git a/mainwindow.h b/mainwindow.h index 1db9119..0215040 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -15,7 +15,20 @@ #include #include #include +// + +#include +#include +#include +#include #include +#include +#include +#include +#include +#include + +#include "src/zqthread.hpp" QT_BEGIN_NAMESPACE namespace Ui { @@ -37,14 +50,24 @@ class MainWindow : public QMainWindow { private: std::map m_regdisplayer; + std::unique_ptr m_thread; + public: + Ui::MainWindow *ui; + MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: - Ui::MainWindow *ui; - void construct_reg_table(); void push_reg(QWidget *parent, int off, const char *regname, int32_t regadd, int32_t rwflag); + /******************************************************************************* + * 日志 * + *******************************************************************************/ + static void log_output(QtMsgType type, const QMessageLogContext &context, const QString &msg); + private slots: + void append_log_slot(QString ); + signals: + void append_log_signal(QString str); }; #endif // MAINWINDOW_H diff --git a/src/main.cpp b/src/main.cpp index d1892e7..fcd9a6f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,10 +24,11 @@ #define TAG "Main" using namespace std; QT_CHARTS_USE_NAMESPACE + int main(int argc, char *argv[]) { QApplication a(argc, argv); - ZLOGI(TAG, "hello world"); - MainWindow w; + MainWindow w; w.show(); + ZLOGI(TAG, "start"); return a.exec(); } diff --git a/src/xsync_udp_factory_impl.cpp b/src/xsync_udp_factory_impl.cpp new file mode 100644 index 0000000..70eec6b --- /dev/null +++ b/src/xsync_udp_factory_impl.cpp @@ -0,0 +1,77 @@ +#include "xsync_udp_factory_impl.hpp" + +#include +// +#include +#pragma comment(lib, "ws2_32") +using namespace iflytop; +class XSUDP : public I_XSUDP { + uint32_t m_ip; + int m_localport; + + struct sockaddr_in localadd = {}; + int m_sock_fd = 0; + + public: + virtual xs_error_code_t initialize(string ip, int localport) override; + virtual xs_error_code_t sendto(const XsyncNetAdd& to, const char* data, int32_t length) override; + virtual xs_error_code_t receive(const char* data, int32_t length, XsyncNetAdd& from, int overtimems) override; +}; + +xs_error_code_t XSUDP::initialize(string ip, int localport) { + localadd.sin_family = AF_INET; + localadd.sin_addr.s_addr = inet_addr(ip.c_str()); + localadd.sin_port = htons(localport); + + // 创建客户端用于通信的Socket + m_sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (m_sock_fd < 0) return kxs_ec_socket_fail; + + int ret = bind(m_sock_fd, (struct sockaddr*)&localadd, sizeof(localadd)); + if (ret < 0) return kxs_ec_bind_fail; + return kxs_ec_success; +} + +xs_error_code_t XSUDP::sendto(const XsyncNetAdd& to, const char* data, int32_t length) { + struct sockaddr_in sockaddr; + sockaddr.sin_family = AF_INET; + sockaddr.sin_addr.s_addr = htonl(to.ip); + sockaddr.sin_port = htons(to.port); + int ret = ::sendto(m_sock_fd, data, length, 0, (struct sockaddr*)&sockaddr, sizeof(sockaddr)); + if (ret != length) return kxs_ec_send_fail; + return kxs_ec_success; +} +xs_error_code_t XSUDP::receive(const char* data, int32_t length, XsyncNetAdd& from, int overtimems) { + struct sockaddr_in sockaddr; + + timeval timeout; + timeout.tv_sec = overtimems; + timeout.tv_usec = 0; + if (setsockopt(m_sock_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)) == -1) { + return kxs_ec_setsockopt_rx_timeout_fail; + } + + sockaddr.sin_family = AF_INET; + sockaddr.sin_addr.s_addr = htonl(from.ip); + sockaddr.sin_port = htons(from.port); + int senderAddressLen = sizeof(sockaddr); + int ret = ::recvfrom(m_sock_fd, (char*)data, length, 0, (struct sockaddr*)&sockaddr, &senderAddressLen); + if (ret != length) return kxs_ec_receive_fail; + return kxs_ec_success; +} + +/******************************************************************************* + * xSyncUdpFactoryImpl * + *******************************************************************************/ + +void XSyncUdpFactoryImpl::initialize() { + static bool isInit = false; + if (!isInit) { + WSADATA wsaData; + WSAStartup(MAKEWORD(2, 2), &wsaData); + isInit = true; + } +} + +shared_ptr XSyncUdpFactoryImpl::createXSUDPListener() { return nullptr; } +shared_ptr XSyncUdpFactoryImpl::createXSUDP() { return make_shared(); } \ No newline at end of file diff --git a/src/xsync_udp_factory_impl.hpp b/src/xsync_udp_factory_impl.hpp new file mode 100644 index 0000000..c523b29 --- /dev/null +++ b/src/xsync_udp_factory_impl.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libxsync\xsync.hpp" +namespace iflytop { +using namespace std; + +class XSyncUdpFactoryImpl : public I_XSyncUDPFactory { + private: + /* data */ + public: + void initialize(); + virtual shared_ptr createXSUDPListener() override; + virtual shared_ptr createXSUDP() override; +}; + +} // namespace iflytop \ No newline at end of file diff --git a/src/zqthread.cpp b/src/zqthread.cpp new file mode 100644 index 0000000..ac072a5 --- /dev/null +++ b/src/zqthread.cpp @@ -0,0 +1,12 @@ +#include "zqthread.hpp" +using namespace iflytop; +using namespace std; + +ZQThread::ZQThread(const char* name, function cb) { + name_ = QString(name); + cb_ = cb; +} + +void ZQThread::run() { + if (cb_) cb_(); +} \ No newline at end of file diff --git a/src/zqthread.hpp b/src/zqthread.hpp new file mode 100644 index 0000000..05c3751 --- /dev/null +++ b/src/zqthread.hpp @@ -0,0 +1,33 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +#include +#include + +namespace iflytop { +using namespace std; + +class ZQThread : public QThread { + Q_OBJECT + private: + QString name_; + function cb_; + + public: + ZQThread(const char* name, function cb); + + protected: + virtual void run() override; +}; + +} // namespace iflytop \ No newline at end of file