run:初步调试通讯。

This commit is contained in:
taynpg 2024-12-11 22:51:43 +08:00
parent 506ea67d3e
commit fc96f48ee0
11 changed files with 178 additions and 51 deletions

15
.vscode/settings.json vendored
View File

@ -20,7 +20,7 @@
] ]
}, },
"cmake.configureSettings": { "cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE": "${env:VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" //"CMAKE_TOOLCHAIN_FILE": "${env:VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}, },
"cmake.options.statusBarVisibility": "visible", "cmake.options.statusBarVisibility": "visible",
"cmake.generator": "Ninja", "cmake.generator": "Ninja",
@ -110,6 +110,17 @@
"xstddef": "cpp", "xstddef": "cpp",
"xtr1common": "cpp", "xtr1common": "cpp",
"xtree": "cpp", "xtree": "cpp",
"xutility": "cpp" "xutility": "cpp",
"bit": "cpp",
"compare": "cpp",
"concepts": "cpp",
"coroutine": "cpp",
"expected": "cpp",
"format": "cpp",
"forward_list": "cpp",
"source_location": "cpp",
"span": "cpp",
"stop_token": "cpp",
"*.ipp": "cpp"
} }
} }

View File

@ -19,5 +19,7 @@ add_subdirectory(util)
add_subdirectory(server) add_subdirectory(server)
add_subdirectory(client) add_subdirectory(client)
add_executable(transm_test test.cpp) add_executable(transm_test1 test1.cpp)
target_link_libraries(transm_test PRIVATE trans_net trans_util) target_link_libraries(transm_test1 PRIVATE trans_net trans_util)
add_executable(transm_test2 test2.cpp)
target_link_libraries(transm_test2 PRIVATE trans_net trans_util)

View File

@ -8,7 +8,8 @@ CServer::~CServer()
{ {
} }
CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger), io_context_(), socket_(io_context_) CClient::CClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger)
: logger_(logger), io_context_(io_context), socket_(io_context_)
{ {
} }
@ -54,16 +55,26 @@ bool CClient::Send(const char* data, int len)
} }
} }
std::string CClient::Receive() void CClient::register_func(ExFun_t& f)
{ {
try { fun_ = f;
std::vector<char> buffer(1024); }
size_t length = socket_.read_some(asio::buffer(buffer));
std::string received_data(buffer.data(), length); void CClient::Receive()
logger_->info("Received data len: {}", length); {
return received_data; auto self(shared_from_this());
} catch (const std::exception& ex) { socket_.async_read_some(asio::buffer(tmp_buf_), [this, self](std::error_code ec, std::size_t length) {
logger_->error("Receive failed: {}", ex.what()); if (!ec) {
return ""; std::lock_guard<std::mutex> lock(mutex_);
} buffer_.push(tmp_buf_.data(), length);
auto* frame = CTransProtocal::parse(buffer_);
if (frame) {
if (fun_) {
fun_(frame);
}
delete frame;
}
Receive();
}
});
} }

View File

@ -3,8 +3,11 @@
#include "util.h" #include "util.h"
#include <asio.hpp> #include <asio.hpp>
#include <of_util.h> #include <of_util.h>
#include <functional>
#include <mutex>
using namespace ofen; using namespace ofen;
using ExFun_t = std::function<void(CFrameBuffer* buf)>;
class CServer class CServer
{ {
public: public:
@ -12,20 +15,24 @@ public:
~CServer(); ~CServer();
}; };
class CClient class CClient : public std::enable_shared_from_this<CClient>
{ {
public: public:
CClient(const std::shared_ptr<spdlog::logger>& logger); CClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger);
~CClient(); ~CClient();
public: public:
bool Connect(const std::string& host, const std::string& port); bool Connect(const std::string& host, const std::string& port);
void Disconnect(); void Disconnect();
bool Send(const char* data, int len); bool Send(const char* data, int len);
std::string Receive(); void register_func(ExFun_t& f);
void Receive();
private: private:
std::shared_ptr<spdlog::logger> logger_; std::shared_ptr<spdlog::logger> logger_;
asio::io_context io_context_; asio::io_context& io_context_;
asio::ip::tcp::socket socket_; asio::ip::tcp::socket socket_;
std::mutex mutex_;
CMutBuffer buffer_; CMutBuffer buffer_;
std::array<char, 1024> tmp_buf_;
ExFun_t fun_;
}; };

2
ofen

@ -1 +1 @@
Subproject commit 79e086bae5509753eff4dce5669b3a7f5eac525f Subproject commit 0f7a8d3c928ef93c6250fa048b5c1cda2e42ed57

View File

@ -1,16 +0,0 @@
#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;
}

38
test1.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "util.h"
#include <iostream>
#include <net_base.h>
int main()
{
CFrameBuffer* buf = new CFrameBuffer();
buf->data_ = new char[256];
std::memset(buf->data_, 0x0, 256);
int buf_len = std::snprintf(buf->data_, 256, "%s", "Hello Cplusplus.");
buf->len_ = buf_len;
char* data = nullptr;
int len = 0;
if (!CTransProtocal::pack(buf, &data, len)) {
delete buf;
return -1;
}
auto logger = get_logger("test1", "test1.log");
asio::io_context io_context;
CClient client(io_context, logger);
if (!client.Connect("127.0.0.1", "8080")) {
return -1;
}
logger->info("send len:{}", len);
std::cout << client.Send(data, len) << std::endl;
std::thread t([&io_context]() { io_context.run(); });
char line[512]{};
while (std::cin.getline(line, 512)) {
if (std::strstr(line, "end")) {
break;
}
}
client.Disconnect();
t.join();
delete buf;
return 0;
}

35
test2.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <net_base.h>
#include <util.h>
std::shared_ptr<spdlog::logger> g_Logger;
void TestHandle(CFrameBuffer* buf)
{
g_Logger->info("type: {}", buf->type_);
g_Logger->info("len: {}", buf->len_);
}
int main()
{
char buffer[] = "Java";
g_Logger = get_logger("test1", "test1.log");
asio::io_context io_context;
std::shared_ptr<CClient> client = std::make_shared<CClient>(io_context, g_Logger);
if (!client->Connect("127.0.0.1", "8080")) {
return -1;
}
client->Send(buffer, sizeof(buffer));
std::function<void(CFrameBuffer*)> func = TestHandle;
client->register_func(func);
client->Receive();
std::thread t([&io_context]() { io_context.run(); });
char line[512]{};
while (std::cin.getline(line, 512)) {
if (std::strstr(line, "end")) {
break;
}
}
client->Disconnect();
t.join();
return 0;
}

View File

@ -1,15 +1,13 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(trans_util LANGUAGES CXX) project(trans_util LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
if (MSVC) if (MSVC)
add_definitions(-D_WIN32_WINNT=0x0601)
add_compile_options(/source-charset:utf-8) add_compile_options(/source-charset:utf-8)
endif() endif()
set(SOURCES set(SOURCES util.h util.cpp)
util.h util.cpp
)
add_library(trans_util STATIC ${SOURCES}) add_library(trans_util STATIC ${SOURCES})
target_link_libraries(trans_util PUBLIC Ofen) target_link_libraries(trans_util PUBLIC Ofen)
target_include_directories(trans_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(trans_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -1,4 +1,6 @@
#include "util.h" #include "util.h"
#include <cstddef>
#include <cstdint>
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file) std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file)
{ {
@ -32,21 +34,56 @@ CTransProtocal::~CTransProtocal()
CFrameBuffer* CTransProtocal::parse(CMutBuffer& buffer) CFrameBuffer* CTransProtocal::parse(CMutBuffer& buffer)
{ {
CFrameBuffer* result = nullptr; CFrameBuffer* result = nullptr;
char header[] = {0xFF, 0xFE}; unsigned char header[] = {0xFF, 0xFE};
char tail[] = {0xFF, 0xFF}; unsigned char tail[] = {0xFF, 0xFF};
int find = buffer.index_of(header, sizeof(header)); int find = buffer.index_of((const char*)header, sizeof(header));
if (find < 0) { if (find < 0) {
return result; return result;
} }
short int type = *(reinterpret_cast<const short int*>(buffer.get_data() + find)); int16_t type = *(reinterpret_cast<const int16_t*>(buffer.get_data() + find + 2));
int32_t len = *(reinterpret_cast<const int32_t*>(buffer.get_data() + find + 2 + sizeof(int16_t)));
int32_t tail_index = find + 2 + sizeof(int16_t) + sizeof(int32_t) + len;
if (buffer.get_len() - 2 < tail_index || len < 0) {
return result;
}
unsigned char taila = *((unsigned char*)(buffer.get_data() + tail_index));
unsigned char tailb = *((unsigned char*)(buffer.get_data() + tail_index + 1));
if (taila != tail[0] || tailb != tail[1]) {
return result;
}
result = new CFrameBuffer();
result->data_ = new char[len];
result->len_ = len;
std::memset(result->data_, 0x0, len);
std::memcpy(result->data_, buffer.get_data() + find + 2 + sizeof(int16_t) + sizeof(int32_t), len);
buffer.remove_of(0, tail_index + 2);
return result; return result;
} }
bool CTransProtocal::pack(CFrameBuffer* buf, char** out_buf, int& len)
{
if (buf == nullptr || buf->data_ == nullptr || buf->len_ < 1) {
return false;
}
unsigned char header[] = {0xFF, 0xFE};
unsigned char tail[] = {0xFF, 0xFF};
len = buf->len_ + 10;
*out_buf = new char[len];
std::memcpy(*out_buf, header, 2);
std::memcpy(*out_buf + 2, &buf->type_, 2);
std::memcpy(*out_buf + 4, &buf->len_, 4);
std::memcpy(*out_buf + 8, buf->data_, buf->len_);
std::memcpy(*out_buf + len - 2, tail, 2);
return true;
}
CFrameBuffer::CFrameBuffer() CFrameBuffer::CFrameBuffer()
{ {
} }
CFrameBuffer::~CFrameBuffer() CFrameBuffer::~CFrameBuffer()
{ {
} delete[] data_;
len_ = 0;
}

View File

@ -1,9 +1,9 @@
#ifndef TRANSM_UTIL #pragma once
#define TRANSM_UTIL
#include <spdlog/sinks/rotating_file_sink.h> #include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h> #include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include "of_util.h" #include "of_util.h"
#include <cstdint>
using namespace ofen; using namespace ofen;
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file); std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file);
@ -12,6 +12,10 @@ class CFrameBuffer
public: public:
CFrameBuffer(); CFrameBuffer();
~CFrameBuffer(); ~CFrameBuffer();
public:
int16_t type_{};
char* data_{};
int len_{};
}; };
/* /*
@ -28,6 +32,6 @@ public:
CTransProtocal(); CTransProtocal();
~CTransProtocal(); ~CTransProtocal();
public: public:
CFrameBuffer* parse(CMutBuffer& buffer); static CFrameBuffer* parse(CMutBuffer& buffer);
}; static bool pack(CFrameBuffer* buf, char** out_buf, int& len);
#endif };