使用c++实现gRPC远程调用框架中传输文件,proto文件如下:

syntax = "proto3";
package transferfile;
service TransferFile {
rpc Upload(stream Chunk) returns (Reply) {}
}
message Chunk {
bytes buffer = 1;
}
message Reply {
int32 length = 1;
}

对应的c++代码如下:

client端:

#include <iostream>
#include <string>
#include <fstream>
#include <sys/time.h>
#include <grpc/grpc.h>
#include <grpc++/channel.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/security/credentials.h>
#include "transfer_file.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::ClientWriter;
using grpc::Status;
using transferfile::Chunk;
using transferfile::Reply;
using transferfile::TransferFile;
#define CHUNK_SIZE 1024 * 1024
class TransferFileClient
{
public:
TransferFileClient(std::shared_ptr<Channel> channel) : stub_(TransferFile::NewStub(channel)){};
void Upload();
private:
std::unique_ptr<TransferFile::Stub> stub_;
}; void TransferFileClient::Upload()
{
Chunk chunk;
char data[CHUNK_SIZE];
Reply stats;
ClientContext context;
const char *filename = "./large_file_in";
std::ifstream infile;
int len = ;
struct timeval start, end; gettimeofday(&start, NULL);
infile.open(filename, std::ifstream::in | std::ifstream::binary);
std::unique_ptr<ClientWriter<Chunk>> writer(stub_->Upload(&context, &stats));
while (!infile.eof()) {
infile.read(data, CHUNK_SIZE);
chunk.set_buffer(data, infile.gcount());
if (!writer->Write(chunk)) {
// Broken stream.
break;
}
len += infile.gcount();
}
writer->WritesDone();
Status status = writer->Finish();
if (status.ok()) {
gettimeofday(&end, NULL);
std::cout << (end.tv_sec-start.tv_sec)+ (double)(end.tv_usec-start.tv_usec)/ << std::endl;
} else {
std::cout << "TransferFile rpc failed." << std::endl;
}
}
int main(int argc, char** argv){
TransferFileClient guide(grpc::CreateChannel("localhost:10000", grpc::InsecureChannelCredentials()));
guide.Upload();
return ;
}

server端:

#include <iostream>
#include <fstream>
#include <string>
#include <grpc/grpc.h>
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc++/security/server_credentials.h>
#include "transfer_file.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerReader;
using grpc::Status;
using transferfile::Chunk;
using transferfile::Reply;
using transferfile::TransferFile;
#define CHUNK_SIZE 1024 * 1024 class TransferFileImpl final : public TransferFile::Service {
public:
Status Upload(ServerContext* context, ServerReader<Chunk>* reader, Reply* reply);
}; Status TransferFileImpl::Upload(ServerContext* context, ServerReader<Chunk>* reader, Reply* reply) {
Chunk chunk;
const char *filename = "./server_tmp";
std::ofstream outfile;
const char *data;
outfile.open(filename, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
while (reader->Read(&chunk)) {
data = chunk.buffer().c_str();
outfile.write(data, chunk.buffer().length());
}
long pos = outfile.tellp();
reply->set_length(pos);
outfile.close();
return Status::OK;
} void RunServer() {
std::string server_address("0.0.0.0:50051");
TransferFileImpl service;
ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
server->Wait();
} int main(int argc, char** argv) {
RunServer();
return ;
}

使用c++如何实现在gRPC中传输文件的更多相关文章

  1. Linux学习系列--如何在Linux中进行文件的管理

    文件 在常见的Linux的文件系统中,经常使用能了解到的文件管理系统是分为多个文件夹进行管理的. 如何查看文件路径 pwd ,在文件目录中,会有一个点(.)代表的是当前目录,两个点(..)代表的是当前 ...

  2. grpc中的拦截器

    0.1.索引 https://waterflow.link/articles/1665853719750 当我们编写 HTTP 应用程序时,您可以使用 HTTP 中间件包装特定于路由的应用程序处理程序 ...

  3. linux命令(28):Linux下SCP无需输入密码传输文件,python 中scp文件

    python 中scp文件:(如果下面的发送免密码已经完成的话,就直接能用下面这个) os.system('scp "%s" "%s:%s"' % (" ...

  4. 工具WinSCP:windows和Linux中进行文件传输

    工具WinSCP:windows和Linux中进行文件传输 2016-09-21 [转自]使用WinSCP软件在windows和Linux中进行文件传输 当我们的开发机是Windows,服务器是Lin ...

  5. Linux中常用文件传输命令及使用方法

    sftp sftp即Secure Ftp 是一个基于SSH安全协议的文件传输管理工具.由于它是基于SSH的,会在传输过程中对用户的密码.数据等敏感信息进行加密,因此可以有效的防止用户信息在传输的过程中 ...

  6. 【转】参照protobuf,将json数据转换成二进制在网络中传输。

    http://blog.csdn.net/gamesofsailing/article/details/38335753?utm_source=tuicool&utm_medium=refer ...

  7. 用WPF实现在ListView中的鼠标悬停Tooltip显示

    原文:用WPF实现在ListView中的鼠标悬停Tooltip显示 一.具体需求描述 在WPF下实现,当鼠标悬停在ListView中的某一元素的时候能弹出一个ToolTip以显示需要的信息. 二.代码 ...

  8. 使用VUE实现在table中文字信息超过5个隐藏,鼠标移到时弹窗显示全部

    使用VUE实现在table中文字信息超过5个隐藏,鼠标移到时弹窗显示全部 <template> <div> <table> <tr v-for="i ...

  9. PySpark 的背后原理--在Driver端,通过Py4j实现在Python中调用Java的方法.pyspark.executor 端一个Executor上同时运行多少个Task,就会有多少个对应的pyspark.worker进程。

    PySpark 的背后原理 Spark主要是由Scala语言开发,为了方便和其他系统集成而不引入scala相关依赖,部分实现使用Java语言开发,例如External Shuffle Service等 ...

随机推荐

  1. Python3+unitest自动化测试初探(下篇)

    目录 9.用例结果校验 10.跳过用例 11.Test Discovery 12.加载用例 unittest官方文档 本篇随笔承接: Python3+unitest自动化测试初探(中篇) Python ...

  2. jenkins maven 自动远程发布到服务器,钉钉提醒团队

    jenkins 自动远程发布到服务器 1.安装jenkins 安装过程:自行百度 英文不好的,不要装最新版的jenkins.建议安装Jenkins ver. 2.138.4,此版本可以设置中文语言,设 ...

  3. Python:黑板课爬虫闯关第二关

    第二关依然是非常的简单 地址:http://www.heibanke.com/lesson/crawler_ex01/ 随便输入昵称呢密码,点击提交,显示如下: 这样看来就很简单了,枚举密码循环 po ...

  4. 自定义Visual Studio.net Extensions 开发符合ABP vnext框架代码生成插件[附源码]

    介绍 我很早之前一直在做mvc5 scaffolder的开发功能做的已经非常完善,使用代码对mvc5的项目开发效率确实能成倍的提高,就算是刚进团队的新成员也能很快上手,如果你感兴趣 可以参考 http ...

  5. 微擎模块的安装文件manifest.xml

    微擎在安装或卸载模块时会根据manifest.xml生成(或删除)数据库中相应记录,并执行manifest.xml里指定的脚本. manifest.xml文件内容详细介绍如下: manifest - ...

  6. 3D GIS 应用开发 —— 基于 Mapbox GL 的实践总结

    最近在折腾的 web 端的可视化项目,由于相关业务的需要,用到了 Mapbox 这一地图开发的神器.在此先奉上一个基于mapbox-gl实现的demo(来源:uber的deck.gl项目): 下面我们 ...

  7. JavaScript 节流函数 Throttle 详解

    在浏览器 DOM 事件里面,有一些事件会随着用户的操作不间断触发.比如:重新调整浏览器窗口大小(resize),浏览器页面滚动(scroll),鼠标移动(mousemove).也就是说用户在触发这些浏 ...

  8. java8新特性-默认方法

    作为一个java程序猿,经常会被问基础怎么样,对于这个问题,我理解的有两方面:一是对于java基础的理解和掌握,比如JDK的相关特性:二是工作的经历,毕竟,语言编程是一门实战性质的艺术,就算掌握了千万 ...

  9. C++设计模式视频讲解

    设计模式(C++) 视频网址: http://www.qghkt.com/ 设计模式(C++)视频地址: https://ke.qq.com/course/318637?tuin=a508ea62 目 ...

  10. qt5.11.2+vs2017环境下配置pcl1.8.1以及第三方依赖库vtk的编译

    1.准备工作 我所用的开发环境是win10下的qt5.11.2配置了vs2017的编译器,根据自己所用的VS的版本去官网下载对应版本的pcl库,如下 PCL-1.8.1-AllInOne-msvc20 ...