diff --git a/.clang-format b/.clang-format index 402f22e..36e4997 100644 --- a/.clang-format +++ b/.clang-format @@ -11,7 +11,7 @@ ReflowComments: true SpacesBeforeTrailingComments: 3 TabWidth: 4 ConstructorInitializerAllOnOneLineOrOnePerLine: true -ColumnLimit: 80 +ColumnLimit: 130 AllowShortBlocksOnASingleLine: Never AllowShortFunctionsOnASingleLine: None AllowShortEnumsOnASingleLine: false diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f5e7bbc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ofen"] + path = ofen + url = https://www.sinxmiao.cn/taynpg/ofen diff --git a/.vscode/settings.json b/.vscode/settings.json index 3399912..98a3287 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -31,5 +31,85 @@ "ja": true, "zh-hant": true, "zh-hans": true + }, + "files.associations": { + "memory": "cpp", + "xstring": "cpp", + "algorithm": "cpp", + "any": "cpp", + "array": "cpp", + "atomic": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "codecvt": "cpp", + "complex": "cpp", + "condition_variable": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "deque": "cpp", + "exception": "cpp", + "filesystem": "cpp", + "fstream": "cpp", + "functional": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "mutex": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "shared_mutex": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "xthread": "cpp", + "thread": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "utility": "cpp", + "variant": "cpp", + "vector": "cpp", + "xfacet": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xmemory0": "cpp", + "xstddef": "cpp", + "xtr1common": "cpp", + "xtree": "cpp", + "xutility": "cpp" } } \ No newline at end of file diff --git a/3rd/spdlog/fmt/bundled/base.h b/3rd/spdlog/fmt/bundled/base.h index 6276494..ccf1955 100644 --- a/3rd/spdlog/fmt/bundled/base.h +++ b/3rd/spdlog/fmt/bundled/base.h @@ -288,7 +288,7 @@ #endif #ifndef FMT_UNICODE -# define FMT_UNICODE 1 +# define FMT_UNICODE 0 #endif // Check if rtti is available. diff --git a/CMakeLists.txt b/CMakeLists.txt index fa67d31..ff0af29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(transm LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_DEBUG_POSTFIX "d") message(STATUS "System: ${CMAKE_SYSTEM_NAME}") message(STATUS "Compiler CXX ID: ${CMAKE_CXX_COMPILER_ID}") @@ -11,5 +12,12 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/) include_directories(3rd) +include_directories(.) +add_subdirectory(ofen) +add_subdirectory(net) +add_subdirectory(util) add_subdirectory(server) -add_subdirectory(client) \ No newline at end of file +add_subdirectory(client) + +add_executable(transm_test test.cpp) +target_link_libraries(transm_test PRIVATE trans_net trans_util) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e3115aa --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# transm + +以一个网点作为中转传输文件。 + +# 简要说明 + +- `client`和`server`均为命令行端程序,无GUI。 +- `client`从`server`下载文件,如果本地有重复则覆盖。 +- 工作方式为`client A`端提交待传输的文件列表到`server`,`client B`端从`server`获取有哪些客户机提交的哪些任务,可以从中下载。 \ No newline at end of file diff --git a/client/main.cpp b/client/main.cpp index 74d43e4..1642d5e 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,8 +1,8 @@ #include + int main() { - return 0; } \ No newline at end of file diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 157fa26..0000000 --- a/main.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -int main() -{ - std::cout << "Done" << std::endl; - return 0; -} \ No newline at end of file diff --git a/net/CMakeLists.txt b/net/CMakeLists.txt new file mode 100644 index 0000000..9f240de --- /dev/null +++ b/net/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.16) + +project(trans_net LANGUAGES CXX) + +if (MSVC) + add_definitions(-D_WIN32_WINNT=0x0601) + add_compile_options(/source-charset:utf-8) +endif() + +set(SOURCES + net_base.h net_base.cpp +) +add_library(trans_net STATIC ${SOURCES}) +target_link_libraries(trans_net PUBLIC Ofen trans_util) +target_include_directories(trans_net PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/net/net_base.cpp b/net/net_base.cpp new file mode 100644 index 0000000..7d0c9ce --- /dev/null +++ b/net/net_base.cpp @@ -0,0 +1,69 @@ +#include "net_base.h" + +CServer::CServer() +{ +} + +CServer::~CServer() +{ +} + +CClient::CClient(const std::shared_ptr& logger) : logger_(logger), io_context_(), socket_(io_context_) +{ +} + +CClient::~CClient() +{ +} + +bool CClient::Connect(const std::string& host, const std::string& port) +{ + try { + asio::ip::tcp::resolver resolver(io_context_); + asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(host, port); + asio::connect(socket_, endpoints); + logger_->info("Connected to {}:{}", host, port); + return true; + } catch (const std::exception& ex) { + logger_->error("Connection failed: {}", ex.what()); + return false; + } +} + +void CClient::Disconnect() +{ + if (socket_.is_open()) { + try { + socket_.close(); + logger_->info("Disconnected."); + } catch (const std::exception& ex) { + logger_->error("Error during disconnection: {}", ex.what()); + } + } +} + +bool CClient::Send(const char* data, int len) +{ + try { + auto send_size = asio::write(socket_, asio::buffer(data, len)); + logger_->info("Need Send len: {} Real Send len: {}", len, send_size); + return static_cast(send_size) == len; + } catch (const std::exception& ex) { + logger_->error("Send failed: {}", ex.what()); + return false; + } +} + +std::string CClient::Receive() +{ + try { + std::vector buffer(1024); + size_t length = socket_.read_some(asio::buffer(buffer)); + std::string received_data(buffer.data(), length); + logger_->info("Received data len: {}", length); + return received_data; + } catch (const std::exception& ex) { + logger_->error("Receive failed: {}", ex.what()); + return ""; + } +} diff --git a/net/net_base.h b/net/net_base.h new file mode 100644 index 0000000..e86a549 --- /dev/null +++ b/net/net_base.h @@ -0,0 +1,31 @@ +#pragma once + +#include "util.h" +#include +#include + +using namespace ofen; +class CServer +{ +public: + CServer(); + ~CServer(); +}; + +class CClient +{ +public: + CClient(const std::shared_ptr& logger); + ~CClient(); +public: + bool Connect(const std::string& host, const std::string& port); + void Disconnect(); + bool Send(const char* data, int len); + std::string Receive(); + +private: + std::shared_ptr logger_; + asio::io_context io_context_; + asio::ip::tcp::socket socket_; + CMutBuffer buffer_; +}; \ No newline at end of file diff --git a/ofen b/ofen new file mode 160000 index 0000000..76c702c --- /dev/null +++ b/ofen @@ -0,0 +1 @@ +Subproject commit 76c702cea8f4aa572d9c4319a5bb41d2ea969680 diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..841d4c4 --- /dev/null +++ b/test.cpp @@ -0,0 +1,16 @@ +#include "util.h" +#include +#include + +int main() +{ + char testBuffer[] = "NIhao"; + auto logger = get_logger("test", "test.log"); + CClient client(logger); + if (client.Connect("127.0.0.1", "8989")) { + std::cout << client.Send(testBuffer, sizeof(testBuffer)) << std::endl; + client.Receive(); + } + client.Disconnect(); + return 0; +} \ No newline at end of file diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt new file mode 100644 index 0000000..c5433ca --- /dev/null +++ b/util/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.16) + +project(trans_util LANGUAGES CXX) + +if (MSVC) + add_definitions(-D_WIN32_WINNT=0x0601) + add_compile_options(/source-charset:utf-8) +endif() + +set(SOURCES + util.h util.cpp +) +add_library(trans_util STATIC ${SOURCES}) +target_include_directories(trans_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/util/util.cpp b/util/util.cpp new file mode 100644 index 0000000..d789408 --- /dev/null +++ b/util/util.cpp @@ -0,0 +1,14 @@ +#include "util.h" + +std::shared_ptr get_logger(const std::string& mark, const std::string& log_file) +{ + auto file_sink = std::make_shared(log_file, 1024 * 50, 3); + auto console_sink = std::make_shared(); + file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e][%l]: %v"); + console_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e][%l]: %v"); + std::vector sinks{file_sink, console_sink}; + auto logger = std::make_shared(mark, sinks.begin(), sinks.end()); + logger->set_level(spdlog::level::info); + spdlog::register_logger(logger); + return logger; +} \ No newline at end of file diff --git a/util/util.h b/util/util.h new file mode 100644 index 0000000..5a38464 --- /dev/null +++ b/util/util.h @@ -0,0 +1,8 @@ +#ifndef TRANSM_UTIL +#define TRANSM_UTIL +#include +#include +#include + +std::shared_ptr get_logger(const std::string& mark, const std::string& log_file); +#endif \ No newline at end of file