1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| package com.chat.protocol;
import com.chat.message.Message; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.ByteToMessageCodec; import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.handler.logging.LoggingHandler; import lombok.Data; import lombok.ToString; import lombok.extern.slf4j.Slf4j;
import java.io.*; import java.util.List;
@Data abstract class MessageTest implements Serializable {
private int sequenceId;
private int messageType;
public abstract int getMessageType();
}
@Data @ToString(callSuper = true) class LoginRequestMessageTest extends MessageTest { private String username; private String password;
public LoginRequestMessageTest() { }
public LoginRequestMessageTest(String username, String password) { this.username = username; this.password = password; }
@Override public int getMessageType() { return 1; } }
@Slf4j class MessageCode extends ByteToMessageCodec<MessageTest> { @Override protected void encode(ChannelHandlerContext channelHandlerContext, MessageTest message, ByteBuf byteBuf) throws Exception { byteBuf.writeBytes(new byte[]{1, 2, 3, 4}); byteBuf.writeByte(1); byteBuf.writeByte(0); byteBuf.writeByte(message.getMessageType()); byteBuf.writeInt(message.getSequenceId()); byteBuf.writeByte(0xff); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(message); byte[] bytes = bos.toByteArray(); byteBuf.writeInt(bytes.length); byteBuf.writeBytes(bytes);
}
@Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
int magicNum = byteBuf.readInt(); byte version = byteBuf.readByte(); byte serializerType = byteBuf.readByte(); byte messageType = byteBuf.readByte(); int sequenceId = byteBuf.readInt(); byteBuf.readByte(); int length = byteBuf.readInt(); byte[] bytes = new byte[length]; byteBuf.readBytes(bytes, 0, length);
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes)); MessageTest message = (MessageTest) objectInputStream.readObject();
log.debug("{},{},{},{},{},{}", magicNum, version, serializerType, messageType, sequenceId, length); log.debug("{}", message);
list.add(message); } }
public class TestMessageCode {
public static void main(String[] args) throws Exception { EmbeddedChannel embeddedChannel = new EmbeddedChannel( new LengthFieldBasedFrameDecoder(1024, 12, 4, 0, 0), new LoggingHandler(), new MessageCode() );
LoginRequestMessageTest loginRequestMessage = new LoginRequestMessageTest("a", "b"); embeddedChannel.writeOutbound(loginRequestMessage);
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); new MessageCode().encode(null, loginRequestMessage, buf); embeddedChannel.writeInbound(buf);
}
}
|