gusucode.com > 它是一个c_s网络编程框架源码程序 > st_asio_wrapper/file_client/file_client.cpp
#include <boost/tokenizer.hpp> //configuration #define SERVER_PORT 5050 //configuration #include "file_client.h" #define QUIT_COMMAND "quit" #define REQUEST_FILE "get" boost::uint32_t completed_client_num; int link_num = 1; __off64_t file_size; int main(int argc, const char* argv[]) { puts("usage: asio_client link_num"); if (argc > 1) link_num = std::min(256, std::max(atoi(argv[1]), 1)); puts("\nthis is a file client."); puts("type quit to end this client."); auto file_clients = new file_client[link_num]; for (auto i = 0; i < link_num; ++i) { // file_clients[i].set_server_addr(SERVER_PORT, "::1"); //ipv6 // file_clients[i].set_server_addr(SERVER_PORT, "127.0.0.1"); //ipv4 file_clients[i].set_index(i); file_clients[i].start_service(); } std::string str; while(true) { std::getline(std::cin, str); if (str == QUIT_COMMAND) { for (auto i = 0; i < link_num; ++i) file_clients[i].stop_service(); delete[] file_clients; break; } else if (str.size() > sizeof(REQUEST_FILE) && !strncmp(REQUEST_FILE, str.data(), sizeof(REQUEST_FILE) - 1) && isspace(str[sizeof(REQUEST_FILE) - 1])) { str.erase(0, sizeof(REQUEST_FILE)); char_separator<char> sep(" \t"); tokenizer<char_separator<char>> tok(str, sep); #if _MSC_VER >= 1700 || !defined _MSC_VER for (const auto& item : tok) { #else for (auto iter = begin(tok); iter != end(tok); ++iter) { const auto& item = *iter; #endif atomic_write32(&completed_client_num, 0); file_size = 0; auto begin_time = get_system_time().time_of_day().total_seconds(); if (file_clients[0].get_file(item)) { for (auto i = 1; i < link_num; ++i) file_clients[i].get_file(item); printf("transfer %s begin.\n", item.data()); unsigned percent = -1; while (atomic_read32(&completed_client_num) != (unsigned) link_num) { this_thread::sleep(get_system_time() + posix_time::milliseconds(50)); if (file_size > 0) { __off64_t total_rest_size = 0; for (auto i = 0; i < link_num; ++i) total_rest_size += file_clients[i].get_rest_size(); if (total_rest_size > 0) { auto new_percent = (unsigned) ((file_size - total_rest_size) * 100 / file_size); if (percent != new_percent) { percent = new_percent; printf("\r%u%%", percent); fflush(stdout); } } } } auto used_time = get_system_time().time_of_day().total_seconds() - begin_time; if (used_time > 0) printf("\r100%%\ntransfer %s end, speed: %ldkB/s.\n", item.data(), file_size / 1024 / used_time); else printf("\r100%%\ntransfer %s end.\n", item.data()); } } } else file_clients[0].talk(str); } return 0; } //restore configuration #undef SERVER_PORT //restore configuration