transm/server/server.h

60 lines
1.6 KiB
C
Raw Normal View History

#pragma once
#include <chrono>
#include <list>
#include <map>
#include <net_base.h>
#include <of_util.h>
#include <shared_mutex>
2024-12-12 23:11:55 +08:00
#include <string>
#include <util.h>
#include <vector>
using namespace ofen;
using high_c = std::chrono::time_point<std::chrono::high_resolution_clock>;
2024-12-12 23:11:55 +08:00
struct ClientCache {
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{};
high_c last_active_time_;
FrameType cur_type_{TYPE_DEFAULT};
};
2024-12-12 23:11:55 +08:00
class TransmServer
{
public:
2025-04-15 08:48:41 +08:00
explicit TransmServer(asio::io_context& io_context);
~TransmServer();
public:
bool start(unsigned short port);
void stop();
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:
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);
private:
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);
void monitor_idle();
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_;
std::shared_mutex cli_mut_;
std::thread th_monitor_idle_;
std::string server_ip_;
CThreadSleep sleep_;
};