rest_rpc应用-从服务器下载文件到本地

本文介绍了一个基于REST RPC的文件传输系统实现,包括服务端和客户端的C++代码示例。服务端通过RPC方式提供文件下载服务,客户端则请求并接收文件数据,适用于小到中型文件的高效传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值