ProtocolDecoderDeprecation.java
1.7 KB
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
/**
*
*/
package com.waylau.netty.demo.protocol;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
/**
* 说明:
*
* @author <a href="http://www.waylau.com">waylau.com</a> 2015年11月10日
*/
public class ProtocolDecoderDeprecation extends ByteToMessageDecoder {
private static final int HEADER_SIZE = 10;
private byte magic; // 魔数
private byte msgType; // 消息类型
private short reserve; // 保留字
private short sn; // 序列号
private int len; // 长度
/**
*
*/
public ProtocolDecoderDeprecation() {
// TODO Auto-generated constructor stub
}
/*
* (non-Javadoc)
*
* @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.
* ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)
*/
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out) throws Exception {
if (in.readableBytes() < HEADER_SIZE) {
return;// response header is 10 bytes
}
magic = in.readByte();
msgType = in.readByte();
reserve = in.readShort();
sn = in.readShort();
len = in.readInt();
if (in.readableBytes() < len) {
return; // until we have the entire payload return
}
ByteBuf buf = in.readBytes(len);
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String body = new String(req, "UTF-8");
ProtocolMsg msg = new ProtocolMsg();
// ProtocolBody body2 = new ProtocolBody();
// body2.setBody(body);
ProtocolHeader protocolHeader = new ProtocolHeader(magic, msgType,
reserve, sn, len);
//msg.setProtocolBody(body2);
msg.setBody(body);
msg.setProtocolHeader(protocolHeader);
out.add(msg);
}
}