transm/server/server.h

54 lines
1.4 KiB
C
Raw Normal View History

#pragma once
#include <net_base.h>
#include <of_util.h>
#include <queue>
2024-12-12 23:11:55 +08:00
#include <string>
#include <util.h>
#include <vector>
using namespace ofen;
2024-12-12 23:11:55 +08:00
struct ClientCache {
std::shared_ptr<asio::ip::tcp::socket> socket_;
2024-12-12 23:11:55 +08:00
CMutBuffer buffer_;
std::array<char, 1024> tmp_buf_;
2024-12-13 16:59:31 +08:00
std::string task_;
2024-12-12 23:11:55 +08:00
};
class CTcpServer
{
public:
2024-12-12 23:11:55 +08:00
CTcpServer(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger);
~CTcpServer();
public:
bool start(unsigned short port);
void stop();
2024-12-12 23:11:55 +08:00
private:
2024-12-13 16:59:31 +08:00
std::vector<std::pair<std::string, std::string>> get_clients();
SimpleBuffer* get_client_list();
2024-12-12 23:11:55 +08:00
private:
bool push_frame(CFrameBuffer* buf);
void handle_frame();
void send_simple_buf();
private:
void accept_client();
void th_client(std::shared_ptr<asio::ip::tcp::socket> socket, const std::string& client_key);
private:
bool th_run_{false};
2024-12-12 23:11:55 +08:00
asio::io_context& io_context_;
asio::ip::tcp::acceptor acceptor_;
std::shared_ptr<spdlog::logger> logger_;
2024-12-12 23:11:55 +08:00
std::map<std::string, std::shared_ptr<ClientCache>> client_map_;
std::map<std::string, std::thread> client_threads_;
std::mutex cli_mut_;
std::queue<CFrameBuffer*> cache_;
std::queue<SimpleBuffer*> scache_;
std::mutex buf_mut_;
std::mutex sbuf_mut_;
std::shared_ptr<CThreadPool> handle_pool_;
std::shared_ptr<CThreadPool> send_pool_;
};