2025-03-04 15:17:10 +08:00
|
|
|
#ifndef SERVER_H
|
|
|
|
#define SERVER_H
|
|
|
|
|
|
|
|
#include <asio.hpp>
|
|
|
|
#include <mutex>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2025-04-14 21:01:36 +08:00
|
|
|
#include "communicate.hpp"
|
|
|
|
#include "jsondata.h"
|
|
|
|
#include "zapi.h"
|
|
|
|
|
2025-03-04 15:17:10 +08:00
|
|
|
struct ClientCache {
|
|
|
|
std::array<char, g_BuffSize> tmp_buf_{};
|
2025-04-14 22:49:10 +08:00
|
|
|
CMutBuffer buffer_;
|
2025-03-04 15:17:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class Server
|
|
|
|
{
|
|
|
|
public:
|
2025-04-14 22:49:10 +08:00
|
|
|
Server(asio::io_context& io_context, uint16_t port);
|
2025-03-05 08:10:55 +08:00
|
|
|
~Server();
|
|
|
|
|
|
|
|
public:
|
2025-03-04 15:17:10 +08:00
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void set_worker(std::shared_ptr<COpenAI> worker, std::shared_ptr<CJsonOper> json);
|
2025-04-14 22:49:10 +08:00
|
|
|
void set_token(int32_t tokens);
|
2025-04-14 21:01:36 +08:00
|
|
|
|
2025-03-04 15:17:10 +08:00
|
|
|
private:
|
|
|
|
void do_accept();
|
2025-03-05 09:20:03 +08:00
|
|
|
void th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket, const std::string& client_key);
|
2025-03-04 15:17:10 +08:00
|
|
|
|
2025-03-04 19:46:29 +08:00
|
|
|
std::string post_data(const std::string& data);
|
|
|
|
bool send_frame(const std::shared_ptr<asio::ip::tcp::socket>& socket, FrameData& data);
|
2025-03-05 10:20:56 +08:00
|
|
|
void print_exception(const std::exception& e);
|
2025-03-04 19:46:29 +08:00
|
|
|
|
2025-03-04 15:17:10 +08:00
|
|
|
private:
|
|
|
|
asio::io_context& io_context_;
|
|
|
|
asio::ip::tcp::acceptor acceptor_;
|
|
|
|
|
2025-04-14 22:49:10 +08:00
|
|
|
std::shared_ptr<COpenAI> worker_;
|
|
|
|
std::shared_ptr<CJsonOper> json_;
|
2025-03-04 15:17:10 +08:00
|
|
|
std::mutex ask_mutex_;
|
|
|
|
std::mutex cli_mutex_;
|
|
|
|
std::unordered_map<std::string, std::thread> clients_;
|
|
|
|
std::map<std::string, std::shared_ptr<ClientCache>> client_map_;
|
2025-04-14 22:49:10 +08:00
|
|
|
CMutBuffer buffer_;
|
|
|
|
uint16_t port_;
|
|
|
|
int32_t tokens_{};
|
|
|
|
int32_t use_tokens_{};
|
2025-03-04 15:17:10 +08:00
|
|
|
};
|
|
|
|
|
2025-04-14 21:01:36 +08:00
|
|
|
#endif
|