netty之编解码
1、netty的编码和解码,在数据传输的时候,考虑数据安全,数据完整性都是很有必要的。这里主要是介绍netty3和netty5的编解码方式。其实从StringEncoder和StringDecoder中也可以获取源码的编解码规则。然后改变成自己的编解码规则也是可以的。
2、netty3和netty5的编解码方式还是存在一定差别的。个人感觉netty5来的更加实用和方便。
3、netty3的编解码规则
1)数据编码规则(我这里只是用于显示,数据规则很简单)
包头+模块+数据(请求编解码)
包头+模块+状态+数据(响应编解码)
2)目录

3)请求和相应对象
package com.troy.data.domain; //请求数据
public class Request { //模块类型
private int model;
//数据
private byte[] data; public int getModel() {
return model;
} public void setModel(int model) {
this.model = model;
} public byte[] getData() {
return data;
} public void setData(byte[] data) {
this.data = data;
} @Override
public String toString() {
return "Request{" +
"model=" + model +
", data=" + new String(data) +
'}';
}
}
package com.troy.data.domain; import java.util.Arrays; //响应数据
public class Response { //模块类型
private int model;
//状态码
private int status;
//数据
private byte[] data; public int getModel() {
return model;
} public void setModel(int model) {
this.model = model;
} public int getStatus() {
return status;
} public void setStatus(int status) {
this.status = status;
} public byte[] getData() {
return data;
} public void setData(byte[] data) {
this.data = data;
} @Override
public String toString() {
return "Response{" +
"model=" + model +
", status=" + status +
", data=" + new String(data) +
'}';
}
}
4)常量(这里的包头,因为是固定的就写了一个常量)
package com.troy.data.constant; //常量
public class ConsantUtil { //固定常量用于数据拼接,确认
public static final int PACKAGE_HEADER = -; }
5)请求编解码
package com.troy.data.codec; import com.troy.data.constant.ConsantUtil;
import com.troy.data.domain.Request;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; public class RequestEncode extends OneToOneEncoder { protected Object encode(ChannelHandlerContext channelHandlerContext, Channel channel, Object o) throws Exception { Request request = (Request) o;
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
channelBuffer.writeInt(ConsantUtil.PACKAGE_HEADER);
channelBuffer.writeInt(ConsantUtil.PACKAGE_HEADER);
channelBuffer.writeBytes(request.getData());
return channelBuffer;
}
}
package com.troy.data.codec; import com.troy.data.constant.ConsantUtil;
import com.troy.data.domain.Request;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneDecoder; //请求数据解码
public class RequestDecode extends OneToOneDecoder { protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, Object o) throws Exception {
ChannelBuffer channelBuffer = (ChannelBuffer) o;
if (ConsantUtil.PACKAGE_HEADER == channelBuffer.readInt()) {
Request request = new Request();
request.setModel(channelBuffer.readInt());
byte[] bytes = new byte[channelBuffer.readableBytes()];
channelBuffer.readBytes(bytes);
request.setData(bytes);
return request;
}
return null;
}
}
6)响应编解码
package com.troy.data.codec; import com.troy.data.constant.ConsantUtil;
import com.troy.data.domain.Response;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; //响应编码器
public class ResponseEncode extends OneToOneEncoder { protected Object encode(ChannelHandlerContext channelHandlerContext, Channel channel, Object o) throws Exception {
Response response = (Response) o;
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
channelBuffer.writeInt(ConsantUtil.PACKAGE_HEADER);
channelBuffer.writeInt(response.getModel());
channelBuffer.writeInt(response.getStatus());
channelBuffer.writeBytes(response.getData());
return channelBuffer;
}
}
package com.troy.data.codec; import com.troy.data.constant.ConsantUtil;
import com.troy.data.domain.Response;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneDecoder; //响应解码
public class ResponseDecode extends OneToOneDecoder{ protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, Object o) throws Exception {
ChannelBuffer channelBuffer = (ChannelBuffer) o;
if (ConsantUtil.PACKAGE_HEADER == channelBuffer.readInt()) {
Response response = new Response();
response.setModel(channelBuffer.readInt());
response.setStatus(channelBuffer.readInt());
byte[] bytes = new byte[channelBuffer.readableBytes()];
channelBuffer.readBytes(bytes);
response.setData(bytes);
return response;
}
return null;
}
}
7)设置对应的管道编解码就可以了
a、客户端
//设置管道工厂
clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline channelPipeline = Channels.pipeline();
channelPipeline.addLast("decode",new RequestEncode());
channelPipeline.addLast("encode",new ResponseDecode());
channelPipeline.addLast("client",new ClientHandler());
return channelPipeline;
}
});
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Response response = (Response) e.getMessage();
System.out.println(response.toString());
super.messageReceived(ctx, e);
}
b、服务端
//设置管道流
serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline channelPipeline = Channels.pipeline();
//添加处理方式
channelPipeline.addLast("idle",new IdleStateHandler(new HashedWheelTimer(),,,));
channelPipeline.addLast("decode",new RequestDecode());
channelPipeline.addLast("encode",new ResponseEncode());
channelPipeline.addLast("server",new ServerHandler());
return channelPipeline;
}
});
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Request request = (Request) e.getMessage();
System.out.println("client:"+request.toString());
Response response = new Response();
response.setModel();
response.setStatus();
response.setData("hello client".getBytes());
ctx.getChannel().write(response);
super.messageReceived(ctx, e);
}
4、netty5的编解码规则
1)数据结构、目录结构、对象、常量都是一样。
2)编解码的编写方式有些不一样
a、请求编解码
package com.troy.data.codec; import com.troy.data.constant.ConsantUtil;
import com.troy.data.domain.Request;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder; import java.util.List;
@ChannelHandler.Sharable
public class RequestEncode extends MessageToMessageEncoder<Request> { protected void encode(ChannelHandlerContext channelHandlerContext, Request request, List<Object> list) throws Exception {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeInt(ConsantUtil.PACKAGE_HEADER);
byteBuf.writeInt(request.getModel());
byteBuf.writeBytes(request.getData());
list.add(byteBuf);
}
}
package com.troy.data.codec; import com.troy.data.constant.ConsantUtil;
import com.troy.data.domain.Request;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder; import java.util.List; //请求数据解码
@ChannelHandler.Sharable
public class RequestDecode extends MessageToMessageDecoder<ByteBuf>{ protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
if (ConsantUtil.PACKAGE_HEADER == byteBuf.readInt()) {
//当数据超过指定值的时候跳过这部分数据
if (byteBuf.readableBytes() > ) {
byteBuf.skipBytes(byteBuf.readableBytes());
}
//一个字节一个字节的读取,知道读取到包头
while(true) {
byteBuf.markReaderIndex();
if (ConsantUtil.PACKAGE_HEADER == byteBuf.readInt()) {
break;
}
byteBuf.resetReaderIndex();
byteBuf.readByte();
}
Request request = new Request();
request.setModel(byteBuf.readInt());
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);
request.setData(bytes);
list.add(request);
}
}
}
b、响应编解码
package com.troy.data.codec; import com.troy.data.constant.ConsantUtil;
import com.troy.data.domain.Response;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder; import java.util.List; //响应编码器
@ChannelHandler.Sharable
public class ResponseEncode extends MessageToMessageEncoder<Response> { protected void encode(ChannelHandlerContext channelHandlerContext, Response response, List<Object> list) throws Exception {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeInt(ConsantUtil.PACKAGE_HEADER);
byteBuf.writeInt(response.getModel());
byteBuf.writeInt(response.getStatus());
byteBuf.writeBytes(response.getData());
list.add(byteBuf);
}
}
package com.troy.data.codec; import com.troy.data.constant.ConsantUtil;
import com.troy.data.domain.Response;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder; import java.util.List; //响应解码
@ChannelHandler.Sharable
public class ResponseDecode extends MessageToMessageDecoder<ByteBuf>{ protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
if (ConsantUtil.PACKAGE_HEADER == byteBuf.readInt()) {
//当数据超过指定值的时候跳过这部分数据
if (byteBuf.readableBytes() > ) {
byteBuf.skipBytes(byteBuf.readableBytes());
}
//一个字节一个字节的读取,知道读取到包头
while(true) {
byteBuf.markReaderIndex();
if (ConsantUtil.PACKAGE_HEADER == byteBuf.readInt()) {
break;
}
byteBuf.resetReaderIndex();
byteBuf.readByte();
}
Response response = new Response();
response.setModel(byteBuf.readInt());
response.setStatus(byteBuf.readInt());
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);
response.setData(bytes);
list.add(response);
}
}
}
3)处理上面基本上都是一样的。
5、netty的编解码,主要目的就是处理通讯问题,对数据进行自定义处理!
6、源码下载:https://pan.baidu.com/s/1nvUnmEt
netty之编解码的更多相关文章
- Netty入门系列(3) --使用Netty进行编解码的操作
前言 何为编解码,通俗的来说,我们需要将一串文本信息从A发送到B并且将这段文本进行加工处理,如:A将信息文本信息编码为2进制信息进行传输.B接受到的消息是一串2进制信息,需要将其解码为文本信息才能正常 ...
- java架构之路-(netty专题)netty的编解码(出入战)与粘包拆包
上次回归: 上次博客我们主要说了netty的基本使用,都是一些固定的模式去写的,我们只需要关注我们的拦截器怎么去写就可以了,然后我们用我们的基础示例,改造了一个简单的聊天室程序,可以看到内部加了一个S ...
- Netty 编解码技术 数据通信和心跳监控案例
Netty 编解码技术 数据通信和心跳监控案例 多台服务器之间在进行跨进程服务调用时,需要使用特定的编解码技术,对需要进行网络传输的对象做编码和解码操作,以便完成远程调用.Netty提供了完善,易扩展 ...
- Netty 源码 ChannelHandler(四)编解码技术
Netty 源码 ChannelHandler(四)编解码技术 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) 一.拆包与粘 ...
- 【转】Netty系列之Netty编解码框架分析
http://www.infoq.com/cn/articles/netty-codec-framework-analyse/ 1. 背景 1.1. 编解码技术 通常我们也习惯将编码(Encode)称 ...
- Netty系列之Netty编解码框架分析
1. 背景 1.1. 编解码技术 通常我们也习惯将编码(Encode)称为序列化(serialization),它将对象序列化为字节数组,用于网络传输.数据持久化或者其它用途. 反之,解码(Decod ...
- netty源码解解析(4.0)-19 ChannelHandler: codec--常用编解码实现
数据包编解码过程中主要的工作就是:在编码过程中进行序列化,在解码过程中从Byte流中分离出数据包然后反序列化.在MessageToByteEncoder中,已经解决了序列化之后的问题,ByteToMe ...
- Netty对常用编解码的支持
参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! Netty对编解码的支持 打开Netty的源码,它对很多的编码器都提 ...
- Netty 编解码奥秘
Netty中编解码 Netty 的解码器有很多种,比如基于长度的,基于分割符的,私有协议的.但是,总体的思路都是一致的. 拆包思路:当数据满足了 解码条件时,将其拆开.放到数组.然后发送到业务 han ...
随机推荐
- 误用MySQL关键字导致的错误
使用Hibernate整合Spring的过程中,我使用explain作为表的字段,结果一直给我报错. 报错如下: ERROR: You have an error in your SQL syntax ...
- CRT公钥登录
1.实现原理: 通过CRT生成的密钥对,把公钥上传到Linux服务器指定用户下的.ssh目录中,在客户端上只需输入秘钥的密码即可登陆,而且验证一次以后可以免密码登陆 2.具体过程: 转自:http:/ ...
- springmvc使用map接收form表单的参数
其实只需要在map前面加上@RequestParam参数即可,jsp的name等都不变 public String queryByCondition(@RequestParam Map<Stri ...
- 使用libcurl下载文件小例
libcurl是一个很强大的开源网络处理库,支持包括HTTP.HTTPS.FTP……一系列网络协议.用它来进行HTTP的get\post 或者下载文件更是小菜一碟,chrome内核都用到了它,本文主要 ...
- 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
“/”应用程序中的服务器错误. 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“c552ea94-6fbb-11d5-a9c1-00104bb6fc1c”. 说明: 执行当前 Web ...
- java中StringBuffer与String、StringBuilder的区别
在java中我们经常可以看到StringBuffer和String的用法,但是我自己在使用过程中,经常会将两者弄混淆,今天我们就来了解一下两者的区别: 我们首先来看一下我们的官方API中的简单介绍: ...
- 在Mac机器上给ITerm2配置lrzsz,便捷的传输文件到远程服务器上
可直接参考文档:http://danqingdani.blog.163.com/blog/static/18609419520141201215750 需要使用到的github脚本:https://g ...
- 【题解】洛谷P4391 [BOI2009] Radio Transmission(KMP)
洛谷P4391:https://www.luogu.org/problemnew/show/P4391 思路 对于给定的字符串 运用KMP思想 设P[x]为前x个字符前缀和后缀相同的最长长度 则对于题 ...
- python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学
首发于:python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=431 本文 ...
- git获取步骤
$ git init $ git config --global user.name "[name]" $ git config --global user.email [emai ...