transm/net/net_base.h

32 lines
742 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:
2025-02-06 09:37:24 +08:00
explicit CTcpClient(asio::io_context& io_context);
2024-12-11 23:23:48 +08:00
~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();
2025-02-06 09:37:24 +08:00
bool is_normal() const;
2024-12-11 10:22:14 +08:00
private:
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_;
2025-02-06 09:37:24 +08:00
std::array<char, g_BuffSize> tmp_buf_{};
2024-12-11 22:51:43 +08:00
ExFun_t fun_;
std::string remote_key_;
2024-12-19 15:54:42 +08:00
bool is_normal_{true};
2024-12-11 10:22:14 +08:00
};