transm/net/net_base.h

30 lines
733 B
C
Raw Normal View History

2024-12-11 10:22:14 +08:00
#pragma once
#include "util.h"
#include <asio.hpp>
2024-12-11 22:51:43 +08:00
#include <mutex>
2024-12-11 23:23:48 +08:00
#include <of_util.h>
2024-12-11 10:22:14 +08:00
using namespace ofen;
2024-12-11 23:23:48 +08:00
class CTcpClient : public std::enable_shared_from_this<CTcpClient>
2024-12-11 10:22:14 +08:00
{
public:
2024-12-11 23:23:48 +08:00
CTcpClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger);
~CTcpClient();
2024-12-11 10:22:14 +08:00
public:
2024-12-11 23:23:48 +08:00
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);
2024-12-11 23:23:48 +08:00
void async_recv();
2024-12-11 10:22:14 +08:00
private:
std::shared_ptr<spdlog::logger> logger_;
2024-12-11 22:51:43 +08:00
asio::io_context& io_context_;
2024-12-11 10:22:14 +08:00
asio::ip::tcp::socket socket_;
2024-12-11 22:51:43 +08:00
std::mutex mutex_;
2024-12-11 10:22:14 +08:00
CMutBuffer buffer_;
2024-12-11 22:51:43 +08:00
std::array<char, 1024> tmp_buf_;
ExFun_t fun_;
2024-12-11 10:22:14 +08:00
};