2024-12-12 22:43:24 +08:00
|
|
|
#pragma once
|
2024-12-18 16:55:32 +08:00
|
|
|
#include <list>
|
2024-12-12 22:43:24 +08:00
|
|
|
#include <net_base.h>
|
2024-12-13 12:35:08 +08:00
|
|
|
#include <of_util.h>
|
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-14 19:49:44 +08:00
|
|
|
CMutBuffer buffer_{};
|
2024-12-14 23:59:13 +08:00
|
|
|
std::array<char, g_BuffSize> tmp_buf_{};
|
2024-12-14 19:49:44 +08:00
|
|
|
std::string task_{};
|
|
|
|
std::string time_{};
|
2024-12-14 11:57:33 +08:00
|
|
|
FrameType cur_type_{TYPE_DEFAULT};
|
2024-12-13 23:03:12 +08:00
|
|
|
};
|
|
|
|
struct TaskList {
|
2024-12-14 19:49:44 +08:00
|
|
|
std::string id_{};
|
|
|
|
std::string task_{};
|
|
|
|
std::string time_{};
|
2024-12-12 23:11:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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 23:03:12 +08:00
|
|
|
std::vector<TaskList> get_clients();
|
2024-12-14 19:49:44 +08:00
|
|
|
void get_client_list(CFrameBuffer** buf);
|
2024-12-12 23:11:55 +08:00
|
|
|
|
|
|
|
private:
|
2024-12-18 16:55:32 +08:00
|
|
|
void trans_data(CFrameBuffer* buf);
|
2024-12-19 15:54:42 +08:00
|
|
|
bool check_double(CFrameBuffer* buf, std::shared_ptr<ClientCache>& fcli,
|
|
|
|
std::shared_ptr<ClientCache>& tcli);
|
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);
|
|
|
|
|
2024-12-18 16:55:32 +08:00
|
|
|
/// @brief 不删除 buf
|
|
|
|
/// @param socket
|
|
|
|
/// @param buf
|
|
|
|
/// @return
|
2024-12-14 11:57:33 +08:00
|
|
|
bool send_frame(std::shared_ptr<asio::ip::tcp::socket> socket, CFrameBuffer* buf);
|
|
|
|
|
2024-12-13 12:35:08 +08:00
|
|
|
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::mutex buf_mut_;
|
2024-12-14 16:20:25 +08:00
|
|
|
std::string server_ip_;
|
2024-12-12 22:43:24 +08:00
|
|
|
};
|