debug:调试通过,待进一步测试。
This commit is contained in:
		
							parent
							
								
									2cdeb965c3
								
							
						
					
					
						commit
						7ae8a6cf70
					
				@ -6,12 +6,9 @@
 | 
				
			|||||||
#include <of_util.h>
 | 
					#include <of_util.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace fs = std::filesystem;
 | 
					namespace fs = std::filesystem;
 | 
				
			||||||
constexpr int g_SendPoolNum = 1;
 | 
					 | 
				
			||||||
CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger)
 | 
					CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    client_ = std::make_shared<CTcpClient>(io_context_, logger_);
 | 
					    client_ = std::make_shared<CTcpClient>(io_context_, logger_);
 | 
				
			||||||
    send_pool_ = std::make_shared<CThreadPool>(g_SendPoolNum);
 | 
					 | 
				
			||||||
    send_pool_->init();
 | 
					 | 
				
			||||||
    supported_.push_back("Get");
 | 
					    supported_.push_back("Get");
 | 
				
			||||||
    sleep_.set_timeout(2000);
 | 
					    sleep_.set_timeout(2000);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -34,6 +31,12 @@ CClient::~CClient()
 | 
				
			|||||||
    if (hearts_.joinable()) {
 | 
					    if (hearts_.joinable()) {
 | 
				
			||||||
        hearts_.join();
 | 
					        hearts_.join();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (auto& item : ths_) {
 | 
				
			||||||
 | 
					        if (item.joinable()) {
 | 
				
			||||||
 | 
					            item.join();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void CClient::run(const std::string& ip, const std::string& port)
 | 
					void CClient::run(const std::string& ip, const std::string& port)
 | 
				
			||||||
@ -304,7 +307,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
 | 
				
			|||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case TYPE_OPEN_FILE: {
 | 
					    case TYPE_OPEN_FILE: {
 | 
				
			||||||
        char* keys = nullptr;
 | 
					        std::string keys{};
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            std::lock_guard<std::mutex> lock(mutex_);
 | 
					            std::lock_guard<std::mutex> lock(mutex_);
 | 
				
			||||||
            up_[buf->fid_] = std::make_shared<TransInfomation>();
 | 
					            up_[buf->fid_] = std::make_shared<TransInfomation>();
 | 
				
			||||||
@ -314,15 +317,15 @@ void CClient::handle_frame(CFrameBuffer* buf)
 | 
				
			|||||||
            up_[buf->fid_]->cur_file_ = std::string(buf->data_, buf->len_);
 | 
					            up_[buf->fid_]->cur_file_ = std::string(buf->data_, buf->len_);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
            up_[buf->fid_]->file_ = fopen(up_[buf->fid_]->cur_file_.c_str(), "rb");
 | 
					            up_[buf->fid_]->file_ = fopen(up_[buf->fid_]->cur_file_.c_str(), "rb");
 | 
				
			||||||
 | 
					            up_[buf->fid_]->trans_state_ = TRANS_REDAY;
 | 
				
			||||||
            if (up_[buf->fid_]->file_ == nullptr) {
 | 
					            if (up_[buf->fid_]->file_ == nullptr) {
 | 
				
			||||||
                logger_->error("Ready Send File {} Open Failed.", up_[buf->fid_]->cur_file_);
 | 
					                logger_->error("Ready Send File {} Open Failed.", up_[buf->fid_]->cur_file_);
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            keys = new char[512]();
 | 
					            keys = buf->fid_;
 | 
				
			||||||
            std::snprintf(keys, 512, "%s", buf->fid_.c_str());
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (keys) {
 | 
					        if (!keys.empty()) {
 | 
				
			||||||
            send_pool_->submit([&]() { send_file_data_th(keys); });
 | 
					            ths_.emplace_back(std::thread([this, keys]() { send_file_data_th(keys.c_str()); }));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -348,14 +351,10 @@ void CClient::handle_frame(CFrameBuffer* buf)
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void CClient::send_file_data_th(char* keys)
 | 
					void CClient::send_file_data_th(const char* keys)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    std::string str_key(keys);
 | 
					    std::string str_key(keys);
 | 
				
			||||||
    std::shared_ptr<TransInfomation> t = nullptr;
 | 
					    std::shared_ptr<TransInfomation> t = nullptr;
 | 
				
			||||||
    std::shared_ptr<int> deleter(new int(0), [&](int* p) {
 | 
					 | 
				
			||||||
        delete p;
 | 
					 | 
				
			||||||
        delete[] keys;
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        std::lock_guard<std::mutex> lock(mutex_);
 | 
					        std::lock_guard<std::mutex> lock(mutex_);
 | 
				
			||||||
 | 
				
			|||||||
@ -51,22 +51,22 @@ private:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    void handle_frame(CFrameBuffer* buf);
 | 
					    void handle_frame(CFrameBuffer* buf);
 | 
				
			||||||
    void send_file_data_th(char* keys);
 | 
					    void send_file_data_th(const char* keys);
 | 
				
			||||||
    void hearts();
 | 
					    void hearts();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
 | 
					    std::mutex mutex_;
 | 
				
			||||||
 | 
					    std::mutex send_mut_;
 | 
				
			||||||
 | 
					    std::string work_key_;
 | 
				
			||||||
 | 
					    std::thread hearts_;
 | 
				
			||||||
 | 
					    CThreadSleep sleep_;
 | 
				
			||||||
 | 
					    bool th_run_{false};
 | 
				
			||||||
    std::shared_ptr<spdlog::logger> logger_;
 | 
					    std::shared_ptr<spdlog::logger> logger_;
 | 
				
			||||||
    asio::io_context io_context_;
 | 
					    asio::io_context io_context_;
 | 
				
			||||||
    std::shared_ptr<CTcpClient> client_;
 | 
					    std::shared_ptr<CTcpClient> client_;
 | 
				
			||||||
    std::vector<std::string> supported_;
 | 
					    std::vector<std::string> supported_;
 | 
				
			||||||
    std::map<int, std::shared_ptr<DownClientInfo>> task_list_;
 | 
					    std::map<int, std::shared_ptr<DownClientInfo>> task_list_;
 | 
				
			||||||
    std::shared_ptr<TransInfomation> down_;
 | 
					    std::shared_ptr<TransInfomation> down_;
 | 
				
			||||||
 | 
					    std::vector<std::thread> ths_;
 | 
				
			||||||
    std::map<std::string, std::shared_ptr<TransInfomation>> up_;
 | 
					    std::map<std::string, std::shared_ptr<TransInfomation>> up_;
 | 
				
			||||||
    std::mutex mutex_;
 | 
					 | 
				
			||||||
    std::mutex send_mut_;
 | 
					 | 
				
			||||||
    std::shared_ptr<CThreadPool> send_pool_;
 | 
					 | 
				
			||||||
    std::string work_key_;
 | 
					 | 
				
			||||||
    std::thread hearts_;
 | 
					 | 
				
			||||||
    CThreadSleep sleep_;
 | 
					 | 
				
			||||||
    bool th_run_{false};
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@ -136,7 +136,7 @@ void CTcpServer::trans_data(CFrameBuffer* buf)
 | 
				
			|||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    default:
 | 
					    default:
 | 
				
			||||||
        logger_->warn("No Mathched type.");
 | 
					        logger_->warn("No Mathched type. {}", static_cast<int>(buf->type_));
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user