2024-12-12 22:43:24 +08:00
|
|
|
#pragma once
|
2025-01-14 00:09:39 +08:00
|
|
|
#include <chrono>
|
2024-12-18 16:55:32 +08:00
|
|
|
#include <list>
|
2025-01-21 21:43:09 +08:00
|
|
|
#include <map>
|
2024-12-12 22:43:24 +08:00
|
|
|
#include <net_base.h>
|
2024-12-13 12:35:08 +08:00
|
|
|
#include <of_util.h>
|
2025-01-14 00:09:39 +08:00
|
|
|
#include <shared_mutex>
|
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;
|
2025-01-06 14:39:53 +08:00
|
|
|
using high_c = std::chrono::time_point<std::chrono::high_resolution_clock>;
|
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_;
|
2025-04-15 08:48:41 +08:00
|
|
|
CMutBuffer buffer_;
|
2024-12-14 23:59:13 +08:00
|
|
|
std::array<char, g_BuffSize> tmp_buf_{};
|
2025-04-15 08:48:41 +08:00
|
|
|
std::string task_;
|
|
|
|
std::string uuid;
|
|
|
|
std::string task_time_;
|
|
|
|
std::string online_time_;
|
2025-04-07 23:12:27 +08:00
|
|
|
uint64_t timestamp{};
|
2025-01-06 14:39:53 +08:00
|
|
|
high_c last_active_time_;
|
2024-12-14 11:57:33 +08:00
|
|
|
FrameType cur_type_{TYPE_DEFAULT};
|
2024-12-13 23:03:12 +08:00
|
|
|
};
|
2024-12-12 23:11:55 +08:00
|
|
|
|
2025-04-12 22:51:06 +08:00
|
|
|
class TransmServer
|
2024-12-12 22:43:24 +08:00
|
|
|
{
|
|
|
|
public:
|
2025-04-15 08:48:41 +08:00
|
|
|
explicit TransmServer(asio::io_context& io_context);
|
2025-04-12 22:51:06 +08:00
|
|
|
~TransmServer();
|
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:
|
2025-04-07 17:28:50 +08:00
|
|
|
void get_client_list(CMessageInfo& msg_info);
|
2024-12-12 23:11:55 +08:00
|
|
|
|
|
|
|
private:
|
2024-12-18 16:55:32 +08:00
|
|
|
void trans_data(CFrameBuffer* buf);
|
2025-04-15 08:48:41 +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();
|
2025-02-06 09:37:24 +08:00
|
|
|
void th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket, const std::string& client_key);
|
|
|
|
bool send_frame(const std::shared_ptr<asio::ip::tcp::socket>& socket, CFrameBuffer* buf);
|
2025-01-06 14:39:53 +08:00
|
|
|
void monitor_idle();
|
2024-12-14 11:57:33 +08:00
|
|
|
|
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_;
|
|
|
|
std::map<std::string, std::shared_ptr<ClientCache>> client_map_;
|
|
|
|
std::map<std::string, std::thread> client_threads_;
|
2025-01-14 00:09:39 +08:00
|
|
|
std::shared_mutex cli_mut_;
|
2025-01-06 14:39:53 +08:00
|
|
|
std::thread th_monitor_idle_;
|
2024-12-14 16:20:25 +08:00
|
|
|
std::string server_ip_;
|
2025-01-06 14:39:53 +08:00
|
|
|
CThreadSleep sleep_;
|
2024-12-12 22:43:24 +08:00
|
|
|
};
|