36 lines
838 B
C++
36 lines
838 B
C++
#pragma once
|
|
#include <net_base.h>
|
|
#include <string>
|
|
#include <util.h>
|
|
#include <vector>
|
|
|
|
struct ClientCache {
|
|
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:
|
|
void handle_data(CFrameBuffer* buf);
|
|
|
|
private:
|
|
void Accept();
|
|
void HandleClient(std::shared_ptr<asio::ip::tcp::socket> socket, const std::string& client_key);
|
|
|
|
private:
|
|
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 mutex_;
|
|
}; |