From 75f7e8260ebe802aed8d3af364d758143c005c8c Mon Sep 17 00:00:00 2001 From: lhl <1281544429@qq.com> Date: 星期四, 07 十一月 2024 11:29:31 +0800 Subject: [PATCH] Merge branch 'master' of ssh://115.28.86.8:29418/~admin/昆仑_1024 --- Server/王琨元/code/MysqlConn.h | 41 ++-- Client/龚启祥/log/日志模板_龚启祥_1106.doc | 0 Client/申烜/log/日志_申烜_1106.doc | 0 Server/王琨元/log/日志模板_王琨元_1103 .doc | 0 kunlun.sql | 1 Client/李宵增/log/历史查询_李宵增_1104.doc | 0 Server/王琨元/log/日志模板_王琨元_1101.doc | 0 Server/王琨元/log/日志模板_王琨元_1104.doc | 0 Server/马渝杭/log/日志_马渝杭_20241106.doc | 0 Server/王琨元/code/ConnectionPool.h | 58 ++--- Client/宋昊昳/log/日志_宋昊昳_1106.doc | 0 Client/郑凯文/log/郑凯文1106.doc | 0 Server/王琨元/code/ConnectionPool.cpp | 182 ++++++++----------- /dev/null | 0 Server/王琨元/code/MysqlConn.cpp | 133 ++++++++++---- Client/李宵增/log/历史查询_李宵增_1105.doc | 0 Client/朱航/log/朱航1105.doc | 0 Server/马渝杭/log/20241106昆仑日报.doc | 0 common.h | 124 +++++++++---- Client/王鑫鑫/log/日志_王鑫鑫_1106.doc | 0 Server/王琨元/log/日志模板_王琨元_1105.doc | 0 21 files changed, 308 insertions(+), 231 deletions(-) diff --git "a/Client/\345\256\213\346\230\212\346\230\263/log/\346\227\245\345\277\227_\345\256\213\346\230\212\346\230\263_1106.doc" "b/Client/\345\256\213\346\230\212\346\230\263/log/\346\227\245\345\277\227_\345\256\213\346\230\212\346\230\263_1106.doc" new file mode 100644 index 0000000..1f49258 --- /dev/null +++ "b/Client/\345\256\213\346\230\212\346\230\263/log/\346\227\245\345\277\227_\345\256\213\346\230\212\346\230\263_1106.doc" Binary files differ diff --git "a/Client/\346\234\261\350\210\252/log/\346\234\261\350\210\2521105.doc" "b/Client/\346\234\261\350\210\252/log/\346\234\261\350\210\2521105.doc" new file mode 100644 index 0000000..1d30c29 --- /dev/null +++ "b/Client/\346\234\261\350\210\252/log/\346\234\261\350\210\2521105.doc" Binary files differ diff --git "a/Client/\346\235\216\345\256\265\345\242\236/log/\345\216\206\345\217\262\346\237\245\350\257\242_\346\235\216\345\256\265\345\242\236_1102.doc" "b/Client/\346\235\216\345\256\265\345\242\236/log/\345\216\206\345\217\262\346\237\245\350\257\242_\346\235\216\345\256\265\345\242\236_1104.doc" similarity index 100% rename from "Client/\346\235\216\345\256\265\345\242\236/log/\345\216\206\345\217\262\346\237\245\350\257\242_\346\235\216\345\256\265\345\242\236_1102.doc" rename to "Client/\346\235\216\345\256\265\345\242\236/log/\345\216\206\345\217\262\346\237\245\350\257\242_\346\235\216\345\256\265\345\242\236_1104.doc" Binary files differ diff --git "a/Client/\346\235\216\345\256\265\345\242\236/log/\345\216\206\345\217\262\346\237\245\350\257\242_\346\235\216\345\256\265\345\242\236_1105.doc" "b/Client/\346\235\216\345\256\265\345\242\236/log/\345\216\206\345\217\262\346\237\245\350\257\242_\346\235\216\345\256\265\345\242\236_1105.doc" new file mode 100644 index 0000000..52d368c --- /dev/null +++ "b/Client/\346\235\216\345\256\265\345\242\236/log/\345\216\206\345\217\262\346\237\245\350\257\242_\346\235\216\345\256\265\345\242\236_1105.doc" Binary files differ diff --git "a/Client/\347\216\213\351\221\253\351\221\253/log/\346\227\245\345\277\227_\347\216\213\351\221\253\351\221\253_1106.doc" "b/Client/\347\216\213\351\221\253\351\221\253/log/\346\227\245\345\277\227_\347\216\213\351\221\253\351\221\253_1106.doc" new file mode 100644 index 0000000..0c5f5e7 --- /dev/null +++ "b/Client/\347\216\213\351\221\253\351\221\253/log/\346\227\245\345\277\227_\347\216\213\351\221\253\351\221\253_1106.doc" Binary files differ diff --git "a/Client/\347\224\263\347\203\234/log/\346\227\245\345\277\227_\347\224\263\347\203\234_1106.doc" "b/Client/\347\224\263\347\203\234/log/\346\227\245\345\277\227_\347\224\263\347\203\234_1106.doc" new file mode 100644 index 0000000..4738a32 --- /dev/null +++ "b/Client/\347\224\263\347\203\234/log/\346\227\245\345\277\227_\347\224\263\347\203\234_1106.doc" Binary files differ diff --git "a/Client/\351\203\221\345\207\257\346\226\207/log/\351\203\221\345\207\257\346\226\2071106.doc" "b/Client/\351\203\221\345\207\257\346\226\207/log/\351\203\221\345\207\257\346\226\2071106.doc" new file mode 100644 index 0000000..ee4c004 --- /dev/null +++ "b/Client/\351\203\221\345\207\257\346\226\207/log/\351\203\221\345\207\257\346\226\2071106.doc" Binary files differ diff --git "a/Client/\351\276\232\345\220\257\347\245\245/log/\346\227\245\345\277\227\346\250\241\346\235\277_\351\276\232\345\220\257\347\245\245_1106.doc" "b/Client/\351\276\232\345\220\257\347\245\245/log/\346\227\245\345\277\227\346\250\241\346\235\277_\351\276\232\345\220\257\347\245\245_1106.doc" new file mode 100644 index 0000000..3341269 --- /dev/null +++ "b/Client/\351\276\232\345\220\257\347\245\245/log/\346\227\245\345\277\227\346\250\241\346\235\277_\351\276\232\345\220\257\347\245\245_1106.doc" Binary files differ diff --git "a/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.cpp" "b/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.cpp" index ce7acb2..2fff64c 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.cpp" +++ "b/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.cpp" @@ -1,125 +1,101 @@ #include "stdafx.h" #include "ConnectionPool.h" -#include <fstream> -#include <iostream> -#include <thread> - -ConnectionPool* ConnectionPool::getInstance() { - static ConnectionPool connPool; - return &connPool; -} ConnectionPool::ConnectionPool() { - // 加载配置文件 - if (!parseJsonFile()) { - std::cout << "parseJsonFile is failed!" << std::endl; - return; - } - for ( int i = 0 ; i < m_min_conn ; i++ ) { - addConnection( ); - } - // 创建一个线程,对连接数进行监控 ,连接数不足就再继续创建 - std::thread producer(&ConnectionPool::produce, this); - // 对连接数进行监控 ,如果有太多空闲得线程 ,那么就对其进行销毁 - std::thread recycler( &ConnectionPool::recycle, this); - // 线程分离 - producer.detach(); - recycler.detach(); + + m_min_conn = 5; // 假设最小连接数为 5 + m_max_conn = 10; // 假设最大连接数为 10 + m_timeout = 1000; // 假设连接超时时间为 1000 毫秒 + max_del_time = 60000; // 假设最大空闲时间为 60000 毫秒 + + for (int i = 0; i < m_min_conn; i++) { + addConnection(); + } + // 创建一个线程,对连接数进行监控 ,连接数不足就再继续创建 + thread producer(&ConnectionPool::produce, this); + // 对连接数进行监控 ,如果有太多空闲得线程 ,那么就对其进行销毁 + thread recycler(&ConnectionPool::recycle, this); + + // 线程分离 + producer.detach(); + recycler.detach(); } -bool ConnectionPool::parseJsonFile() { - - /*std::ifstream ifs("dbconf.json"); - Reader rd; - Value root; - rd.parse(ifs, root); - if ( root.isObject( ) ) { - m_ip = root["ip"].asString(); - m_port = root["port"].asInt(); - m_userName = root["userName"].asString(); - m_passwd = root["password"].asString(); - m_db = root["dbName"].asString(); - m_min_conn = root["minSize"].asInt(); - m_max_conn = root["maxSize"].asInt(); - max_del_time = root["maxDleTime"].asInt(); - m_timeout = root["timeout"].asInt(); - return true; - }*/ - return false; -} - -void ConnectionPool::description() { - std::cout << m_ip << ". " << m_userName << ". " << m_passwd << ". " << m_db << ". " - << m_port << ". " << m_max_conn << ". " << m_min_conn << ". " << m_timeout << ". " - << max_del_time << std::endl; -} ConnectionPool::~ConnectionPool() { - while (!m_connkQueue.empty()) { - MysqlConn* conn = m_connkQueue.front(); - m_connkQueue.pop(); - delete conn; - } - + while (!m_connkQueue.empty()) { + MysqlConn* conn = m_connkQueue.front(); + m_connkQueue.pop(); + delete conn; + } } void ConnectionPool::addConnection() { - MysqlConn* conn = new MysqlConn(); - if ( !conn->connect( m_ip, m_userName, m_passwd, m_db, m_port ) ) { - std::cout << "ConnectionPool connect to mysql is failed!" << std::endl; - delete conn; - return; - } - conn->refreshActiveTime( ); - m_connkQueue.push(conn); - m_cond.notify_one(); // 唤醒一个线程 + MysqlConn* conn = new MysqlConn(); + if (!conn->isConnected()) { + cout << "ConnectionPool connect to mysql is failed!" << endl; + delete conn; + return; + } + conn->refreshActiveTime(); + m_connkQueue.push(conn); + m_cond.notify_one(); // 唤醒一个线程 } -void ConnectionPool::produce(){ - while (true) { - std::unique_lock<std::mutex> lock(m_mutex); - while (m_connkQueue.size() >= m_min_conn) { // 连接队列的数量大于最小的连接数 - m_cond.wait(lock); - } - addConnection(); - } +void ConnectionPool::produce() { + while (true) { + unique_lock<mutex> lock(m_mutex); + while (m_connkQueue.size() >= m_min_conn) { // 连接队列的数量大于最小的连接数 + m_cond.wait(lock); + } + addConnection(); + } } // 删除空闲连接 void ConnectionPool::recycle() { - while ( true ) { - std::this_thread::sleep_for( std::chrono::milliseconds(500)); // 休眠500 ms - std::unique_lock<std::mutex>lock(m_mutex); - while ( m_connkQueue.size() > m_min_conn ) { - MysqlConn* conn = m_connkQueue.front( ); - if (conn->getActiveTime() >= max_del_time) { - m_connkQueue.pop(); - delete conn; - } - else { - break; - } - } - } + while (true) { + this_thread::sleep_for(chrono::milliseconds(500)); // 休眠 500 ms + unique_lock<mutex> lock(m_mutex); + while (m_connkQueue.size() > m_min_conn) { + MysqlConn* conn = m_connkQueue.front(); + if (conn->getActiveTime() >= max_del_time) { + m_connkQueue.pop(); + delete conn; + } + else { + break; + } + } + } } -std::shared_ptr<MysqlConn> ConnectionPool::getMysqlConn() { - - std::unique_lock<std::mutex>lock(m_mutex); - while ( m_connkQueue.empty()) { - // 如果等待一段时间后,队列还是为空,返回一个 null - if ( std::cv_status::timeout == m_cond.wait_for(lock, std::chrono::milliseconds(m_timeout)) ) { - if( m_connkQueue.empty( ) ) return nullptr; - } - } - std::shared_ptr<MysqlConn> connPtr(std::move(m_connkQueue.front()), [this](MysqlConn* conn) { - std::unique_lock <std::mutex>lock(m_mutex) ; - conn->refreshActiveTime(); - m_connkQueue.push(conn); - }); - m_connkQueue.pop(); - m_cond.notify_one(); // 唤醒阻塞的生产者线程,开始生产 - return connPtr; +shared_ptr<MysqlConn> ConnectionPool::getMysqlConn() { + unique_lock<mutex> lock(m_mutex); + while (m_connkQueue.empty()) { + // 如果等待一段时间后,队列还是为空,返回一个 null + if (cv_status::timeout == m_cond.wait_for(lock, chrono::milliseconds(m_timeout))) { + if (m_connkQueue.empty()) return nullptr; + } + } + shared_ptr<MysqlConn> connPtr(move(m_connkQueue.front()), [this](MysqlConn* conn) { + unique_lock<mutex> lock(m_mutex); + conn->refreshActiveTime(); + m_connkQueue.push(conn); + }); + m_connkQueue.pop(); + m_cond.notify_one(); // 唤醒阻塞的生产者线程,开始生产 + return connPtr; +} +void ConnectionPool::description() { + cout << m_ip << ". " << m_userName << ". " << m_passwd << ". " << m_db << ". " + << m_port << ". " << m_max_conn << ". " << m_min_conn << ". " << m_timeout << ". " + << max_del_time << endl; +} + +ConnectionPool* ConnectionPool::getInstance() { + static ConnectionPool connPool; + return &connPool; } \ No newline at end of file diff --git "a/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.h" "b/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.h" index bce6a07..c78a46e 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.h" +++ "b/Server/\347\216\213\347\220\250\345\205\203/code/ConnectionPool.h" @@ -1,50 +1,44 @@ #pragma once -#include <queue> -#include <mutex> -#include <condition_variable> -//#include <json/json.h> - #include "MysqlConn.h" - -//using namespace Json; class ConnectionPool { public: - static ConnectionPool* getInstance( ); + static ConnectionPool* getInstance(); - std::shared_ptr<MysqlConn> getMysqlConn(); // 从数据库连接池获取连接 + shared_ptr<MysqlConn> getMysqlConn(); // 从数据库连接池获取连接 - ConnectionPool(const ConnectionPool& other) = delete; - ConnectionPool& operator = (const ConnectionPool & other ) = delete; - - ~ConnectionPool(); + ConnectionPool(const ConnectionPool& other) = delete; + ConnectionPool& operator=(const ConnectionPool& other) = delete; - void description(); + ~ConnectionPool(); + + void description(); protected: - ConnectionPool( ); // 构造函数 + ConnectionPool(); // 构造函数 private: - bool parseJsonFile(); // 解析配置 + // 不再需要解析配置文件的函数 + // bool parseJsonFile(); // 解析配置 - void produce(); - void recycle(); - void addConnection(); // 增加连接数 + void produce(); + void recycle(); + void addConnection(); // 增加连接数 - std::string m_ip; - std::string m_userName; - std::string m_passwd; - std::string m_db; - unsigned short m_port; + // 写死的数据库连接参数 + string m_ip = "127.0.0.1"; + string m_userName = "root"; + string m_passwd = "123456"; + string m_db = "mayi_kunlun"; + unsigned short m_port = 3306; - int m_max_conn; - int m_min_conn; + int m_max_conn; + int m_min_conn; - int m_timeout; // 连接超时时间 - int max_del_time; // 最大删除时间( 连接空闲时间超过这个,就给当前连接关闭 ) - std::queue<MysqlConn*>m_connkQueue ; // 连接队列 - std::mutex m_mutex; // 互斥锁 - std::condition_variable m_cond; // 条件变量 + int m_timeout; // 连接超时时间 + int max_del_time; // 最大删除时间( 连接空闲时间超过这个,就给当前连接关闭 ) + queue<MysqlConn*> m_connkQueue; // 连接队列 + mutex m_mutex; // 互斥锁 + condition_variable m_cond; // 条件变量 }; - diff --git "a/Server/\347\216\213\347\220\250\345\205\203/code/MysqlConn.cpp" "b/Server/\347\216\213\347\220\250\345\205\203/code/MysqlConn.cpp" index 5725af8..071cf5f 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/code/MysqlConn.cpp" +++ "b/Server/\347\216\213\347\220\250\345\205\203/code/MysqlConn.cpp" @@ -1,37 +1,60 @@ #include "stdafx.h" #include "MysqlConn.h" -#include <regex> + // 初始化连接 +// 初始化连接 MysqlConn::MysqlConn() { - mysql_ = mysql_init(mysql_); - // 设置字符集 - if (mysql_) mysql_set_character_set(mysql_, "gbk"); -} - -// 连接数据库 -bool MysqlConn::connect(std::string ip, std::string userName, std::string passwd, std::string db, int port) { - mysql_ = mysql_real_connect(mysql_, ip.c_str(), userName.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0); - if (!mysql_) { - return false; + m_mysql = mysql_init(nullptr); + if (!m_mysql) { + cout << "Failed to initialize MySQL." << endl; + return; } - return true; + // 设置字符集 + if (m_mysql) mysql_set_character_set(m_mysql, "gbk"); + // 使用写死的参数连接数据库 + m_mysql = mysql_real_connect(m_mysql, "127.0.0.1", "root", "123456", "mayi_kunlun", 3306, nullptr, 0); + if (!m_mysql) { + cout << "MysqlConn connect to mysql is failed!" << endl; + // 确保在连接失败时正确清理资源 + mysql_close(m_mysql); + m_mysql = nullptr; + } + // 初始化 row_ 和 res_ + m_row = nullptr; + m_res = nullptr; } +bool MysqlConn::isConnected() { + return m_mysql != nullptr; +} +// 连接数据库 +//bool MysqlConn::connect() { +// string ip = "127.0.0.1"; +// string userName = "root"; +// string passwd = "123456"; +// string db = "mayi_kunlun"; +// int port = 3306; +// m_mysql = mysql_real_connect(mysql_, ip.c_str(), userName.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0); +// if (!m_mysql) { +// return false; +// } +// return true; +//} // 释放资源 MysqlConn::~MysqlConn() { - if (mysql_) { - mysql_close(mysql_); - mysql_ = nullptr; + if (m_mysql) { + mysql_close(m_mysql); + m_mysql = nullptr; } freeRes(); } // 更新数据 -bool MysqlConn::update(std::string sql) { +bool MysqlConn::update(string sql) { // 参数化查询优化后的安全检查 - MYSQL_STMT* stmt = mysql_stmt_init(mysql_); + MYSQL_STMT* stmt = mysql_stmt_init(m_mysql); if (!stmt) { return false; } @@ -53,11 +76,36 @@ return true; } +bool MysqlConn::query(string sql, vector<vector<string>>& result) +{ + freeRes(); + int ret = mysql_query(m_mysql, sql.c_str()); + if (ret != 0) { + fprintf(stderr, "Query failed: %s\n", mysql_error(m_mysql)); + return false; + } + // 获取查询结果 + m_res = mysql_store_result(m_mysql); + if (!m_res) return false; + //将结果集转到二维数组result里面 + //int col_cnt = m_mysql->field_count; + int col_cnt = mysql_field_count(m_mysql); + cout << col_cnt << endl; + while (m_row = mysql_fetch_row(m_res)) { + vector<string> tmp; + for (int i = 0; i < col_cnt; i++) { + tmp.push_back(m_row[i]); + } + result.push_back(tmp); + } + return true; +} + // 查询数据库 -bool MysqlConn::query(std::string sql) { +bool MysqlConn::query(string sql) { freeRes(); // 参数化查询优化后的安全检查 - MYSQL_STMT* stmt = mysql_stmt_init(mysql_); + MYSQL_STMT* stmt = mysql_stmt_init(m_mysql); if (!stmt) { return false; } @@ -75,8 +123,8 @@ mysql_stmt_close(stmt); return false; } - res_ = mysql_stmt_result_metadata(stmt); - if (!res_) { + m_res = mysql_stmt_result_metadata(stmt); + if (!m_row) { mysql_stmt_close(stmt); return false; } @@ -86,59 +134,60 @@ // 得到结果集 bool MysqlConn::getResult() { - if (res_) { - row_ = mysql_fetch_row(res_); - if (row_) return true; + if (m_row) { + m_row = mysql_fetch_row(m_res); + if (m_row) return true; } return false; } // 获取结果集的字段 -std::string MysqlConn::getField(int index) { - int cols = mysql_num_fields(res_); - if (index >= cols || index < 0) return std::string(""); +string MysqlConn::getField(int index) { + int cols = mysql_num_fields(m_res); + if (!m_res || index >= cols || index < 0) return string(""); - char* value = row_[index]; - unsigned long len = mysql_fetch_lengths(res_)[index]; - return std::string(value, len); + char* value = m_row[index]; + // 使用显式类型转换 + unsigned long len = static_cast<unsigned long>(mysql_fetch_lengths(m_res)[index]); + return string(value, len); } // 事务操作 bool MysqlConn::transaction() { - return mysql_autocommit(mysql_, false); + return mysql_autocommit(m_mysql, false); } // 提交事务 bool MysqlConn::commit() { - return mysql_commit(mysql_); + return mysql_commit(m_mysql); } // 事务回滚 bool MysqlConn::rollback() { - return mysql_rollback(mysql_); + return mysql_rollback(m_mysql); } void MysqlConn::refreshActiveTime() { - activeTime_ = std::chrono::steady_clock::now(); + m_activeTime = chrono::steady_clock::now(); } long long MysqlConn::getActiveTime() { // 纳米 - std::chrono::nanoseconds nased = std::chrono::steady_clock::now() - activeTime_; + chrono::nanoseconds nased = chrono::steady_clock::now() - m_activeTime; // 转换成毫米 - std::chrono::microseconds millsed = std::chrono::duration_cast<std::chrono::microseconds>(nased); + chrono::microseconds millsed = chrono::duration_cast<chrono::microseconds>(nased); return millsed.count(); // 多少毫秒 } // 安全校验实现,这里简单使用正则表达式判断是否包含危险字符 -bool MysqlConn::isSqlSafe(const std::string& sql) { - std::regex dangerousPattern(".*(['\";\\-+=]).*"); - return!std::regex_search(sql, dangerousPattern); +bool MysqlConn::isSqlSafe(const string& sql) { + regex dangerousPattern(".*(['\";\\-+=]).*"); + return!regex_search(sql, dangerousPattern); } void MysqlConn::freeRes() { - if (res_) { - mysql_free_result(res_); - res_ = nullptr; + if (m_res) { + mysql_free_result(m_res); + m_res = nullptr; } } \ No newline at end of file diff --git "a/Server/\347\216\213\347\220\250\345\205\203/code/MysqlConn.h" "b/Server/\347\216\213\347\220\250\345\205\203/code/MysqlConn.h" index 00c9917..a6506d1 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/code/MysqlConn.h" +++ "b/Server/\347\216\213\347\220\250\345\205\203/code/MysqlConn.h" @@ -2,48 +2,53 @@ #include <string> #include <WinSock2.h> -//#include <mysql.h> #include "MySQL/include/mysql.h" #include <string.h> #include <chrono> - +#include <iostream> +#include <memory> +#include <queue> +#include <mutex> +#include <condition_variable> +#include <regex> #pragma comment(lib,"./MySQL/lib/libmysql.lib") +using namespace std; class MysqlConn { public: // 初始化连接 MysqlConn(); // 连接数据库 - bool connect(std::string ip, std::string userName, std::string passwd, std::string db , int port = 3306); + //bool connect(); // 释放资源 - ~MysqlConn( ); + ~MysqlConn(); // 更新数据库 ( insert update delete ) - bool update(std::string sql); + bool update(string sql); // 查询数据库 - bool query(std::string sql); + bool query(string sql, vector<vector<string>>& result); + bool query(string sql); // 得到结果集 bool getResult(); // 获取结果集的字段 - std::string getField(int index); + string getField(int index); // 事务操作 bool transaction(); // 提交事务 - bool commit( ); + bool commit(); // 事务回滚 bool rollback(); - void refreshActiveTime( ); // 刷新活跃时间 + + void refreshActiveTime(); // 刷新活跃时间 long long getActiveTime(); // 获取当前活跃的时间间隔 // 安全校验接口 - bool isSqlSafe(const std::string& sql); - -private: - + bool isSqlSafe(const string& sql); + bool isConnected(); +protected: void freeRes(); - MYSQL* mysql_ = NULL ; - MYSQL_RES* res_ = NULL; - MYSQL_ROW row_ = nullptr; - std::chrono::steady_clock::time_point activeTime_; + MYSQL* m_mysql; + MYSQL_RES* m_res; + MYSQL_ROW m_row; + chrono::steady_clock::time_point m_activeTime; }; - diff --git "a/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1101.doc" "b/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1101.doc" index cbc2406..7b25abd 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1101.doc" +++ "b/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1101.doc" Binary files differ diff --git "a/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1103 .doc" "b/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1103 .doc" index 9461d22..02c7ceb 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1103 .doc" +++ "b/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1103 .doc" Binary files differ diff --git "a/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1104.doc" "b/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1104.doc" index 8bda98e..f61ee94 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1104.doc" +++ "b/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1104.doc" Binary files differ diff --git "a/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1105.doc" "b/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1105.doc" new file mode 100644 index 0000000..53afd59 --- /dev/null +++ "b/Server/\347\216\213\347\220\250\345\205\203/log/\346\227\245\345\277\227\346\250\241\346\235\277_\347\216\213\347\220\250\345\205\203_1105.doc" Binary files differ diff --git "a/Server/\351\251\254\346\270\235\346\235\255/log/20241106\346\230\206\344\273\221\346\227\245\346\212\245.doc" "b/Server/\351\251\254\346\270\235\346\235\255/log/20241106\346\230\206\344\273\221\346\227\245\346\212\245.doc" new file mode 100644 index 0000000..38b1180 --- /dev/null +++ "b/Server/\351\251\254\346\270\235\346\235\255/log/20241106\346\230\206\344\273\221\346\227\245\346\212\245.doc" Binary files differ diff --git "a/Server/\351\251\254\346\270\235\346\235\255/log/\346\227\245\345\277\227_\351\251\254\346\270\235\346\235\255_20241106.doc" "b/Server/\351\251\254\346\270\235\346\235\255/log/\346\227\245\345\277\227_\351\251\254\346\270\235\346\235\255_20241106.doc" new file mode 100644 index 0000000..02219c4 --- /dev/null +++ "b/Server/\351\251\254\346\270\235\346\235\255/log/\346\227\245\345\277\227_\351\251\254\346\270\235\346\235\255_20241106.doc" Binary files differ diff --git a/common.h b/common.h index 8729396..7eb4357 100644 --- a/common.h +++ b/common.h @@ -50,7 +50,11 @@ //璀︽姤绠$悊 WARNING_REQ, // 璀︽姤璇锋眰 - WARNING_RES, // 璀︽姤鍝嶅簲 + WARNING_RES, // 璀︽姤鍝嶅簲 + DATA_REQ, // 鏁版嵁璇锋眰 + DATA_RES, // 鏁版嵁鍝嶅簲 + THRESHOLD_REQ, //闃堝�艰姹� + THRESHOLD_RES, //闃堝�煎搷搴� //鐢熶骇璁″垝绠$悊 @@ -575,51 +579,99 @@ }; //璀︽姤绠$悊 -// 璀︽姤璇锋眰缁撴瀯浣� +struct warningInfo { + char device_name[32];//璁惧鍚嶇О + char des[32];//璀︽姤鎻忚堪 + char time[32];//璀︽姤鏃堕棿 + float warningdata;//璀︽姤鏁版嵁 + char type[16];//璀︽姤绫诲瀷 + char status[16];//璀︽姤鐘舵�� +}; + +struct dataInfo { + char device_name[32]; + char type[16];//鐜鏁版嵁绫诲瀷 + float data; + char time[32];//涓婁紶鏁版嵁鏃堕棿 +}; + +struct threshInfo { + char device_name[32]; + char type[16];//鐜绫诲瀷 + float min;//鏈�灏忛槇鍊� + float max;//鏈�澶ч槇鍊� + char time[32];//闃堝�间慨鏀规椂闂� +}; + +//璀︽姤璇锋眰缁撴瀯浣� struct WarningReq { - Head head; + Head head; + warningInfo info[0]; - float oxygenWarning; - float carbonWarning; - float tempWarning; - float humidityWarning; + WarningReq() { + head.type = WARNING_REQ; + head.len = sizeof(WarningReq); + } +}; - WarningReq() { - head.type = WARNING_REQ; - head.len = sizeof(WarningReq); - } +//鏁版嵁璇锋眰缁撴瀯浣� +struct DataReq { + Head head; + dataInfo Info[0]; + + DataReq() { + head.type = DATA_REQ; + head.len = sizeof(DataReq); + } +}; + +//闃堝�艰姹傜粨鏋勪綋 +struct ThresholdReq { + Head head; + threshInfo threInfo[0]; + + ThresholdReq() { + head.type = THRESHOLD_REQ; + head.len = sizeof(ThresholdReq); + } }; -struct DataThreshold { - //闃堝�� - float oxygenThreshold; - float carbonThreshold; - float tempThreshold; - float humidityThreshold; -}; - -struct Data { - float oxygen; // 姘ф皵娴撳害 - float carbon; // 涓�姘у寲纰虫祿搴� - float temp; // 娓╁害 - float humidity; // 婀垮害 -}; - -// 璀︽姤鍝嶅簲缁撴瀯浣� +//璀︽姤鍝嶅簲缁撴瀯浣� struct WarningRes { - Head head; - int status; // 鍝嶅簲鐘舵�侊紙姣斿 0 琛ㄧず鎴愬姛锛�1 琛ㄧず澶辫触绛夛級 - const char* message; // 鍝嶅簲娑堟伅鎻忚堪 + Head head; + int status;// 鍝嶅簲鐘舵�� + warningInfo warninginfo[0]; - WarningRes(int stat, const char* msg) { - head.type = WARNING_RES; - head.len = sizeof(WarningRes); - status = stat; - message = msg; - } + WarningRes() { + head.type = WARNING_RES; + head.len = sizeof(WarningRes); + } }; +struct DataRes { + Head head; + int status;// 鍝嶅簲鐘舵�� + dataInfo datainfo[0]; + + DataRes() { + head.type = DATA_RES; + head.len = sizeof(DataRes); + } +}; + +struct ThresholdRes { + Head head; + int status;// 鍝嶅簲鐘舵�� + threshInfo threInfo[0]; + + ThresholdRes() { + head.type = THRESHOLD_RES; + head.len = sizeof(ThresholdRes); + } +}; + + //鐢熶骇璁″垝绠$悊 struct PdplanInfo { diff --git a/kunlun.sql b/kunlun.sql index aa88b37..f3d7846 100644 --- a/kunlun.sql +++ b/kunlun.sql @@ -211,3 +211,4 @@ /*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */; +SELECT id,devices_name,longitude,latitude,devices_status FROM devices_management; \ No newline at end of file diff --git "a/~$\350\241\250.docx" "b/~$\350\241\250.docx" deleted file mode 100644 index 8b245e2..0000000 --- "a/~$\350\241\250.docx" +++ /dev/null Binary files differ -- Gitblit v1.8.0