diff --git a/.vscode/settings.json b/.vscode/settings.json index ef06018..1cf3ae4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -123,6 +123,7 @@ "shared_mutex": "cpp", "thread": "cpp", "variant": "cpp", - "*.ipp": "cpp" + "*.ipp": "cpp", + "xthread": "cpp" } } \ No newline at end of file diff --git a/README.md b/README.md index 5e5aea3..010ebae 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 例如: -`API`功能示例描述:帮我起一个简短的全大写的名字,如`Absolute battery charging voltage too low`,可以起名`MOI_CLOSE_FAILED`,帮我总结:LoadPort not initialized or initialized incorrectly,注意,直接回复我结果就行,不要添加任何其他内容。 +`API`功能示例描述:帮我起一个简短的全大写的名字,如`Absolute battery charging voltage too low`,可以起名`AB_BATTERY_LOW_VOL`,帮我总结:LoadPort not initialized or initialized incorrectly,注意,直接回复我结果就行,不要添加任何其他内容。 然后只需要提交数据,获取返回结果。 diff --git a/communicate.hpp b/communicate.hpp index 48869b9..5576c3e 100644 --- a/communicate.hpp +++ b/communicate.hpp @@ -12,8 +12,8 @@ enum FrameType : int16_t { TYPE_REQUEST = 0, - TYPE_RESPONSE, - TYPE_ERROR + TYPE_RESPONSE_SUCCESS, + TYPE_RESPONSE_ERROR }; struct FrameData { diff --git a/server.cxx b/server.cxx index c837573..f39c75a 100644 --- a/server.cxx +++ b/server.cxx @@ -74,10 +74,43 @@ void Server::th_client(const std::shared_ptr& socket, con } if (frame->type == FrameType::TYPE_REQUEST) { ask_mutex_.lock(); - // + std::string recv_data(frame->data, frame->len); + std::string out{}; + if (!worker_->post(post_data(recv_data), out)) { + std::cout << client_key << " data post error" << std::endl; + FrameData req; + req.type = FrameType::TYPE_RESPONSE_ERROR; + send_frame(socket, req); + } else { + FrameData req; + req.type = FrameType::TYPE_RESPONSE_SUCCESS; + req.data = new char[out.size()]; + req.len = out.size(); + memcpy(req.data, out.c_str(), out.size()); + send_frame(socket, req); + } ask_mutex_.unlock(); } delete frame; } } } + +std::string Server::post_data(const std::string& data) +{ + return std::string(); +} + +bool Server::send_frame(const std::shared_ptr& socket, FrameData& data) +{ + asio::error_code error; + char* send_data{}; + int len{}; + + if (!com_pack(&data, &send_data, len)) { + return false; + } + + auto send_len = socket->send(asio::buffer(send_data, len)); + return send_len == len; +} diff --git a/server.h b/server.h index 4f60645..f91b20a 100644 --- a/server.h +++ b/server.h @@ -30,6 +30,9 @@ private: void th_client(const std::shared_ptr& socket, const std::string& client_key); + std::string post_data(const std::string& data); + bool send_frame(const std::shared_ptr& socket, FrameData& data); + private: asio::io_context& io_context_; asio::ip::tcp::acceptor acceptor_;