使用c++如何实现在gRPC中传输文件

使用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 
#include <string>
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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) : stub_(TransferFile::NewStub(channel)){}; void Upload();
private: std::unique_ptr 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 = 0; struct timeval start, end; gettimeofday(&start, NULL); infile.open(filename, std::ifstream::in | std::ifstream::binary); std::unique_ptr> 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)/1000000 << 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 0; }

server端:

#include 
#include 
#include <string>
#include 
#include 
#include 
#include 
#include 
#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* reader, Reply* reply);
};

Status TransferFileImpl::Upload(ServerContext* context, ServerReader* 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(builder.BuildAndStart()); std::cout << "Server listening on " << server_address << std::endl; server->Wait(); } int main(int argc, char** argv) { RunServer(); return 0; }

 

转载于:https://www.cnblogs.com/xingmuxin/p/10738488.html

你可能感兴趣的:(使用c++如何实现在gRPC中传输文件)