Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
- if (msg instanceof Response) {
- ctx.writeAndFlush(generateDataByProtocol((Response) msg), promise);
- } else {
- ctx.writeAndFlush(msg, promise);
- }
- }
- private ByteBuf generateDataByProtocol(Response response) {
- ByteBuf[] byteBufs;
- if (response.hasMessage()) {
- byteBufs = new ByteBuf[5];
- } else {
- byteBufs = new ByteBuf[3];
- }
- byteBufs[0] = Unpooled.buffer(1).writeByte(response.getActionType().getValue());
- byteBufs[1] = Unpooled.buffer(1).writeByte(response.getOptionType().getValue());
- byteBufs[2] = Unpooled.copyShort(response.getStatus());
- if (response.hasMessage()) {
- byte[] messageBytes = response.getMessage().getBytes();
- byteBufs[3] = Unpooled.copyInt(messageBytes.length);
- byteBufs[4] = Unpooled.wrappedBuffer(messageBytes);
- }
- return Unpooled.wrappedBuffer(byteBufs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement