Reputation: 43
My Plan is to have a message protocol as outlined below:
MessageLength
: integer with total message lengthMessageID
: integer with auto incrementing identifierMessageType
: integer (enum
) with the type of messagePayload
: byte[]
with the Payload of the message (Serialised POJO)At the moment I just am looking at making sure I create the ByteBuf
in the correct and optimal way. At this stage I have the following:
io.netty.buffer.ByteBuf buf = Unpooled.buffer();
// Test Data Class
TestData test = new TestData("FirstName", "LastName", 40);
// Convert to Bytes is method to return the Byte Array (Serialised POJO)
byte[] payload = convertToBytes(test);
// Write 1 simulating MessageID 1
buf.writeInt(1);
// Write 2 simulating MessageType of 2
buf.writeInt(2);
// Write The Payload data<BR>
buf.writeBytes(payload);
The goal would be to them pass on to a LengthFieldPrepender
and up the pipeline.
Is this the right way to create the ByteBuf
? Or is there a better way?
Upvotes: 1
Views: 706
Reputation: 23567
You should use the allocator to create the ByteBuf if possible as this may give you a pooled ByteBuf (depending on the configuration).
Something like:
Channel channel = ...
channel.alloc().buffer(expectedSize)
Also note that I passed in the expected size of the ByteBuf as this will ensure we not need to expand it later on when you fill it.
Upvotes: 2