2024-12-12 22:43:24 +08:00
|
|
|
#pragma once
|
|
|
|
#include <net_base.h>
|
2024-12-13 12:35:08 +08:00
|
|
|
#include <of_util.h>
|
|
|
|
#include <queue>
|
2024-12-12 23:11:55 +08:00
|
|
|
#include <string>
|
2024-12-12 22:43:24 +08:00
|
|
|
#include <util.h>
|
|
|
|
#include <vector>
|
|
|
|
|
2024-12-13 12:35:08 +08:00
|
|
|
using namespace ofen;
|
2024-12-12 23:11:55 +08:00
|
|
|
struct ClientCache {
|
2024-12-13 12:35:08 +08:00
|
|
|
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
2024-12-12 23:11:55 +08:00
|
|
|
CMutBuffer buffer_;
|
|
|
|
std::array<char, 1024> tmp_buf_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CTcpServer
|
2024-12-12 22:43:24 +08:00
|
|
|
{
|
|
|
|
public:
|
2024-12-12 23:11:55 +08:00
|
|
|
CTcpServer(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger);
|
|
|
|
~CTcpServer();
|
2024-12-12 22:43:24 +08:00
|
|
|
|
|
|
|
public:
|
2024-12-13 12:35:08 +08:00
|
|
|
bool start(unsigned short port);
|
|
|
|
void stop();
|
2024-12-12 22:43:24 +08:00
|
|
|
|
2024-12-12 23:11:55 +08:00
|
|
|
private:
|
2024-12-13 12:35:08 +08:00
|
|
|
std::vector<std::string> get_clients();
|
|
|
|
SimpleBuffer* get_client_list();
|
2024-12-12 23:11:55 +08:00
|
|
|
|
|
|
|
private:
|
2024-12-13 12:35:08 +08:00
|
|
|
bool push_frame(CFrameBuffer* buf);
|
|
|
|
void handle_frame();
|
|
|
|
void send_simple_buf();
|
2024-12-12 22:43:24 +08:00
|
|
|
|
|
|
|
private:
|
2024-12-13 12:35:08 +08:00
|
|
|
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_;
|
2024-12-12 22:43:24 +08:00
|
|
|
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_;
|
2024-12-13 12:35:08 +08:00
|
|
|
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_;
|
2024-12-12 22:43:24 +08:00
|
|
|
};
|