2012-11-20 15 views
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異常。

如何解決這個問題?提前致謝!

回答

2

這有我的擔心:

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

protobuf的數據不是文本,這是肯定不UTF8。沒有任何有效的方式將protobuf二進制文件作爲UTF8傳遞,而不會破壞它。選項:

  1. 交換它完全作爲原始二進制; 沒有文字
  2. 使用類似基地-64,它可以任意的二進制編碼,安全地轉換成ASCII
相關問題