使用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. 前端神器-神级代码编辑软件Sublime Text下载、使用教程、插件推荐说明、全套快捷键

    Sublime Text 是一个代码编辑器,也是HTML和散文先进的文本编辑器.Sublime Text是由程序员Jon Skinner于2008年1月份所开发出来,它最初被设计为一个具有丰富扩展功能 ...

  2. 【面试】我是如何在面试别人Spring事务时“套路”对方的

    “中国最好面试官” 自从上次写了一篇“[面试]我是如何面试别人List相关知识的,深度有点长文”的文章后,有读者专门加我微信,说我是“中国最好面试官”,这个我可受不起呀. 我只是希望把面试当作是一次交 ...

  3. Spring Boot(六):如何优雅的使用 Mybatis

    *:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.m ...

  4. Springboot 系列(八)动态Banner与图片转字符图案的手动实现

    使用过 Springboot 的对上面这个图案肯定不会陌生,Springboot 启动的同时会打印上面的图案,并带有版本号.查看官方文档可以找到关于 banner 的描述 The banner tha ...

  5. c# 解决Randoms伪随机重复问题

    /// <summary> /// 解决伪随机问题 /// </summary> public static void Random() { ; ; i < ; i++) ...

  6. C++,std::shared_future的使用

    今天给大家分享一个类似多线程任务的方法,具体如下: std::shared_future<int> tmp = std::async(p1,p2,p3); int tmpInt = tmp ...

  7. 如何将Eclipse的javaWeb项目改为IDEA的maven项目

    1.首先去IDEA开发工具创建一个maven项目,把该项目改为Web项目, a.在pom.xml中,添加packaging标签,值为war b.右键File,选中project structure, ...

  8. 使用pyton在本地指定目录模拟服务器

    1.cd 到指定目录 2.运行命令 python 3之前 python -m SimpleHTTPServer & python 3+ python -m http.server & ...

  9. selenium-自动化用例(十一)

    思路 将页面操作与用例case分别封装,编写case时就可以用同一个操作方法对应多个case 如下图: PageGUI:存放页面操作方法,每个页面写一个文件,每个文件中写同一个页面不同的操作,例如检索 ...

  10. 简单的C#实体映射 AutoMapper

    AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 要映射实体 public class SourceModel { public int ...