diff --git a/.vscode/settings.json b/.vscode/settings.json index aedf9d7..21915c1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -124,6 +124,15 @@ "thread": "cpp", "variant": "cpp", "*.ipp": "cpp", - "xthread": "cpp" + "xthread": "cpp", + "bitset": "cpp", + "charconv": "cpp", + "coroutine": "cpp", + "format": "cpp", + "hash_map": "cpp", + "set": "cpp", + "source_location": "cpp", + "stop_token": "cpp", + "unordered_set": "cpp" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index f1f42d9..5949349 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,3 +26,5 @@ find_package(CURL REQUIRED) add_executable(deepseek-use main.cxx zapi.h zapi.cxx jsondata.h jsondata.cxx handle.h handle.cxx server.h server.cxx) target_link_libraries(deepseek-use PRIVATE CURL::libcurl) + +add_executable(deepseek-client-test client_test.cxx) \ No newline at end of file diff --git a/client_test.cxx b/client_test.cxx new file mode 100644 index 0000000..e1f7de8 --- /dev/null +++ b/client_test.cxx @@ -0,0 +1,110 @@ +#include "communicate.hpp" + +#include +#include +#include +#include +#include + +constexpr size_t g_BuffSize = 1024 * 10; +class Client +{ +public: + Client(asio::io_context& io_context) : io_context_(io_context), socket_(io_context) + { + ip_ = "127.0.0.1"; + port_ = "9999"; + } + +public: + bool connect() + { + try { + asio::ip::tcp::resolver resolver(io_context_); + asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(ip_, port_); + asio::connect(socket_, endpoints); + std::cout << "Connected to server " << ip_ << ":" << port_ << std::endl; + return true; + } catch (const std::exception& ex) { + std::cerr << "Exception: " << ex.what() << "\n"; + return false; + } + } + + FrameData* post_deepseek(const std::string& text) + { + FrameData send; + send.type = FrameType::TYPE_REQUEST; + send.data = new char[text.size()]; + send.len = text.size(); + memcpy(send.data, text.c_str(), text.size()); + + char* send_data{}; + int len{}; + + std::shared_ptr deleter(new int(1), [send_data](int* p) { + delete p; + delete[] send_data; + }); + + if (!com_pack(&send, &send_data, len)) { + std::cerr << "com_pack error" << std::endl; + return nullptr; + } + + auto send_size = socket_.write_some(asio::buffer(send_data, len)); + if (send_size != len) { + std::cerr << "send_size != text.size()" << std::endl; + return nullptr; + } + size_t read_size = socket_.read_some(asio::buffer(tmp_buf_)); + FrameData* ret = nullptr; + while (read_size > 0) { + buffer_.push(tmp_buf_.data(), read_size); + auto* frame = com_parse(buffer_); + if (frame) { + ret = frame; + return ret; + } + read_size = socket_.read_some(asio::buffer(tmp_buf_)); + } + return ret; + } + +private: + std::string ip_{}; + std::string port_{}; + asio::ip::tcp::socket socket_; + asio::io_context& io_context_; + CMutBuffer buffer_{}; + std::array tmp_buf_{}; +}; + +int main() +{ +#ifdef _WIN32 + system("chcp 65001"); +#endif + + asio::io_context io_context; + Client client(io_context); + + if (!client.connect()) { + return -1; + } + + std::thread t([&io_context]() { io_context.run(); }); + + std::string text = "将【天文历】翻译为英文,直接给出结果。"; + // std::string text = "This is a test."; + FrameData* frame = client.post_deepseek(text); + if (frame) { + std::cout << "type: " << frame->type << std::endl; + std::cout << "data: " << frame->data << std::endl; + std::cout << "len: " << frame->len << std::endl; + std::cout << "protk: " << frame->protk << std::endl; + std::cout << "coptk: " << frame->coptk << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/communicate.hpp b/communicate.hpp index 5576c3e..673ce39 100644 --- a/communicate.hpp +++ b/communicate.hpp @@ -25,8 +25,8 @@ struct FrameData { FrameType type; char* data{}; int len{}; - int16_t protk; - int16_t coptk; + int16_t protk{}; + int16_t coptk{}; }; class CMutBuffer @@ -113,13 +113,13 @@ inline FrameData* com_parse(CMutBuffer& buffer) return r; } - int tail_index = sizeof(header) + sizeof(type) + sizeof(len) + sizeof(protk) + sizeof(coptk); + int tail_index = sizeof(header) + sizeof(type) + sizeof(len) + sizeof(protk) + sizeof(coptk) + len; if (std::memcmp(buffer.get_data() + tail_index, tail, sizeof(tail)) != 0) { return r; } - std::memcpy(&protk, buffer.get_data() + find + sizeof(header) + sizeof(type) + sizeof(len), sizeof(protk)); - std::memcpy(&coptk, buffer.get_data() + find + sizeof(header) + sizeof(type) + sizeof(len) + sizeof(protk), sizeof(coptk)); + std::memcpy(&protk, buffer.get_data() + find + sizeof(header) + sizeof(type) + sizeof(len) + len, sizeof(protk)); + std::memcpy(&coptk, buffer.get_data() + find + sizeof(header) + sizeof(type) + sizeof(len) + sizeof(protk) + len, sizeof(coptk)); r = new FrameData(); r->type = static_cast(type); @@ -128,9 +128,21 @@ inline FrameData* com_parse(CMutBuffer& buffer) std::memcpy(r->data, buffer.get_data() + find + sizeof(header) + sizeof(type) + sizeof(len), len); r->protk = protk; r->coptk = coptk; + + buffer.remove_of(0, tail_index + 2); return r; } +/* +【TCP 数据协议 】 + header 2 char: 0xFF 0xFE + type 2 char: + len 4 char: + data xxxxx: + protk 2 char: + coptk 2 char: + tail 2 char: 0xFF 0xFF +*/ inline bool com_pack(FrameData* data, char** out_buf, int& len) { if (data == nullptr) { diff --git a/main.cxx b/main.cxx index b39f5f8..55d2613 100644 --- a/main.cxx +++ b/main.cxx @@ -54,10 +54,10 @@ int main(int argc, char* argv[]) std::cout << "model_name:" << config.model_name << std::endl; std::cout << "api:" << show_api(key) << std::endl; - // if (show_api(key) == "NULL") { - // std::cerr << "api is invalid." << std::endl; - // return -1; - // } + if (show_api(key) == "NULL") { + std::cerr << "api is invalid." << std::endl; + return -1; + } auto api = std::make_shared(); api->set_base(config.base_url, key); @@ -86,6 +86,7 @@ int main(int argc, char* argv[]) server.set_worker(api, json); server.start(); + io_context.run(); return 0; } diff --git a/server.cxx b/server.cxx index 997fb5f..d932f30 100644 --- a/server.cxx +++ b/server.cxx @@ -2,14 +2,18 @@ #include Server::Server(asio::io_context& io_context, short port) - : io_context_(io_context), acceptor_(io_context, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)) + : io_context_(io_context), acceptor_(io_context) { + port_ = port; } void Server::start() { + asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port_); try { + acceptor_.open(endpoint.protocol()); acceptor_.set_option(asio::socket_base::reuse_address(true)); + acceptor_.bind(endpoint); acceptor_.listen(); do_accept(); } catch (const std::exception& e) { @@ -69,6 +73,7 @@ void Server::th_client(const std::shared_ptr& socket, con if (frame == nullptr) { break; } + std::cout << client_key << " 's data." << std::endl; if (frame->type == FrameType::TYPE_REQUEST) { ask_mutex_.lock(); std::string recv_data(frame->data, frame->len); diff --git a/server.h b/server.h index f91b20a..a99be80 100644 --- a/server.h +++ b/server.h @@ -44,6 +44,7 @@ private: std::unordered_map clients_; std::map> client_map_; CMutBuffer buffer_{}; + short port_; }; #endif \ No newline at end of file