《netty权威指南》私有协议栈开发中遇到的坑
2016-03-08 16:15
656 查看
这是我上传的项目的链接:直接下载项目
项目里面把:编解码、登录权限、心跳包都实现了。
《netty权威指南》私有协议栈开发:主要是书上写的不太全,而且有些地方要用到前几章学得东西,所以这里直接把一些书上的编解码粘贴出来
书上用到JBoss的marshalling进行编解码,所以首先要添加marshalling的依赖
在编解码前还有一个地方要注意一下,就是在client和server中书上有个坑
简单介绍一下:第一个参数是指数据包的最大值,第二个是协议中“长度”字段的偏移地址,第三个是“长度字段”的长度,第四个是针对2和3的一个容量修正,因为源码里面会会把参数2和3加起来,会使frameLength过大而抛异常
(1)frameLength就是协议可读范围的窗口,值是当前消息包的字节数。
(2)lengthFieldEndOffset是“长度”字段的偏移量(因为这里在编解码的时候要同时把长度字段一起处理,所以要把调整长度)
![](https://img-blog.csdn.net/20160308162012366?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160308162110470?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
由于netty包中关于marshalling的MarshallingDecode和MarshallingEncoder不知道什么原因变得protected了,所以要扩展成public
(编解码其实是用于协议的attachment字段,也就是附加数据)
项目里面把:编解码、登录权限、心跳包都实现了。
《netty权威指南》私有协议栈开发:主要是书上写的不太全,而且有些地方要用到前几章学得东西,所以这里直接把一些书上的编解码粘贴出来
书上用到JBoss的marshalling进行编解码,所以首先要添加marshalling的依赖
<pre name="code" class="java"><dependency> <groupId>org.jboss.marshalling</groupId> <artifactId>jboss-marshalling-serial</artifactId> <version>2.0.0.Beta2</version> </dependency>
在编解码前还有一个地方要注意一下,就是在client和server中书上有个坑
ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4, -8, 0));
简单介绍一下:第一个参数是指数据包的最大值,第二个是协议中“长度”字段的偏移地址,第三个是“长度字段”的长度,第四个是针对2和3的一个容量修正,因为源码里面会会把参数2和3加起来,会使frameLength过大而抛异常
(1)frameLength就是协议可读范围的窗口,值是当前消息包的字节数。
(2)lengthFieldEndOffset是“长度”字段的偏移量(因为这里在编解码的时候要同时把长度字段一起处理,所以要把调整长度)
由于netty包中关于marshalling的MarshallingDecode和MarshallingEncoder不知道什么原因变得protected了,所以要扩展成public
(编解码其实是用于协议的attachment字段,也就是附加数据)
public class NettyMarshallingDecoder extends MarshallingDecoder{ public NettyMarshallingDecoder(UnmarshallerProvider provider) { super(provider); } public NettyMarshallingDecoder(UnmarshallerProvider provider, int maxObjectSize) { super(provider, maxObjectSize); } @Override public Object decode(ChannelHandlerContext arg0, ByteBuf arg1) throws Exception { return super.decode(arg0, arg1); } }<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
public class NettyMarshallingEncoder extends MarshallingEncoder{ public NettyMarshallingEncoder(MarshallerProvider provider) { super(provider); } @Override public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { super.encode(ctx, msg, out); } }使用工厂方法去创建编解码器
public class MarshallingCodeCFactory { public static NettyMarshallingDecoder buildMarshallingDecoder() { MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial"); MarshallingConfiguration configuration = new MarshallingConfiguration(); configuration.setVersion(5); UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration); NettyMarshallingDecoder decoder = new NettyMarshallingDecoder(provider, 10240); return decoder; } public static NettyMarshallingEncoder buildMarshallingEncoder() { MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial"); MarshallingConfiguration configuration = new MarshallingConfiguration(); configuration.setVersion(5); MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration); NettyMarshallingEncoder encoder = new NettyMarshallingEncoder(provider); return encoder; } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树