From f99b2cbabbbaf2ed01ffee62cf86820ae4e2e8d1 Mon Sep 17 00:00:00 2001 From: taynpg Date: Wed, 5 Mar 2025 10:20:37 +0800 Subject: [PATCH] =?UTF-8?q?change=EF=BC=9A1.=E9=85=8D=E7=BD=AE=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E4=BD=BF=E7=94=A8v3=E3=80=822.asio=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E8=BD=ACu8=E3=80=823.com*hpp=E6=B7=BB=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AAtrim=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- communicate.hpp | 12 +++++++++++- openai.ini | 2 +- server.cxx | 10 +++++++--- util.hpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 util.hpp diff --git a/communicate.hpp b/communicate.hpp index 1228d0a..e316f9a 100644 --- a/communicate.hpp +++ b/communicate.hpp @@ -75,6 +75,15 @@ public: std::lock_guard lock(mutex_); buffer_.clear(); } + static std::string com_trim(const std::string& input) + { + size_t start = input.find_first_not_of(" \t\n\r\f\v"); + if (start == std::string::npos) { + return ""; + } + size_t end = input.find_last_not_of(" \t\n\r\f\v"); + return input.substr(start, end - start + 1); + } private: std::vector buffer_; @@ -120,7 +129,8 @@ inline FrameData* com_parse(CMutBuffer& buffer) } 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)); + 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); diff --git a/openai.ini b/openai.ini index e6e3125..fd2c077 100644 --- a/openai.ini +++ b/openai.ini @@ -2,6 +2,6 @@ BaseURL = https://api.lkeap.cloud.tencent.com/v1/chat/completions ApiEnvKey = TENCENT_DEEPSEEK_KEY UserName = user -ModelName = deepseek-r1 +ModelName = deepseek-v3 AssistantName = assistant MaxTokens = 50000 diff --git a/server.cxx b/server.cxx index 5e19a07..ef96332 100644 --- a/server.cxx +++ b/server.cxx @@ -1,4 +1,5 @@ #include "server.h" +#include "util.hpp" #include Server::Server(asio::io_context& io_context, short port) : io_context_(io_context), acceptor_(io_context) @@ -18,12 +19,16 @@ 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_.set_option(asio::socket_base::reuse_address(true)); acceptor_.bind(endpoint); acceptor_.listen(); do_accept(); } catch (const std::exception& e) { +#ifdef _WIN32 + std::cerr << ansi_to_u8(e.what()) << '\n'; +#else std::cerr << e.what() << '\n'; +#endif } } @@ -123,8 +128,7 @@ void Server::th_client(const std::shared_ptr& socket, con req.coptk = parse.completion_tokens; use_tokens_ += req.protk; use_tokens_ += req.coptk; - std::cout << "Already use " << use_tokens_ << " tokens.\r"; - std::cout.flush(); + std::cout << "Already use " << use_tokens_ << " tokens.\n"; memcpy(req.data, parse.message_content.c_str(), parse.message_content.size()); req.data[req.len - 1] = '\0'; send_frame(socket, req); diff --git a/util.hpp b/util.hpp new file mode 100644 index 0000000..c6b2fcb --- /dev/null +++ b/util.hpp @@ -0,0 +1,31 @@ +#ifndef UTIL_HPP +#define UTIL_HPP + +#ifdef _WIN32 +#include +#endif + +#include + +#ifdef _WIN32 +std::string ansi_to_u8(const std::string& str) +{ + int wideCharLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0); + if (wideCharLen <= 0) { + return ""; + } + std::wstring wideStr(wideCharLen, L'\0'); + MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wideStr[0], wideCharLen); + + int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, nullptr, 0, nullptr, nullptr); + if (utf8Len <= 0) { + return ""; + } + std::string utf8Str(utf8Len, '\0'); + WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, &utf8Str[0], utf8Len, nullptr, nullptr); + + utf8Str.resize(utf8Len - 1); + return utf8Str; +} +#endif +#endif // UTIL_HPP \ No newline at end of file