struct:基本结构添加。
This commit is contained in:
parent
aa12beb727
commit
684c670195
@ -11,7 +11,7 @@ ReflowComments: true
|
||||
SpacesBeforeTrailingComments: 3
|
||||
TabWidth: 4
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ColumnLimit: 80
|
||||
ColumnLimit: 130
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[submodule "ofen"]
|
||||
path = ofen
|
||||
url = https://www.sinxmiao.cn/taynpg/ofen
|
80
.vscode/settings.json
vendored
80
.vscode/settings.json
vendored
@ -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"
|
||||
}
|
||||
}
|
@ -288,7 +288,7 @@
|
||||
#endif
|
||||
|
||||
#ifndef FMT_UNICODE
|
||||
# define FMT_UNICODE 1
|
||||
# define FMT_UNICODE 0
|
||||
#endif
|
||||
|
||||
// Check if rtti is available.
|
||||
|
@ -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)
|
||||
add_subdirectory(client)
|
||||
|
||||
add_executable(transm_test test.cpp)
|
||||
target_link_libraries(transm_test PRIVATE trans_net trans_util)
|
9
README.md
Normal file
9
README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# transm
|
||||
|
||||
以一个网点作为中转传输文件。
|
||||
|
||||
# 简要说明
|
||||
|
||||
- `client`和`server`均为命令行端程序,无GUI。
|
||||
- `client`从`server`下载文件,如果本地有重复则覆盖。
|
||||
- 工作方式为`client A`端提交待传输的文件列表到`server`,`client B`端从`server`获取有哪些客户机提交的哪些任务,可以从中下载。
|
@ -1,8 +1,8 @@
|
||||
#include <iostream>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
7
main.cpp
7
main.cpp
@ -1,7 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Done" << std::endl;
|
||||
return 0;
|
||||
}
|
15
net/CMakeLists.txt
Normal file
15
net/CMakeLists.txt
Normal file
@ -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})
|
69
net/net_base.cpp
Normal file
69
net/net_base.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include "net_base.h"
|
||||
|
||||
CServer::CServer()
|
||||
{
|
||||
}
|
||||
|
||||
CServer::~CServer()
|
||||
{
|
||||
}
|
||||
|
||||
CClient::CClient(const std::shared_ptr<spdlog::logger>& 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<int>(send_size) == len;
|
||||
} catch (const std::exception& ex) {
|
||||
logger_->error("Send failed: {}", ex.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string CClient::Receive()
|
||||
{
|
||||
try {
|
||||
std::vector<char> 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 "";
|
||||
}
|
||||
}
|
31
net/net_base.h
Normal file
31
net/net_base.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "util.h"
|
||||
#include <asio.hpp>
|
||||
#include <of_util.h>
|
||||
|
||||
using namespace ofen;
|
||||
class CServer
|
||||
{
|
||||
public:
|
||||
CServer();
|
||||
~CServer();
|
||||
};
|
||||
|
||||
class CClient
|
||||
{
|
||||
public:
|
||||
CClient(const std::shared_ptr<spdlog::logger>& 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<spdlog::logger> logger_;
|
||||
asio::io_context io_context_;
|
||||
asio::ip::tcp::socket socket_;
|
||||
CMutBuffer buffer_;
|
||||
};
|
1
ofen
Submodule
1
ofen
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 76c702cea8f4aa572d9c4319a5bb41d2ea969680
|
16
test.cpp
Normal file
16
test.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include "util.h"
|
||||
#include <iostream>
|
||||
#include <net_base.h>
|
||||
|
||||
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;
|
||||
}
|
14
util/CMakeLists.txt
Normal file
14
util/CMakeLists.txt
Normal file
@ -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})
|
14
util/util.cpp
Normal file
14
util/util.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
#include "util.h"
|
||||
|
||||
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file)
|
||||
{
|
||||
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_file, 1024 * 50, 3);
|
||||
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
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<spdlog::sink_ptr> sinks{file_sink, console_sink};
|
||||
auto logger = std::make_shared<spdlog::logger>(mark, sinks.begin(), sinks.end());
|
||||
logger->set_level(spdlog::level::info);
|
||||
spdlog::register_logger(logger);
|
||||
return logger;
|
||||
}
|
8
util/util.h
Normal file
8
util/util.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef TRANSM_UTIL
|
||||
#define TRANSM_UTIL
|
||||
#include <spdlog/sinks/rotating_file_sink.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file);
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user