google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
23.28k stars 3.25k forks source link

Why Deserialize object is NULL in JAVA? #4070

Closed Gompangs closed 8 years ago

Gompangs commented 8 years ago

Hi, I'm using flatbuffer for my sample game.

Client : C#(Unity) Server : JAVA(netty)

Protocol Schema is `table EchoMsg{ ProtocolId:short; Msg:string; }

table TestMsg{ Msg:string; }`

So, I tried on Client(C#) object serialize like below `var fbb = new FlatBufferBuilder(1); string testStr = "FlatBuffer~ Flatbuffer Test !@#"; var strOffset = fbb.CreateString(testStr); EchoMsg.StartEchoMsg(fbb); EchoMsg.AddProtocolId(fbb, (short)1234); EchoMsg.AddMsg(fbb, strOffset); var endOffset = EchoMsg.EndEchoMsg(fbb); fbb.Finish(endOffset.Value);

sendToServer(fbb.DataBuffer.Data, SendOption.Reliable); ` = 128 Bytes

and Server side receive those data with ByteBuf(netty based buffer)

Server(Java) Receive and deserialize code is below @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    ByteBuf msgBuffer = (ByteBuf) msg;
    byte[] bytes;
    int length = msgBuffer.readableBytes();

    if (msgBuffer.hasArray()) {
        bytes = msgBuffer.array();
    } else {
        bytes = new byte[length];
        msgBuffer.getBytes(msgBuffer.readerIndex(), bytes);
    }

    String test = "";
    for(int i=4; i<bytes.length-4; i++){
        test += bytes[i];
    }

    logger.debug(test);

            // This is 132 Bytes(128 + 4Bytes(size) )
    ByteBuffer data = ByteBuffer.wrap(bytes);

    // Packet Size extract(first 4Bytes is Packet size)
    int PacketSize = data.getInt();

    EchoMsg echoMsg = EchoMsg.getRootAsEchoMsg(data);

    logger.debug("{}", echoMsg.ProtocolId());
    logger.debug("{}", echoMsg.Msg());

    ctx.writeAndFlush(msg); // Echo Message
}

but it printed just string ->"null" and short ->"0".. I'm wondering which part was wrong.

On Java side just simple way to check data is append byte and print it. So , I tried that and Raw Data is exist! but it doesn't Deserialized in flatbuffers.

Received Bytes => 000000000000000000000000000000000000000000000000000000001200080120100408000800000-46442000-19-108-116-21-98-85-21-78-124-19-115-6832-19-123-116-20-118-92-19-118-723270108971169811710210210111432841011151163233

To String like this=>

zxcz

I was tried almost things.. but I didn't found mistake. I guess it's Netty(NIO TCP Server) problem cause it using own buffer(ByteBuf).

Please give me some idea to solve this problem. Thanks!

ghost commented 8 years ago

Note what it says here: http://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html

// This must be called after `Finish()`.
var buf = builder.DataBuffer; // Of type `FlatBuffers.ByteBuffer`.
// The data in this ByteBuffer does NOT start at 0, but at buf.Position.
// The end of the data is marked by buf.Length, so the size is
// buf.Length - buf.Position.
// Alternatively this copies the above data out of the ByteBuffer for you:
bytes[] buf = builder.SizedByteArray();
Gompangs commented 8 years ago

I changed from fbb.DataBuffer.Data to fbb.SizedByteArray() and it's perpectly working now ! Thanks for your reply.