我有一个架构:
namespace ExampleApp.Assets.Communications;
table FlatServerToClientMessage {
messageid:string;
imagebytes:[ubyte];
}
root_type FlatServerToClientMessage;
为了清楚起见,这里是ServerToClientMessage.cs
:
public class ServerToClientMessage : EventArgs
{
public Guid MessageId { get; set; }
public byte[] ImageBytes { get; set; }
}
我正在尝试通过 TCP 发送我的ServerToClientMessage
. 当我使用 JSON 时这很好用,所以在其他地方不是问题。
我有一个FlatSerialize
扩展方法,它应该序列化ServerToClientMessage
并返回一个字节数组。然而,我buffer.Data
的总是空的......它只是充满了零。我的ServerToClientMessage
确实有数据 - 它确实有 MessageId 和 ImageBytes。
这是序列化扩展:
public static byte[] FlatSerialize(this ServerToClientMessage message) {
var builder = new FlatBufferBuilder(1);
//Create an ID
var MessageId = builder.CreateString(message.MessageId.ToString());
//Start the vector...
//Loop over each byte and add it - my god, is there not a better way?
FlatServerToClientMessage.StartImagebytesVector(builder, message.ImageBytes.Length);
foreach(var imageByte in message.ImageBytes) {
builder.AddByte(imageByte);
}
var imagebytes = builder.EndVector();
//Start the FlatServerToClientMessage and add the MessageId and imagebytes
FlatServerToClientMessage.StartFlatServerToClientMessage(builder);
FlatServerToClientMessage.AddMessageid(builder, MessageId);
FlatServerToClientMessage.AddImagebytes(builder, imagebytes);
//End the FlatServerToClientMessage and finish it...
var flatMessage = FlatServerToClientMessage.EndFlatServerToClientMessage(builder);
FlatServerToClientMessage.FinishFlatServerToClientMessageBuffer(builder, flatMessage);
var buffer = builder.DataBuffer;
return buffer.Data;
}
有谁知道为什么我buffer.Data
的只是用零填充?