Переглянути джерело

V0.3.0
1、添加了基于Qt的单个子线程的支持
2、完善了ftp工程

Apple 6 місяців тому
батько
коміт
7d0e5334c2

+ 8 - 12
CMakeLists.txt

@@ -28,7 +28,7 @@ message(STATUS "Target System is ${CMAKE_SIZEOF_VOID_P} bit")
 
 
 #设置可执行文件名称
-set(exec_name "Library")
+# set(exec_name "Library")
 #包含子文件夹
 #include(${CMAKE_SOURCE_DIR}/CommonModule/common.cmake)
 
@@ -123,20 +123,12 @@ include(${CMAKE_SOURCE_DIR}/External/Libraries/Libraries.cmake)
 
 #包含头文件
 include_directories(
-    ${CMAKE_SOURCE_DIR}
-    ${CMAKE_SOURCE_DIR}/common/Logs
-    # ${CMAKE_SOURCE_DIR}/common/http
-    ${CMAKE_SOURCE_DIR}/common/LightLog
-    # ${CMAKE_SOURCE_DIR}/common/mqtt
+
 )
 
 #包含源文件
 file(GLOB GLOBAL_SRC
-    ${CMAKE_SOURCE_DIR}/*.cpp
-    ${CMAKE_SOURCE_DIR}/common/Logs/*.cpp
-    # ${CMAKE_SOURCE_DIR}/common/mqtt/*.cpp
-    # ${CMAKE_SOURCE_DIR}/common/http/*.cpp
-    ${CMAKE_SOURCE_DIR}/common/LightLog/*.cpp
+
 )
 
 
@@ -147,4 +139,8 @@ file(GLOB GLOBAL_SRC
 
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/mqtt)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/http)
-add_subdirectory(${CMAKE_SOURCE_DIR}/demo/threadPool)
+# add_subdirectory(${CMAKE_SOURCE_DIR}/demo/threadPool)
+add_subdirectory(${CMAKE_SOURCE_DIR}/demo/ftp)
+# add_subdirectory(${CMAKE_SOURCE_DIR}/demo/OneThread)
+
+

+ 73 - 0
demo/OneThread/CMakeLists.txt

@@ -0,0 +1,73 @@
+cmake_minimum_required(VERSION 3.5)
+
+set(this_exe OneThread)
+
+
+#包含源文件
+file(GLOB LOCAL_SRC
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.qrc
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.rc
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.ui
+
+    ${CMAKE_SOURCE_DIR}/External/common/OneThread/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/common/LightLog/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/common/Logs/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/common/ftp/*.cpp
+
+    # ${CMAKE_SOURCE_DIR}/External/Libraries/NetworkCurl/ftp/*.cpp
+)
+
+
+# 生成可执行程序
+
+add_executable(${this_exe}
+    # WIN32
+    ${GLOBAL_SRC}
+    ${LOCAL_SRC} 
+)
+
+
+#添加头文件
+target_include_directories(${this_exe} PRIVATE
+
+    ${CMAKE_CURRENT_SOURCE_DIR}
+    ${CMAKE_SOURCE_DIR}/External/common/OneThread
+    ${CMAKE_SOURCE_DIR}/External/common/LightLog
+    ${CMAKE_SOURCE_DIR}/External/common/Logs
+    ${CMAKE_SOURCE_DIR}/External/common/ftp
+
+)
+
+target_link_libraries(${this_exe} PRIVATE
+    Qt5::Widgets
+    Qt5::Core
+    Qt5::Network
+    # Qt5::Multimedia
+    # Qt5::Xml
+    # Qt5::Sql
+)
+
+target_link_libraries(${this_exe} PRIVATE 
+    fmt::fmt
+    spdlog::spdlog
+)
+
+target_link_libraries(${this_exe} PRIVATE
+    # ${CURL_LIBRARY}
+    # ${LHHTTPAPI_LIBRARY}
+)
+# message(STATUS "CURL_LIBRARY: ${CURL_LIBRARY}")
+
+
+# if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
+#     target_link_libraries(${this_exe} PRIVATE
+#         # debug spdlogd.lib
+#         # optimized spdlog.lib
+#     )
+# elseif(CMAKE_CXX_COMPILER_ID MATCHES GNU)
+#     target_link_libraries(${this_exe} PRIVATE
+#         # debug 
+#         # optimized ${SM_DLL}
+#     )
+# endif()

+ 14 - 0
demo/OneThread/main.cpp

@@ -0,0 +1,14 @@
+#include "widget.h"
+
+#include <QApplication>
+#include "loginit.h"
+
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    init_log();
+    Widget w;
+    w.show();
+    return a.exec();
+}

+ 57 - 0
demo/OneThread/widget.cpp

@@ -0,0 +1,57 @@
+#include "widget.h"
+#include "./ui_widget.h"
+
+#include <QNetworkReply>
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QTimer>
+#include <QNetworkProxy>
+
+#include "spdlog/spdlog.h"
+
+
+Widget::Widget(QWidget *parent)
+    : QWidget(parent)
+    , ui(new Ui::Widget)
+{
+    ui->setupUi(this);
+
+    m_thread = new QThread();
+    m_oneThread = new OneThread(m_thread);
+    m_thread->start();
+
+
+    SPDLOG_INFO("***** Qt Library *****");
+}
+
+Widget::~Widget()
+{
+
+    delete ui;
+}
+
+void print(const QString& str, const char* str2, const char* str3, const char* str4, const char* str5)
+{
+    SPDLOG_INFO("{}", str.toStdString());
+    SPDLOG_INFO("{}", str2);
+    SPDLOG_INFO("{}", str3);
+    SPDLOG_INFO("{}", str4);
+    SPDLOG_INFO("{}", str5);
+}
+
+
+void Widget::on_pBtn_connect_clicked()
+{
+    SPDLOG_INFO("点击了“连接按钮”");
+    qDebug() << "main Thread ID: " << QThread::currentThreadId();
+    auto task = m_oneThread->bindTask(print, "Hello ", "World ", "this ", "is ", "Qt");
+    emit m_oneThread->signal_runTask(task);
+}
+
+void Widget::on_pBtn_downloadFile_clicked()
+{
+    SPDLOG_INFO("点击了“下载文件”");
+    
+    
+}
+

+ 33 - 0
demo/OneThread/widget.h

@@ -0,0 +1,33 @@
+#ifndef WIDGET_H
+#define WIDGET_H
+
+#include <QWidget>
+#include <QThread>
+
+#include "OneThread.h"
+
+
+QT_BEGIN_NAMESPACE
+namespace Ui { class Widget; }
+QT_END_NAMESPACE
+
+class Widget : public QWidget
+{
+    Q_OBJECT
+
+public:
+    Widget(QWidget *parent = nullptr);
+    ~Widget();
+
+private slots:
+
+    void on_pBtn_connect_clicked();
+
+    void on_pBtn_downloadFile_clicked();
+
+private:
+    Ui::Widget *ui;
+    QThread* m_thread = nullptr;
+    OneThread* m_oneThread = nullptr;
+};
+#endif // WIDGET_H

+ 48 - 0
demo/OneThread/widget.ui

@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Widget</class>
+ <widget class="QWidget" name="Widget">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>800</width>
+    <height>600</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Widget</string>
+  </property>
+  <widget class="QWidget" name="widget_pBtn" native="true">
+   <property name="geometry">
+    <rect>
+     <x>30</x>
+     <y>30</y>
+     <width>681</width>
+     <height>311</height>
+    </rect>
+   </property>
+   <layout class="QGridLayout" name="gridLayout">
+    <item row="0" column="1">
+     <widget class="QPushButton" name="pBtn_downloadFile">
+      <property name="text">
+       <string>下载文件</string>
+      </property>
+     </widget>
+    </item>
+    <item row="0" column="0">
+     <widget class="QPushButton" name="pBtn_connect">
+      <property name="text">
+       <string>连接</string>
+      </property>
+     </widget>
+    </item>
+    <item row="1" column="0" colspan="2">
+     <widget class="QLineEdit" name="lineEdit"/>
+    </item>
+   </layout>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 75 - 0
demo/ftp/CMakeLists.txt

@@ -0,0 +1,75 @@
+cmake_minimum_required(VERSION 3.5)
+
+set(this_exe curlFtp)
+
+
+#包含源文件
+file(GLOB LOCAL_SRC
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.qrc
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.rc
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/*.ui
+
+    ${CMAKE_SOURCE_DIR}/External/common/http/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/common/LightLog/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/common/Logs/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/common/ftp/*.cpp
+    ${CMAKE_SOURCE_DIR}/External/common/CurlFtp/*.cpp
+    
+)
+
+
+# 生成可执行程序
+
+add_executable(${this_exe}
+    # WIN32
+    ${GLOBAL_SRC}
+    ${LOCAL_SRC} 
+)
+
+
+#添加头文件
+target_include_directories(${this_exe} PRIVATE
+
+    ${CMAKE_CURRENT_SOURCE_DIR}
+    ${CMAKE_SOURCE_DIR}/External/common/http
+    ${CMAKE_SOURCE_DIR}/External/common/LightLog
+    ${CMAKE_SOURCE_DIR}/External/common/Logs
+    ${CMAKE_SOURCE_DIR}/External/common/ftp
+    ${CMAKE_SOURCE_DIR}/External/common/CurlFtp
+    ${CMAKE_SOURCE_DIR}/External/common/FmtLog
+)
+
+target_link_libraries(${this_exe} PRIVATE
+    Qt5::Widgets
+    Qt5::Core
+    Qt5::Network
+    # Qt5::Multimedia
+    # Qt5::Xml
+    # Qt5::Sql
+)
+
+target_link_libraries(${this_exe} PRIVATE 
+    fmt::fmt
+    spdlog::spdlog
+    CURL::libcurl
+)
+
+# target_link_libraries(${this_exe} PRIVATE
+#     ${CURL_LIBRARY}
+    
+# )
+# message(STATUS "CURL_LIBRARY: ${CURL_LIBRARY}")
+
+
+# if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
+#     target_link_libraries(${this_exe} PRIVATE
+#         # debug spdlogd.lib
+#         # optimized spdlog.lib
+#     )
+# elseif(CMAKE_CXX_COMPILER_ID MATCHES GNU)
+#     target_link_libraries(${this_exe} PRIVATE
+#         # debug 
+#         # optimized ${SM_DLL}
+#     )
+# endif()

+ 14 - 0
demo/ftp/main.cpp

@@ -0,0 +1,14 @@
+#include "widget.h"
+
+#include <QApplication>
+#include "loginit.h"
+
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    init_log();
+    Widget w;
+    w.show();
+    return a.exec();
+}

+ 95 - 0
demo/ftp/widget.cpp

@@ -0,0 +1,95 @@
+#include "widget.h"
+#include "./ui_widget.h"
+
+#include <QNetworkReply>
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QTimer>
+#include <QNetworkProxy>
+#include <regex>
+#include <stdlib.h>
+#include "QtFtp.h"
+
+#include "spdlog/spdlog.h"
+#include "fmtlog.h"
+
+
+Widget::Widget(QWidget *parent)
+    : QWidget(parent)
+    , ui(new Ui::Widget)
+{
+    ui->setupUi(this);
+
+    m_curlFtp.setFtpIPAndPort("192.168.50.100", "21");
+    m_curlFtp.setFtpUsernameAndPassword("microsoft", "19980714Lq");
+
+    SPDLOG_INFO("***** Qt Library *****");
+}
+
+Widget::~Widget()
+{
+
+    delete ui;
+}
+
+
+void Widget::on_pBtn_connect_clicked()
+{
+    SPDLOG_INFO("点击了“连接按钮”");
+    QString url = "ftp://192.168.50.100/SSD1/Picture/";
+    QString username = "microsoft";
+    QString password = "19980714Lq";
+
+    std::vector<std::string> list;
+    CurlFtp::listFiles(url.toStdString(), username.toStdString(), password.toStdString(), list);
+    
+    for(const std::string& filename : list)
+    {
+        SPDLOG_INFO("{}", filename);
+    }
+
+    FMTLOG_INFO("***** Curl Library *****");
+    FMTLOG_INFO("{}","你好");
+}
+
+/* 使用Windows API进行编码转换 */
+char* G2U(const char* gb2312)
+{
+    int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
+    wchar_t* wstr = new wchar_t[len+1];
+    memset(wstr, 0, len+1);
+    MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
+    len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
+    char* str = new char[len+1];
+    memset(str, 0, len+1);
+    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
+    if(wstr) delete[] wstr;
+    return str;
+}
+
+void Widget::on_pBtn_downloadFile_clicked()
+{
+    SPDLOG_INFO("点击了“下载文件”");
+    std::vector<std::string> fileList;
+    m_curlFtp.getFileList("/SSD1/Picture/", fileList);
+}
+
+void Widget::on_pBtn_downloadVideo_clicked()
+{
+    SPDLOG_INFO("点击了“下载视频”");
+    std::shared_ptr<QtFtp> ftp = std::make_shared<QtFtp>();
+    ftp->setHostAndPort("192.168.50.100");
+    ftp->setUserPasswd("microsoft", "19980714Lq");
+    QString localPath = QApplication::applicationDirPath() + "/v1.mp4";
+    ftp->getFile(localPath, "/SSD1/Video/v1.mp4");
+    ftp->waitFinished(-1);
+    if(ftp->getResult())
+    {
+        SPDLOG_INFO("下载成功");
+    }
+    else
+    {
+        SPDLOG_INFO("下载失败");
+    }
+}
+

+ 32 - 0
demo/ftp/widget.h

@@ -0,0 +1,32 @@
+#ifndef WIDGET_H
+#define WIDGET_H
+
+#include <QWidget>
+#include "CurlFtp.h"
+
+QT_BEGIN_NAMESPACE
+namespace Ui { class Widget; }
+QT_END_NAMESPACE
+
+class Widget : public QWidget
+{
+    Q_OBJECT
+
+public:
+    Widget(QWidget *parent = nullptr);
+    ~Widget();
+
+private slots:
+
+    void on_pBtn_connect_clicked();
+
+    void on_pBtn_downloadFile_clicked();
+
+    void on_pBtn_downloadVideo_clicked();
+
+private:
+    Ui::Widget *ui;
+
+    CurlFtp m_curlFtp;
+};
+#endif // WIDGET_H

+ 55 - 0
demo/ftp/widget.ui

@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Widget</class>
+ <widget class="QWidget" name="Widget">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>800</width>
+    <height>600</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Widget</string>
+  </property>
+  <widget class="QWidget" name="widget_pBtn" native="true">
+   <property name="geometry">
+    <rect>
+     <x>30</x>
+     <y>30</y>
+     <width>681</width>
+     <height>311</height>
+    </rect>
+   </property>
+   <layout class="QGridLayout" name="gridLayout">
+    <item row="0" column="1">
+     <widget class="QPushButton" name="pBtn_downloadFile">
+      <property name="text">
+       <string>下载文件</string>
+      </property>
+     </widget>
+    </item>
+    <item row="0" column="0">
+     <widget class="QPushButton" name="pBtn_connect">
+      <property name="text">
+       <string>连接</string>
+      </property>
+     </widget>
+    </item>
+    <item row="2" column="0" colspan="2">
+     <widget class="QLineEdit" name="lineEdit"/>
+    </item>
+    <item row="1" column="0">
+     <widget class="QPushButton" name="pBtn_downloadVideo">
+      <property name="text">
+       <string>下载视频</string>
+      </property>
+     </widget>
+    </item>
+   </layout>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>