From 97c3e80230c400af97b198df1894fc3e72144dd6 Mon Sep 17 00:00:00 2001 From: taynpg Date: Sat, 14 Dec 2024 11:57:33 +0800 Subject: [PATCH] =?UTF-8?q?change=EF=BC=9A=E5=BE=AE=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E7=BB=86=E8=8A=82=EF=BC=8C=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E6=9E=9A=E4=B8=BE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 ++++++++++++--- client/client.cpp | 33 ++++++++++++++---------- client/client.h | 2 +- server/server.cpp | 64 +++++++++++++++++++++++++++++++++++++---------- server/server.h | 7 ++++++ test2.cpp | 2 +- util/util.cpp | 11 +++++--- util/util.h | 19 +++++++++++--- 8 files changed, 120 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 6a5dd58..30cb470 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,22 @@ 当`mark`为`0`时表示数据的最后一包,其他数据表示非最后一包。 -`type`: 199,特殊标记,表示询问在线客户端及挂载任务。 +`type`: 199,表示询问在线客户端及挂载任务。 -`type`: 198,特殊标记,下载任务。 +`type`: 198,下载清单文件。 -`type`: 197,特殊标记,上载任务。 +`type`: 197,上载清单。 -`type`: 196,特殊标记,取消上载任务。 \ No newline at end of file +`type`: 196,取消上载任务。 + +`type`: 195,请求打开文件,返回mark值为1表示OK,为0表示失败。 + +`type`: 194,可以传输文件。 + +`type`: 193,文件数据。 + +`type`: 192,传输结束。 + +`type`:191,异常中断传输。 + +`type`:190,没有此清单。 diff --git a/client/client.cpp b/client/client.cpp index 73ac177..ff8b6ce 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -72,11 +72,14 @@ void CClient::run() bool CClient::get_task_list() { - return send_frame(199, gGet, std::strlen(gGet)); + std::shared_ptr buf = std::make_shared(); + buf->type_ = TYPE_GET_LIST; + return send_frame(buf.get()); } bool CClient::down_task() { + // if (send_frame(198, )) return false; } @@ -91,25 +94,30 @@ bool CClient::up_task(const std::string& cmd) msg.append("|" + item); } } - return send_frame(197, msg.data(), msg.size()); + if (msg.empty()) { + logger_->warn("{} msg empty.", __FUNCTION__); + return false; + } + std::shared_ptr buf = std::make_shared(); + buf->type_ = TYPE_UP_LIST; + buf->data_ = new char[msg.size()]; + std::memset(buf->data_, 0x0, msg.size()); + buf->len_ = std::snprintf(buf->data_, msg.size(), "%s", msg.data()); + return send_frame(buf.get()); } bool CClient::cancel_task() { - char buffer[] = "None"; - return send_frame(196, buffer, sizeof(buffer)); + std::shared_ptr buf = std::make_shared(); + buf->type_ = TYPE_CANCEL_LIST; + return send_frame(buf.get()); } -bool CClient::send_frame(int type, const char* data, int len) +bool CClient::send_frame(CFrameBuffer* buf) { - std::shared_ptr buf = std::make_shared(); - buf->type_ = type; - buf->data_ = new char[len + 1]; - std::memset(buf->data_, 0x0, len + 1); - buf->len_ = std::snprintf(buf->data_, len + 1, "%s", data); char* out_buf{}; int out_len{}; - if (!CTransProtocal::pack(buf.get(), &out_buf, out_len)) { + if (!CTransProtocal::pack(buf, &out_buf, out_len)) { logger_->error("{} pack failed.", __FUNCTION__); return false; } @@ -138,8 +146,7 @@ void CClient::handle_frame(CFrameBuffer* buf) for (const auto& item : vec) { if (item.find("[") == std::string::npos) { logger_->info("FILE ==> {}", item); - } - else { + } else { logger_->debug("***********************************************"); logger_->debug("{}", item); } diff --git a/client/client.h b/client/client.h index 458cb81..6660969 100644 --- a/client/client.h +++ b/client/client.h @@ -21,7 +21,7 @@ public: bool cancel_task(); private: - bool send_frame(int type, const char* data, int len); + bool send_frame(CFrameBuffer* buf); private: void handle_frame(CFrameBuffer* buf); diff --git a/server/server.cpp b/server/server.cpp index 90f0e5f..35ed610 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -74,7 +74,7 @@ std::vector CTcpServer::get_clients() SimpleBuffer* CTcpServer::get_client_list() { CFrameBuffer* buf = new CFrameBuffer(); - buf->type_ = 199; + buf->type_ = TYPE_GET_LIST; auto vec = get_clients(); std::string msg; @@ -122,31 +122,52 @@ void CTcpServer::handle_frame() continue; } - // 拿到该包后,要看转发给谁或者处理 - if (buf->type_ == 199) { // 询问在线客户端 + FrameType t = static_cast(buf->type_); + switch (t) { + case TYPE_GET_LIST: { logger_->info("GetList."); auto* sbuf = get_client_list(); if (sbuf == nullptr) { - continue; + break; } - sbuf->id_ = buf->id_; + sbuf->id_ = buf->fid_; std::lock_guard lock(sbuf_mut_); scache_.push(sbuf); - } else if (buf->type_ == 197) { + break; + } + case TYPE_UP_LIST: { logger_->info("UpList. {}", std::string(buf->data_, buf->len_)); std::lock_guard lock(cli_mut_); - if (client_map_.count(buf->id_)) { - auto& cli = client_map_[buf->id_]; + if (client_map_.count(buf->fid_)) { + auto& cli = client_map_[buf->fid_]; cli->task_ = std::string(buf->data_, buf->len_); cli->time_ = OfUtil::now_time(); } - } else if (buf->type_ == 196) { + break; + } + case TYPE_CANCEL_LIST: { logger_->info("Cancle Task."); std::lock_guard lock(cli_mut_); - if (client_map_.count(buf->id_)) { - auto& cli = client_map_[buf->id_]; + if (client_map_.count(buf->fid_)) { + auto& cli = client_map_[buf->fid_]; cli->task_.clear(); } + break; + } + case TYPE_OPEN_FILE: + case TYPE_READY_TRANS: + case TYPE_TRANS_FILE: { + std::lock_guard lock(cli_mut_); + if (client_map_.count(buf->tid_)) { + auto& cli = client_map_[buf->tid_]; + if (!send_frame(cli->socket_, buf)) { + logger_->error("turn failed to {}", buf->tid_); + } + } + break; + } + default: + break; } delete buf; buf = nullptr; @@ -245,11 +266,28 @@ void CTcpServer::th_client(std::shared_ptr socket, const cache->buffer_.push(cache->tmp_buf_.data(), length); auto* frame = CTransProtocal::parse(cache->buffer_); if (frame) { - frame->id_ = client_key; + frame->fid_ = client_key; push_frame(frame); } } } catch (std::exception& e) { logger_->error("Error with client {}: {}", client_key, e.what()); } -} \ No newline at end of file +} + +bool CTcpServer::send_frame(std::shared_ptr socket, CFrameBuffer* buf) +{ + char* out_buf{}; + int out_len{}; + if (!CTransProtocal::pack(buf, &out_buf, out_len)) { + logger_->error("{} pack failed.", __FUNCTION__); + return false; + } + if (!socket->send(asio::buffer(out_buf, out_len))) { + logger_->error("{} send failed.", __FUNCTION__); + delete[] out_buf; + return false; + } + delete[] out_buf; + return true; +} diff --git a/server/server.h b/server/server.h index fd123e7..bd75e1b 100644 --- a/server/server.h +++ b/server/server.h @@ -13,6 +13,7 @@ struct ClientCache { std::array tmp_buf_; std::string task_; std::string time_; + FrameType cur_type_{TYPE_DEFAULT}; }; struct TaskList { std::string id_; @@ -43,6 +44,12 @@ private: void accept_client(); void th_client(std::shared_ptr socket, const std::string& client_key); + /// @brief 不删除 buf + /// @param socket + /// @param buf + /// @return + bool send_frame(std::shared_ptr socket, CFrameBuffer* buf); + private: bool th_run_{false}; asio::io_context& io_context_; diff --git a/test2.cpp b/test2.cpp index 64505bc..d28f7ca 100644 --- a/test2.cpp +++ b/test2.cpp @@ -6,7 +6,7 @@ std::shared_ptr g_Logger; void TestHandle(CFrameBuffer* buf) { std::string chinese_test("中文测试"); - g_Logger->debug("type: {}", buf->type_); + g_Logger->debug("type: {}", static_cast(buf->type_)); g_Logger->info("len: {}", buf->len_); g_Logger->warn("{} exec. {} 1", __FUNCTION__, chinese_test); g_Logger->error("{} exec. {} 2", __FUNCTION__, chinese_test); diff --git a/util/util.cpp b/util/util.cpp index 079c751..01fc2f3 100644 --- a/util/util.cpp +++ b/util/util.cpp @@ -59,7 +59,7 @@ CFrameBuffer* CTransProtocal::parse(CMutBuffer& buffer) result->data_ = new char[len]; result->len_ = len; result->mark_ = mark; - result->type_ = type; + result->type_ = static_cast(type); std::memset(result->data_, 0x0, len); std::memcpy(result->data_, buffer.get_data() + find + 2 + 2 + 1 + 4, len); buffer.remove_of(0, tail_index + 2); @@ -68,9 +68,12 @@ CFrameBuffer* CTransProtocal::parse(CMutBuffer& buffer) bool CTransProtocal::pack(CFrameBuffer* buf, char** out_buf, int& len) { - if (buf == nullptr || buf->data_ == nullptr || buf->len_ < 1) { + if (buf == nullptr) { return false; } + if (buf->data_ == nullptr) { + buf->len_ = 0; + } unsigned char header[] = {0xFF, 0xFE}; unsigned char tail[] = {0xFF, 0xFF}; len = buf->len_ + 11; @@ -79,7 +82,9 @@ bool CTransProtocal::pack(CFrameBuffer* buf, char** out_buf, int& len) std::memcpy(*out_buf + 2, &buf->type_, 2); std::memcpy(*out_buf + 2 + 2, &buf->mark_, 1); std::memcpy(*out_buf + 2 + 2 + 1, &buf->len_, 4); - std::memcpy(*out_buf + 2 + 2 + 1 + 4, buf->data_, buf->len_); + if (buf->data_ != nullptr) { + std::memcpy(*out_buf + 2 + 2 + 1 + 4, buf->data_, buf->len_); + } std::memcpy(*out_buf + len - 2, tail, 2); return true; } diff --git a/util/util.h b/util/util.h index ab3c79e..00eefd9 100644 --- a/util/util.h +++ b/util/util.h @@ -6,7 +6,19 @@ #include #include -constexpr auto gGet = "Get"; +enum FrameType : int16_t { + TYPE_DEFAULT = 0, + TYPE_GET_LIST, + TYPE_DOWN_LIST, + TYPE_UP_LIST, + TYPE_CANCEL_LIST, + TYPE_OPEN_FILE, + TYPE_TRANS_FILE, + TYPE_READY_TRANS, + TYPE_INTERRUPT, + TYPE_NO_HIT_TASK, + TYPE_WAITTING +}; using namespace ofen; std::shared_ptr get_logger(const std::string& mark, const std::string& log_file); @@ -17,10 +29,11 @@ public: ~CFrameBuffer(); public: - std::string id_{}; + std::string fid_{}; + std::string tid_{}; public: - int16_t type_{}; + FrameType type_{}; char* data_{}; int len_{}; char mark_{};