c# 使用grpc实现文件上传下载

以下为纯手打, 如有错误及欠妥的地方,欢迎指正。

1. 关于根据.proto自动生成代码网上有很多教程,这里略过

2. file.proto代码添加

syntax = "proto3";
option csharp_namespace = "GrpcProto";
package greet;
service GrpcService{
    rpc FileDownLoad (FileInfo) returns (stream BytesContent);
    rpc FileUpLoad (stream BytesContent) returns();
}

message FileInfo{
    string fileName = 1;
}

message BytesContent{
    string fileName = 1;
    int32 Block = 2;
    bytes content = 3;
}

3. 服务端方法

download

public class MethodUtil:GrpcService.GrpcServicBase
{
    public override async Task FileDownLoad(FileInfo info, IServerStreamWriter responseStream, ServerCallContext context){
    if(File.Exist(info.fileName)){
        FileStream fs = FileStream(info.fileName, FileMode.Opne, FileAccess.Read)
        BinaryReader br = new BinaryReader(fs);
        byte[] byteArray = br.ReadBytes((int)fs.Length);
        int btSize = byteArray.Length;
        int buffSize = 1024*1024; //1M
        int lastBiteSize = biSize%buffSize;
        int currentTimes = 0;
        int loopTimes = btSize/buffSize;
        BytesContent contentStruct;
        while(currentTimes<=loopTimes){
            ByteString sbytes;
            if(currentTimes == loopTimes){
                sbytes = ByteString.CopyFrom(byteArray, currentTimes*buffSize, lastBiteSize);
            }
            else{
                sbytes = ByteString.CopyFrom(byteArray, currentTimes*buffSize, lastBiteSize);
            }
            contentStruct= new BytesContent{
            fileName = info.fileName;
            Block = currentTimes;
            content = sbytes 
            };
            await responseStream.WriteAsync(sbytes);
            currentTimes++;
        }
        
    }
    }
}

upload

public class MethodUtil:GrpcService.GrpcServicBase{

    public overrid async Task FileUpload(IAsyncStreamReader requestStream, ServerCallContext context){
    BytesContent contentStruct;
List contentList = new List();
using (var call = client.FileDownLoad(info)){
    while(await requestStream.MoveNext()){
        contentStruct = reaponseStream.Current;
        contentList.Add(contentStruct);
    }
    FileStream fs = new FileStream(contentList[0].fileName, FileMode.Create, FileAccess.ReadWrite);
    contentList.OrderBy(x=>x.Block).ToList.ForEach(x=>x.content.WriteTo(fs));
    fs.close(); 
}
}

4. 客户端调用

 download

//client 是通过channal初始化的对象
FileInfo v= new FileInfo{
    FileName = "test.pdf"
}
BytesContent contentStruct;
List contentList = new List();
using (var call = client.FileDownLoad(info)){
    var reaponseStream = call.ResponseStream;
    while(await reaponseStream.MoveNext()){
        contentStruct = reaponseStream.Current;
        contentList.Add(contentStruct );
    }
    FileStream fs = new FileStream(contentList[0].fileName, FileMode.Create, FileAccess.ReadWrite);
    contentList.OrderBy(x=>x.Block).ToList.ForEach(x=>x.content.WriteTo(fs));
    fs.close(); 
}

upload

using (var stream = client.FileUpload())
{
        FileStream fs = FileStream(info.fileName, FileMode.Opne, FileAccess.Read)
        BinaryReader br = new BinaryReader(fs);
        byte[] byteArray = br.ReadBytes((int)fs.Length);
        int btSize = byteArray.Length;
        int buffSize = 1024*1024; //1M
        int lastBiteSize = biSize%buffSize;
        int currentTimes = 0;
        int loopTimes = btSize/buffSize;
        BytesContent contentStruct;
        while(currentTimes<=loopTimes){
            ByteString sbytes;
            if(currentTimes == loopTimes){
                sbytes = ByteString.CopyFrom(byteArray, currentTimes*buffSize, lastBiteSize);
            }
            else{
                sbytes = ByteString.CopyFrom(byteArray, currentTimes*buffSize, lastBiteSize);
            }
            contentStruct= new BytesContent{
            fileName = info.fileName;
            Block = currentTimes;
            content = sbytes 
            };
            await stream.RequestStream.WriteAsync(contentStruct);
            currentTimes++;
        }
}

 

 

你可能感兴趣的:(rpc,c#)