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

256 篇文章 3 订阅
149 篇文章 2 订阅

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

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
rest_rpc 是一个 C++ 的 RPC 框架,它支持 JSON 作为数据传输格式。在使用 rest_rpc 进行 JSON 解析时,可以使用第三方库 jsoncpp。 以下是在 rest_rpc 中解析 JSON 的步骤: 1. 引入 jsoncpp 库:在 CMakeLists.txt 中添加 jsoncpp 库的链接; 2. 定义 JSON 字符串:将 JSON 字符串作为参数传递给 rest_rpc 的远程调用函数; 3. 解析 JSON:使用 jsoncpp 库的解析函数将 JSON 字符串解析为 jsoncpp::Value 类型的对象,然后使用该对象进行数据的读取和操作。 以下是一个简单的示例代码,演示了如何在 rest_rpc 中解析 JSON: ```c++ #include <iostream> #include <rest_rpc/client.hpp> #include <json/json.h> using namespace rest_rpc; using namespace rpc_service; using namespace std; int main() { // 连接远端服务 client c("127.0.0.1", 9000); // 定义 JSON 字符串 string json_str = "{\"name\":\"Tom\", \"age\":20}"; // 解析 JSON Json::Value root; Json::Reader reader; bool parsing_successful = reader.parse(json_str, root); if (!parsing_successful) { cerr << "Failed to parse JSON string: " << json_str << endl; return -1; } // 读取 JSON 数据 string name = root["name"].asString(); int age = root["age"].asInt(); // 调用远端服务并传递 JSON 数据 int result = c.call<int>("add", name, age); cout << "The result is " << result << endl; return 0; } ``` 在以上代码中,我们使用了 jsoncpp 库中的 Json::Reader 类和 Json::Value 类来解析 JSON 字符串。首先,我们将 JSON 字符串传递给 Json::Reader 的 parse 函数进行解析,如果解析成功,则 Json::Value 类型的 root 对象中包含了 JSON 数据。然后,我们使用 root 对象中的 asString 和 asInt 函数来读取 JSON 数据,并将其作为参数传递给远端服务的 add 函数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值