1

私はprotobuf-netについて読んでいますが、それは素晴らしいことです。

一般的に、それは完璧に動作します。しかし、私はいくつかの問題に直面します。

私はprotobufを使ってPythonとC#の間の通信コードを書き込もうとしています。

以下の.proto:

message GetAllCalculate{
    required string agentID=1;
}

message CalculateInfo{
    required string CalStarttime=1;
    optional string CalEndtime=2;
    required string Smiles=3;
    optional string CAS=4;
    optional string ChName=5;
    optional string EnName=6;
    required string Param=7;
    required string Result=8;
    required bool IsFinished=9;
}

message GetAllCalulateResponse{
    required bool  isSuccessful = 1;
    required int32 Count=2;
    repeated CalculateInfo History=3;

}

Pythonクライアントでは、次のようなコードがあります。

msg_resp = GetAllCalulateResponse()
  calculateInfo = [None] * 2
    cnt = 0
    for result in resultSets:   #resultSets can read from other place,like database
        calculateInfo[cnt] = msg_resp.History.add()
        calculateInfo[cnt].CalStarttime = str(result.calculateStartTime)
        calculateInfo[cnt].CalEndtime = result.calculateEndTime.strftime('%Y-%m-%d %X')
        calculateInfo[cnt].IsFinished = result.isFinished
        calculateInfo[cnt].Param = result.paramInfo
        **calculateInfo[cnt].Result = str('ff'*50) #result.result**

        calculateInfo[cnt].Smiles = result.smilesInfo.smilesInfo
        calculateInfo[cnt].CAS = result.smilesInfo.casInfo


        nameSets = CompoundName.objects.filter(simlesInfo=result.smilesInfo.pk,isDefault=True)
        for nameSet in nameSets:
            if nameSet.languageID.languageStr == Chinese_Name_Label:
                calculateInfo[cnt].ChName = nameSet.nameStr 
            elif nameSet.languageID.languageStr == English_Name_Label:
                calculateInfo[cnt].EnName = nameSet.nameStr

        cnt = cnt +1 

C#コード(Protobuf-netを使用):

string retString = HTTPPost2UTF8(bytes, GetAllCalculateHandlerAPI); //Get from Python Clint
bytesOut = System.Text.Encoding.UTF8.GetBytes(retString);
MemoryStream streamOut = new MemoryStream(bytesOut);
GetAllCalulateResponse response = Serializer.Deserialize <GetAllCalulateResponse>(streamOut);

しかし**calculateInfo[cnt].Result = str('ff'*50) #result.result**、str('ff')* 5000のように大きくすると、C#クライアントはOverFlowExceptionをスローします。str('ff')* 100に設定すると、EndOfStreamExceptionがスローされます。

この問題をどのように解決できますか?前もって感謝します!

4

1 に答える 1

2

これは私が心配しています:

string retString = HTTPPost2UTF8(bytes, GetAllCalculateHandlerAPI); //Get from Python
bytesOut = System.Text.Encoding.UTF8.GetBytes(retString);
MemoryStream streamOut = new MemoryStream(bytesOut);

protobuf データは textではなく、確実に UTF8 ではありません。protobuf バイナリを破損せずに UTF8 として渡す有効な方法はありません。オプション:

  1. 生のバイナリとして完全に交換します。テキストなし
  2. 任意のバイナリを ASCII に安全にエンコードできる base-64 などを使用します。
于 2012-11-20T21:35:07.227 に答える