1.rest_rpc:https://github.com/qicosmos/rest_rpc
2.服务端:
server.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <thread>
#include "rpc_server.h"
using namespace rest_rpc;
using namespace rpc_service;
static const size_t MAX_RECORD_SIZE = 5 * 1024 * 1024; // 5M
struct file_record {
std::string content;
size_t offset = 0;
size_t total_size = 0;
MSGPACK_DEFINE(content, offset, total_size);
};
file_record download(rpc_conn conn, const std::string &file_path) {
static size_t offset = 0;
static size_t file_size = 0;
static bool file_is_open = false;
static bool file_read_over = false;
static std::ifstream file;
file_record record;
if (false == file_is_open) {
file.open(file_path.c_str(), std::ios::in | std::ios::binary);
if (!file.is_open()) {
return record;
}
file_is_open = true;
file.seekg(0, std::ios::end);
file_size = file.tellg();
file.seekg(0, std::ios::beg);
}
size_t len = 0;
if (offset + MAX_RECORD_SIZE <= file_size) {
offset += MAX_RECORD_SIZE;
len = MAX_RECORD_SIZE;
}
else {
len = file_size - offset;
offset = file_size;
}
record.content.resize(len);
file.read(&record.content[0], len);
record.offset = offset;
record.total_size = file_size;
if (offset == file_size) {
file_read_over = true;
}
if (true == file_read_over) {
file.close();
return record;
}
return record;
}
int main(int argc, const char **argv) {
if (argc != 2) {
std::cerr << "userage:./server port" << std::endl;
return -1;
}
rpc_server server(atoi(argv[1]), std::thread::hardware_concurrency(), 0, 0);
server.register_handler("download", download);
server.run();
return 0;
}
3.客户端
client.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <thread>
#include <chrono>
#include "rpc_client.hpp"
using namespace rest_rpc;
using namespace rpc_service;
struct file_record {
std::string content;
size_t offset = 0;
size_t total_size = 0;
MSGPACK_DEFINE(content, offset, total_size);
};
int main(int argc, const char **argv) {
if (argc != 5) {
std::cerr << "userage:./client ip port file_path dest_file_path" << std::endl;
return -1;
}
rpc_client client(argv[1], atoi(argv[2]));
bool r = client.connect(5);
if (!r) {
std::cerr << argv[1] << ":" << argv[2] << " connect timeout." << std::endl;
return -1;
}
file_record record;
std::string content;
do {
try {
record = client.call<file_record>("download", argv[3]);
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return -1;
}
content += record.content;
if (record.offset == record.total_size) {
break;
}
} while (true);
std::ofstream file(argv[4], std::ios::out | std::ios::binary);
if (!file) {
std::cerr << argv[4] << " file open failed." << std::endl;
return -1;
}
file.write(content.data(), content.size());
file.close();
return 0;
}
4.编译:
g++ -g -o server_file_rest_rpc server.cpp -std=c++11 -I ../../rest_rpc/include -I ../../rest_rpc/third/msgpack/include -I /opt/boost_1_71_0 -pthread -lpthread -lbenchmark -D LOCAL
g++ -g -o client_file_rest_rpc client.cpp -std=c++11 -I ../../rest_rpc/include -I ../../rest_rpc/third/msgpack/include -I /opt/boost_1_71_0 -pthread -lpthread -lbenchmark -D LOCAL