debug:初步调试通过(仅逻辑通过)。

This commit is contained in:
taynpg 2025-03-04 22:47:13 +08:00
parent 6e7c4a0a03
commit f9e2662cdb
7 changed files with 151 additions and 11 deletions

11
.vscode/settings.json vendored
View File

@ -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"
}
}

View File

@ -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)

110
client_test.cxx Normal file
View File

@ -0,0 +1,110 @@
#include "communicate.hpp"
#include <array>
#include <asio.hpp>
#include <iostream>
#include <string>
#include <thread>
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<int> 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<char, g_BuffSize> 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;
}

View File

@ -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<FrameType>(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) {

View File

@ -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<COpenAI>();
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;
}

View File

@ -2,14 +2,18 @@
#include <iostream>
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<asio::ip::tcp::socket>& 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);

View File

@ -44,6 +44,7 @@ private:
std::unordered_map<std::string, std::thread> clients_;
std::map<std::string, std::shared_ptr<ClientCache>> client_map_;
CMutBuffer buffer_{};
short port_;
};
#endif