1、根据协议实现粘包、拆包处理器
/**
* 根据协议计算长度,实现粘包、拆包处理
*/
public abstract class AbstractDecoder extends LengthFieldBasedFrameDecoder {
public AbstractDecoder() {
super(Integer.MAX_VALUE, 2, 4, -6, 0, true);
}
}
2、编解码接口、消息处理接口
/**
* 编解码接口
*
* @param <T>
*/
public interface ICoder<T> extends Serializable {
T decode(byte[] bytes);
byte[] encode();
}
/**
* 消息处理接口
*/
public interface IMessageHandler {
// 请求处理
void request(ChannelHandlerContext ctx, BasicMessage request) throws IOException;
// 及时响应处理
void response(ChannelHandlerContext ctx, BasicMessage response);
// 定时响应处理
void scheduleResponse(ChannelHandlerContext ctx, BasicMessage response, Long time, TimeUnit timeUnit);
}
3、根据协议定义消息类并继承 ICoder 编解码接口
基础消息类
@Getter
@Setter
&