add:添加基本client。
This commit is contained in:
parent
fc96f48ee0
commit
40c71fc22e
@ -11,7 +11,7 @@ ReflowComments: true
|
|||||||
SpacesBeforeTrailingComments: 3
|
SpacesBeforeTrailingComments: 3
|
||||||
TabWidth: 4
|
TabWidth: 4
|
||||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
ColumnLimit: 130
|
ColumnLimit: 1100
|
||||||
AllowShortBlocksOnASingleLine: Never
|
AllowShortBlocksOnASingleLine: Never
|
||||||
AllowShortFunctionsOnASingleLine: None
|
AllowShortFunctionsOnASingleLine: None
|
||||||
AllowShortEnumsOnASingleLine: false
|
AllowShortEnumsOnASingleLine: false
|
||||||
|
@ -7,4 +7,5 @@ if (MSVC)
|
|||||||
add_compile_options(/source-charset:utf-8)
|
add_compile_options(/source-charset:utf-8)
|
||||||
endif()
|
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 <iostream>
|
||||||
|
#include "client.h"
|
||||||
|
|
||||||
|
std::shared_ptr<spdlog::logger> g_Logger = nullptr;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
g_Logger = get_logger("client", "client.log");
|
||||||
|
CClient client(g_Logger);
|
||||||
|
client.run();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -1,23 +1,23 @@
|
|||||||
#include "net_base.h"
|
#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_)
|
: 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 {
|
try {
|
||||||
asio::ip::tcp::resolver resolver(io_context_);
|
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()) {
|
if (socket_.is_open()) {
|
||||||
try {
|
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 {
|
try {
|
||||||
auto send_size = asio::write(socket_, asio::buffer(data, len));
|
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;
|
fun_ = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CClient::Receive()
|
void CTcpClient::async_recv()
|
||||||
{
|
{
|
||||||
auto self(shared_from_this());
|
auto self(shared_from_this());
|
||||||
socket_.async_read_some(asio::buffer(tmp_buf_), [this, self](std::error_code ec, std::size_t length) {
|
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;
|
delete frame;
|
||||||
}
|
}
|
||||||
Receive();
|
async_recv();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -2,30 +2,31 @@
|
|||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include <asio.hpp>
|
#include <asio.hpp>
|
||||||
#include <of_util.h>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <of_util.h>
|
||||||
|
|
||||||
using namespace ofen;
|
using namespace ofen;
|
||||||
using ExFun_t = std::function<void(CFrameBuffer* buf)>;
|
using ExFun_t = std::function<void(CFrameBuffer* buf)>;
|
||||||
class CServer
|
class CTcpServer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CServer();
|
CTcpServer();
|
||||||
~CServer();
|
~CTcpServer();
|
||||||
};
|
};
|
||||||
|
|
||||||
class CClient : public std::enable_shared_from_this<CClient>
|
class CTcpClient : public std::enable_shared_from_this<CTcpClient>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger);
|
CTcpClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger);
|
||||||
~CClient();
|
~CTcpClient();
|
||||||
|
|
||||||
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);
|
||||||
void register_func(ExFun_t& f);
|
void register_func(ExFun_t& f);
|
||||||
void Receive();
|
void async_recv();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<spdlog::logger> logger_;
|
std::shared_ptr<spdlog::logger> logger_;
|
||||||
|
@ -17,12 +17,12 @@ int main()
|
|||||||
}
|
}
|
||||||
auto logger = get_logger("test1", "test1.log");
|
auto logger = get_logger("test1", "test1.log");
|
||||||
asio::io_context io_context;
|
asio::io_context io_context;
|
||||||
CClient client(io_context, logger);
|
CTcpClient client(io_context, logger);
|
||||||
if (!client.Connect("127.0.0.1", "8080")) {
|
if (!client.connect("127.0.0.1", "8080")) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
logger->info("send len:{}", len);
|
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(); });
|
std::thread t([&io_context]() { io_context.run(); });
|
||||||
char line[512]{};
|
char line[512]{};
|
||||||
while (std::cin.getline(line, 512)) {
|
while (std::cin.getline(line, 512)) {
|
||||||
@ -30,7 +30,7 @@ int main()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
client.Disconnect();
|
client.disconnect();
|
||||||
t.join();
|
t.join();
|
||||||
|
|
||||||
delete buf;
|
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("type: {}", buf->type_);
|
||||||
g_Logger->info("len: {}", buf->len_);
|
g_Logger->info("len: {}", buf->len_);
|
||||||
|
g_Logger->info("{} exec. 中文测试", __FUNCTION__);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@ -14,14 +15,14 @@ int main()
|
|||||||
char buffer[] = "Java";
|
char buffer[] = "Java";
|
||||||
g_Logger = get_logger("test1", "test1.log");
|
g_Logger = get_logger("test1", "test1.log");
|
||||||
asio::io_context io_context;
|
asio::io_context io_context;
|
||||||
std::shared_ptr<CClient> client = std::make_shared<CClient>(io_context, g_Logger);
|
std::shared_ptr<CTcpClient> client = std::make_shared<CTcpClient>(io_context, g_Logger);
|
||||||
if (!client->Connect("127.0.0.1", "8080")) {
|
if (!client->connect("127.0.0.1", "8080")) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
client->Send(buffer, sizeof(buffer));
|
client->send(buffer, sizeof(buffer));
|
||||||
std::function<void(CFrameBuffer*)> func = TestHandle;
|
std::function<void(CFrameBuffer*)> func = TestHandle;
|
||||||
client->register_func(func);
|
client->register_func(func);
|
||||||
client->Receive();
|
client->async_recv();
|
||||||
std::thread t([&io_context]() { io_context.run(); });
|
std::thread t([&io_context]() { io_context.run(); });
|
||||||
char line[512]{};
|
char line[512]{};
|
||||||
while (std::cin.getline(line, 512)) {
|
while (std::cin.getline(line, 512)) {
|
||||||
@ -29,7 +30,7 @@ int main()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
client->Disconnect();
|
client->disconnect();
|
||||||
t.join();
|
t.join();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user