-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[功能重构]: 重构 HTTP 客户端和 TCP 客户端相关代码,优化结构与功能
- 重构 `HttpClient` 类,引入 PIMPL 模式,隐藏实现细节,便于维护和扩展 - 将私有成员变量和相关函数封装到 `HttpClientPrivate` 类中 - 优化 HTTP 请求相关函数的实现,如 `get`、`post`、`put` 等 - 新增 `download`、`upload_put`、`upload_post` 等函数,支持文件下载和上传 - 重构 `HttpClientAsync` 类,引入 PIMPL 模式,优化异步请求处理逻辑 - 封装异步请求相关数据和函数到 `HttpClientAsyncPrivate` 类 - 优化异步请求的发起、处理和回调机制 - 新增异步文件下载和上传相关功能 - 新增 `file_utils.hpp` 和 `file_utils.cc` 文件,提供文件操作相关工具函数 - `createFile`:创建并写入文件 - `removeFile`:删除文件 - `assertFileData`:断言文件内容 - `formatBytes`:格式化字节大小 - 修改 `CMakeLists.txt` 文件,调整目标链接库和源文件列表,适应代码重构后的变化 - 更新相关测试文件,如 `httpclient_test.cc`、`httpclient_async_test.cc`、`httpclient_file_test.cc`、`httpclient_async_file_test.cc` 等,以适配重构后的类接口和功能 - 重构 `TcpClient` 类,引入 PIMPL 模式,优化 TCP 客户端功能实现 - 封装私有成员到 `TcpClientPrivate` 类,简化类结构 - 优化连接、发送、接收等函数的实现,提高代码可读性和可维护性
- Loading branch information
Showing
13 changed files
with
1,217 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,32 @@ | ||
add_executable(tcpclient_test tcpclient_test.cc tcpclient.cc tcpclient.hpp) | ||
target_link_libraries(tcpclient_test PRIVATE CURL::libcurl) | ||
target_link_libraries( | ||
tcpclient_test PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main | ||
GTest::gmock GTest::gmock_main) | ||
|
||
add_executable(httpclient_test httpclient_test.cc httpclient.cc httpclient.hpp) | ||
target_link_libraries(httpclient_test PRIVATE CURL::libcurl) | ||
set(HttpClientSource file_utils.cc file_utils.hpp httpclient.cc httpclient.hpp) | ||
|
||
add_executable(httpclient_async_test httpclient_async_test.cc | ||
httpclient_async.cc httpclient_async.hpp) | ||
target_link_libraries(httpclient_async_test PRIVATE CURL::libcurl) | ||
add_executable(httpclient_test ${HttpClientSource} httpclient_test.cc) | ||
target_link_libraries( | ||
httpclient_test PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main | ||
GTest::gmock GTest::gmock_main) | ||
|
||
add_executable(httpclient_file_test ${HttpClientSource} httpclient_file_test.cc) | ||
target_link_libraries( | ||
httpclient_file_test PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main | ||
GTest::gmock GTest::gmock_main) | ||
|
||
set(HttpClientAsyncSource file_utils.cc file_utils.hpp httpclient_async.cc | ||
httpclient_async.hpp) | ||
|
||
add_executable(httpclient_async_test ${HttpClientAsyncSource} | ||
httpclient_async_test.cc) | ||
target_link_libraries( | ||
httpclient_async_test PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main | ||
GTest::gmock GTest::gmock_main) | ||
|
||
add_executable(httpclient_async_file_test ${HttpClientAsyncSource} | ||
httpclient_async_file_test.cc) | ||
target_link_libraries( | ||
httpclient_async_file_test | ||
PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main GTest::gmock | ||
GTest::gmock_main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "file_utils.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <filesystem> | ||
#include <fstream> | ||
#include <iostream> | ||
|
||
void createFile(const std::string &filename, const std::string &data) | ||
{ | ||
auto filepath = std::filesystem::current_path() / filename; | ||
std::filesystem::create_directories(filepath.parent_path()); | ||
std::ofstream file(filepath); | ||
if (!file.is_open()) { | ||
std::cerr << "Cannot open the file: " << filepath << std::endl; | ||
return; | ||
} | ||
file << data; | ||
file.close(); | ||
} | ||
|
||
void removeFile(const std::string &filename) | ||
{ | ||
auto filepath = std::filesystem::current_path() / filename; | ||
if (std::filesystem::exists(filepath)) { | ||
std::filesystem::remove(filepath); | ||
} | ||
} | ||
|
||
void assertFileData(const std::string &filename, const std::string &data) | ||
{ | ||
auto filepath = std::filesystem::current_path() / filename; | ||
std::ifstream file(filepath); | ||
if (!file.is_open()) { | ||
std::cerr << "Cannot open the file: " << filepath << std::endl; | ||
return; | ||
} | ||
std::string fileData((std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>())); | ||
file.close(); | ||
EXPECT_EQ(fileData, data); | ||
} | ||
|
||
auto formatBytes(double value, int precision) -> std::string | ||
{ | ||
std::vector<std::string> units = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; | ||
|
||
int i = 0; | ||
while (value > 1024) { | ||
value /= 1024; | ||
i++; | ||
} | ||
|
||
std::ostringstream out; | ||
out.precision(precision); | ||
out << std::fixed << value; | ||
|
||
return out.str() + " " + units[i]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
void createFile(const std::string &filename, const std::string &data); | ||
|
||
void removeFile(const std::string &filename); | ||
|
||
void assertFileData(const std::string &filename, const std::string &data); | ||
|
||
auto formatBytes(double value, int precision = 2) -> std::string; |
Oops, something went wrong.