add:添加基本client。
This commit is contained in:
parent
fc96f48ee0
commit
40c71fc22e
@ -11,7 +11,7 @@ ReflowComments: true
|
||||
SpacesBeforeTrailingComments: 3
|
||||
TabWidth: 4
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ColumnLimit: 130
|
||||
ColumnLimit: 1100
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
|
@ -7,4 +7,5 @@ if (MSVC)
|
||||
add_compile_options(/source-charset:utf-8)
|
||||
endif()
|
||||
|
||||
add_executable(transmc main.cpp)
|
||||
add_executable(transmc main.cpp client.h client.cpp)
|
||||
target_link_libraries(transmc PRIVATE trans_net trans_util)
|
25
client/client.cpp
Normal file
25
client/client.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "client.h"
|
||||
#include <iostream>
|
||||
|
||||
CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger)
|
||||
{
|
||||
client_ = std::make_shared<CTcpClient>(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__);
|
||||
}
|
18
client/client.h
Normal file
18
client/client.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <net_base.h>
|
||||
#include <util.h>
|
||||
|
||||
class CClient
|
||||
{
|
||||
public:
|
||||
CClient(const std::shared_ptr<spdlog::logger>& logger);
|
||||
~CClient();
|
||||
|
||||
public:
|
||||
void run();
|
||||
|
||||
private:
|
||||
std::shared_ptr<spdlog::logger> logger_;
|
||||
asio::io_context io_context_;
|
||||
std::shared_ptr<CTcpClient> client_;
|
||||
};
|
@ -1,8 +1,12 @@
|
||||
#include <iostream>
|
||||
#include "client.h"
|
||||
|
||||
std::shared_ptr<spdlog::logger> g_Logger = nullptr;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
g_Logger = get_logger("client", "client.log");
|
||||
CClient client(g_Logger);
|
||||
client.run();
|
||||
return 0;
|
||||
}
|
@ -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<spdlog::logger>& logger)
|
||||
CTcpClient::CTcpClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -2,30 +2,31 @@
|
||||
|
||||
#include "util.h"
|
||||
#include <asio.hpp>
|
||||
#include <of_util.h>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <of_util.h>
|
||||
|
||||
using namespace ofen;
|
||||
using ExFun_t = std::function<void(CFrameBuffer* buf)>;
|
||||
class CServer
|
||||
class CTcpServer
|
||||
{
|
||||
public:
|
||||
CServer();
|
||||
~CServer();
|
||||
CTcpServer();
|
||||
~CTcpServer();
|
||||
};
|
||||
|
||||
class CClient : public std::enable_shared_from_this<CClient>
|
||||
class CTcpClient : public std::enable_shared_from_this<CTcpClient>
|
||||
{
|
||||
public:
|
||||
CClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger);
|
||||
~CClient();
|
||||
CTcpClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& 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<spdlog::logger> logger_;
|
||||
|
@ -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;
|
||||
|
11
test2.cpp
11
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<CClient> client = std::make_shared<CClient>(io_context, g_Logger);
|
||||
if (!client->Connect("127.0.0.1", "8080")) {
|
||||
std::shared_ptr<CTcpClient> client = std::make_shared<CTcpClient>(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<void(CFrameBuffer*)> 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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user