diff --git a/.clang-format b/.clang-format index 36e4997..7626823 100644 --- a/.clang-format +++ b/.clang-format @@ -11,7 +11,7 @@ ReflowComments: true SpacesBeforeTrailingComments: 3 TabWidth: 4 ConstructorInitializerAllOnOneLineOrOnePerLine: true -ColumnLimit: 130 +ColumnLimit: 1100 AllowShortBlocksOnASingleLine: Never AllowShortFunctionsOnASingleLine: None AllowShortEnumsOnASingleLine: false diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index cfc5cb5..eb83093 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -7,4 +7,5 @@ if (MSVC) add_compile_options(/source-charset:utf-8) endif() -add_executable(transmc main.cpp) \ No newline at end of file +add_executable(transmc main.cpp client.h client.cpp) +target_link_libraries(transmc PRIVATE trans_net trans_util) \ No newline at end of file diff --git a/client/client.cpp b/client/client.cpp new file mode 100644 index 0000000..4e8adcd --- /dev/null +++ b/client/client.cpp @@ -0,0 +1,25 @@ +#include "client.h" +#include + +CClient::CClient(const std::shared_ptr& logger) : logger_(logger) +{ + client_ = std::make_shared(io_context_, logger_); +} + +CClient::~CClient() +{ +} + +void CClient::run() +{ + std::thread thread([this]() { io_context_.run(); }); + char line[512]{}; + while (std::cin.getline(line, 512)) { + if (std::strstr(line, "end")) { + break; + } + } + client_->disconnect(); + thread.join(); + logger_->info("{} exit.", __FUNCTION__); +} diff --git a/client/client.h b/client/client.h new file mode 100644 index 0000000..f445fd2 --- /dev/null +++ b/client/client.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include + +class CClient +{ +public: + CClient(const std::shared_ptr& logger); + ~CClient(); + +public: + void run(); + +private: + std::shared_ptr logger_; + asio::io_context io_context_; + std::shared_ptr client_; +}; \ No newline at end of file diff --git a/client/main.cpp b/client/main.cpp index 1642d5e..2b1cfee 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,8 +1,12 @@ #include +#include "client.h" +std::shared_ptr g_Logger = nullptr; int main() { - + g_Logger = get_logger("client", "client.log"); + CClient client(g_Logger); + client.run(); return 0; } \ No newline at end of file diff --git a/net/net_base.cpp b/net/net_base.cpp index e5ab068..17b8b35 100644 --- a/net/net_base.cpp +++ b/net/net_base.cpp @@ -1,23 +1,23 @@ #include "net_base.h" -CServer::CServer() +CTcpServer::CTcpServer() { } -CServer::~CServer() +CTcpServer::~CTcpServer() { } -CClient::CClient(asio::io_context& io_context, const std::shared_ptr& logger) +CTcpClient::CTcpClient(asio::io_context& io_context, const std::shared_ptr& logger) : logger_(logger), io_context_(io_context), socket_(io_context_) { } -CClient::~CClient() +CTcpClient::~CTcpClient() { } -bool CClient::Connect(const std::string& host, const std::string& port) +bool CTcpClient::connect(const std::string& host, const std::string& port) { try { asio::ip::tcp::resolver resolver(io_context_); @@ -31,7 +31,7 @@ bool CClient::Connect(const std::string& host, const std::string& port) } } -void CClient::Disconnect() +void CTcpClient::disconnect() { if (socket_.is_open()) { try { @@ -43,7 +43,7 @@ void CClient::Disconnect() } } -bool CClient::Send(const char* data, int len) +bool CTcpClient::send(const char* data, int len) { try { auto send_size = asio::write(socket_, asio::buffer(data, len)); @@ -55,12 +55,12 @@ bool CClient::Send(const char* data, int len) } } -void CClient::register_func(ExFun_t& f) +void CTcpClient::register_func(ExFun_t& f) { fun_ = f; } -void CClient::Receive() +void CTcpClient::async_recv() { auto self(shared_from_this()); socket_.async_read_some(asio::buffer(tmp_buf_), [this, self](std::error_code ec, std::size_t length) { @@ -74,7 +74,7 @@ void CClient::Receive() } delete frame; } - Receive(); + async_recv(); } }); } diff --git a/net/net_base.h b/net/net_base.h index c9e5142..95f133a 100644 --- a/net/net_base.h +++ b/net/net_base.h @@ -2,30 +2,31 @@ #include "util.h" #include -#include #include #include +#include using namespace ofen; using ExFun_t = std::function; -class CServer +class CTcpServer { public: - CServer(); - ~CServer(); + CTcpServer(); + ~CTcpServer(); }; -class CClient : public std::enable_shared_from_this +class CTcpClient : public std::enable_shared_from_this { public: - CClient(asio::io_context& io_context, const std::shared_ptr& logger); - ~CClient(); + CTcpClient(asio::io_context& io_context, const std::shared_ptr& logger); + ~CTcpClient(); + public: - bool Connect(const std::string& host, const std::string& port); - void Disconnect(); - bool Send(const char* data, int len); + bool connect(const std::string& host, const std::string& port); + void disconnect(); + bool send(const char* data, int len); void register_func(ExFun_t& f); - void Receive(); + void async_recv(); private: std::shared_ptr logger_; diff --git a/test1.cpp b/test1.cpp index 5ea9342..3d589ae 100644 --- a/test1.cpp +++ b/test1.cpp @@ -17,12 +17,12 @@ int main() } 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")) { + CTcpClient 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::cout << client.send(data, len) << std::endl; std::thread t([&io_context]() { io_context.run(); }); char line[512]{}; while (std::cin.getline(line, 512)) { @@ -30,7 +30,7 @@ int main() break; } } - client.Disconnect(); + client.disconnect(); t.join(); delete buf; diff --git a/test2.cpp b/test2.cpp index fe4c0c1..891f761 100644 --- a/test2.cpp +++ b/test2.cpp @@ -7,6 +7,7 @@ void TestHandle(CFrameBuffer* buf) { g_Logger->info("type: {}", buf->type_); g_Logger->info("len: {}", buf->len_); + g_Logger->info("{} exec. 中文测试", __FUNCTION__); } int main() @@ -14,14 +15,14 @@ int main() char buffer[] = "Java"; g_Logger = get_logger("test1", "test1.log"); asio::io_context io_context; - std::shared_ptr client = std::make_shared(io_context, g_Logger); - if (!client->Connect("127.0.0.1", "8080")) { + std::shared_ptr client = std::make_shared(io_context, g_Logger); + if (!client->connect("127.0.0.1", "8080")) { return -1; } - client->Send(buffer, sizeof(buffer)); + client->send(buffer, sizeof(buffer)); std::function func = TestHandle; client->register_func(func); - client->Receive(); + client->async_recv(); std::thread t([&io_context]() { io_context.run(); }); char line[512]{}; while (std::cin.getline(line, 512)) { @@ -29,7 +30,7 @@ int main() break; } } - client->Disconnect(); + client->disconnect(); t.join(); return 0; } \ No newline at end of file