transm/net/net_base.h

38 lines
853 B
C
Raw Normal View History

2024-12-11 10:22:14 +08:00
#pragma once
#include "util.h"
#include <asio.hpp>
#include <of_util.h>
2024-12-11 22:51:43 +08:00
#include <functional>
#include <mutex>
2024-12-11 10:22:14 +08:00
using namespace ofen;
2024-12-11 22:51:43 +08:00
using ExFun_t = std::function<void(CFrameBuffer* buf)>;
2024-12-11 10:22:14 +08:00
class CServer
{
public:
CServer();
~CServer();
};
2024-12-11 22:51:43 +08:00
class CClient : public std::enable_shared_from_this<CClient>
2024-12-11 10:22:14 +08:00
{
public:
2024-12-11 22:51:43 +08:00
CClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger);
2024-12-11 10:22:14 +08:00
~CClient();
public:
bool Connect(const std::string& host, const std::string& port);
void Disconnect();
bool Send(const char* data, int len);
2024-12-11 22:51:43 +08:00
void register_func(ExFun_t& f);
void Receive();
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
};