67 lines
1.7 KiB
C++
67 lines
1.7 KiB
C++
#pragma once
|
|
#include <net_base.h>
|
|
#include <of_util.h>
|
|
#include <queue>
|
|
#include <string>
|
|
#include <util.h>
|
|
#include <vector>
|
|
|
|
using namespace ofen;
|
|
struct ClientCache {
|
|
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
|
CMutBuffer buffer_;
|
|
std::array<char, 1024> tmp_buf_;
|
|
std::string task_;
|
|
std::string time_;
|
|
FrameType cur_type_{TYPE_DEFAULT};
|
|
};
|
|
struct TaskList {
|
|
std::string id_;
|
|
std::string task_;
|
|
std::string time_;
|
|
};
|
|
|
|
class CTcpServer
|
|
{
|
|
public:
|
|
CTcpServer(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger);
|
|
~CTcpServer();
|
|
|
|
public:
|
|
bool start(unsigned short port);
|
|
void stop();
|
|
|
|
private:
|
|
std::vector<TaskList> get_clients();
|
|
SimpleBuffer* get_client_list();
|
|
|
|
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);
|
|
|
|
/// @brief 不删除 buf
|
|
/// @param socket
|
|
/// @param buf
|
|
/// @return
|
|
bool send_frame(std::shared_ptr<asio::ip::tcp::socket> socket, CFrameBuffer* buf);
|
|
|
|
private:
|
|
bool th_run_{false};
|
|
asio::io_context& io_context_;
|
|
asio::ip::tcp::acceptor acceptor_;
|
|
std::shared_ptr<spdlog::logger> logger_;
|
|
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_;
|
|
}; |