TCP数据从Xamarin Android应用程序损坏
本文关键字:应用程序 损坏 Android Xamarin 数据 TCP | 更新日期: 2023-09-27 18:08:47
我有一个Xamarin Android应用程序,从Android 4.4 (KitKat)设备(x86硬件)捕获h264视频帧,并通过TCP发送到Windows 10客户端(通过WiFi)。我使用protobuf.net打包帧SerializeWithLengthPrefix (Fixed32)。这在大多数情况下工作得很好,但随机地(在20秒到10分钟之间)接收端的数据会被损坏。您可以看到,我还将数据保存到设备中以供调试。使用客户端应用程序读取这些数据不会产生任何错误(它没有损坏)。我不知道问题出在哪里。这似乎是Xamarin的TCP客户端的一个平台错误,但我很难相信我是唯一一个遇到这个问题的人。注意:TCP通信在它自己的线程中工作。
private static void ProcessFrameQueue(TcpClient client)
{
//debug log for comparing TCP socket sent data with client recieved
_tempDumpFile = StreamControl.GetOutputTempFilePath(DateTime.Now.Ticks.ToString() + "-probuf-dump.bin");
var sentFrameCount = 0;
try
{
while (client.Client.IsConnected())
{
var data = _packetQueue.Take();
try
{
using (var stream = new MemoryStream())
{
Serializer.SerializeWithLengthPrefix(stream, data, PrefixStyle.Fixed32);
var protoBufData = stream.ToArray();
client.Client.Send(protoBufData);
//for debugging -- save the TCP data for comparison to what is recieved
//todo: delete as this is debuggng
using (var filestream = new FileStream(_tempDumpFile, FileMode.Append))
{
filestream.Write(protoBufData, 0, protoBufData.Length);
filestream.Flush(true);
}
}
sentFrameCount++;
}
catch (Exception ex)
{
//log error
}
} //end while
}
catch (Exception ex)
{
//log error
}
}
这是我为调试编写的一个简单的客户端(我手动计算数据包大小以确保它不是protobuf.net中的错误——这不是protobuf.net的问题)。最终,大小包将包含坏数据,导致溢出。
var client = new TcpClient("x.x.x.x", 19901);
client.ReceiveTimeout = 100000000;
byte[] bytes = new byte[client.ReceiveBufferSize];
var netStream = client.GetStream();
var sizezReadBtyes = 0;
var sizeBytes = new byte[4];
var packetCount = 0;
while (true)
{
//reads until it gets 4 bytes to calculate the packet size
var sizeOffset = 0;
var sizeLength = 4;
while ((sizezReadBtyes = netStream.Read(sizeBytes, sizeOffset, sizeLength)) > 0)
{
sizeOffset += sizezReadBtyes;
sizeLength -= sizezReadBtyes;
}
//read the remaining data...
var offset = 0;
var packetBytes = 0;
int packetlength = BitConverter.ToInt32(sizeBytes, 0);
var buffer = new byte[packetlength];
while (packetlength > 0 && (packetBytes = netStream.Read(buffer, offset, packetlength)) > 0)
{
offset += packetBytes;
packetlength -= packetBytes;
}
using (var ms = new MemoryStream(buffer))
{
var obj = ProtoBuf.Serializer.Deserialize<NetworkMediaPacket>(ms);
Console.WriteLine($"packet found {packetCount++} {obj.Data.Length}");
}
if (packetlength > 0) throw new EndOfStreamException();
}
我能够通过删除我的阻塞队列(_packetQueue)来修复。我只是在TCPClient NetStream上使用了writeasync。在我看来,这像是Xamarin/Mono/.net代码中的一个bug。