[英]Sending binary data with Protobuf-net
使用Protobuf-net發送二進制數組時,有什么特殊要求嗎? 我有一個消息類,它包裝了一些二進制數據:
[ProtoContract]
public class BitmapPackage : BaseMessage
{
[ProtoMember(1)]
public byte[] BitmapData;
}
當嘗試反序列化消息時,我捕獲了Protobuf網絡拋出的異常:
16:52:59.902:(007):[錯誤] StartReceiveLoop()中的內部異常:消息:試圖讀取流的末尾。堆棧跟蹤:在c中ProtoBuf.ProtoReader.Ensure(Int32 count,Boolean strict) :\\ Dev \\ protobuf-net \\ protobuf-net \\ ProtoReader.cs:C:\\ Dev \\ protobuf-net \\ protobuf-net \\ ProtoReader.cs中ProtoBuf.ProtoReader.AppendBytes(字節[]值,ProtoReader讀取器)的257行:位於ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read的proto_4(Object,ProtoReader)的第921行(對象值,ProtoReader源)在c:\\ Dev \\ protobuf-net \\ protobuf-net \\ Serialerizers \\ Comp。 :ProtoBuf.Meta.RuntimeTypeModel.Deserialize中的第57行(C:\\ Dev \\ protobuf-net \\ protobuf-net \\ Meta \\ RuntimeTypeModel.cs中的Int32鍵,對象值,ProtoReader源):ProtoBuf.Meta.TypeModel處的775行。在ProtoBuf.Meta.TypeModel.Deserialize(Stream sour)中的c:\\ Dev \\ protobuf-net \\ protobuf-net \\ Meta \\ TypeModel.cs:line 700中的DeserializeCore(ProtoReader reader,Type type,Object value,Boolean noAutoCreate) ce,對象值,類型類型,SerializationContext上下文)在c:\\ Dev \\ protobuf-net \\ protobuf-net \\ Meta \\ TypeModel.cs:ProtoBuf.Serializer.Deserialize [T](Stream source)在c:\\ Dev \\ protobuf-net \\ protobuf-net \\ Serializer.cs:第77行
僅當我發送一個特定的位圖時才會發生這種情況。 所以我的問題是,在使用protobuf-net反序列化之前,我應該以某種方式“轉義”二進制數據嗎?
更新:這是執行接收的代碼:
private async Task StartReceiveLoop()
{
await Task.Yield();
while(!_readOpCancelToken.IsCancellationRequested)
{
try
{
var buffer = new byte[4096];
int bytesRx = await _pipe.ReadAsync(
buffer, 0, buffer.Length, _readOpCancelToken);
Logger.LogInfo("Pipe connection: Got " + bytesRx + " bytes");
if (bytesRx == 0) break;
if (_readOpCancelToken.IsCancellationRequested) break;
await _receivedDataStream.WriteAsync(buffer, 0, bytesRx);
if (_pipe.IsMessageComplete)
{
// Get out the routing data
_receivedDataStream.Position = 0;
byte[] intbuffer = new byte[4];
_receivedDataStream.Read(intbuffer, 0, 4);
int size = BitConverter.ToInt32(intbuffer, 0);
_receivedDataStream.Read(intbuffer, 0, 4);
int typeId = BitConverter.ToInt32(intbuffer, 0);
_receivedDataStream.Read(intbuffer, 0, 4);
int routeTag = BitConverter.ToInt32(intbuffer, 0);
messageObj = messageObj = Serializer.NonGeneric.Deserialize(messageType, _receivedDataStream);
// Process message on UI thread via tick queue
TickSystem.OneTimeTickQueue.Enqueue(new Action(() =>
{
ProcessMessageExtApp.ProcessThisMessage(messageObj);
}));
// Reset the stream
_receivedDataStream = new MemoryStream();
}
}
catch (Exception e)
{
Logger.LogError(
"Exception in StartReceiveLoop():" +
"Message: " + e.Message +
"Stack Trace: " + e.StackTrace);
if (e.InnerException != null)
{
Logger.LogError(
"Inner Exception in StartReceiveLoop():" +
"Message: " + e.InnerException.Message +
"Stack Trace: " + e.InnerException.StackTrace);
}
}
}
}
實際的問題不是Protobuf網絡,而是一個事實,即IsMessageComplete提前發出信號。 解決方法是使用字節數組而不是流復制在管道服務器上寫入管道,並在完成每個數據包后刷新管道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.