1
我一直在閱讀有關protobuf-net的內容,它很棒!Protobuf-net中的OverFlowException和EndOfStreamException
一般來說,它是完美的。但是我遇到了一些問題。
我想寫Python和C#之間的通信代碼protobuf的。
初級講座的.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網):
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異常。
如何解決這個問題?提前致謝!