debug:初步调试通过(仅逻辑通过)。
This commit is contained in:
parent
6e7c4a0a03
commit
f9e2662cdb
11
.vscode/settings.json
vendored
11
.vscode/settings.json
vendored
@ -124,6 +124,15 @@
|
|||||||
"thread": "cpp",
|
"thread": "cpp",
|
||||||
"variant": "cpp",
|
"variant": "cpp",
|
||||||
"*.ipp": "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"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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)
|
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)
|
target_link_libraries(deepseek-use PRIVATE CURL::libcurl)
|
||||||
|
|
||||||
|
add_executable(deepseek-client-test client_test.cxx)
|
110
client_test.cxx
Normal file
110
client_test.cxx
Normal 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;
|
||||||
|
}
|
@ -25,8 +25,8 @@ struct FrameData {
|
|||||||
FrameType type;
|
FrameType type;
|
||||||
char* data{};
|
char* data{};
|
||||||
int len{};
|
int len{};
|
||||||
int16_t protk;
|
int16_t protk{};
|
||||||
int16_t coptk;
|
int16_t coptk{};
|
||||||
};
|
};
|
||||||
|
|
||||||
class CMutBuffer
|
class CMutBuffer
|
||||||
@ -113,13 +113,13 @@ inline FrameData* com_parse(CMutBuffer& buffer)
|
|||||||
return r;
|
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) {
|
if (std::memcmp(buffer.get_data() + tail_index, tail, sizeof(tail)) != 0) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::memcpy(&protk, buffer.get_data() + find + sizeof(header) + sizeof(type) + sizeof(len), sizeof(protk));
|
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), sizeof(coptk));
|
std::memcpy(&coptk, buffer.get_data() + find + sizeof(header) + sizeof(type) + sizeof(len) + sizeof(protk) + len, sizeof(coptk));
|
||||||
|
|
||||||
r = new FrameData();
|
r = new FrameData();
|
||||||
r->type = static_cast<FrameType>(type);
|
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);
|
std::memcpy(r->data, buffer.get_data() + find + sizeof(header) + sizeof(type) + sizeof(len), len);
|
||||||
r->protk = protk;
|
r->protk = protk;
|
||||||
r->coptk = coptk;
|
r->coptk = coptk;
|
||||||
|
|
||||||
|
buffer.remove_of(0, tail_index + 2);
|
||||||
return r;
|
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)
|
inline bool com_pack(FrameData* data, char** out_buf, int& len)
|
||||||
{
|
{
|
||||||
if (data == nullptr) {
|
if (data == nullptr) {
|
||||||
|
9
main.cxx
9
main.cxx
@ -54,10 +54,10 @@ int main(int argc, char* argv[])
|
|||||||
std::cout << "model_name:" << config.model_name << std::endl;
|
std::cout << "model_name:" << config.model_name << std::endl;
|
||||||
std::cout << "api:" << show_api(key) << std::endl;
|
std::cout << "api:" << show_api(key) << std::endl;
|
||||||
|
|
||||||
// if (show_api(key) == "NULL") {
|
if (show_api(key) == "NULL") {
|
||||||
// std::cerr << "api is invalid." << std::endl;
|
std::cerr << "api is invalid." << std::endl;
|
||||||
// return -1;
|
return -1;
|
||||||
// }
|
}
|
||||||
|
|
||||||
auto api = std::make_shared<COpenAI>();
|
auto api = std::make_shared<COpenAI>();
|
||||||
api->set_base(config.base_url, key);
|
api->set_base(config.base_url, key);
|
||||||
@ -86,6 +86,7 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
server.set_worker(api, json);
|
server.set_worker(api, json);
|
||||||
server.start();
|
server.start();
|
||||||
|
io_context.run();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,18 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
Server::Server(asio::io_context& io_context, short port)
|
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()
|
void Server::start()
|
||||||
{
|
{
|
||||||
|
asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), port_);
|
||||||
try {
|
try {
|
||||||
|
acceptor_.open(endpoint.protocol());
|
||||||
acceptor_.set_option(asio::socket_base::reuse_address(true));
|
acceptor_.set_option(asio::socket_base::reuse_address(true));
|
||||||
|
acceptor_.bind(endpoint);
|
||||||
acceptor_.listen();
|
acceptor_.listen();
|
||||||
do_accept();
|
do_accept();
|
||||||
} catch (const std::exception& e) {
|
} 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) {
|
if (frame == nullptr) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
std::cout << client_key << " 's data." << std::endl;
|
||||||
if (frame->type == FrameType::TYPE_REQUEST) {
|
if (frame->type == FrameType::TYPE_REQUEST) {
|
||||||
ask_mutex_.lock();
|
ask_mutex_.lock();
|
||||||
std::string recv_data(frame->data, frame->len);
|
std::string recv_data(frame->data, frame->len);
|
||||||
|
1
server.h
1
server.h
@ -44,6 +44,7 @@ private:
|
|||||||
std::unordered_map<std::string, std::thread> clients_;
|
std::unordered_map<std::string, std::thread> clients_;
|
||||||
std::map<std::string, std::shared_ptr<ClientCache>> client_map_;
|
std::map<std::string, std::shared_ptr<ClientCache>> client_map_;
|
||||||
CMutBuffer buffer_{};
|
CMutBuffer buffer_{};
|
||||||
|
short port_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
x
Reference in New Issue
Block a user