0

我有一个架构:

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的只是用零填充?

4

1 回答 1

2

您的缓冲区不是空的,它只是没有从偏移量 0 开始Data(FlatBuffers 是从前构建的),它从Offset. 您可以从那里访问字节。或者,您可以调用FlatBufferBuilder.SizedByteArray()创建一个仅包含 FlatBuffer 数据的字节数组。

于 2016-08-06T14:15:56.783 回答