transm/server/server.h

53 lines
1.3 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_;
};
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<std::string> 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);
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_;
};